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