b24c27d1 |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
3 | # |
4 | # This program is free software: you can redistribute it and/or modify |
5 | # it under the terms of the GNU Affero General Public License as published by |
6 | # the Free Software Foundation, either version 3 of the License, or |
7 | # (at your option) any later version. |
8 | # |
9 | # This program is distributed in the hope that it will be useful, |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | # GNU Affero General Public License for more details. |
13 | # |
14 | # You should have received a copy of the GNU Affero General Public License |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
16 | from mediagoblin import mg_globals |
17 | from mediagoblin.db.base import Session |
18 | from mediagoblin.db.models import MediaEntry |
19 | from mediagoblin.decorators import uses_pagination, user_not_banned,\ |
20 | user_has_privilege, get_user_media_entry |
21 | from mediagoblin.tools.response import render_to_response, redirect |
22 | from mediagoblin.tools.pagination import Pagination |
23 | |
24 | from mediagoblin.plugins.archivalook.tools import ( |
b24c27d1 |
25 | split_featured_media_list, |
26 | create_featured_media_textbox, |
27 | automatically_add_new_feature, |
28 | automatically_remove_feature) |
29 | from mediagoblin.plugins.archivalook import forms as archivalook_forms |
30 | from mediagoblin.plugins.archivalook.models import FeaturedMedia |
31 | from mediagoblin.plugins.archivalook.utils import feature_template |
32 | from mediagoblin.plugins.archivalook.tools import (promote_feature, |
33 | demote_feature) |
34 | from mediagoblin.tools.translate import lazy_pass_to_ugettext as _ |
35 | |
36 | @user_not_banned |
37 | def root_view(request): |
38 | """ |
39 | This is an alternative to the typical root view. This display centers around |
40 | displaying featured media. |
41 | """ |
42 | featured_media = { |
43 | u'primary':FeaturedMedia.query.order_by( |
44 | FeaturedMedia.order.asc()).filter( |
45 | FeaturedMedia.display_type==u'primary').all(), |
46 | u'secondary':FeaturedMedia.query.order_by( |
47 | FeaturedMedia.order.asc()).filter( |
48 | FeaturedMedia.display_type==u'secondary').all(), |
49 | u'tertiary':FeaturedMedia.query.order_by( |
50 | FeaturedMedia.order.asc()).filter( |
51 | FeaturedMedia.display_type==u'tertiary').all()} |
52 | |
53 | return render_to_response( |
54 | request, 'archivalook/root.html', |
55 | {'featured_media': featured_media, |
56 | 'allow_registration': mg_globals.app_config["allow_registration"], |
57 | 'feature_template': feature_template}) |
58 | |
59 | @user_has_privilege(u'featurer') |
60 | def featured_media_panel(request): |
61 | """ |
62 | This is a new administrator panel to manage featured media. This is an |
63 | entirely optional panel, as there are other ways to manage it, but this way |
64 | gives the admin more control. |
65 | """ |
66 | form = archivalook_forms.FeaturedMediaList(request.form) |
67 | |
68 | if request.method == 'POST' and form.validate(): |
69 | featured_media = split_featured_media_list(form.box_content.data) |
70 | previous_features = FeaturedMedia.query.all() |
71 | for index, (media_entry, display_type) in enumerate(featured_media): |
72 | target = FeaturedMedia.query.filter( |
73 | FeaturedMedia.media_entry == media_entry).first() |
74 | # If this media was already featured, we don't have to create a new |
75 | # feature, we just have to edit the old one's values |
76 | if target is not None: |
77 | target.order = index |
78 | target.display_type = display_type |
79 | previous_features.remove(target) |
80 | Session.add(target) |
81 | else: |
82 | new_feature = FeaturedMedia( |
83 | media_entry=media_entry, |
84 | display_type=display_type, |
85 | order=index) |
86 | Session.add(new_feature) |
87 | [Session.delete(feature) for feature in previous_features] |
88 | |
89 | Session.commit() |
90 | |
91 | form.box_content.data = create_featured_media_textbox() |
92 | return render_to_response( |
93 | request, 'archivalook/feature.html', |
94 | {'form' : form}) |
95 | |
96 | @uses_pagination |
97 | @user_not_banned |
98 | def recent_media_gallery_view(request, page): |
99 | """ |
100 | The replaced homepage is available through this view. |
101 | """ |
102 | cursor = MediaEntry.query.filter_by(state=u'processed').\ |
103 | order_by(MediaEntry.created.desc()) |
104 | |
105 | pagination = Pagination(page, cursor) |
106 | media_entries = pagination() |
107 | return render_to_response( |
108 | request, 'archivalook/recent_media.html', |
109 | {'media_entries': media_entries, |
110 | 'pagination': pagination}) |
111 | |
112 | def add_featured_media_to_media_home(context): |
113 | """ |
114 | A context hook which allows the media home page to know whether the media |
115 | has been featured or not. |
116 | """ |
117 | context['featured_media'] = FeaturedMedia.query |
118 | return context |
119 | |
120 | @user_has_privilege(u'featurer') |
121 | @get_user_media_entry |
122 | def feature_media(request, media, **kwargs): |
123 | """ |
124 | A view to feature a new piece of media |
125 | """ |
126 | already_featured_media_ids = [f.media_entry.id |
127 | for f in FeaturedMedia.query.all()] |
128 | if not media.id in already_featured_media_ids: |
129 | new_feature = automatically_add_new_feature(media) |
130 | return redirect( |
131 | request, 'index') |
132 | |
133 | @user_has_privilege(u'featurer') |
134 | @get_user_media_entry |
135 | def unfeature_media(request, media, **kwargs): |
136 | """ |
137 | A view to unfeature a piece of media which has previously been featured. |
138 | """ |
139 | already_featured_media_ids = [f.media_entry.id |
140 | for f in FeaturedMedia.query.all()] |
141 | if media.id in already_featured_media_ids: |
142 | automatically_remove_feature(media) |
143 | return redirect( |
144 | request, 'index') |
145 | |
146 | @user_has_privilege(u'featurer') |
147 | @get_user_media_entry |
148 | def promote_featured_media(request, media, **kwargs): |
149 | """ |
150 | A view to move a piece of media up the featured stack |
151 | """ |
152 | featured_media = FeaturedMedia.query.filter( |
153 | FeaturedMedia.media_entry_id == media.id).first() |
154 | if featured_media is not None: |
155 | promote_feature(media) |
156 | return redirect( |
157 | request, 'index') |
158 | |
159 | @user_has_privilege(u'featurer') |
160 | @get_user_media_entry |
161 | def demote_featured_media(request, media, **kwargs): |
162 | """ |
163 | A view to move a piece of media down the featured stack |
164 | """ |
165 | featured_media = FeaturedMedia.query.filter( |
166 | FeaturedMedia.media_entry_id == media.id).first() |
167 | if featured_media is not None: |
168 | demote_feature(media) |
169 | return redirect( |
170 | request, 'index') |
171 | |
172 | def get_root_view(): |
173 | return root_view |