From 72d126c04debdbed7474397f178bdb45dfb63d2b Mon Sep 17 00:00:00 2001 From: viennet Date: Sat, 12 Dec 2020 18:05:28 +0100 Subject: [PATCH] code cleaning --- sco_photos.py | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/sco_photos.py b/sco_photos.py index 575c8c4e..72b8e2e5 100644 --- a/sco_photos.py +++ b/sco_photos.py @@ -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 @@ -64,7 +64,7 @@ from scolog import logdb PHOTO_DIR = os.path.join(os.environ["INSTANCE_HOME"], "var", "scodoc", "photos") ICONS_DIR = os.path.join(SCO_SRCDIR, "static", "icons") UNKNOWN_IMAGE_PATH = os.path.join(ICONS_DIR, "unknown.jpg") -UNKNOWN_IMAGE_URL = "get_photo_image?etudid=" # with empty etudid => unknown face image +UNKNOWN_IMAGE_URL = "get_photo_image?etudid=" # with empty etudid => unknown face image IMAGE_EXT = ".jpg" JPG_QUALITY = 0.92 REDUCED_HEIGHT = 90 # pixels @@ -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,23 +161,24 @@ 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): - """HTML img tag for the photo, either in small size (h90) +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") """ if not etud: 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")