forked from enterstudio/ckanext-datagovau
-
Notifications
You must be signed in to change notification settings - Fork 0
/
related-image-upload.patch
108 lines (101 loc) · 5.07 KB
/
related-image-upload.patch
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
diff --git a/ckan/lib/dictization/model_dictize.py b/ckan/lib/dictization/model_dictize.py
index ce0f4df..992d881 100644
--- a/ckan/lib/dictization/model_dictize.py
+++ b/ckan/lib/dictization/model_dictize.py
@@ -171,7 +171,18 @@ def resource_dictize(res, context):
return resource
def related_dictize(rel, context):
- return d.table_dictize(rel, context)
+ related = d.table_dictize(rel, context)
+ image_url = related.get('image_url')
+ related['image_display_url'] = image_url
+ if image_url and not image_url.startswith('http'):
+ #munge here should not have an effect only doing it incase
+ #of potential vulnerability of dodgy api input
+ image_url = munge.munge_filename(image_url)
+ related['image_display_url'] = h.url_for_static(
+ 'uploads/related/%s' % related.get('image_url'),
+ qualified=True
+ )
+ return related
def _execute_with_revision(q, rev_table, context):
'''
diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py
index 29ae102..b945d97 100644
--- a/ckan/logic/action/create.py
+++ b/ckan/logic/action/create.py
@@ -317,6 +317,9 @@ def related_create(context, data_dict):
userobj = model.User.get(user)
_check_access('related_create', context, data_dict)
+ upload = uploader.Upload('related')
+ upload.update_data_dict(data_dict, 'image_url',
+ 'image_upload', 'clear_upload')
data_dict["owner_id"] = userobj.id
data, errors = _validate(data_dict,
@@ -325,7 +328,7 @@ def related_create(context, data_dict):
if errors:
model.Session.rollback()
raise ValidationError(errors)
-
+ upload.upload(uploader.get_max_image_size())
related = model_save.related_dict_save(data, context)
if not context.get('defer_commit'):
model.repo.commit_and_remove()
diff --git a/ckan/logic/action/update.py b/ckan/logic/action/update.py
index a0b80db..a3eb692 100644
--- a/ckan/logic/action/update.py
+++ b/ckan/logic/action/update.py
@@ -137,7 +137,9 @@ def related_update(context, data_dict):
related = model.Related.get(id)
context["related"] = related
-
+ upload = uploader.Upload('related', related.image_url)
+ upload.update_data_dict(data_dict, 'image_url',
+ 'image_upload', 'clear_upload')
if not related:
logging.error('Could not find related ' + id)
raise NotFound(_('Item was not found.'))
@@ -174,7 +176,7 @@ def related_update(context, data_dict):
}
_get_action('activity_create')(activity_create_context, activity_dict)
-
+ upload.upload(uploader.get_max_image_size())
if not context.get('defer_commit'):
model.repo.commit()
return model_dictize.related_dictize(related, context)
diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py
index e9bd407..980b726 100644
--- a/ckan/logic/schema.py
+++ b/ckan/logic/schema.py
@@ -326,7 +326,7 @@ def default_related_schema():
'title': [not_empty, unicode],
'description': [ignore_missing, unicode],
'type': [not_empty, unicode],
- 'image_url': [ignore_missing, unicode, url_validator],
+ 'image_url': [ignore_missing, unicode],
'url': [ignore_missing, unicode, url_validator],
'owner_id': [not_empty, unicode],
'created': [ignore],
diff --git a/ckan/templates/related/snippets/related_form.html b/ckan/templates/related/snippets/related_form.html
index 23ab88c..2220b9a 100644
--- a/ckan/templates/related/snippets/related_form.html
+++ b/ckan/templates/related/snippets/related_form.html
@@ -1,6 +1,6 @@
{% import 'macros/form.html' as form %}
-<form class="dataset-form form-horizontal" method="post" data-module="basic-form">
+<form class="dataset-form form-horizontal" method="post" data-module="basic-form" enctype="multipart/form-data">
{% block error_summary %}
{% if error_summary | count %}
<div class="alert alert-error error-explanation">
@@ -17,7 +17,10 @@
{% block fields %}
{{ form.input('title', label=_('Title'), id='field-title', placeholder=_('My Related Item'), value=data.title, error=errors.title, classes=['control-full']) }}
{{ form.input('url', label=_('URL'), id='field-url', placeholder=_('http://example.com/'), value=data.url, error=errors.url, classes=['control-full']) }}
- {{ form.input('image_url', label=_('Image URL'), id='field-image-url', placeholder=_('http://example.com/image.png'), value=data.image_url, error=errors.image_url, classes=['control-full']) }}
+ {% set is_upload = data.image_url and not data.image_url.startswith('http') %}
+ {% set is_url = data.image_url and data.image_url.startswith('http') %}
+
+ {{ form.image_upload(data, errors, is_upload_enabled=h.uploads_enabled(), is_url=is_url, is_upload=is_upload) }}
{{ form.markdown('description', label=_('Description'), id='field-description', placeholder=_('A little information about the item...'), value=data.description, error=errors.description) }}
{{ form.select('type', label=_('Type'), id='field-types', selected=data.type, options=c.types, error=errors.type) }}
{% endblock %}