From e363050c75a69ec56e6ff7ea69a9e989c2be12cf Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Sun, 27 Nov 2011 14:13:16 +0100 Subject: [PATCH] Make it possible to autocreate an object in simple_form Previously, one had to specify "new" as objectid to create a new one, but in some cases it may not be known ahead of time if the object exists or not. --- pgweb/util/helpers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pgweb/util/helpers.py b/pgweb/util/helpers.py index d62ea42f..16dfaab3 100644 --- a/pgweb/util/helpers.py +++ b/pgweb/util/helpers.py @@ -5,12 +5,15 @@ from django.template import Context from django.template.loader import get_template import django.utils.xmlutils -def simple_form(instancetype, itemid, request, formclass, formtemplate='base/form.html', redirect='/account/', navsection='account', fixedfields=None): +def simple_form(instancetype, itemid, request, formclass, formtemplate='base/form.html', redirect='/account/', navsection='account', fixedfields=None, createifempty=False): if itemid == 'new': instance = instancetype() else: - # Regular news item, attempt to edit it - instance = get_object_or_404(instancetype, pk=itemid) + # Regular form item, attempt to edit it + 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 Exception("You are not the owner of this item!") -- 2.39.5