Add media tagged "featured" to the front page
[mediagoblin-libreplanet.git] / mediagoblin_libreplanet / __init__.py
CommitLineData
724f8731
DT
1# MediaGoblin for LibrePlanet
2# Copyright (C) 2015 David Thompson <davet@gnu.org>
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
17import logging
18import os
19
20from mediagoblin import mg_globals
21from mediagoblin.tools.pluginapi import get_config, register_template_path, register_routes
22from mediagoblin.db.models import MediaEntry
23from mediagoblin.db.util import media_entries_for_tag_slug
24from mediagoblin.tools.pagination import Pagination
25from mediagoblin.tools.response import render_to_response
2530cc8e 26from mediagoblin.tools.licenses import SORTED_LICENSES, SUPPORTED_LICENSES, License
724f8731
DT
27from mediagoblin.decorators import uses_pagination, user_not_banned
28
2530cc8e
DT
29# Add CC BY-SA 4.0 to licenses
30cc_by_sa_4 = License("CC BY-SA 4.0",
31 "Creative Commons Attribution-ShareAlike 4.0 International",
32 "https://creativecommons.org/licenses/by-sa/4.0/")
33SORTED_LICENSES.insert(1, cc_by_sa_4)
34SUPPORTED_LICENSES[cc_by_sa_4.uri] = cc_by_sa_4
35
724f8731
DT
36PLUGIN_DIR = os.path.dirname(__file__)
37MAX_HOME_ITEMS = 20
724f8731
DT
38
39_log = logging.getLogger(__name__)
40
41# This is the function that gets called when the setup
42# hook fires.
43def setup_plugin():
44 _log.info("Setting up Libreplanet...")
45
46 # Register the template path.
47 register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
48
2fba5967
AE
49def lp_media_for_type(db, type, tag=None):
50 if (tag == None):
51 cursor = MediaEntry.query
52 else:
53 cursor = media_entries_for_tag_slug(db, tag)
54
55 return cursor.\
b1c32b81
DT
56 filter((MediaEntry.media_type == type)
57 & (MediaEntry.state == u'processed')).\
724f8731
DT
58 order_by(MediaEntry.created.desc()).\
59 limit(MAX_HOME_ITEMS)
60
61@user_not_banned
62def frontpage_view(request):
26712623
AE
63 images = lp_media_for_type(request.db, u'mediagoblin.media_types.image')
64 videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video')
65 featured_images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', "featured")
66 featured_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "featured")
724f8731
DT
67
68 return render_to_response(
69 request, 'libreplanet/root.html',
70 {'images': images,
71 'videos': videos,
26712623
AE
72 'featured_images': featured_images,
73 'featured_videos': featured_videos,
724f8731
DT
74 'allow_registration': mg_globals.app_config["allow_registration"]})
75
76def frontpage_view_hook():
77 return frontpage_view
78
79register_routes([('all-videos', '/videos',
7f884782 80 'mediagoblin.plugins.libreplanet.views:video_listing'),
724f8731 81 ('all-photos', '/photos',
35fd7d3b 82 'mediagoblin.plugins.libreplanet.views:image_listing'),
f10f1755 83 ('all-photos', '/videos/featured',
35fd7d3b 84 'mediagoblin.plugins.libreplanet.views:featured_video_listing'),
f10f1755
AE
85 ('all-photos', '/photos/featured',
86 'mediagoblin.plugins.libreplanet.views:featured_image_listing')
724f8731
DT
87 ])
88
89# This is a dict that specifies which hooks this plugin uses.
90# This one only uses one hook: setup.
91hooks = {
92 'setup': setup_plugin,
93 'frontpage_view': frontpage_view_hook
94}