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
|
from django.shortcuts import render, get_object_or_404
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect, Http404
from django.template.loader import get_template
import django.utils.xmlutils
from django.conf import settings
from pgweb.util.contexts import render_pgweb
from pgweb.util.moderation import ModerationState
import io
import difflib
from pgweb.mailqueue.util import send_simple_mail
from pgweb.util.markup import pgmarkdown
def MarkdownValidator(val):
return pgmarkdown(val)
def simple_form(instancetype, itemid, request, formclass, formtemplate='base/form.html', redirect='/account/', navsection='account', fixedfields=None, createifempty=False, extracontext={}):
if itemid == 'new':
instance = instancetype()
is_new = True
else:
is_new = False
# Regular form item, attempt to edit it
try:
int(itemid)
except ValueError:
raise Http404("Invalid URL")
if createifempty:
(instance, wascreated) = instancetype.objects.get_or_create(pk=itemid)
else:
instance = get_object_or_404(instancetype, pk=itemid)
if hasattr(instance, 'submitter'):
if not instance.submitter == request.user:
raise PermissionDenied("You are not the owner of this item!")
elif hasattr(instance, 'verify_submitter'):
if not instance.verify_submitter(request.user):
raise PermissionDenied("You are not the owner of this item!")
if getattr(instance, 'block_edit', False):
raise PermissionDenied("You cannot edit this item")
if request.method == 'POST':
if 'modstate' in (f.name for f in instance._meta.get_fields()) and instance.modstate == ModerationState.CREATED and request.POST.get('delete', '') == 'delete':
# Don't care to validate, just delete.
instance.delete()
return HttpResponseRedirect(redirect)
# Process this form
form = formclass(data=request.POST, instance=instance)
for fn in form.fields:
if fn in getattr(instancetype, 'markdown_fields', []):
form.fields[fn].validators.append(MarkdownValidator)
# Save away the old value from the instance before it's saved
if not is_new:
old_values = {fn: str(getattr(instance, fn)) for fn in form.changed_data if hasattr(instance, fn)}
if form.is_valid():
# We are handling notifications, so disable the ones we'd otherwise send
do_notify = getattr(instance, 'send_notification', False)
instance.send_notification = False
# If the object has an "approved" field and it's set to false, we don't
# bother notifying about the changes. But if it lacks this field, we notify
# about everything, as well as if the field exists and the item has already
# been approved.
# Newly added objects are always notified if they are two-state, but not if they
# are tri-state (in which case they get notified when submitted for
# moderation).
if is_new:
if hasattr(instance, 'modstate'):
# Tri-state indicated by the existence of the modstate field
do_notify = False
else:
if hasattr(instance, 'approved'):
if not getattr(instance, 'approved', True):
do_notify = False
elif hasattr(instance, 'modstate'):
if getattr(instance, 'modstate', None) == ModerationState.CREATED:
do_notify = False
notify = io.StringIO()
r = form.save(commit=False)
r.submitter = request.user
# Set fixed fields. Note that this will not work if the fixed fields are ManyToMany,
# but we'll fix that sometime in the future
if fixedfields:
for k, v in list(fixedfields.items()):
setattr(r, k, v)
r.save()
# If we have a callback with the current user
if hasattr(form, 'apply_submitter'):
form.apply_submitter(r, request.user)
r.save()
if is_new:
subj = 'A new {0} has been added'.format(instance._meta.verbose_name)
for f in form.fields:
notify.write("{}:\n".format(f))
if instance._meta.get_field(f) in instance._meta.many_to_many:
notify.write("{}\n".format("\n".join([str(x) for x in form.cleaned_data[f]])))
else:
notify.write("{}\n".format(str(form.cleaned_data[f])))
notify.write("\n")
else:
subj = '{0} id {1} ({2}) has been modified'.format(instance._meta.verbose_name, instance.id, str(instance))
for fn in form.changed_data:
if not hasattr(instance, fn):
continue
f = instance._meta.get_field(fn)
if f in instance._meta.many_to_many:
# m2m field have separate config of notificatgions
if getattr(instance, 'send_m2m_notification', False):
for f in instance._meta.many_to_many:
if f.name in form.cleaned_data:
old = set([str(x) for x in getattr(instance, f.name).all()])
new = set([str(x) for x in form.cleaned_data[f.name]])
added = new.difference(old)
removed = old.difference(new)
if added or removed:
notify.write("--- {}\n+++ {}\n{}\n{}\n".format(
f.verbose_name,
f.verbose_name,
"\n".join(["+ %s" % a for a in added]),
"\n".join(["- %s" % r for r in removed]),
))
else:
# Regular field!
# Sometimes it shows up as changed even if it hasn't changed, so do
# a second check on if the diff is non-empty.
diffrows = [x for x in
difflib.unified_diff(
old_values[f.name].splitlines(),
str(form.cleaned_data[f.name]).splitlines(),
n=1,
lineterm='',
fromfile=f.verbose_name,
tofile=f.verbose_name,
) if not x.startswith("@@")]
if diffrows:
notify.write("\n".join(diffrows))
notify.write("\n\n")
if do_notify and notify.tell():
send_simple_mail(
settings.NOTIFICATION_FROM,
settings.NOTIFICATION_EMAIL,
"%s by %s" % (subj, request.user.username),
"Title: {0}\n\n{1}".format(
str(instance),
notify.getvalue(),
),
)
form.save_m2m()
return HttpResponseRedirect(redirect)
else:
# Generate form
form = formclass(instance=instance)
if hasattr(form, 'filter_by_user'):
form.filter_by_user(request.user)
for fn in form.fields:
if fn in getattr(instancetype, 'markdown_fields', []):
form.fields[fn].widget.attrs.update({'class': 'markdown-content'})
for togg in getattr(form, 'toggle_fields', []):
form.fields[togg['name']].widget.attrs.update({
'data-toggles': ','.join(togg['fields']),
'data-toggle-invert': togg['invert'] and 'true' or 'false',
'class': 'toggle-checkbox',
})
if itemid == 'new' and hasattr(form, 'new_form_intro'):
form_intro = form.new_form_intro
elif hasattr(form, 'form_intro'):
form_intro = form.form_intro
else:
form_intro = None
savebutton = 'Save'
deletebutton = None
if itemid == 'new':
if 'modstate' in (f.name for f in instance._meta.get_fields()):
# This is a three-state moderated entry, so don't say "submit new" for new
savebutton = 'Save draft'
else:
savebutton = 'Submit New'
else:
# If it's a three-state moderated entry that is not yet approved, we are still editing the draft
if 'modstate' in (f.name for f in instance._meta.get_fields()):
if instance.modstate == ModerationState.CREATED:
savebutton = 'Save draft'
deletebutton = 'Delete draft'
ctx = {
'form': form,
'formitemtype': instance._meta.verbose_name,
'form_intro': form_intro,
'described_checkboxes': getattr(form, 'described_checkboxes', {}),
'savebutton': savebutton,
'deletebutton': deletebutton,
'operation': (itemid == "new") and "New" or "Edit",
}
ctx.update(extracontext)
return render_pgweb(request, navsection, formtemplate, ctx)
def template_to_string(templatename, attrs={}):
return get_template(templatename).render(attrs)
def HttpServerError(request, msg):
r = render(request, 'errors/500.html', {
'message': msg,
})
r.status_code = 500
return r
def HttpSimpleResponse(request, title, msg):
return render(request, 'simple.html', {
'title': title,
'message': msg,
})
class PgXmlHelper(django.utils.xmlutils.SimplerXMLGenerator):
def __init__(self, outstream, skipempty=False):
django.utils.xmlutils.SimplerXMLGenerator.__init__(self, outstream, 'utf-8')
self.skipempty = skipempty
def add_xml_element(self, name, value):
if self.skipempty and value == '':
return
self.startElement(name, {})
self.characters(value)
self.endElement(name)
|