1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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
24 from mediagoblin
.plugins
.archivalook
.tools
import (
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
,
34 from mediagoblin
.tools
.translate
import lazy_pass_to_ugettext
as _
37 def root_view(request
):
39 This is an alternative to the typical root view. This display centers around
40 displaying 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()}
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
})
59 @user_has_privilege(u
'featurer')
60 def featured_media_panel(request
):
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.
66 form
= archivalook_forms
.FeaturedMediaList(request
.form
)
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:
78 target
.display_type
= display_type
79 previous_features
.remove(target
)
82 new_feature
= FeaturedMedia(
83 media_entry
=media_entry
,
84 display_type
=display_type
,
86 Session
.add(new_feature
)
87 [Session
.delete(feature
) for feature
in previous_features
]
91 form
.box_content
.data
= create_featured_media_textbox()
92 return render_to_response(
93 request
, 'archivalook/feature.html',
98 def recent_media_gallery_view(request
, page
):
100 The replaced homepage is available through this view.
102 cursor
= MediaEntry
.query
.filter_by(state
=u
'processed').\
103 order_by(MediaEntry
.created
.desc())
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
})
112 def add_featured_media_to_media_home(context
):
114 A context hook which allows the media home page to know whether the media
115 has been featured or not.
117 context
['featured_media'] = FeaturedMedia
.query
120 @user_has_privilege(u
'featurer')
121 @get_user_media_entry
122 def feature_media(request
, media
, **kwargs
):
124 A view to feature a new piece of media
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
)
133 @user_has_privilege(u
'featurer')
134 @get_user_media_entry
135 def unfeature_media(request
, media
, **kwargs
):
137 A view to unfeature a piece of media which has previously been featured.
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
)
146 @user_has_privilege(u
'featurer')
147 @get_user_media_entry
148 def promote_featured_media(request
, media
, **kwargs
):
150 A view to move a piece of media up the featured stack
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
)
159 @user_has_privilege(u
'featurer')
160 @get_user_media_entry
161 def demote_featured_media(request
, media
, **kwargs
):
163 A view to move a piece of media down the featured stack
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
)