1 # MediaGoblin for LibrePlanet
2 # Copyright (C) 2015 David Thompson <davet@gnu.org>
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/>.
20 from mediagoblin
import mg_globals
21 from mediagoblin
.tools
.pluginapi
import get_config
, register_template_path
, register_routes
22 from mediagoblin
.db
.models
import MediaEntry
23 from mediagoblin
.db
.util
import media_entries_for_tag_slug
24 from lp_helper
import media_entries_for_tag_slug_case_insensitive
25 from mediagoblin
.tools
.pagination
import Pagination
26 from mediagoblin
.tools
.response
import render_to_response
27 from mediagoblin
.tools
.licenses
import SORTED_LICENSES
, SUPPORTED_LICENSES
, License
28 from mediagoblin
.decorators
import uses_pagination
, user_not_banned
30 # Add CC BY-SA 4.0 to licenses
31 cc_by_sa_4
= License("CC BY-SA 4.0",
32 "Creative Commons Attribution-ShareAlike 4.0 International",
33 "https://creativecommons.org/licenses/by-sa/4.0/")
34 SORTED_LICENSES
.insert(1, cc_by_sa_4
)
35 SUPPORTED_LICENSES
[cc_by_sa_4
.uri
] = cc_by_sa_4
37 PLUGIN_DIR
= os
.path
.dirname(__file__
)
40 MAX_HOME_FEATURED_ITEMS
= 10
41 MAX_HOME_LP_ITEMS
= 10
43 FEATURED_TAG
= "featured"
44 LATEST_LP_VIDEO_TAG
= "LibrePlanet 2016 video"
46 _log
= logging
.getLogger(__name__
)
48 # This is the function that gets called when the setup
51 _log
.info("Setting up Libreplanet...")
53 # Register the template path.
54 register_template_path(os
.path
.join(PLUGIN_DIR
, 'templates'))
56 def lp_media_for_type(db
, type, tag
=None, max_items
=MAX_HOME_ITEMS
):
58 cursor
= MediaEntry
.query
60 ## case insensitive tag search is not working yet -- sudoman
61 #cursor = media_entries_for_tag_slug_case_insensitive(db, tag)
62 cursor
= media_entries_for_tag_slug(db
, tag
)
65 filter((MediaEntry
.media_type
== type)
66 & (MediaEntry
.state
== u
'processed')).\
67 order_by(MediaEntry
.created
.desc()).\
71 def frontpage_view(request
):
72 images
= lp_media_for_type(request
.db
, u
'mediagoblin.media_types.image')
73 videos
= lp_media_for_type(request
.db
, u
'mediagoblin.media_types.video')
75 lp2016_videos
= lp_media_for_type(request
.db
, u
'mediagoblin.media_types.video', LATEST_LP_VIDEO_TAG
, MAX_HOME_LP_ITEMS
)
77 featured_images
= lp_media_for_type(request
.db
, u
'mediagoblin.media_types.image', FEATURED_TAG
, MAX_HOME_FEATURED_ITEMS
)
78 featured_videos
= lp_media_for_type(request
.db
, u
'mediagoblin.media_types.video', FEATURED_TAG
, MAX_HOME_FEATURED_ITEMS
)
80 return render_to_response(
81 request
, 'libreplanet/root.html',
84 'featured_images': featured_images
,
85 'featured_videos': featured_videos
,
86 'allow_registration': mg_globals
.app_config
["allow_registration"]})
88 def frontpage_view_hook():
91 register_routes([('all-videos', '/videos',
92 'mediagoblin.plugins.libreplanet.views:video_listing'),
93 ('all-photos', '/photos',
94 'mediagoblin.plugins.libreplanet.views:image_listing'),
96 ('featured-videos', '/videos/featured',
97 'mediagoblin.plugins.libreplanet.views:featured_video_listing'),
98 ('featured-photos', '/photos/featured',
99 'mediagoblin.plugins.libreplanet.views:featured_image_listing')
102 # This is a dict that specifies which hooks this plugin uses.
103 # This one only uses one hook: setup.
105 'setup': setup_plugin
,
106 'frontpage_view': frontpage_view_hook