Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2 from archen/master

Python 2/3, Django 1.5+, general cleanup
  • Loading branch information...
commit 723baf0ea78209f7889bf422a4dff6ba1f9c704b 2 parents 31ce2ae + cb22216
@sontek sontek authored
View
1  .gitignore
@@ -2,5 +2,6 @@
*.pyo
*.db
*.swp
+.idea
local_settings.py
haystack/
View
1  requirements.txt
@@ -0,0 +1 @@
+Django>=1.5
View
8 tekextensions/context_processors.py
@@ -1,13 +1,17 @@
from django.conf import settings
from django.contrib.sites.models import Site, RequestSite
+
def admin_media_prefix(request):
return {'ADMIN_MEDIA_PREFIX': settings.ADMIN_MEDIA_PREFIX }
+
def current_site(request):
- '''
+ """
A context processor to add the "current_site" to the current Context
- '''
+
+ """
+
context_name = 'CURRENT_SITE'
try:
View
9 tekextensions/forms.py
@@ -1,14 +1,16 @@
from django.forms.models import modelform_factory
-from django.db.models.loading import get_models, get_app, get_apps
+from django.db.models.loading import get_models, get_apps
+
def normalize_model_name(model_name):
- if (model_name.lower() == 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:
@@ -17,5 +19,4 @@ def get_model_form(model_name):
form = modelform_factory(model)
return form
- raise Exception('Did not find the model %s' % (model_name))
-
+ raise Exception('Did not find the model %s' % model_name)
View
3  tekextensions/templates/popup.html
@@ -1,7 +1,8 @@
+{% load staticfiles %}
<html>
<head>
<title>Add {{ field }}</title>
-<script src="{{ ADMIN_MEDIA_PREFIX }}js/admin/RelatedObjectLookups.js"></script>
+<script src="{% static 'admin/js/admin/RelatedObjectLookups.js' %}"></script>
</head>
<body>
<h1>Add {{ field }}</h1>
View
11 tekextensions/views.py
@@ -1,10 +1,11 @@
-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 django.forms import ValidationError
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)
@@ -16,16 +17,16 @@ def add_new_model(request, model_name, form=None):
if form.is_valid():
try:
new_obj = form.save()
- except forms.ValidationError, error:
+ except ValidationError as error:
new_obj = None
if new_obj:
- return HttpResponse('<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");</script>' % \
+ 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))
-
+ return render_to_response('popup.html', page_context, context_instance=RequestContext(request))
View
12 tekextensions/widgets.py
@@ -2,6 +2,7 @@
from django.template.loader import render_to_string
from django.contrib.admin.widgets import FilteredSelectMultiple
+
class PopUpBaseWidget(object):
def __init__(self, model=None, template='addnew.html', *args, **kwargs):
self.model = model
@@ -17,11 +18,22 @@ def render(self, name, *args, **kwargs):
popupplus = render_to_string(self.template, {'field': name, 'model': self.model})
return html+popupplus
+ def _media(self):
+ js = ["admin/js/core.js", "admin/js/admin/RelatedObjectLookups.js"]
+
+ return forms.widgets.Media(
+ js=js
+ )
+ media = property(_media)
+
+
class FilteredMultipleSelectWithPopUp(PopUpBaseWidget, FilteredSelectMultiple):
pass
+
class MultipleSelectWithPopUp(PopUpBaseWidget, forms.SelectMultiple):
pass
+
class SelectWithPopUp(PopUpBaseWidget, forms.Select):
pass
Please sign in to comment.
Something went wrong with that request. Please try again.