/
related.py
129 lines (105 loc) · 4.29 KB
/
related.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import ckan.model as model
import ckan.logic as logic
import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.navl.dictization_functions as df
import pylons.i18n as i18n
c = base.c
_ = i18n._
class RelatedController(base.BaseController):
def new(self, id):
return self._edit_or_new(id, None, False)
def edit(self, id, related_id):
return self._edit_or_new(id, related_id, True)
def list(self, id):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'extras_as_string': True,
'for_view': True}
data_dict = {'id': id}
try:
logic.check_access('package_show', context, data_dict)
except logic.NotFound:
base.abort(404, base._('Dataset not found'))
except logic.NotAuthorized:
base.abort(401, base._('Not authorized to see this page'))
try:
c.pkg_dict = logic.get_action('package_show')(context, data_dict)
c.pkg = context['package']
c.resources_json = h.json.dumps(c.pkg_dict.get('resources', []))
except logic.NotFound:
base.abort(404, base._('Dataset not found'))
except logic.NotAuthorized:
base.abort(401, base._('Unauthorized to read package %s') % id)
c.related_count = len(c.pkg.related)
c.action = 'related'
return base.render("package/related_list.html")
def _edit_or_new(self, id, related_id, is_edit):
"""
Edit and New were too similar and so I've put the code together
and try and do as much up front as possible.
"""
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
data_dict = {}
if is_edit:
tpl = 'related/edit.html'
auth_name = 'related_update'
auth_dict = {'id': related_id}
action_name = 'related_update'
try:
related = logic.get_action('related_show')(
context, {'id': related_id})
except logic.NotFound:
base.abort(404, _('Related item not found'))
else:
tpl = 'related/new.html'
auth_name = 'related_create'
auth_dict = {}
action_name = 'related_create'
try:
logic.check_access(auth_name, context, auth_dict)
except logic.NotAuthorized:
base.abort(401, base._('Not authorized'))
try:
c.pkg_dict = logic.get_action('package_show')(context, {'id': id})
except logic.NotFound:
base.abort(404, _('Package not found'))
data, errors, error_summary = {}, {}, {}
if base.request.method == "POST":
try:
data = logic.clean_dict(
df.unflatten(
logic.tuplize_dict(
logic.parse_params(base.request.params)
)))
if is_edit:
data['id'] = related_id
else:
data['dataset_id'] = id
data['owner_id'] = c.userobj.id
related = logic.get_action(action_name)(context, data)
if not is_edit:
h.flash_success(_("Related item was successfully created"))
else:
h.flash_success(_("Related item was successfully updated"))
h.redirect_to(controller='related',
action='list',
id=c.pkg_dict['name'])
except df.DataError:
base.abort(400, _(u'Integrity Error'))
except logic.ValidationError, e:
errors = e.error_dict
error_summary = e.error_summary
else:
if is_edit:
data = related
c.types = {
"application": "Application",
"idea": "Idea",
"news_article": "News Article",
"paper": "Paper",
"visualization": "Visualization"
}
vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
c.form = base.render("related/edit_form.html", extra_vars=vars)
return base.render(tpl)