mirror of
https://github.com/postgres/pgweb.git
synced 2025-08-13 13:12:42 +00:00

render_to_response does not work on newer django, so it needs to be replaced. And using a speicfic context actually overcomplicates things, it's easier to just use a wrapper function. For those cases where we don't need NavContext, just use render() (the new shortcut function from django), which also removes the need to use RequestContext.
99 lines
3.2 KiB
Python
99 lines
3.2 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
from pgweb.util.contexts import render_pgweb
|
|
from django.http import HttpResponseRedirect, Http404
|
|
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, createifempty=False):
|
|
if itemid == 'new':
|
|
instance = instancetype()
|
|
else:
|
|
# Regular form item, attempt to edit it
|
|
try:
|
|
i = 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 Exception("You are not the owner of this item!")
|
|
elif hasattr(instance, 'verify_submitter'):
|
|
if not instance.verify_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
|
|
# 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 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()
|
|
|
|
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)
|
|
|
|
if hasattr(instancetype, 'markdown_fields'):
|
|
markdownfields = instancetype.markdown_fields
|
|
else:
|
|
markdownfields = None
|
|
|
|
if hasattr(form, 'described_checkboxes'):
|
|
described_checkboxes = form.described_checkboxes
|
|
else:
|
|
described_checkboxes = None
|
|
|
|
return render_pgweb(request, navsection, formtemplate, {
|
|
'form': form,
|
|
'formitemtype': instance._meta.verbose_name,
|
|
'markdownfields': markdownfields,
|
|
'described_checkboxes': described_checkboxes,
|
|
'form_intro': hasattr(form, 'form_intro') and form.form_intro or None,
|
|
'toggle_fields': hasattr(form, 'toggle_fields') and form.toggle_fields or None,
|
|
'jquery': hasattr(form, 'jquery') and form.jquery or None,
|
|
'savebutton': (itemid == "new") and "New" or "Save",
|
|
'operation': (itemid == "new") and "New" or "Edit",
|
|
})
|
|
|
|
def template_to_string(templatename, attrs = {}):
|
|
return get_template(templatename).render(Context(attrs))
|
|
|
|
def HttpServerError(msg):
|
|
r = render(request, 'errors/500.html', {
|
|
'message': msg,
|
|
})
|
|
r.status_code = 500
|
|
return r
|
|
|
|
|
|
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)
|