##// END OF EJS Templates
git: adjusted code for new libgit2 backend...
git: adjusted code for new libgit2 backend - made @CachedProperty use a custom decarator that can do invalidations. This is required for tests - made few cleanups of code since we use a simpler libgit2 backend now - removed some unused obsolete code

File last commit:

r3810:d0cfe4e5 stable
r3842:8bd67598 default
Show More
repo_feed.py
238 lines | 9.4 KiB | text/x-python | PythonLexer
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 # -*- coding: utf-8 -*-
docs: updated copyrights to 2019
r3363 # Copyright (C) 2017-2019 RhodeCode GmbH
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# 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 Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import pytz
import logging
from pyramid.view import view_config
from pyramid.response import Response
from webhelpers.feedgenerator import Rss201rev2Feed, Atom1Feed
from rhodecode.apps._base import RepoAppView
from rhodecode.lib import audit_logger
caches: new cache context managers....
r2932 from rhodecode.lib import rc_cache
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 from rhodecode.lib import helpers as h
repositories: rewrote whole admin section to pyramid....
r2014 from rhodecode.lib.auth import (
LoginRequired, HasRepoPermissionAnyDecorator)
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 from rhodecode.lib.diffs import DiffProcessor, LimitedDiffContainer
feeds: generate entries with proper unique ids....
r2071 from rhodecode.lib.utils2 import str2bool, safe_int, md5_safe
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 from rhodecode.model.db import UserApiKeys, CacheKey
log = logging.getLogger(__name__)
class RepoFeedView(RepoAppView):
def load_default_context(self):
c = self._get_local_tmpl_context()
pylons: remove pylons as dependency...
r2351
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 self._load_defaults()
return c
def _get_config(self):
import rhodecode
config = rhodecode.CONFIG
return {
'language': 'en-us',
'feed_ttl': '5', # TTL of feed,
'feed_include_diff':
str2bool(config.get('rss_include_diff', False)),
'feed_items_per_page':
safe_int(config.get('rss_items_per_page', 20)),
'feed_diff_limit':
# we need to protect from parsing huge diffs here other way
# we can kill the server
safe_int(config.get('rss_cut_off_limit', 32 * 1024)),
}
def _load_defaults(self):
_ = self.request.translate
config = self._get_config()
# common values for feeds
self.description = _('Changes on %s repository')
self.title = self.title = _('%s %s feed') % (self.db_repo_name, '%s')
self.language = config["language"]
self.ttl = config["feed_ttl"]
self.feed_include_diff = config['feed_include_diff']
self.feed_diff_limit = config['feed_diff_limit']
self.feed_items_per_page = config['feed_items_per_page']
def _changes(self, commit):
diff_processor = DiffProcessor(
commit.diff(), diff_limit=self.feed_diff_limit)
_parsed = diff_processor.prepare(inline_diff=False)
limited_diff = isinstance(_parsed, LimitedDiffContainer)
feed: fixed problem with 500 errors on rendering atom feed with diffs.
r2475 return diff_processor, _parsed, limited_diff
dan
repo-feed: moved from pylons controller to pyramid views.
r1899
def _get_title(self, commit):
return h.shorter(commit.message, 160)
def _get_description(self, commit):
_renderer = self.request.get_partial_renderer(
partial-renderer: use package resource format for templates....
r2313 'rhodecode:templates/feed/atom_feed_entry.mako')
feed: fixed problem with 500 errors on rendering atom feed with diffs.
r2475 diff_processor, parsed_diff, limited_diff = self._changes(commit)
path-permissions: Initial support for path-based permissions
r2618 filtered_parsed_diff, has_hidden_changes = self.path_filter.filter_patchset(parsed_diff)
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 return _renderer(
'body',
commit=commit,
path-permissions: Initial support for path-based permissions
r2618 parsed_diff=filtered_parsed_diff,
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 limited_diff=limited_diff,
feed_include_diff=self.feed_include_diff,
feed: fixed problem with 500 errors on rendering atom feed with diffs.
r2475 diff_processor=diff_processor,
path-permissions: Initial support for path-based permissions
r2618 has_hidden_changes=has_hidden_changes
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 )
def _set_timezone(self, date, tzinfo=pytz.utc):
if not getattr(date, "tzinfo", None):
date.replace(tzinfo=tzinfo)
return date
def _get_commits(self):
return list(self.rhodecode_vcs_repo[-self.feed_items_per_page:])
feeds: generate entries with proper unique ids....
r2071 def uid(self, repo_id, commit_id):
return '{}:{}'.format(md5_safe(repo_id), md5_safe(commit_id))
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 @LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
@HasRepoPermissionAnyDecorator(
'repository.read', 'repository.write', 'repository.admin')
@view_config(
route_name='atom_feed_home', request_method='GET',
renderer=None)
def atom(self):
"""
Produce an atom-1.0 feed via feedgenerator module
"""
self.load_default_context()
caches: new cache context managers....
r2932 cache_namespace_uid = 'cache_repo_instance.{}_{}'.format(
self.db_repo.repo_id, CacheKey.CACHE_TYPE_FEED)
invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format(
repo_id=self.db_repo.repo_id)
region = rc_cache.get_or_create_region('cache_repo_longterm',
cache_namespace_uid)
condition = not self.path_filter.is_enabled
@region.conditional_cache_on_arguments(namespace=cache_namespace_uid,
condition=condition)
def generate_atom_feed(repo_id, _repo_name, _feed_type):
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 feed = Atom1Feed(
caches: new cache context managers....
r2932 title=self.title % _repo_name,
link=h.route_url('repo_summary', repo_name=_repo_name),
description=self.description % _repo_name,
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 language=self.language,
ttl=self.ttl
)
for commit in reversed(self._get_commits()):
date = self._set_timezone(commit.date)
feed.add_item(
caches: new cache context managers....
r2932 unique_id=self.uid(repo_id, commit.raw_id),
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 title=self._get_title(commit),
author_name=commit.author,
description=self._get_description(commit),
link=h.route_url(
caches: new cache context managers....
r2932 'repo_commit', repo_name=_repo_name,
repo-commits: ported changeset code into pyramid views....
r1951 commit_id=commit.raw_id),
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 pubdate=date,)
return feed.mime_type, feed.writeString('utf-8')
caches: new cache context managers....
r2932 inv_context_manager = rc_cache.InvalidationContext(
uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
with inv_context_manager as invalidation_context:
caches: use .refresh() instead of .invalidate()...
r2939 args = (self.db_repo.repo_id, self.db_repo.repo_name, 'atom',)
# re-compute and store cache if we get invalidate signal
caches: new cache context managers....
r2932 if invalidation_context.should_invalidate():
caches: use .refresh() instead of .invalidate()...
r2939 mime_type, feed = generate_atom_feed.refresh(*args)
else:
mime_type, feed = generate_atom_feed(*args)
caches: store computation time inside context manager as helper. Since the with block is full...
r2936
log.debug('Repo ATOM feed computed in %.3fs',
inv_context_manager.compute_time)
dan
repo-feed: moved from pylons controller to pyramid views.
r1899
response = Response(feed)
response.content_type = mime_type
return response
@LoginRequired(auth_token_access=[UserApiKeys.ROLE_FEED])
@HasRepoPermissionAnyDecorator(
'repository.read', 'repository.write', 'repository.admin')
@view_config(
route_name='rss_feed_home', request_method='GET',
renderer=None)
def rss(self):
"""
Produce an rss2 feed via feedgenerator module
"""
self.load_default_context()
caches: new cache context managers....
r2932 cache_namespace_uid = 'cache_repo_instance.{}_{}'.format(
self.db_repo.repo_id, CacheKey.CACHE_TYPE_FEED)
invalidation_namespace = CacheKey.REPO_INVALIDATION_NAMESPACE.format(
repo_id=self.db_repo.repo_id)
region = rc_cache.get_or_create_region('cache_repo_longterm',
cache_namespace_uid)
condition = not self.path_filter.is_enabled
@region.conditional_cache_on_arguments(namespace=cache_namespace_uid,
condition=condition)
def generate_rss_feed(repo_id, _repo_name, _feed_type):
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 feed = Rss201rev2Feed(
caches: new cache context managers....
r2932 title=self.title % _repo_name,
link=h.route_url('repo_summary', repo_name=_repo_name),
description=self.description % _repo_name,
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 language=self.language,
ttl=self.ttl
)
for commit in reversed(self._get_commits()):
date = self._set_timezone(commit.date)
feed.add_item(
caches: new cache context managers....
r2932 unique_id=self.uid(repo_id, commit.raw_id),
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 title=self._get_title(commit),
author_name=commit.author,
description=self._get_description(commit),
link=h.route_url(
caches: new cache context managers....
r2932 'repo_commit', repo_name=_repo_name,
repo-commits: ported changeset code into pyramid views....
r1951 commit_id=commit.raw_id),
dan
repo-feed: moved from pylons controller to pyramid views.
r1899 pubdate=date,)
return feed.mime_type, feed.writeString('utf-8')
caches: new cache context managers....
r2932 inv_context_manager = rc_cache.InvalidationContext(
uid=cache_namespace_uid, invalidation_namespace=invalidation_namespace)
with inv_context_manager as invalidation_context:
caches: use .refresh() instead of .invalidate()...
r2939 args = (self.db_repo.repo_id, self.db_repo.repo_name, 'rss',)
# re-compute and store cache if we get invalidate signal
caches: new cache context managers....
r2932 if invalidation_context.should_invalidate():
caches: use .refresh() instead of .invalidate()...
r2939 mime_type, feed = generate_rss_feed.refresh(*args)
else:
mime_type, feed = generate_rss_feed(*args)
caches: store computation time inside context manager as helper. Since the with block is full...
r2936 log.debug(
'Repo RSS feed computed in %.3fs', inv_context_manager.compute_time)
dan
repo-feed: moved from pylons controller to pyramid views.
r1899
response = Response(feed)
response.content_type = mime_type
return response