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 from cStringIO import StringIO
import glob import glob
from sco_utils import * from sco_utils import CONFIG, SCO_SRCDIR
from notes_log import log from notes_log import log
from notesdb import *
import scolars import scolars
import sco_portal_apogee import sco_portal_apogee
from scolog import logdb from scolog import logdb
@ -81,6 +81,7 @@ def photo_portal_url(context, etud):
else: else:
return None return None
def etud_photo_url(context, etud, size="small", REQUEST=None): def etud_photo_url(context, etud, size="small", REQUEST=None):
"""url to the image of the student, in "small" size or "orig" size. """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. 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 photo_url = UNKNOWN_IMAGE_URL
else: else:
# essaie de copier la photo du portail # 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: if not new_path:
# copy failed, can we use external url ? # copy failed, can we use external url ?
# nb: rarement utile, car le portail est rarement accessible sans authentification # 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 filename = UNKNOWN_IMAGE_PATH
else: else:
etud = context.getEtudInfo(etudid=etudid, filled=1, REQUEST=REQUEST)[0] 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: if not filename:
filename = UNKNOWN_IMAGE_PATH filename = UNKNOWN_IMAGE_PATH
return _http_jpeg_file(context, filename, REQUEST=REQUEST) 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) 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) """HTML img tag for the photo, either in small size (h90)
or original size (size=="orig") 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: if etudid:
etud = context.getEtudInfo(etudid=etudid, filled=1, REQUEST=REQUEST)[0] etud = context.getEtudInfo(etudid=etudid, filled=1, REQUEST=REQUEST)[0]
else: 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) photo_url = etud_photo_url(context, etud, size=size, REQUEST=REQUEST)
nom = etud.get("nomprenom", etud["nom_disp"]) nom = etud.get("nomprenom", etud["nom_disp"])
if title is None: if title is None:
title = nom title = nom
if not etud_photo_is_local(context, etud): if not etud_photo_is_local(context, etud):
fallback = ( fallback = (
"""onerror='this.onerror = null; this.src="%s"'""" """onerror='this.onerror = null; this.src="%s"'""" % UNKNOWN_IMAGE_URL
% UNKNOWN_IMAGE_URL
) )
else: else:
fallback = "" fallback = ""
@ -192,12 +194,16 @@ def etud_photo_html(context, etud=None, etudid=None, title=None, size="small", R
fallback, fallback,
) )
def etud_photo_orig_html(context, etud=None, etudid=None, title=None, REQUEST=None): def etud_photo_orig_html(context, etud=None, etudid=None, title=None, REQUEST=None):
"""HTML img tag for the photo, in full size. """HTML img tag for the photo, in full size.
Full-size images are always stored locally in the filesystem. Full-size images are always stored locally in the filesystem.
They are the original uploaded images, converted in jpeg. 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"): def photo_pathname(context, etud, size="orig"):
"""Returns full path of image file if etud has a photo (in the filesystem), or False. """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) scolars.identite_edit_nocheck(cnx, etud)
cnx.commit() cnx.commit()
# 2- erase images files # 2- erase images files
#log("rel_path=%s" % rel_path)
if rel_path: if rel_path:
# remove extension and glob # remove extension and glob
rel_path = rel_path[: -len(IMAGE_EXT)] rel_path = rel_path[: -len(IMAGE_EXT)]
@ -274,6 +279,7 @@ def suppress_photo(context, etud, REQUEST=None):
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Internal functions # Internal functions
def save_image(context, etudid, data): def save_image(context, etudid, data):
"""img_file is a file-like object. """img_file is a file-like object.
Save image in JPEG in 2 sizes (original and h90). Save image in JPEG in 2 sizes (original and h90).
@ -284,7 +290,7 @@ def save_image(context, etudid, data):
data_file.seek(0) data_file.seek(0)
img = PILImage.open(data_file) img = PILImage.open(data_file)
filename = get_new_filename(context, etudid) 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)) log("saving %dx%d jpeg to %s" % (img.size[0], img.size[1], path))
img.save(path + IMAGE_EXT, format="JPEG", quality=92) img.save(path + IMAGE_EXT, format="JPEG", quality=92)
# resize: # 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 f = urllib2.urlopen(url, timeout=portal_timeout) # python >= 2.7
except: except:
log("download failed: exception:\n%s" % traceback.format_exc()) 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) return None, "%s: erreur chargement de %s" % (etud["nomprenom"], url)
if not f: if not f:
log("download failed") log("download failed")