summaryrefslogtreecommitdiff
path: root/pgweb/util/helpers.py
diff options
context:
space:
mode:
authorMagnus Hagander2009-09-14 12:39:25 +0000
committerMagnus Hagander2009-09-14 12:39:25 +0000
commit90b758c247ad4f630f1775c6154daaef62284f52 (patch)
tree800d94792715e6b57d14cc7da618a771abb036e9 /pgweb/util/helpers.py
A first very basic import.
Contains basic functionality, and an import of most of the static content from the old site. There is still plenty more to do...
Diffstat (limited to 'pgweb/util/helpers.py')
-rw-r--r--pgweb/util/helpers.py30
1 files changed, 30 insertions, 0 deletions
diff --git a/pgweb/util/helpers.py b/pgweb/util/helpers.py
new file mode 100644
index 00000000..99b1ff80
--- /dev/null
+++ b/pgweb/util/helpers.py
@@ -0,0 +1,30 @@
+from django.shortcuts import render_to_response, get_object_or_404
+from pgweb.util.contexts import NavContext
+from django.http import HttpResponseRedirect
+
+def simple_form(instancetype, itemid, request, formclass, formtemplate='base/form.html', redirect='/account/', navsection='account'):
+ if itemid == 'new':
+ instance = instancetype()
+ else:
+ # Regular news item, attempt to edit it
+ instance = get_object_or_404(instancetype, pk=itemid)
+ if not instance.submitter == request.user:
+ raise Exception("You are not the owner of this item!")
+
+ if request.method == 'POST':
+ # Process this form
+ form = formclass(data=request.POST, instance=instance)
+ if form.is_valid():
+ r = form.save(commit=False)
+ r.submitter = request.user
+ r.save()
+ return HttpResponseRedirect(redirect)
+ else:
+ # Generate form
+ form = formclass(instance=instance)
+
+ return render_to_response(formtemplate, {
+ 'form': form,
+ 'formitemtype': instance._meta.verbose_name,
+ }, NavContext(request, navsection))
+