Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Adrian Holovaty authored January 21, 2007
83  django/contrib/admin/options.py
@@ -31,43 +31,6 @@ def unquote(s):
31 31
             myappend('_' + item)
32 32
     return "".join(res)
33 33
 
34  
-class AdminFieldSet(object):
35  
-    def __init__(self, name, classes, field_locator_func, field_list, description):
36  
-        self.name = name
37  
-        self.field_lines = [AdminFieldLine(field_locator_func, field) for field in field_list]
38  
-        self.classes = classes
39  
-        self.description = description
40  
-
41  
-    def __repr__(self):
42  
-        return "FieldSet: (%s, %s)" % (self.name, self.field_lines)
43  
-
44  
-    def bind(self, field_mapping, original, bound_field_set_class):
45  
-        return bound_field_set_class(self, field_mapping, original)
46  
-
47  
-    def __iter__(self):
48  
-        for field_line in self.field_lines:
49  
-            yield field_line
50  
-
51  
-    def __len__(self):
52  
-        return len(self.field_lines)
53  
-
54  
-class AdminFieldLine(object):
55  
-    def __init__(self, field_locator_func, field_name):
56  
-        if isinstance(field_name, basestring):
57  
-            self.fields = [field_locator_func(field_name)]
58  
-        else:
59  
-            self.fields = [field_locator_func(name) for name in field_name]
60  
-
61  
-    def bind(self, field_mapping, original, bound_field_line_class):
62  
-        return bound_field_line_class(self, field_mapping, original)
63  
-
64  
-    def __iter__(self):
65  
-        for field in self.fields:
66  
-            yield field
67  
-
68  
-    def __len__(self):
69  
-        return len(self.fields)
70  
-
71 34
 class AdminForm(object):
72 35
     def __init__(self, form, fieldsets):
73 36
         self.form, self.fieldsets = form, fieldsets
@@ -174,19 +137,37 @@ def __call__(self, request, url):
174 137
         else:
175 138
             return self.change_view(request, unquote(url))
176 139
 
177  
-    def get_field_sets(self):
178  
-        "Returns a list of AdminFieldSet objects according to self.fields."
179  
-        opts = self.opts
180  
-        if self.fields is None:
181  
-            field_struct = ((None, {'fields': [f.name for f in opts.fields + opts.many_to_many if f.editable and not isinstance(f, models.AutoField)]}),)
182  
-        else:
183  
-            field_struct = self.fields
184  
-        new_fieldset_list = []
185  
-        for name, options in field_struct:
186  
-            classes = options.get('classes', ())
187  
-            description = options.get('description', '')
188  
-            new_fieldset_list.append(AdminFieldSet(name, classes, opts.get_field, options['fields'], description))
189  
-        return new_fieldset_list
  140
+    def javascript(self, request, fieldsets):
  141
+        """
  142
+        Returns a list of URLs to include via <script> statements.
  143
+        """
  144
+        from django.conf import settings
  145
+        js = ['js/core.js', 'js/admin/RelatedObjectLookups.js']
  146
+        # TODO: This.
  147
+        #if auto_populated_fields:
  148
+            #js.append('js/urlify.js')
  149
+        if self.opts.has_field_type(models.DateTimeField) or self.opts.has_field_type(models.TimeField) or self.opts.has_field_type(models.DateField):
  150
+            js.extend(['js/calendar.js', 'js/admin/DateTimeShortcuts.js'])
  151
+        if self.opts.get_ordered_objects():
  152
+            js.extend(['js/getElementsBySelector.js', 'js/dom-drag.js' , 'js/admin/ordering.js'])
  153
+        if self.js:
  154
+            js.extend(self.js)
  155
+        for f in self.opts.many_to_many:
  156
+            if f.rel.filter_interface:
  157
+                js.extend(['js/SelectBox.js' , 'js/SelectFilter2.js'])
  158
+                break
  159
+        for fs in fieldsets:
  160
+            if 'collapse' in fs.classes:
  161
+                js.append('js/admin/CollapsedFieldsets.js')
  162
+                break
  163
+        prefix = settings.ADMIN_MEDIA_PREFIX
  164
+        return ['%s%s' % (prefix, url) for url in js]
  165
+
  166
+    def javascript_add(self, request):
  167
+        return self.javascript(request, self.fieldsets_add(request))
  168
+
  169
+    def javascript_change(self, request, object_id):
  170
+        return self.javascript(request, self.fieldsets_change(request, object_id))
190 171
 
191 172
     def fieldsets(self, request):
192 173
         """
@@ -297,6 +278,7 @@ def add_view(self, request, form_url='', post_url_continue='../%s/'):
297 278
             'oldform': oldforms.FormWrapper(model.AddManipulator(), {}, {}),
298 279
             'is_popup': request.REQUEST.has_key('_popup'),
299 280
             'show_delete': False,
  281
+            'javascript_imports': self.javascript_add(request),
300 282
         })
301 283
 
302 284
         return render_change_form(self, model, model.AddManipulator(), c, add=True)
@@ -388,6 +370,7 @@ def change_view(self, request, object_id):
388 370
             'object_id': object_id,
389 371
             'original': obj,
390 372
             'is_popup': request.REQUEST.has_key('_popup'),
  373
+            'javascript_imports': self.javascript_change(request, object_id),
391 374
         })
392 375
         return render_change_form(self, model, model.ChangeManipulator(object_id), c, change=True)
393 376
 
3  django/contrib/admin/templates/admin/change_form.html
@@ -3,7 +3,8 @@
3 3
 
4 4
 {% block extrahead %}{{ block.super }}
5 5
 <script type="text/javascript" src="../../../jsi18n/"></script>
6  
-{% for js in javascript_imports %}{% include_admin_script js %}{% endfor %}
  6
+{% for js in javascript_imports %}<script type="text/javascript" src="{{ js }}"></script>
  7
+{% endfor %}
7 8
 {% endblock %}
8 9
 
9 10
 {% block stylesheet %}{% admin_media_prefix %}css/forms.css{% endblock %}
17  django/contrib/admin/templatetags/admin_modify.py
@@ -15,23 +15,6 @@
15 15
 def class_name_to_underscored(name):
16 16
     return '_'.join([s.lower() for s in word_re.findall(name)[:-1]])
17 17
 
18  
-def include_admin_script(script_path):
19  
-    """
20  
-    Returns an HTML script element for including a script from the admin
21  
-    media url.
22  
-
23  
-    Example usage::
24  
-
25  
-        {% include_admin_script js/calendar.js %}
26  
-
27  
-    could return::
28  
-
29  
-        <script type="text/javascript" src="/media/admin/js/calendar.js">
30  
-    """
31  
-
32  
-    return '<script type="text/javascript" src="%s%s"></script>' % (settings.ADMIN_MEDIA_PREFIX, script_path)
33  
-include_admin_script = register.simple_tag(include_admin_script)
34  
-
35 18
 def submit_row(context):
36 19
     opts = context['opts']
37 20
     change = context['change']
29  django/contrib/admin/views/main.py
@@ -48,33 +48,6 @@ def quote(s):
48 48
             res[i] = '_%02X' % ord(c)
49 49
     return ''.join(res)
50 50
 
51  
-def get_javascript_imports(opts, auto_populated_fields, field_sets):
52  
-# Put in any necessary JavaScript imports.
53  
-    js = ['js/core.js', 'js/admin/RelatedObjectLookups.js']
54  
-    if auto_populated_fields:
55  
-        js.append('js/urlify.js')
56  
-    if opts.has_field_type(models.DateTimeField) or opts.has_field_type(models.TimeField) or opts.has_field_type(models.DateField):
57  
-        js.extend(['js/calendar.js', 'js/admin/DateTimeShortcuts.js'])
58  
-    if opts.get_ordered_objects():
59  
-        js.extend(['js/getElementsBySelector.js', 'js/dom-drag.js' , 'js/admin/ordering.js'])
60  
-    if opts.admin.js:
61  
-        js.extend(opts.admin.js)
62  
-    seen_collapse = False
63  
-    for field_set in field_sets:
64  
-        if not seen_collapse and 'collapse' in field_set.classes:
65  
-            seen_collapse = True
66  
-            js.append('js/admin/CollapsedFieldsets.js')
67  
-
68  
-        for field_line in field_set:
69  
-            try:
70  
-                for f in field_line:
71  
-                    if f.rel and isinstance(f, models.ManyToManyField) and f.rel.filter_interface:
72  
-                        js.extend(['js/SelectBox.js' , 'js/SelectFilter2.js'])
73  
-                        raise StopIteration
74  
-            except StopIteration:
75  
-                break
76  
-    return js
77  
-
78 51
 def model_admin_view(request, app_label, model_name, rest_of_url):
79 52
     model = models.get_model(app_label, model_name)
80 53
     if model is None:
@@ -142,7 +115,6 @@ def render_change_form(model_admin, model, manipulator, context, add=False, chan
142 115
     opts = model._meta
143 116
     app_label = opts.app_label
144 117
     auto_populated_fields = [f for f in opts.fields if f.prepopulate_from]
145  
-    field_sets = model_admin.get_field_sets()
146 118
     original = getattr(manipulator, 'original_object', None)
147 119
     ordered_objects = opts.get_ordered_objects()
148 120
     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
154 126
         'has_file_field': opts.has_field_type(models.FileField),
155 127
         'has_absolute_url': hasattr(model, 'get_absolute_url'),
156 128
         'auto_populated_fields': auto_populated_fields,
157  
-        'javascript_imports': get_javascript_imports(opts, auto_populated_fields, field_sets),
158 129
         'ordered_objects': ordered_objects,
159 130
         'inline_related_objects': inline_related_objects,
160 131
         'form_url': form_url,

0 notes on commit 259f083

Please sign in to comment.
Something went wrong with that request. Please try again.