added missing template variable association
[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
e39ca90b 23from mediagoblin.db.util import media_entries_for_tag_slug
0319c127 24from lp_helper import media_entries_for_tag_slug_case_insensitive
724f8731
DT
25from mediagoblin.tools.pagination import Pagination
26from mediagoblin.tools.response import render_to_response
2530cc8e 27from mediagoblin.tools.licenses import SORTED_LICENSES, SUPPORTED_LICENSES, License
724f8731
DT
28from mediagoblin.decorators import uses_pagination, user_not_banned
29
2530cc8e
DT
30# Add CC BY-SA 4.0 to licenses
31cc_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/")
34SORTED_LICENSES.insert(1, cc_by_sa_4)
35SUPPORTED_LICENSES[cc_by_sa_4.uri] = cc_by_sa_4
36
724f8731 37PLUGIN_DIR = os.path.dirname(__file__)
ae3cb218 38
724f8731 39MAX_HOME_ITEMS = 20
2cfcf16b 40MAX_HOME_FEATURED_ITEMS = 10
88cb2585
AE
41MAX_HOME_LP_ITEMS = 10
42
ae3cb218 43FEATURED_TAG = "featured"
88cb2585 44LATEST_LP_VIDEO_TAG = "LibrePlanet 2016 video"
724f8731
DT
45
46_log = logging.getLogger(__name__)
47
48# This is the function that gets called when the setup
49# hook fires.
50def setup_plugin():
51 _log.info("Setting up Libreplanet...")
52
53 # Register the template path.
54 register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
55
2cfcf16b 56def lp_media_for_type(db, type, tag=None, max_items=MAX_HOME_ITEMS):
2fba5967
AE
57 if (tag == None):
58 cursor = MediaEntry.query
59 else:
e39ca90b
AE
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)
2fba5967
AE
63
64 return cursor.\
b1c32b81
DT
65 filter((MediaEntry.media_type == type)
66 & (MediaEntry.state == u'processed')).\
724f8731 67 order_by(MediaEntry.created.desc()).\
2cfcf16b 68 limit(max_items)
724f8731
DT
69
70@user_not_banned
71def frontpage_view(request):
26712623
AE
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')
88cb2585 74
bfedf48a 75 lp2016_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', LATEST_LP_VIDEO_TAG, MAX_HOME_LP_ITEMS)
88cb2585 76
ae3cb218
AE
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)
724f8731
DT
79
80 return render_to_response(
81 request, 'libreplanet/root.html',
82 {'images': images,
83 'videos': videos,
bfedf48a 84 'lp2016_videos': lp2016_videos,
26712623
AE
85 'featured_images': featured_images,
86 'featured_videos': featured_videos,
724f8731
DT
87 'allow_registration': mg_globals.app_config["allow_registration"]})
88
89def frontpage_view_hook():
90 return frontpage_view
91
92register_routes([('all-videos', '/videos',
7f884782 93 'mediagoblin.plugins.libreplanet.views:video_listing'),
724f8731 94 ('all-photos', '/photos',
35fd7d3b 95 'mediagoblin.plugins.libreplanet.views:image_listing'),
c8265415 96
86b9f47d 97 ('featured-videos', '/videos/featured',
35fd7d3b 98 'mediagoblin.plugins.libreplanet.views:featured_video_listing'),
86b9f47d 99 ('featured-photos', '/photos/featured',
3d204960 100 'mediagoblin.plugins.libreplanet.views:featured_image_listing')
724f8731
DT
101 ])
102
103# This is a dict that specifies which hooks this plugin uses.
104# This one only uses one hook: setup.
105hooks = {
106 'setup': setup_plugin,
107 'frontpage_view': frontpage_view_hook
108}