5af3195b734d372f3b64554428d446a687ba4435
[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 gfdl_1_3 = License("GFDL 1.3",
40 "GNU Free Documentation License 1.3",
41 "https://www.gnu.org/licenses/fdl-1.3.en.html")
42 see_desc = License("See description for license",
43 "See description for license",
44 "./")
45 SORTED_LICENSES.insert(1, see_desc)
46 SORTED_LICENSES.insert(2, cc_by_4)
47 SORTED_LICENSES.insert(3, cc_by_sa_4)
48 SORTED_LICENSES.insert(4, cc_by_nd_4)
49 SORTED_LICENSES.insert(5, gfdl_1_3)
50 SUPPORTED_LICENSES[see_desc.uri] = see_desc
51 SUPPORTED_LICENSES[cc_by_4.uri] = cc_by_4
52 SUPPORTED_LICENSES[cc_by_sa_4.uri] = cc_by_sa_4
53 SUPPORTED_LICENSES[cc_by_nd_4.uri] = cc_by_nd_4
54 SUPPORTED_LICENSES[gfdl_1_3.uri] = gfdl_1_3
55
56 PLUGIN_DIR = os.path.dirname(__file__)
57
58 MAX_HOME_ITEMS_DEFAULT = 10
59
60 MAX_HOME_ALL_VIDEO_ITEMS = 10
61 MAX_HOME_ALL_PHOTO_ITEMS = 20
62 MAX_HOME_FEATURED_ITEMS = 10
63 MAX_HOME_LP_ITEMS = 10
64
65 # make tags lowercase and use dashes in place of spaces.
66 # uppercase tags will be included by the lowercase form.
67 FEATURED_TAG = "featured"
68
69
70 _log = logging.getLogger(__name__)
71
72 # This is the function that gets called when the setup
73 # hook fires.
74 def setup_plugin():
75 _log.info("Setting up Libreplanet...")
76
77 # Register the template path.
78 register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
79
80 def lp_media_for_type(db, type, tag=None, max_items=MAX_HOME_ITEMS_DEFAULT):
81 if (tag == None):
82 cursor = MediaEntry.query
83 else:
84 cursor = media_entries_for_tag_slug(db, tag)
85
86 return cursor.\
87 filter((MediaEntry.media_type == type)
88 & (MediaEntry.state == u'processed')).\
89 order_by(MediaEntry.created.desc()).\
90 limit(max_items)
91
92 @user_not_banned
93 def frontpage_view(request):
94 images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', None, MAX_HOME_ALL_PHOTO_ITEMS)
95 videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', None, MAX_HOME_ALL_VIDEO_ITEMS)
96
97 lp2017_keynotes = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2017-keynote", MAX_HOME_LP_ITEMS)
98 lp2017_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2017-video", MAX_HOME_LP_ITEMS)
99
100 lp2016_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2016-video", MAX_HOME_LP_ITEMS)
101
102 featured_images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS)
103 featured_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS)
104
105 return render_to_response(
106 request, 'libreplanet/root.html',
107 {'images': images,
108 'videos': videos,
109 'lp2017_keynotes': lp2017_keynotes,
110 'lp2017_videos': lp2017_videos,
111 'lp2016_videos': lp2016_videos,
112 'featured_images': featured_images,
113 'featured_videos': featured_videos,
114 'allow_registration': mg_globals.app_config["allow_registration"]})
115
116 def frontpage_view_hook():
117 return frontpage_view
118
119 register_routes([('all-videos', '/videos',
120 'mediagoblin.plugins.libreplanet.views:video_listing'),
121 ('all-photos', '/photos',
122 'mediagoblin.plugins.libreplanet.views:image_listing'),
123
124 ('featured-videos', '/videos/featured',
125 'mediagoblin.plugins.libreplanet.views:featured_video_listing'),
126 ('featured-photos', '/photos/featured',
127 'mediagoblin.plugins.libreplanet.views:featured_image_listing')
128 ])
129
130 # This is a dict that specifies which hooks this plugin uses.
131 # This one only uses one hook: setup.
132 hooks = {
133 'setup': setup_plugin,
134 'frontpage_view': frontpage_view_hook
135 }
136
137 register_template_hooks(
138 {'header_left': "libreplanet/banner.html",
139 'header_extra': "libreplanet/join.html"}
140 )
141