From e05339937a43e852bd5c40a3262c8df029442a0d Mon Sep 17 00:00:00 2001 From: tr4ck3ur Date: Sat, 30 Sep 2017 20:19:20 +0200 Subject: [PATCH] Handle follow media when rename room --- jm2l/upload.py | 46 +++++++++++++++++++++++++++++++++++----------- jm2l/views.py | 13 ++++++++++++- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/jm2l/upload.py b/jm2l/upload.py index 8384550..e4c9630 100644 --- a/jm2l/upload.py +++ b/jm2l/upload.py @@ -88,9 +88,40 @@ class MediaPath(): elif MediaType=='Other' and len( os.path.splitext(filename)[1] ): filelist.append(tmpurl) return filelist - + + def move_mediapath(self, media_table, from_id, to_id): + """ + Move target media folder to follow database + :param media_table: type of media + :param from_id: source + :param to_id: destination + :return: Error if any + """ + if media_table in ['tiers', 'place', 'salle', 'users']: + src = IMAGEPATH + [ media_table, from_id ] + dst = IMAGEPATH + [ media_table, to_id ] + else: + raise RuntimeError("Sorry, Media '%s' not supported yet for move." % media_table) + + src_path = os.path.join('jm2l/upload', *src) + dst_path = os.path.join('jm2l/upload', *dst) + if not os.path.isdir(src_path): + # Nothing to do ... + return False + if os.path.isdir(dst_path): + raise RuntimeError('Destination path already exist') + + shutil.move(src_path, dst_path) + + return True + def get_mediapath(self, media_table, linked_id, name): - + """ + :param media_table: type of media + :param linked_id: id of media + :param name: filename + :return: full relative path on server side + """ linked_id = str(linked_id) if media_table in ['tiers', 'place', 'salle']: # Retrieve Slug @@ -113,20 +144,13 @@ class MediaPath(): elif media_table in ['RIB', 'Justif']: slug = User.by_id(linked_id).slug p = IMAGEPATH + ['users', slug , media_table ] - elif media_table=='users': - user = User.by_id(linked_id) - if not user: - raise HTTPNotFound() - else: - slug = user.slug - p = IMAGEPATH + ['users', slug ] - elif media_table=='badge': + elif media_table in ['users', 'badge']: user = User.by_id(linked_id) if not user: raise HTTPNotFound() else: slug = user.slug - p = IMAGEPATH + ['badge', slug ] + p = IMAGEPATH + [media_table, slug ] elif media_table=='event': ev = Event.by_id(linked_id) slug = ev.slug diff --git a/jm2l/views.py b/jm2l/views.py index 7f6fd76..6ba78f8 100644 --- a/jm2l/views.py +++ b/jm2l/views.py @@ -7,7 +7,7 @@ from pyramid.view import view_config from pyramid.response import Response from mako.template import Template from pyramid_mailer.message import Message -from .upload import IMAGEPATH +from .upload import IMAGEPATH, MediaPath # Import Web Forms from .forms import * # Database access imports @@ -739,16 +739,27 @@ def handle_salle_phy(request): salle_id = request.matchdict.get('salle_id') if salle_id: Salle = SallePhy.by_id(int(salle_id)) + orig_slug = Salle.slug if not Salle: raise HTTPNotFound() form = EditSallePhyForm(request.POST, Salle, meta={'csrf_context': request.session}) else: Salle = SallePhy() + orig_slug = None form = SallePhyForm(request.POST, Salle, meta={'csrf_context': request.session}) if request.method == 'POST' and form.validate(): form.populate_obj(Salle) + dest_slug = slugify(Salle.name) + # Do a check in order to move attached files if any + if orig_slug and orig_slug!=dest_slug: + try: + mp = MediaPath().move_mediapath('salle', orig_slug, dest_slug) + except RuntimeError, err: + request.session.flash(('error', "Le nom de cette salle est déjà utilisé : " + err.message)) + return {'form': form} Salle.slug = slugify(Salle.name) + if 'uid' in form._fields.keys(): DBSession.merge(Salle) else: