Ticket #950: 0002-Add-new-hook-remove_media_from_collection.patch

File 0002-Add-new-hook-remove_media_from_collection.patch, 4.3 KB (added by Odin Hørthe Omdal (Velmont), 10 years ago)
  • mediagoblin/user_pages/lib.py

    From 3b5c99134379ee8c64fe68129f115affbbafa1c8 Mon Sep 17 00:00:00 2001
    From: =?UTF-8?q?Odin=20H=C3=B8rthe=20Omdal?= <odinho@opera.com>
    Date: Mon, 18 Aug 2014 22:51:15 +0200
    Subject: [PATCH 2/2] Add new hook 'remove_media_from_collection'
    
    ---
     mediagoblin/user_pages/lib.py   | 13 +++++++++++++
     mediagoblin/user_pages/views.py | 33 ++++++++++++++-------------------
     2 files changed, 27 insertions(+), 19 deletions(-)
    
    diff --git a/mediagoblin/user_pages/lib.py b/mediagoblin/user_pages/lib.py
    index b731657..2b53f03 100644
    a b def add_media_to_collection(collection, media, note=None, commit=True):  
    7979    if commit:
    8080        Session.commit()
    8181
     82def remove_collection_item(collection_item, commit=True):
     83    media = collection_item.get_media_entry
     84    collection = collection_item.in_collection
     85
     86    collection.items = collection.items - 1
     87    Session.delete(collection_item)
     88    Session.add(collection)
     89
     90    hook_runall('remove_media_from_collection',
     91                collection=collection, media_entry=media)
     92
     93    if commit:
     94        Session.commit()
    8295
    8396def build_report_object(report_form, media_entry=None, comment=None):
    8497    """
  • mediagoblin/user_pages/views.py

    diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py
    index 78751a2..81d5aa4 100644
    a b import json  
    2121from mediagoblin import messages, mg_globals
    2222from mediagoblin.db.models import (MediaEntry, MediaTag, Collection,
    2323                                   CollectionItem, User)
    24 from mediagoblin.tools.response import render_to_response, render_404, \
    25     redirect, redirect_obj
     24from mediagoblin.tools.response import (render_to_response, render_404,
     25                                        redirect, redirect_obj)
    2626from mediagoblin.tools.text import cleaned_markdown_conversion
    2727from mediagoblin.tools.translate import pass_to_ugettext as _
    2828from mediagoblin.tools.pagination import Pagination
    2929from mediagoblin.user_pages import forms as user_forms
    30 from mediagoblin.user_pages.lib import (send_comment_email,
    31         add_media_to_collection, build_report_object)
    32 from mediagoblin.notifications import trigger_notification, \
    33     add_comment_subscription, mark_comment_notification_seen
     30from mediagoblin.user_pages.lib import (
     31    add_media_to_collection, build_report_object,
     32    remove_collection_item)
     33from mediagoblin.notifications import (
     34    trigger_notification, add_comment_subscription,
     35    mark_comment_notification_seen)
    3436from mediagoblin.tools.pluginapi import hook_transform
    3537
    36 from mediagoblin.decorators import (uses_pagination, get_user_media_entry,
    37     get_media_entry_by_id, user_has_privilege, user_not_banned,
    38     require_active_login, user_may_delete_media, user_may_alter_collection,
     38from mediagoblin.decorators import (
     39    uses_pagination, get_user_media_entry, get_media_entry_by_id,
     40    user_has_privilege, user_not_banned, require_active_login,
     41    user_may_delete_media, user_may_alter_collection,
    3942    get_user_collection, get_user_collection_item, active_user_from_url,
    4043    get_optional_media_comment_by_id, allow_reporting)
    4144
    def collection_item_confirm_remove(request, collection_item):  
    394397    form = user_forms.ConfirmCollectionItemRemoveForm(request.form)
    395398
    396399    if request.method == 'POST' and form.validate():
    397         username = collection_item.in_collection.get_creator.username
    398400        collection = collection_item.in_collection
    399401
    400402        if form.confirm.data is True:
    401             entry = collection_item.get_media_entry
    402             entry.save()
    403 
    404             collection_item.delete()
    405             collection.items = collection.items - 1
    406             collection.save()
     403            remove_collection_item(collection_item)
    407404
    408405            messages.add_message(
    409406                request, messages.SUCCESS, _('You deleted the item from the collection.'))
    def collection_confirm_delete(request, collection):  
    444441
    445442            # Delete all the associated collection items
    446443            for item in collection.get_collection_items():
    447                 entry = item.get_media_entry
    448                 entry.save()
    449                 item.delete()
     444                remove_collection_item(item)
    450445
    451446            collection.delete()
    452447            messages.add_message(request, messages.SUCCESS,