feed.py
139 lines
| 5.3 KiB
| text/x-python
|
PythonLexer
Bradley M. Kuhn
|
r4187 | # -*- coding: utf-8 -*- | ||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU 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 General Public License for more details. | ||||
# | ||||
# You should have received a copy of the GNU General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
""" | ||||
kallithea.controllers.feed | ||||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||
Bradley M. Kuhn
|
r4212 | Feed controller for Kallithea | ||
Bradley M. Kuhn
|
r4187 | |||
Bradley M. Kuhn
|
r4211 | This file was forked by the Kallithea project in July 2014. | ||
Original author and date, and relevant copyright and licensing information is below: | ||||
Bradley M. Kuhn
|
r4187 | :created_on: Apr 23, 2010 | ||
:author: marcink | ||||
Bradley M. Kuhn
|
r4211 | :copyright: (c) 2013 RhodeCode GmbH, and others. | ||
Bradley M. Kuhn
|
r4208 | :license: GPLv3, see LICENSE.md for more details. | ||
Bradley M. Kuhn
|
r4187 | """ | ||
import logging | ||||
Mads Kiilerich
|
r7831 | from beaker.cache import cache_region | ||
Mads Kiilerich
|
r7718 | from tg import response | ||
from tg import tmpl_context as c | ||||
Mads Kiilerich
|
r6508 | from tg.i18n import ugettext as _ | ||
Bradley M. Kuhn
|
r4187 | from webhelpers.feedgenerator import Atom1Feed, Rss201rev2Feed | ||
Mads Kiilerich
|
r6411 | from kallithea import CONFIG | ||
Bradley M. Kuhn
|
r4187 | from kallithea.lib import helpers as h | ||
Mads Kiilerich
|
r7718 | from kallithea.lib.auth import HasRepoPermissionLevelDecorator, LoginRequired | ||
Bradley M. Kuhn
|
r4187 | from kallithea.lib.base import BaseRepoController | ||
Mads Kiilerich
|
r6839 | from kallithea.lib.diffs import DiffProcessor | ||
Mads Kiilerich
|
r7718 | from kallithea.lib.utils2 import safe_int, safe_unicode, str2bool | ||
Bradley M. Kuhn
|
r4187 | |||
log = logging.getLogger(__name__) | ||||
Mads Kiilerich
|
r6411 | language = 'en-us' | ||
ttl = "5" | ||||
Bradley M. Kuhn
|
r4187 | class FeedController(BaseRepoController): | ||
Mads Kiilerich
|
r7598 | @LoginRequired(allow_default_user=True) | ||
Søren Løvborg
|
r6471 | @HasRepoPermissionLevelDecorator('read') | ||
Thomas De Schampheleire
|
r6513 | def _before(self, *args, **kwargs): | ||
super(FeedController, self)._before(*args, **kwargs) | ||||
Bradley M. Kuhn
|
r4187 | |||
def _get_title(self, cs): | ||||
Mads Kiilerich
|
r5305 | return h.shorter(cs.message, 160) | ||
Bradley M. Kuhn
|
r4187 | |||
Mads Kiilerich
|
r6834 | def __get_desc(self, cs): | ||
desc_msg = [(_('%s committed on %s') | ||||
% (h.person(cs.author), h.fmt_date(cs.date))) + '<br/>'] | ||||
# branches, tags, bookmarks | ||||
Mads Kiilerich
|
r7067 | for branch in cs.branches: | ||
desc_msg.append('branch: %s<br/>' % branch) | ||||
Mads Kiilerich
|
r6834 | for book in cs.bookmarks: | ||
desc_msg.append('bookmark: %s<br/>' % book) | ||||
for tag in cs.tags: | ||||
desc_msg.append('tag: %s<br/>' % tag) | ||||
Bradley M. Kuhn
|
r4187 | changes = [] | ||
Mads Kiilerich
|
r6831 | diff_limit = safe_int(CONFIG.get('rss_cut_off_limit', 32 * 1024)) | ||
Mads Kiilerich
|
r6834 | raw_diff = cs.diff() | ||
diff_processor = DiffProcessor(raw_diff, | ||||
Mads Kiilerich
|
r6838 | diff_limit=diff_limit, | ||
inline_diff=False) | ||||
Bradley M. Kuhn
|
r4187 | |||
Mads Kiilerich
|
r6838 | for st in diff_processor.parsed: | ||
Bradley M. Kuhn
|
r4187 | st.update({'added': st['stats']['added'], | ||
'removed': st['stats']['deleted']}) | ||||
changes.append('\n %(operation)s %(filename)s ' | ||||
'(%(added)s lines added, %(removed)s lines removed)' | ||||
% st) | ||||
Mads Kiilerich
|
r6839 | if diff_processor.limited_diff: | ||
Bradley M. Kuhn
|
r4187 | changes = changes + ['\n ' + | ||
_('Changeset was too big and was cut off...')] | ||||
# rev link | ||||
Mads Kiilerich
|
r4445 | _url = h.canonical_url('changeset_home', repo_name=c.db_repo.repo_name, | ||
revision=cs.raw_id) | ||||
Bradley M. Kuhn
|
r4187 | desc_msg.append('changeset: <a href="%s">%s</a>' % (_url, cs.raw_id[:8])) | ||
desc_msg.append('<pre>') | ||||
Andrew Shadura
|
r4827 | desc_msg.append(h.urlify_text(cs.message)) | ||
Bradley M. Kuhn
|
r4187 | desc_msg.append('\n') | ||
desc_msg.extend(changes) | ||||
Mads Kiilerich
|
r6411 | if str2bool(CONFIG.get('rss_include_diff', False)): | ||
Bradley M. Kuhn
|
r4187 | desc_msg.append('\n\n') | ||
Mads Kiilerich
|
r6834 | desc_msg.append(raw_diff) | ||
Bradley M. Kuhn
|
r4187 | desc_msg.append('</pre>') | ||
return map(safe_unicode, desc_msg) | ||||
Mads Kiilerich
|
r7860 | def _feed(self, repo_name, kind, feed_factory): | ||
"""Produce a simple feed""" | ||||
Bradley M. Kuhn
|
r4187 | |||
Mads Kiilerich
|
r5737 | @cache_region('long_term', '_get_feed_from_cache') | ||
Mads Kiilerich
|
r7830 | def _get_feed_from_cache(*_cache_keys): # parameters are not really used - only as caching key | ||
Mads Kiilerich
|
r7860 | feed = feed_factory( | ||
Mads Kiilerich
|
r6411 | title=_('%s %s feed') % (c.site_name, repo_name), | ||
link=h.canonical_url('summary_home', repo_name=repo_name), | ||||
description=_('Changes on %s repository') % repo_name, | ||||
language=language, | ||||
Mads Kiilerich
|
r7860 | ttl=ttl, # rss only | ||
Bradley M. Kuhn
|
r4187 | ) | ||
Mads Kiilerich
|
r6411 | rss_items_per_page = safe_int(CONFIG.get('rss_items_per_page', 20)) | ||
for cs in reversed(list(c.db_repo_scm_instance[-rss_items_per_page:])): | ||||
Bradley M. Kuhn
|
r4187 | feed.add_item(title=self._get_title(cs), | ||
Mads Kiilerich
|
r4445 | link=h.canonical_url('changeset_home', repo_name=repo_name, | ||
revision=cs.raw_id), | ||||
Bradley M. Kuhn
|
r4187 | author_name=cs.author, | ||
description=''.join(self.__get_desc(cs)), | ||||
pubdate=cs.date, | ||||
) | ||||
response.content_type = feed.mime_type | ||||
return feed.writeString('utf-8') | ||||
Mads Kiilerich
|
r7831 | return _get_feed_from_cache(repo_name, kind, c.db_repo.changeset_cache.get('raw_id')) | ||
Bradley M. Kuhn
|
r4187 | |||
Mads Kiilerich
|
r7860 | def atom(self, repo_name): | ||
"""Produce a simple atom-1.0 feed""" | ||||
return self._feed(repo_name, 'ATOM', Atom1Feed) | ||||
Bradley M. Kuhn
|
r4187 | def rss(self, repo_name): | ||
"""Produce an rss2 feed via feedgenerator module""" | ||||
Mads Kiilerich
|
r7860 | return self._feed(repo_name, 'RSS', Rss201rev2Feed) | ||