Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

started refactoring the code

  • Loading branch information...
commit 7cdead44dcdc0defa647cc9fba6ff473658caf14 1 parent d35620b
@sontek sontek authored
Showing with 35 additions and 15 deletions.
  1. +21 −0 tekextensions/forms.py
  2. +14 −15 tekextensions/views.py
View
21 tekextensions/forms.py
@@ -0,0 +1,21 @@
+from django.forms.models import modelform_factory
+from django.db.models.loading import get_models, get_app, get_apps
+
+def normalize_model_name(model_name):
+ if (model_name.lower() == model_name):
+ normal_model_name = model_name.capitalize()
+ else:
+ normal_model_name = model_name
+
+ return normal_model_name
+
+def get_model_form(model_name):
+ app_list = get_apps()
+ for app in app_list:
+ for model in get_models(app):
+ if model.__name__ == model_name:
+ form = modelform_factory(model)
+ return form
+
+ raise Exception('Did not find the model %s' % (model_name))
+
View
29 tekextensions/views.py
@@ -11,22 +11,21 @@ def add_new_model(request, model_name, form=None):
if not form:
form = get_model_form(normal_model_name)
- if form:
- if request.method == 'POST':
- form = form(request.POST)
- if form.is_valid():
- try:
- new_obj = form.save()
- except forms.ValidationError, error:
- new_obj = None
+ if request.method == 'POST':
+ form = form(request.POST)
+ if form.is_valid():
+ try:
+ new_obj = form.save()
+ except forms.ValidationError, 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)))
+ 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()
+ else:
+ form = form()
- page_context = {'form': form, 'field': normal_model_name}
- return render_to_response('popup.html', page_context, context_instance=RequestContext(request))
+ page_context = {'form': form, 'field': normal_model_name}
+ return render_to_response('popup.html', page_context, context_instance=RequestContext(request))
Please sign in to comment.
Something went wrong with that request. Please try again.