Skip to content

Commit

Permalink
Store complete package dict in activity. Store user name at time of c…
Browse files Browse the repository at this point in the history
…hange. Show historic package versions.
  • Loading branch information
TkTech committed Apr 19, 2017
1 parent 10e5b11 commit a91bab1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
38 changes: 17 additions & 21 deletions ckan/controllers/package.py
Expand Up @@ -5,10 +5,10 @@
import mimetypes
import cgi

from ckan.common import config
from paste.deploy.converters import asbool
import paste.fileapp

from ckan.common import config
import ckan.logic as logic
import ckan.lib.base as base
import ckan.lib.maintain as maintain
Expand Down Expand Up @@ -341,29 +341,21 @@ def read(self, id):
'user': c.user, 'for_view': True,
'auth_user_obj': c.userobj}
data_dict = {'id': id, 'include_tracking': True}

# interpret @<revision_id> or @<date> suffix
split = id.split('@')
if len(split) == 2:
data_dict['id'], revision_ref = split
if model.is_id(revision_ref):
context['revision_id'] = revision_ref
else:
try:
date = h.date_str_to_datetime(revision_ref)
context['revision_date'] = date
except TypeError, e:
abort(400, _('Invalid revision format: %r') % e.args)
except ValueError, e:
abort(400, _('Invalid revision format: %r') % e.args)
elif len(split) > 2:
abort(400, _('Invalid revision format: %r') %
'Too many "@" symbols')
activity_id = request.params.get('activity_id')

# check if package exists
try:
c.pkg_dict = get_action('package_show')(context, data_dict)
c.pkg = context['package']

if activity_id:
c.pkg_dict = context['session'].query(model.Activity).get(
activity_id
).data['package']
# Don't crash on old activity records, which do not include
# resources or extras.
c.pkg_dict.setdefault('resources', [])
c.is_activity_archive = True
except (NotFound, NotAuthorized):
abort(404, _('Dataset not found'))

Expand All @@ -386,8 +378,12 @@ def read(self, id):

template = self._read_template(package_type)
try:
return render(template,
extra_vars={'dataset_type': package_type})
return render(
template,
extra_vars={
'dataset_type': package_type
}
)
except ckan.lib.render.TemplateNotFound:
msg = _("Viewing {package_type} datasets in {format} format is "
"not supported (template file {file} not found).".format(
Expand Down
35 changes: 28 additions & 7 deletions ckan/model/package.py
@@ -1,11 +1,10 @@
# encoding: utf-8

import datetime
from calendar import timegm
import logging
logger = logging.getLogger(__name__)

from sqlalchemy.sql import select, and_, union, or_
from sqlalchemy.sql import and_, or_
from sqlalchemy import orm
from sqlalchemy import types, Column, Table
from ckan.common import config
Expand All @@ -24,7 +23,7 @@

__all__ = ['Package', 'package_table', 'package_revision_table',
'PACKAGE_NAME_MAX_LENGTH', 'PACKAGE_NAME_MIN_LENGTH',
'PACKAGE_VERSION_MAX_LENGTH', 'PackageTag', 'PackageTagRevision',
'PACKAGE_VERSION_MAX_LENGTH', 'PackageTagRevision',
'PackageRevision']


Expand Down Expand Up @@ -508,6 +507,7 @@ def get_fields(core_only=False, fields_to_ignore=None):
def activity_stream_item(self, activity_type, revision, user_id):
import ckan.model
import ckan.logic

assert activity_type in ("new", "changed"), (
str(activity_type))

Expand All @@ -527,17 +527,38 @@ def activity_stream_item(self, activity_type, revision, user_id):
activity_type = 'deleted'

try:
d = {'package': dictization.table_dictize(self,
context={'model': ckan.model})}
return activity.Activity(user_id, self.id, revision.id,
"%s package" % activity_type, d)
# We save the entire rendered package dict so we can support
# viewing the past packages from the activity feed.
dictized_package = ckan.logic.get_action('package_show')({
'model': ckan.model,
'session': ckan.model.Session,
'for_view': True
}, {
'id': self.id,
'include_tracking': True
})
except ckan.logic.NotFound:
# This happens if this package is being purged and therefore has no
# current revision.
# TODO: Purge all related activity stream items when a model object
# is purged.
return None

actor = meta.Session.query(ckan.model.User).get(user_id)

return activity.Activity(
user_id,
self.id,
revision.id,
"%s package" % activity_type,
{
'package': dictized_package,
# We keep the acting user name around so that actions can be
# properly displayed even if the user is deleted in the future.
'actor': actor.name
}
)

def activity_stream_detail(self, activity_id, activity_type):
import ckan.model

Expand Down
11 changes: 11 additions & 0 deletions ckan/templates/package/read.html
Expand Up @@ -11,6 +11,17 @@
{{ _('Private') }}
</span>
{% endif %}
{% block package_archive_notice %}
{% if c.is_activity_archive %}
<div class="alert alert-error">
{% trans url=h.url_for(controller='package', action='read', id=pkg.id) %}
You're currently viewing an old version of this dataset. Some resources
may no longer exist or the dataset may not display correctly. To see the
current version, click <a href="{{ url }}">here</a>.
{% endtrans %}
</div>
{% endif %}
{% endblock %}
<h1>
{% block page_heading %}
{{ h.dataset_display_name(pkg) }}
Expand Down
6 changes: 4 additions & 2 deletions ckan/templates/package/read_base.html
Expand Up @@ -10,8 +10,10 @@
{% endblock -%}

{% block content_action %}
{% if h.check_access('package_update', {'id':pkg.id }) %}
{% link_for _('Manage'), controller='package', action='edit', id=pkg.name, class_='btn', icon='wrench' %}
{% if not c.is_activity_archive %}
{% if h.check_access('package_update', {'id':pkg.id }) %}
{% link_for _('Manage'), controller='package', action='edit', id=pkg.name, class_='btn', icon='wrench' %}
{% endif %}
{% endif %}
{% endblock %}

Expand Down

0 comments on commit a91bab1

Please sign in to comment.