2a0de2c7b15e9432e6696ddbe50802310650a94e
[mediagoblin-libreplanet.git] / mediagoblin_libreplanet / __init__.py
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
17 import logging
18 import os
19
20 from mediagoblin import mg_globals
21 from mediagoblin.tools.pluginapi import get_config, register_template_path, register_routes, register_template_hooks
22 from mediagoblin.db.models import MediaEntry
23 from mediagoblin.db.util import media_entries_for_tag_slug
24 from mediagoblin.tools.pagination import Pagination
25 from mediagoblin.tools.response import render_to_response
26 from mediagoblin.tools.licenses import SORTED_LICENSES, SUPPORTED_LICENSES, License
27 from mediagoblin.decorators import uses_pagination, user_not_banned
28
29 # Add three CC BY non-NC 4.0 to licenses
30 cc_by_4 = License("CC BY 4.0",
31 "Creative Commons Attribution 4.0 International",
32 "https://creativecommons.org/licenses/by/4.0/")
33 cc_by_sa_4 = License("CC BY-SA 4.0",
34 "Creative Commons Attribution-ShareAlike 4.0 International",
35 "https://creativecommons.org/licenses/by-sa/4.0/")
36 cc_by_nd_4 = License("CC BY-ND 4.0",
37 "Creative Commons Attribution-NoDerivatives 4.0 International",
38 "https://creativecommons.org/licenses/by-nd/4.0/")
39 SORTED_LICENSES.insert(1, cc_by_4)
40 SORTED_LICENSES.insert(2, cc_by_sa_4)
41 SORTED_LICENSES.insert(3, cc_by_nd_4)
42 SUPPORTED_LICENSES[cc_by_4.uri] = cc_by_4
43 SUPPORTED_LICENSES[cc_by_sa_4.uri] = cc_by_sa_4
44 SUPPORTED_LICENSES[cc_by_nd_4.uri] = cc_by_nd_4
45
46 PLUGIN_DIR = os.path.dirname(__file__)
47
48 MAX_HOME_ITEMS_DEFAULT = 10
49
50 MAX_HOME_ALL_VIDEO_ITEMS = 10
51 MAX_HOME_ALL_PHOTO_ITEMS = 20
52 MAX_HOME_FEATURED_ITEMS = 10
53 MAX_HOME_LP_ITEMS = 10
54
55 # make tags lowercase and use dashes in place of spaces.
56 # uppercase tags will be included by the lowercase form.
57 FEATURED_TAG = "featured"
58 LATEST_LP_VIDEO_TAG = "libreplanet-2016-video"
59
60
61 _log = logging.getLogger(__name__)
62
63 # This is the function that gets called when the setup
64 # hook fires.
65 def setup_plugin():
66 _log.info("Setting up Libreplanet...")
67
68 # Register the template path.
69 register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
70
71 def lp_media_for_type(db, type, tag=None, max_items=MAX_HOME_ITEMS_DEFAULT):
72 if (tag == None):
73 cursor = MediaEntry.query
74 else:
75 cursor = media_entries_for_tag_slug(db, tag)
76
77 return cursor.\
78 filter((MediaEntry.media_type == type)
79 & (MediaEntry.state == u'processed')).\
80 order_by(MediaEntry.created.desc()).\
81 limit(max_items)
82
83 @user_not_banned
84 def frontpage_view(request):
85 images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', None, MAX_HOME_ALL_PHOTO_ITEMS)
86 videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', None, MAX_HOME_ALL_VIDEO_ITEMS)
87
88 lp2016_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', LATEST_LP_VIDEO_TAG, MAX_HOME_LP_ITEMS)
89
90 featured_images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS)
91 featured_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS)
92
93 return render_to_response(
94 request, 'libreplanet/root.html',
95 {'images': images,
96 'videos': videos,
97 'lp2016_videos': lp2016_videos,
98 'featured_images': featured_images,
99 'featured_videos': featured_videos,
100 'allow_registration': mg_globals.app_config["allow_registration"]})
101
102 def frontpage_view_hook():
103 return frontpage_view
104
105 register_routes([('all-videos', '/videos',
106 'mediagoblin.plugins.libreplanet.views:video_listing'),
107 ('all-photos', '/photos',
108 'mediagoblin.plugins.libreplanet.views:image_listing'),
109
110 ('featured-videos', '/videos/featured',
111 'mediagoblin.plugins.libreplanet.views:featured_video_listing'),
112 ('featured-photos', '/photos/featured',
113 'mediagoblin.plugins.libreplanet.views:featured_image_listing')
114 ])
115
116 # This is a dict that specifies which hooks this plugin uses.
117 # This one only uses one hook: setup.
118 hooks = {
119 'setup': setup_plugin,
120 'frontpage_view': frontpage_view_hook
121 }
122
123 register_template_hooks(
124 {'header_left': "libreplanet/banner.html",
125 'header_extra': "libreplanet/join.html"}
126 )
127