# MediaGoblin for LibrePlanet # Copyright (C) 2015 David Thompson # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . import logging import os from mediagoblin import mg_globals from mediagoblin.tools.pluginapi import get_config, register_template_path, register_routes, register_template_hooks from mediagoblin.db.models import MediaEntry from mediagoblin.db.util import media_entries_for_tag_slug from mediagoblin.tools.pagination import Pagination from mediagoblin.tools.response import render_to_response from mediagoblin.tools.licenses import SORTED_LICENSES, SUPPORTED_LICENSES, License from mediagoblin.decorators import uses_pagination, user_not_banned # Add three CC BY non-NC 4.0 to licenses cc_by_4 = License("CC BY 4.0", "Creative Commons Attribution 4.0 International", "https://creativecommons.org/licenses/by/4.0/") cc_by_sa_4 = License("CC BY-SA 4.0", "Creative Commons Attribution-ShareAlike 4.0 International", "https://creativecommons.org/licenses/by-sa/4.0/") cc_by_nd_4 = License("CC BY-ND 4.0", "Creative Commons Attribution-NoDerivatives 4.0 International", "https://creativecommons.org/licenses/by-nd/4.0/") gfdl_1_3 = License("GFDL 1.3", "GNU Free Documentation License 1.3", "https://www.gnu.org/licenses/fdl-1.3.en.html") SORTED_LICENSES.insert(1, cc_by_4) SORTED_LICENSES.insert(2, cc_by_sa_4) SORTED_LICENSES.insert(3, cc_by_nd_4) SORTED_LICENSES.insert(4, gfdl_1_3) SUPPORTED_LICENSES[cc_by_4.uri] = cc_by_4 SUPPORTED_LICENSES[cc_by_sa_4.uri] = cc_by_sa_4 SUPPORTED_LICENSES[cc_by_nd_4.uri] = cc_by_nd_4 SUPPORTED_LICENSES[gfdl_1_3.uri] = gfdl_1_3 PLUGIN_DIR = os.path.dirname(__file__) MAX_HOME_ITEMS_DEFAULT = 10 MAX_HOME_ALL_VIDEO_ITEMS = 10 MAX_HOME_ALL_PHOTO_ITEMS = 20 MAX_HOME_FEATURED_ITEMS = 10 MAX_HOME_LP_ITEMS = 10 # make tags lowercase and use dashes in place of spaces. # uppercase tags will be included by the lowercase form. FEATURED_TAG = "featured" _log = logging.getLogger(__name__) # This is the function that gets called when the setup # hook fires. def setup_plugin(): _log.info("Setting up Libreplanet...") # Register the template path. register_template_path(os.path.join(PLUGIN_DIR, 'templates')) def lp_media_for_type(db, type, tag=None, max_items=MAX_HOME_ITEMS_DEFAULT): if (tag == None): cursor = MediaEntry.query else: cursor = media_entries_for_tag_slug(db, tag) return cursor.\ filter((MediaEntry.media_type == type) & (MediaEntry.state == u'processed')).\ order_by(MediaEntry.created.desc()).\ limit(max_items) @user_not_banned def frontpage_view(request): images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', None, MAX_HOME_ALL_PHOTO_ITEMS) videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', None, MAX_HOME_ALL_VIDEO_ITEMS) lp2017_keynotes = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2017-keynote", MAX_HOME_LP_ITEMS) lp2017_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2017-video", MAX_HOME_LP_ITEMS) lp2016_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', "libreplanet-2016-video", MAX_HOME_LP_ITEMS) featured_images = lp_media_for_type(request.db, u'mediagoblin.media_types.image', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS) featured_videos = lp_media_for_type(request.db, u'mediagoblin.media_types.video', FEATURED_TAG, MAX_HOME_FEATURED_ITEMS) return render_to_response( request, 'libreplanet/root.html', {'images': images, 'videos': videos, 'lp2017_keynotes': lp2017_keynotes, 'lp2017_videos': lp2017_videos, 'lp2016_videos': lp2016_videos, 'featured_images': featured_images, 'featured_videos': featured_videos, 'allow_registration': mg_globals.app_config["allow_registration"]}) def frontpage_view_hook(): return frontpage_view register_routes([('all-videos', '/videos', 'mediagoblin.plugins.libreplanet.views:video_listing'), ('all-photos', '/photos', 'mediagoblin.plugins.libreplanet.views:image_listing'), ('featured-videos', '/videos/featured', 'mediagoblin.plugins.libreplanet.views:featured_video_listing'), ('featured-photos', '/photos/featured', 'mediagoblin.plugins.libreplanet.views:featured_image_listing') ]) # This is a dict that specifies which hooks this plugin uses. # This one only uses one hook: setup. hooks = { 'setup': setup_plugin, 'frontpage_view': frontpage_view_hook } register_template_hooks( {'header_left': "libreplanet/banner.html", 'header_extra': "libreplanet/join.html"} )