Commit | Line | Data |
---|---|---|
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 | ||
17 | import logging | |
18 | import os | |
19 | ||
20 | from mediagoblin import mg_globals | |
a0364380 | 21 | from mediagoblin.tools.pluginapi import get_config, register_template_path, register_routes, register_template_hooks |
724f8731 | 22 | from mediagoblin.db.models import MediaEntry |
e39ca90b | 23 | from mediagoblin.db.util import media_entries_for_tag_slug |
724f8731 DT |
24 | from mediagoblin.tools.pagination import Pagination |
25 | from mediagoblin.tools.response import render_to_response | |
2530cc8e | 26 | from mediagoblin.tools.licenses import SORTED_LICENSES, SUPPORTED_LICENSES, License |
724f8731 DT |
27 | from mediagoblin.decorators import uses_pagination, user_not_banned |
28 | ||
4c03fef2 AE |
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/") | |
2530cc8e DT |
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/") | |
4c03fef2 AE |
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/") | |
4fdf57b4 AE |
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") | |
4c03fef2 AE |
42 | SORTED_LICENSES.insert(1, cc_by_4) |
43 | SORTED_LICENSES.insert(2, cc_by_sa_4) | |
44 | SORTED_LICENSES.insert(3, cc_by_nd_4) | |
4fdf57b4 | 45 | SORTED_LICENSES.insert(4, gfdl_1_3) |
4c03fef2 | 46 | SUPPORTED_LICENSES[cc_by_4.uri] = cc_by_4 |
2530cc8e | 47 | SUPPORTED_LICENSES[cc_by_sa_4.uri] = cc_by_sa_4 |
4c03fef2 | 48 | SUPPORTED_LICENSES[cc_by_nd_4.uri] = cc_by_nd_4 |
4fdf57b4 | 49 | SUPPORTED_LICENSES[gfdl_1_3.uri] = gfdl_1_3 |
2530cc8e | 50 | |
724f8731 | 51 | PLUGIN_DIR = os.path.dirname(__file__) |
ae3cb218 | 52 | |
7c3fc209 AE |
53 | MAX_HOME_ITEMS_DEFAULT = 10 |
54 | ||
55 | MAX_HOME_ALL_VIDEO_ITEMS = 10 | |
56 | MAX_HOME_ALL_PHOTO_ITEMS = 20 | |
2cfcf16b | 57 | MAX_HOME_FEATURED_ITEMS = 10 |
88cb2585 AE |
58 | MAX_HOME_LP_ITEMS = 10 |
59 | ||
a356d743 AE |
60 | # make tags lowercase and use dashes in place of spaces. |
61 | # uppercase tags will be included by the lowercase form. | |
ae3cb218 | 62 | FEATURED_TAG = "featured" |
a356d743 | 63 | |
724f8731 DT |
64 | |
65 | _log = logging.getLogger(__name__) | |
66 | ||
67 | # This is the function that gets called when the setup | |
68 | # hook fires. | |
69 | def setup_plugin(): | |
70 | _log.info("Setting up Libreplanet...") | |
71 | ||
72 | # Register the template path. | |
73 | register_template_path(os.path.join(PLUGIN_DIR, 'templates')) | |
74 | ||
7c3fc209 | 75 | def lp_media_for_type(db, type, tag=None, max_items=MAX_HOME_ITEMS_DEFAULT): |
2fba5967 AE |
76 | if (tag == None): |
77 | cursor = MediaEntry.query | |
78 | else: | |
e39ca90b | 79 | cursor = media_entries_for_tag_slug(db, tag) |
2fba5967 AE |
80 | |
81 | return cursor.\ | |
b1c32b81 DT |
82 | filter((MediaEntry.media_type == type) |
83 | & (MediaEntry.state == u'processed')).\ | |
724f8731 | 84 | order_by(MediaEntry.created.desc()).\ |
2cfcf16b | 85 | limit(max_items) |
724f8731 DT |
86 | |
87 | @user_not_banned | |
88 | def frontpage_view(request): | |
7c3fc209 AE |
89 | images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', None, MAX_HOME_ALL_PHOTO_ITEMS) |
90 | videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', None, MAX_HOME_ALL_VIDEO_ITEMS) | |
88cb2585 | 91 | |
34b22c5e AE |
92 | lp2017_keynotes = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2017-keynote", MAX_HOME_LP_ITEMS) |
93 | lp2017_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2017-video", MAX_HOME_LP_ITEMS) | |
94 | ||
95 | lp2016_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2016-video", MAX_HOME_LP_ITEMS) | |
88cb2585 | 96 | |
ae3cb218 AE |
97 | featured_images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS) |
98 | featured_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS) | |
724f8731 DT |
99 | |
100 | return render_to_response( | |
101 | request, 'libreplanet/root.html', | |
102 | {'images': images, | |
103 | 'videos': videos, | |
bfedf48a | 104 | 'lp2016_videos': lp2016_videos, |
26712623 AE |
105 | 'featured_images': featured_images, |
106 | 'featured_videos': featured_videos, | |
724f8731 DT |
107 | 'allow_registration': mg_globals.app_config["allow_registration"]}) |
108 | ||
109 | def frontpage_view_hook(): | |
110 | return frontpage_view | |
111 | ||
112 | register_routes([('all-videos', '/videos', | |
7f884782 | 113 | 'mediagoblin.plugins.libreplanet.views:video_listing'), |
724f8731 | 114 | ('all-photos', '/photos', |
35fd7d3b | 115 | 'mediagoblin.plugins.libreplanet.views:image_listing'), |
c8265415 | 116 | |
86b9f47d | 117 | ('featured-videos', '/videos/featured', |
35fd7d3b | 118 | 'mediagoblin.plugins.libreplanet.views:featured_video_listing'), |
86b9f47d | 119 | ('featured-photos', '/photos/featured', |
3d204960 | 120 | 'mediagoblin.plugins.libreplanet.views:featured_image_listing') |
724f8731 DT |
121 | ]) |
122 | ||
123 | # This is a dict that specifies which hooks this plugin uses. | |
124 | # This one only uses one hook: setup. | |
125 | hooks = { | |
126 | 'setup': setup_plugin, | |
127 | 'frontpage_view': frontpage_view_hook | |
128 | } | |
a0364380 AE |
129 | |
130 | register_template_hooks( | |
2b91cd63 AE |
131 | {'header_left': "libreplanet/banner.html", |
132 | 'header_extra': "libreplanet/join.html"} | |
a0364380 AE |
133 | ) |
134 |