# HG changeset patch # User Mads Kiilerich # Date 2017-01-22 00:16:52 # Node ID 3fcb60a152f36b1a201ea23bd74c60bca45415d9 # Parent 26bc2f02d9cdcd8aa3ae424effe4bd33b0972d39 controllers: avoid setting constants as controller instance variables in __before__ Setting constants in __before__ is a weird pattern and we can do fine without doing it. That makes it more clear that there is no state in the controller instances. Real constants can just be set at the module level. Some values depend on configuration and can thus probably not be set as constants at module import time but could perhaps be set in __init__. But reading configuration directly when needed will probably be just as good. diff --git a/kallithea/controllers/feed.py b/kallithea/controllers/feed.py --- a/kallithea/controllers/feed.py +++ b/kallithea/controllers/feed.py @@ -34,6 +34,7 @@ from pylons.i18n.translation import _ from beaker.cache import cache_region, region_invalidate from webhelpers.feedgenerator import Atom1Feed, Rss201rev2Feed +from kallithea import CONFIG from kallithea.lib import helpers as h from kallithea.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator from kallithea.lib.base import BaseRepoController @@ -44,6 +45,10 @@ from kallithea.lib.utils2 import safe_in log = logging.getLogger(__name__) +language = 'en-us' +ttl = "5" + + class FeedController(BaseRepoController): @LoginRequired(api_access=True) @@ -51,26 +56,15 @@ class FeedController(BaseRepoController) 'repository.admin') def __before__(self): super(FeedController, self).__before__() - #common values for feeds - self.description = _('Changes on %s repository') - self.title = self.title = _('%s %s feed') % (c.site_name, '%s') - self.language = 'en-us' - self.ttl = "5" - import kallithea - CONF = kallithea.CONFIG - self.include_diff = str2bool(CONF.get('rss_include_diff', False)) - self.feed_nr = safe_int(CONF.get('rss_items_per_page', 20)) - # we need to protect from parsing huge diffs here other way - # we can kill the server - self.feed_diff_limit = safe_int(CONF.get('rss_cut_off_limit', 32 * 1024)) def _get_title(self, cs): return h.shorter(cs.message, 160) def __changes(self, cs): changes = [] + rss_cut_off_limit = safe_int(CONFIG.get('rss_cut_off_limit', 32 * 1024)) diff_processor = DiffProcessor(cs.diff(), - diff_limit=self.feed_diff_limit) + diff_limit=rss_cut_off_limit) _parsed = diff_processor.prepare(inline_diff=False) limited_diff = False if isinstance(_parsed, LimitedDiffContainer): @@ -108,7 +102,7 @@ class FeedController(BaseRepoController) desc_msg.append(h.urlify_text(cs.message)) desc_msg.append('\n') desc_msg.extend(changes) - if self.include_diff: + if str2bool(CONFIG.get('rss_include_diff', False)): desc_msg.append('\n\n') desc_msg.append(diff_processor.as_raw()) desc_msg.append('') @@ -120,14 +114,15 @@ class FeedController(BaseRepoController) @cache_region('long_term', '_get_feed_from_cache') def _get_feed_from_cache(key, kind): feed = Atom1Feed( - title=self.title % repo_name, - link=h.canonical_url('summary_home', repo_name=repo_name), - description=self.description % repo_name, - language=self.language, - ttl=self.ttl + 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, + ttl=ttl ) - for cs in reversed(list(c.db_repo_scm_instance[-self.feed_nr:])): + 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:])): feed.add_item(title=self._get_title(cs), link=h.canonical_url('changeset_home', repo_name=repo_name, revision=cs.raw_id), @@ -151,14 +146,15 @@ class FeedController(BaseRepoController) @cache_region('long_term', '_get_feed_from_cache') def _get_feed_from_cache(key, kind): feed = Rss201rev2Feed( - title=self.title % repo_name, + title=_('%s %s feed') % (c.site_name, repo_name), link=h.canonical_url('summary_home', repo_name=repo_name), - description=self.description % repo_name, - language=self.language, - ttl=self.ttl + description=_('Changes on %s repository') % repo_name, + language=language, + ttl=ttl ) - for cs in reversed(list(c.db_repo_scm_instance[-self.feed_nr:])): + 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:])): feed.add_item(title=self._get_title(cs), link=h.canonical_url('changeset_home', repo_name=repo_name, revision=cs.raw_id), diff --git a/kallithea/controllers/journal.py b/kallithea/controllers/journal.py --- a/kallithea/controllers/journal.py +++ b/kallithea/controllers/journal.py @@ -55,13 +55,15 @@ from kallithea.lib.utils2 import safe_in log = logging.getLogger(__name__) +language = 'en-us' +ttl = "5" +feed_nr = 20 + + class JournalController(BaseController): def __before__(self): super(JournalController, self).__before__() - self.language = 'en-us' - self.ttl = "5" - self.feed_nr = 20 c.search_term = request.GET.get('filter') def _get_daily_aggregate(self, journal): @@ -118,10 +120,10 @@ class JournalController(BaseController): feed = Atom1Feed(title=_desc, link=_link, description=_desc, - language=self.language, - ttl=self.ttl) + language=language, + ttl=ttl) - for entry in journal[:self.feed_nr]: + for entry in journal[:feed_nr]: user = entry.user if user is None: #fix deleted users @@ -160,10 +162,10 @@ class JournalController(BaseController): feed = Rss201rev2Feed(title=_desc, link=_link, description=_desc, - language=self.language, - ttl=self.ttl) + language=language, + ttl=ttl) - for entry in journal[:self.feed_nr]: + for entry in journal[:feed_nr]: user = entry.user if user is None: #fix deleted users