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):
|
79 | 79 | if commit: |
80 | 80 | Session.commit() |
81 | 81 | |
| 82 | def 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() |
82 | 95 | |
83 | 96 | def build_report_object(report_form, media_entry=None, comment=None): |
84 | 97 | """ |
diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py
index 78751a2..81d5aa4 100644
a
|
b
|
import json
|
21 | 21 | from mediagoblin import messages, mg_globals |
22 | 22 | from mediagoblin.db.models import (MediaEntry, MediaTag, Collection, |
23 | 23 | CollectionItem, User) |
24 | | from mediagoblin.tools.response import render_to_response, render_404, \ |
25 | | redirect, redirect_obj |
| 24 | from mediagoblin.tools.response import (render_to_response, render_404, |
| 25 | redirect, redirect_obj) |
26 | 26 | from mediagoblin.tools.text import cleaned_markdown_conversion |
27 | 27 | from mediagoblin.tools.translate import pass_to_ugettext as _ |
28 | 28 | from mediagoblin.tools.pagination import Pagination |
29 | 29 | from 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 |
| 30 | from mediagoblin.user_pages.lib import ( |
| 31 | add_media_to_collection, build_report_object, |
| 32 | remove_collection_item) |
| 33 | from mediagoblin.notifications import ( |
| 34 | trigger_notification, add_comment_subscription, |
| 35 | mark_comment_notification_seen) |
34 | 36 | from mediagoblin.tools.pluginapi import hook_transform |
35 | 37 | |
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, |
| 38 | from 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, |
39 | 42 | get_user_collection, get_user_collection_item, active_user_from_url, |
40 | 43 | get_optional_media_comment_by_id, allow_reporting) |
41 | 44 | |
… |
… |
def collection_item_confirm_remove(request, collection_item):
|
394 | 397 | form = user_forms.ConfirmCollectionItemRemoveForm(request.form) |
395 | 398 | |
396 | 399 | if request.method == 'POST' and form.validate(): |
397 | | username = collection_item.in_collection.get_creator.username |
398 | 400 | collection = collection_item.in_collection |
399 | 401 | |
400 | 402 | 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) |
407 | 404 | |
408 | 405 | messages.add_message( |
409 | 406 | request, messages.SUCCESS, _('You deleted the item from the collection.')) |
… |
… |
def collection_confirm_delete(request, collection):
|
444 | 441 | |
445 | 442 | # Delete all the associated collection items |
446 | 443 | for item in collection.get_collection_items(): |
447 | | entry = item.get_media_entry |
448 | | entry.save() |
449 | | item.delete() |
| 444 | remove_collection_item(item) |
450 | 445 | |
451 | 446 | collection.delete() |
452 | 447 | messages.add_message(request, messages.SUCCESS, |