Shorten tag name.
[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
23from mediagoblin.db.util import media_entries_for_tag_slug
24from mediagoblin.tools.pagination import Pagination
25from mediagoblin.tools.response import render_to_response
26from mediagoblin.decorators import uses_pagination, user_not_banned
27
28PLUGIN_DIR = os.path.dirname(__file__)
29MAX_HOME_ITEMS = 20
b1995f9a 30LP_TAG = 'lp2015'
724f8731
DT
31
32_log = logging.getLogger(__name__)
33
34# This is the function that gets called when the setup
35# hook fires.
36def setup_plugin():
37 _log.info("Setting up Libreplanet...")
38
39 # Register the template path.
40 register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
41
42def lp_media_for_type(db, type):
e3e1899c 43 return media_entries_for_tag_slug(db, LP_TAG).\
b1c32b81
DT
44 filter((MediaEntry.media_type == type)
45 & (MediaEntry.state == u'processed')).\
724f8731
DT
46 order_by(MediaEntry.created.desc()).\
47 limit(MAX_HOME_ITEMS)
48
49@user_not_banned
50def frontpage_view(request):
51 images = lp_media_for_type(request.db, u'mediagoblin.media_types.image')
52 videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video')
53
54 return render_to_response(
55 request, 'libreplanet/root.html',
56 {'images': images,
57 'videos': videos,
58 'allow_registration': mg_globals.app_config["allow_registration"]})
59
60def frontpage_view_hook():
61 return frontpage_view
62
63register_routes([('all-videos', '/videos',
64 'mediagoblin_libreplanet.views:video_listing'),
65 ('all-photos', '/photos',
66 'mediagoblin_libreplanet.views:image_listing')
67 ])
68
69# This is a dict that specifies which hooks this plugin uses.
70# This one only uses one hook: setup.
71hooks = {
72 'setup': setup_plugin,
73 'frontpage_view': frontpage_view_hook
74}