-
Notifications
You must be signed in to change notification settings - Fork 16
/
views.py
31 lines (24 loc) · 1.1 KB
/
views.py
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
from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponse
from django.utils.html import escape
from tekextensions.forms import get_model_form, normalize_model_name
def add_new_model(request, model_name, form=None):
normal_model_name = normalize_model_name(model_name)
if not form:
form = get_model_form(normal_model_name)
if request.method == 'POST':
form = form(request.POST)
if form.is_valid():
try:
new_obj = form.save()
except form.ValidationError as error:
new_obj = None
if new_obj:
return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
(escape(new_obj._get_pk_val()), escape(new_obj)))
else:
form = form()
page_context = {'form': form, 'field': normal_model_name}
return render_to_response('popup.html', page_context, context_instance=RequestContext(request))