1
0
forked from ScoDoc/ScoDoc

code cleaning

This commit is contained in:
viennet 2020-12-12 18:05:28 +01:00
parent 612b62227c
commit 72d126c04d

View File

@ -53,9 +53,9 @@ from PIL import Image as PILImage
from cStringIO import StringIO
import glob
from sco_utils import *
from sco_utils import CONFIG, SCO_SRCDIR
from notes_log import log
from notesdb import *
import scolars
import sco_portal_apogee
from scolog import logdb
@ -81,6 +81,7 @@ def photo_portal_url(context, etud):
else:
return None
def etud_photo_url(context, etud, size="small", REQUEST=None):
"""url to the image of the student, in "small" size or "orig" size.
If ScoDoc doesn't have an image and a portal is configured, link to it.
@ -95,7 +96,7 @@ def etud_photo_url(context, etud, size="small", REQUEST=None):
photo_url = UNKNOWN_IMAGE_URL
else:
# essaie de copier la photo du portail
new_path, diag = copy_portal_photo_to_fs(context, etud, REQUEST=REQUEST)
new_path, _ = copy_portal_photo_to_fs(context, etud, REQUEST=REQUEST)
if not new_path:
# copy failed, can we use external url ?
# nb: rarement utile, car le portail est rarement accessible sans authentification
@ -114,7 +115,7 @@ def get_photo_image(context, etudid=None, size="small", REQUEST=None):
filename = UNKNOWN_IMAGE_PATH
else:
etud = context.getEtudInfo(etudid=etudid, filled=1, REQUEST=REQUEST)[0]
filename = photo_pathname(context, etud, size=size) # os.path.join( PHOTO_DIR, etud["photo_filename"] )
filename = photo_pathname(context, etud, size=size)
if not filename:
filename = UNKNOWN_IMAGE_PATH
return _http_jpeg_file(context, filename, REQUEST=REQUEST)
@ -160,7 +161,9 @@ def etud_photo_is_local(context, etud, size="small"):
return photo_pathname(context, etud, size=size)
def etud_photo_html(context, etud=None, etudid=None, title=None, size="small", REQUEST=None):
def etud_photo_html(
context, etud=None, etudid=None, title=None, size="small", REQUEST=None
):
"""HTML img tag for the photo, either in small size (h90)
or original size (size=="orig")
"""
@ -168,15 +171,14 @@ def etud_photo_html(context, etud=None, etudid=None, title=None, size="small", R
if etudid:
etud = context.getEtudInfo(etudid=etudid, filled=1, REQUEST=REQUEST)[0]
else:
raise ValueError('etud_photo_html: either etud or etudid must be specified')
raise ValueError("etud_photo_html: either etud or etudid must be specified")
photo_url = etud_photo_url(context, etud, size=size, REQUEST=REQUEST)
nom = etud.get("nomprenom", etud["nom_disp"])
if title is None:
title = nom
if not etud_photo_is_local(context, etud):
fallback = (
"""onerror='this.onerror = null; this.src="%s"'"""
% UNKNOWN_IMAGE_URL
"""onerror='this.onerror = null; this.src="%s"'""" % UNKNOWN_IMAGE_URL
)
else:
fallback = ""
@ -192,12 +194,16 @@ def etud_photo_html(context, etud=None, etudid=None, title=None, size="small", R
fallback,
)
def etud_photo_orig_html(context, etud=None, etudid=None, title=None, REQUEST=None):
"""HTML img tag for the photo, in full size.
Full-size images are always stored locally in the filesystem.
They are the original uploaded images, converted in jpeg.
"""
return etud_photo_html(context, etud=etud, etudid=etudid, title=title, size="orig", REQUEST=REQUEST)
return etud_photo_html(
context, etud=etud, etudid=etudid, title=title, size="orig", REQUEST=REQUEST
)
def photo_pathname(context, etud, size="orig"):
"""Returns full path of image file if etud has a photo (in the filesystem), or False.
@ -256,7 +262,6 @@ def suppress_photo(context, etud, REQUEST=None):
scolars.identite_edit_nocheck(cnx, etud)
cnx.commit()
# 2- erase images files
#log("rel_path=%s" % rel_path)
if rel_path:
# remove extension and glob
rel_path = rel_path[: -len(IMAGE_EXT)]
@ -274,6 +279,7 @@ def suppress_photo(context, etud, REQUEST=None):
# ---------------------------------------------------------------------------
# Internal functions
def save_image(context, etudid, data):
"""img_file is a file-like object.
Save image in JPEG in 2 sizes (original and h90).
@ -284,7 +290,7 @@ def save_image(context, etudid, data):
data_file.seek(0)
img = PILImage.open(data_file)
filename = get_new_filename(context, etudid)
path = os.path.join( PHOTO_DIR, filename )
path = os.path.join(PHOTO_DIR, filename)
log("saving %dx%d jpeg to %s" % (img.size[0], img.size[1], path))
img.save(path + IMAGE_EXT, format="JPEG", quality=92)
# resize:
@ -342,6 +348,7 @@ def copy_portal_photo_to_fs(context, etud, REQUEST=None):
f = urllib2.urlopen(url, timeout=portal_timeout) # python >= 2.7
except:
log("download failed: exception:\n%s" % traceback.format_exc())
log("called from:\n" + "".join(traceback.format_stack()))
return None, "%s: erreur chargement de %s" % (etud["nomprenom"], url)
if not f:
log("download failed")