Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

newforms-admin: Added javascript(), javascript_add() and javascript_c…

…hange() hooks to ModelAdmin. Removed legacy JavaScript-creating code.

git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@4382 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 259f083c24b4af2532821c76d3b3cb959be29fc9 1 parent 7895fa7
@adrianholovaty adrianholovaty authored
View
83 django/contrib/admin/options.py
@@ -31,43 +31,6 @@ def unquote(s):
myappend('_' + item)
return "".join(res)
-class AdminFieldSet(object):
- def __init__(self, name, classes, field_locator_func, field_list, description):
- self.name = name
- self.field_lines = [AdminFieldLine(field_locator_func, field) for field in field_list]
- self.classes = classes
- self.description = description
-
- def __repr__(self):
- return "FieldSet: (%s, %s)" % (self.name, self.field_lines)
-
- def bind(self, field_mapping, original, bound_field_set_class):
- return bound_field_set_class(self, field_mapping, original)
-
- def __iter__(self):
- for field_line in self.field_lines:
- yield field_line
-
- def __len__(self):
- return len(self.field_lines)
-
-class AdminFieldLine(object):
- def __init__(self, field_locator_func, field_name):
- if isinstance(field_name, basestring):
- self.fields = [field_locator_func(field_name)]
- else:
- self.fields = [field_locator_func(name) for name in field_name]
-
- def bind(self, field_mapping, original, bound_field_line_class):
- return bound_field_line_class(self, field_mapping, original)
-
- def __iter__(self):
- for field in self.fields:
- yield field
-
- def __len__(self):
- return len(self.fields)
-
class AdminForm(object):
def __init__(self, form, fieldsets):
self.form, self.fieldsets = form, fieldsets
@@ -174,19 +137,37 @@ def __call__(self, request, url):
else:
return self.change_view(request, unquote(url))
- def get_field_sets(self):
- "Returns a list of AdminFieldSet objects according to self.fields."
- opts = self.opts
- if self.fields is None:
- field_struct = ((None, {'fields': [f.name for f in opts.fields + opts.many_to_many if f.editable and not isinstance(f, models.AutoField)]}),)
- else:
- field_struct = self.fields
- new_fieldset_list = []
- for name, options in field_struct:
- classes = options.get('classes', ())
- description = options.get('description', '')
- new_fieldset_list.append(AdminFieldSet(name, classes, opts.get_field, options['fields'], description))
- return new_fieldset_list
+ def javascript(self, request, fieldsets):
+ """
+ Returns a list of URLs to include via <script> statements.
+ """
+ from django.conf import settings
+ js = ['js/core.js', 'js/admin/RelatedObjectLookups.js']
+ # TODO: This.
+ #if auto_populated_fields:
+ #js.append('js/urlify.js')
+ if self.opts.has_field_type(models.DateTimeField) or self.opts.has_field_type(models.TimeField) or self.opts.has_field_type(models.DateField):
+ js.extend(['js/calendar.js', 'js/admin/DateTimeShortcuts.js'])
+ if self.opts.get_ordered_objects():
+ js.extend(['js/getElementsBySelector.js', 'js/dom-drag.js' , 'js/admin/ordering.js'])
+ if self.js:
+ js.extend(self.js)
+ for f in self.opts.many_to_many:
+ if f.rel.filter_interface:
+ js.extend(['js/SelectBox.js' , 'js/SelectFilter2.js'])
+ break
+ for fs in fieldsets:
+ if 'collapse' in fs.classes:
+ js.append('js/admin/CollapsedFieldsets.js')
+ break
+ prefix = settings.ADMIN_MEDIA_PREFIX
+ return ['%s%s' % (prefix, url) for url in js]
+
+ def javascript_add(self, request):
+ return self.javascript(request, self.fieldsets_add(request))
+
+ def javascript_change(self, request, object_id):
+ return self.javascript(request, self.fieldsets_change(request, object_id))
def fieldsets(self, request):
"""
@@ -297,6 +278,7 @@ def add_view(self, request, form_url='', post_url_continue='../%s/'):
'oldform': oldforms.FormWrapper(model.AddManipulator(), {}, {}),
'is_popup': request.REQUEST.has_key('_popup'),
'show_delete': False,
+ 'javascript_imports': self.javascript_add(request),
})
return render_change_form(self, model, model.AddManipulator(), c, add=True)
@@ -388,6 +370,7 @@ def change_view(self, request, object_id):
'object_id': object_id,
'original': obj,
'is_popup': request.REQUEST.has_key('_popup'),
+ 'javascript_imports': self.javascript_change(request, object_id),
})
return render_change_form(self, model, model.ChangeManipulator(object_id), c, change=True)
View
3  django/contrib/admin/templates/admin/change_form.html
@@ -3,7 +3,8 @@
{% block extrahead %}{{ block.super }}
<script type="text/javascript" src="../../../jsi18n/"></script>
-{% for js in javascript_imports %}{% include_admin_script js %}{% endfor %}
+{% for js in javascript_imports %}<script type="text/javascript" src="{{ js }}"></script>
+{% endfor %}
{% endblock %}
{% block stylesheet %}{% admin_media_prefix %}css/forms.css{% endblock %}
View
17 django/contrib/admin/templatetags/admin_modify.py
@@ -15,23 +15,6 @@
def class_name_to_underscored(name):
return '_'.join([s.lower() for s in word_re.findall(name)[:-1]])
-def include_admin_script(script_path):
- """
- Returns an HTML script element for including a script from the admin
- media url.
-
- Example usage::
-
- {% include_admin_script js/calendar.js %}
-
- could return::
-
- <script type="text/javascript" src="/media/admin/js/calendar.js">
- """
-
- return '<script type="text/javascript" src="%s%s"></script>' % (settings.ADMIN_MEDIA_PREFIX, script_path)
-include_admin_script = register.simple_tag(include_admin_script)
-
def submit_row(context):
opts = context['opts']
change = context['change']
View
29 django/contrib/admin/views/main.py
@@ -48,33 +48,6 @@ def quote(s):
res[i] = '_%02X' % ord(c)
return ''.join(res)
-def get_javascript_imports(opts, auto_populated_fields, field_sets):
-# Put in any necessary JavaScript imports.
- js = ['js/core.js', 'js/admin/RelatedObjectLookups.js']
- if auto_populated_fields:
- js.append('js/urlify.js')
- if opts.has_field_type(models.DateTimeField) or opts.has_field_type(models.TimeField) or opts.has_field_type(models.DateField):
- js.extend(['js/calendar.js', 'js/admin/DateTimeShortcuts.js'])
- if opts.get_ordered_objects():
- js.extend(['js/getElementsBySelector.js', 'js/dom-drag.js' , 'js/admin/ordering.js'])
- if opts.admin.js:
- js.extend(opts.admin.js)
- seen_collapse = False
- for field_set in field_sets:
- if not seen_collapse and 'collapse' in field_set.classes:
- seen_collapse = True
- js.append('js/admin/CollapsedFieldsets.js')
-
- for field_line in field_set:
- try:
- for f in field_line:
- if f.rel and isinstance(f, models.ManyToManyField) and f.rel.filter_interface:
- js.extend(['js/SelectBox.js' , 'js/SelectFilter2.js'])
- raise StopIteration
- except StopIteration:
- break
- return js
-
def model_admin_view(request, app_label, model_name, rest_of_url):
model = models.get_model(app_label, model_name)
if model is None:
@@ -142,7 +115,6 @@ def render_change_form(model_admin, model, manipulator, context, add=False, chan
opts = model._meta
app_label = opts.app_label
auto_populated_fields = [f for f in opts.fields if f.prepopulate_from]
- field_sets = model_admin.get_field_sets()
original = getattr(manipulator, 'original_object', None)
ordered_objects = opts.get_ordered_objects()
inline_related_objects = opts.get_followed_related_objects(manipulator.follow)
@@ -154,7 +126,6 @@ def render_change_form(model_admin, model, manipulator, context, add=False, chan
'has_file_field': opts.has_field_type(models.FileField),
'has_absolute_url': hasattr(model, 'get_absolute_url'),
'auto_populated_fields': auto_populated_fields,
- 'javascript_imports': get_javascript_imports(opts, auto_populated_fields, field_sets),
'ordered_objects': ordered_objects,
'inline_related_objects': inline_related_objects,
'form_url': form_url,
Please sign in to comment.
Something went wrong with that request. Please try again.