-
Notifications
You must be signed in to change notification settings - Fork 99
/
actions.py
297 lines (255 loc) · 10.1 KB
/
actions.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import datetime
import json
import ckan.plugins as p
import ckan.lib.navl.dictization_functions as df
import ckan.lib.uploader as uploader
import ckan.lib.helpers as h
from ckan.plugins import toolkit as tk
from HTMLParser import HTMLParser
try:
import ckan.authz as authz
except ImportError:
import ckan.new_authz as authz
import db
def page_name_validator(key, data, errors, context):
session = context['session']
page = context.get('page')
group_id = context.get('group_id')
if page and page == data[key]:
return
query = session.query(db.Page.name).filter_by(name=data[key], group_id=group_id)
result = query.first()
if result:
errors[key].append(
p.toolkit._('Page name already exists in database'))
def not_empty_if_blog(key, data, errors, context):
value = data.get(key)
if data.get(('page_type',), '') == 'blog':
if value is df.missing or not value:
errors[key].append('Publish Date Must be supplied')
class HTMLFirstImage(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.first_image = None
def handle_starttag(self, tag, attrs):
if tag == 'img' and not self.first_image:
self.first_image = dict(attrs)['src']
schema = {
'id': [p.toolkit.get_validator('ignore_empty'), unicode],
'title': [p.toolkit.get_validator('not_empty'), unicode],
'name': [p.toolkit.get_validator('not_empty'), unicode,
p.toolkit.get_validator('name_validator'), page_name_validator],
'content': [p.toolkit.get_validator('ignore_missing'), unicode],
'page_type': [p.toolkit.get_validator('ignore_missing'), unicode],
# 'lang': [p.toolkit.get_validator('not_empty'), unicode],
'order': [p.toolkit.get_validator('ignore_missing'),
unicode],
'private': [p.toolkit.get_validator('ignore_missing'),
p.toolkit.get_validator('boolean_validator')],
'group_id': [p.toolkit.get_validator('ignore_missing'), unicode],
'user_id': [p.toolkit.get_validator('ignore_missing'), unicode],
'created': [p.toolkit.get_validator('ignore_missing'),
p.toolkit.get_validator('isodate')],
'publish_date': [not_empty_if_blog,
p.toolkit.get_validator('ignore_missing'),
p.toolkit.get_validator('isodate')],
}
def _pages_show(context, data_dict):
if db.pages_table is None:
db.init_db(context['model'])
org_id = data_dict.get('org_id')
page = data_dict.get('page')
out = db.Page.get(group_id=org_id, name=page)
if out:
out = db.table_dictize(out, context)
return out
def _pages_list(context, data_dict):
search = {}
if db.pages_table is None:
db.init_db(context['model'])
org_id = data_dict.get('org_id')
ordered = data_dict.get('order')
order_publish_date = data_dict.get('order_publish_date')
page_type = data_dict.get('page_type')
private = data_dict.get('private', True)
if ordered:
search['order'] = True
if page_type:
search['page_type'] = page_type
if order_publish_date:
search['order_publish_date'] = True
if not org_id:
search['group_id'] = None
try:
p.toolkit.check_access('ckanext_pages_update', context, data_dict)
if not private:
search['private'] = False
except p.toolkit.NotAuthorized:
search['private'] = False
else:
group = context['model'].Group.get(org_id)
user = context['user']
member = authz.has_user_permission_for_group_or_org(
group.id, user, 'read')
search['group_id'] = org_id
if not member:
search['private'] = False
out = db.Page.pages(**search)
out_list = []
for pg in out:
parser = HTMLFirstImage()
parser.feed(pg.content)
img = parser.first_image
pg_row = {'title': pg.title,
'content': pg.content,
'name': pg.name,
'publish_date': pg.publish_date.isoformat() if pg.publish_date else None,
'group_id': pg.group_id,
'page_type': pg.page_type,
}
if img:
pg_row['image'] = img
extras = pg.extras
if extras:
pg_row.update(json.loads(pg.extras))
out_list.append(pg_row)
return out_list
def _pages_delete(context, data_dict):
if db.pages_table is None:
db.init_db(context['model'])
org_id = data_dict.get('org_id')
page = data_dict.get('page')
out = db.Page.get(group_id=org_id, name=page)
if out:
session = context['session']
session.delete(out)
session.commit()
def _pages_update(context, data_dict):
if db.pages_table is None:
db.init_db(context['model'])
org_id = data_dict.get('org_id')
page = data_dict.get('page')
# we need the page in the context for name validation
context['page'] = page
context['group_id'] = org_id
data, errors = df.validate(data_dict, schema, context)
if errors:
raise p.toolkit.ValidationError(errors)
out = db.Page.get(group_id=org_id, name=page)
if not out:
out = db.Page()
out.group_id = org_id
out.name = page
items = ['title', 'content', 'name', 'private',
'order', 'page_type', 'publish_date']
for item in items:
setattr(out, item, data.get(item,'page' if item =='page_type' else None)) #backward compatible with older version where page_type does not exist
extras = {}
extra_keys = set(schema.keys()) - set(items + ['id', 'created'])
for key in extra_keys:
if key in data:
extras[key] = data.get(key)
out.extras = json.dumps(extras)
out.modified = datetime.datetime.utcnow()
out.user_id = p.toolkit.c.userobj.id
out.save()
session = context['session']
session.add(out)
session.commit()
def pages_upload(context, data_dict):
try:
p.toolkit.check_access('ckanext_pages_upload', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
if p.toolkit.check_ckan_version(min_version='2.5'):
upload = uploader.get_uploader('page_images')
else:
upload = uploader.Upload('page_images')
upload.update_data_dict(data_dict, 'image_url',
'upload', 'clear_upload')
upload.upload()
image_url = data_dict.get('image_url')
if image_url:
image_url = h.url_for_static(
'uploads/page_images/%s' % image_url,
qualified = True
)
return {'url': image_url}
@tk.side_effect_free
def pages_show(context, data_dict):
try:
p.toolkit.check_access('ckanext_pages_show', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_show(context, data_dict)
def pages_update(context, data_dict):
try:
p.toolkit.check_access('ckanext_pages_update', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_update(context, data_dict)
def pages_delete(context, data_dict):
try:
p.toolkit.check_access('ckanext_pages_delete', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_delete(context, data_dict)
@tk.side_effect_free
def pages_list(context, data_dict):
try:
p.toolkit.check_access('ckanext_pages_list', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_list(context, data_dict)
@tk.side_effect_free
def org_pages_show(context, data_dict):
try:
p.toolkit.check_access('ckanext_org_pages_show', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_show(context, data_dict)
def org_pages_update(context, data_dict):
try:
p.toolkit.check_access('ckanext_org_pages_update', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_update(context, data_dict)
def org_pages_delete(context, data_dict):
try:
p.toolkit.check_access('ckanext_org_pages_delete', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_delete(context, data_dict)
@tk.side_effect_free
def org_pages_list(context, data_dict):
try:
p.toolkit.check_access('ckanext_org_pages_list', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_list(context, data_dict)
@tk.side_effect_free
def group_pages_show(context, data_dict):
try:
p.toolkit.check_access('ckanext_group_pages_show', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_show(context, data_dict)
def group_pages_update(context, data_dict):
try:
p.toolkit.check_access('ckanext_group_pages_update', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_update(context, data_dict)
def group_pages_delete(context, data_dict):
try:
p.toolkit.check_access('ckanext_group_pages_delete', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_delete(context, data_dict)
@tk.side_effect_free
def group_pages_list(context, data_dict):
try:
p.toolkit.check_access('ckanext_group_pages_list', context, data_dict)
except p.toolkit.NotAuthorized:
p.toolkit.abort(401, p.toolkit._('Not authorized to see this page'))
return _pages_list(context, data_dict)