Skip to content
Browse files

MERGED NEW-ADMIN BRANCH (except for po/mo files, which will come in a…

… separate commit)

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1434 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 4fe5c9b commit 9dda4abee1225db7a7b195b84c915fdd141a7260 @adrianholovaty adrianholovaty committed
Showing with 2,062 additions and 1,125 deletions.
  1. +8 −8 django/bin/validate.py
  2. +152 −0 django/contrib/admin/filterspecs.py
  3. +75 −0 django/contrib/admin/templates/admin/change_form.html
  4. +20 −0 django/contrib/admin/templates/admin/change_list.html
  5. +13 −0 django/contrib/admin/templates/admin/change_list_results.html
  6. +10 −0 django/contrib/admin/templates/admin/date_hierarchy.html
  7. +15 −0 django/contrib/admin/templates/admin/edit_inline_stacked.html
  8. +42 −0 django/contrib/admin/templates/admin/edit_inline_tabular.html
  9. +21 −0 django/contrib/admin/templates/admin/field_line.html
  10. +7 −0 django/contrib/admin/templates/admin/filter.html
  11. +5 −0 django/contrib/admin/templates/admin/filters.html
  12. +9 −0 django/contrib/admin/templates/admin/pagination.html
  13. +14 −0 django/contrib/admin/templates/admin/search_form.html
  14. +8 −0 django/contrib/admin/templates/admin/submit_line.html
  15. +12 −10 django/contrib/admin/templates/admin_doc/bookmarklets.html
  16. +4 −0 django/contrib/admin/templates/widget/date_time.html
  17. +1 −0 django/contrib/admin/templates/widget/default.html
  18. +4 −0 django/contrib/admin/templates/widget/file.html
  19. +7 −0 django/contrib/admin/templates/widget/foreign.html
  20. +1 −0 django/contrib/admin/templates/widget/many_to_many.html
  21. +278 −0 django/contrib/admin/templatetags/admin_list.py
  22. +258 −0 django/contrib/admin/templatetags/admin_modify.py
  23. +431 −876 django/contrib/admin/views/main.py
  24. +130 −24 django/core/formfields.py
  25. +3 −3 django/core/management.py
  26. +307 −145 django/core/meta/__init__.py
  27. +188 −25 django/core/meta/fields.py
  28. +29 −26 django/models/__init__.py
  29. +9 −8 django/views/generic/create_update.py
  30. +1 −0 setup.py
View
16 django/bin/validate.py
@@ -16,19 +16,19 @@ def validate_class(klass):
assert isinstance(f.rel, meta.ManyToMany), \
"ManyToManyField %s should have 'rel' set to a ManyToMany instance." % f.name
# Inline related objects.
- for rel_opts, rel_field in opts.get_inline_related_objects():
- assert len([f for f in rel_opts.fields if f.core]) > 0, \
+ for related in opts.get_followed_related_objects():
+ assert len([f for f in related.opts.fields if f.core]) > 0, \
"At least one field in %s should have core=True, because it's being edited inline by %s." % \
- (rel_opts.object_name, opts.object_name)
+ (related.opts.object_name, opts.object_name)
# All related objects.
related_apps_seen = []
- for rel_opts, rel_field in opts.get_all_related_objects():
- if rel_opts in related_apps_seen:
- assert rel_field.rel.related_name is not None, \
+ for related in opts.get_all_related_objects():
+ if related.opts in related_apps_seen:
+ assert related.field.rel.related_name is not None, \
"Relationship in field %s.%s needs to set 'related_name' because more than one" \
" %s object is referenced in %s." % \
- (rel_opts.object_name, rel_field.name, opts.object_name, rel_opts.object_name)
- related_apps_seen.append(rel_opts)
+ (related.opts.object_name, related.field.name, opts.object_name, rel_opts.object_name)
+ related_apps_seen.append(related.opts)
# Etc.
if opts.admin is not None:
assert opts.admin.ordering or opts.ordering, \
View
152 django/contrib/admin/filterspecs.py
@@ -0,0 +1,152 @@
+"""
+FilterSpec encapsulates the logic for displaying filters in the Django admin.
+Filters are specified in models with the "list_filter" option.
+
+Each filter subclass knows how to display a filter for a field that passes a
+certain test -- e.g. being a DateField or ForeignKey.
+"""
+
+from django.core import meta
+import datetime
+
+class FilterSpec(object):
+ filter_specs = []
+ def __init__(self, f, request, params):
+ self.field = f
+ self.params = params
+
+ def register(cls, test, factory):
+ cls.filter_specs.append( (test, factory) )
+ register = classmethod(register)
+
+ def create(cls, f, request, params):
+ for test, factory in cls.filter_specs:
+ if test(f):
+ return factory(f, request, params)
+ create = classmethod(create)
+
+ def has_output(self):
+ return True
+
+ def choices(self, cl):
+ raise NotImplementedError()
+
+ def title(self):
+ return self.field.verbose_name
+
+ def output(self, cl):
+ t = []
+ if self.has_output():
+ t.append(_('<h3>By %s:</h3>\n<ul>\n') % self.title())
+
+ for choice in self.choices(cl):
+ t.append('<li%s><a href="%s">%s</a></li>\n' % \
+ ((choice['selected'] and ' class="selected"' or ''),
+ choice['query_string'] ,
+ choice['display']))
+ t.append('</ul>\n\n')
+ return "".join(t)
+
+class RelatedFilterSpec(FilterSpec):
+ def __init__(self, f, request, params):
+ super(RelatedFilterSpec, self).__init__(f, request, params)
+ if isinstance(f, meta.ManyToManyField):
+ self.lookup_title = f.rel.to.verbose_name
+ else:
+ self.lookup_title = f.verbose_name
+ self.lookup_kwarg = '%s__%s__exact' % (f.name, f.rel.to.pk.name)
+ self.lookup_val = request.GET.get(self.lookup_kwarg, None)
+ self.lookup_choices = f.rel.to.get_model_module().get_list()
+
+ def has_output(self):
+ return len(self.lookup_choices) > 1
+
+ def title(self):
+ return self.lookup_title
+
+ def choices(self, cl):
+ yield {'selected': self.lookup_val is None,
+ 'query_string': cl.get_query_string({}, [self.lookup_kwarg]),
+ 'display': _('All')}
+ for val in self.lookup_choices:
+ pk_val = getattr(val, self.field.rel.to.pk.attname)
+ yield {'selected': self.lookup_val == str(pk_val),
+ 'query_string': cl.get_query_string( {self.lookup_kwarg: pk_val}),
+ 'display': val}
+
+FilterSpec.register(lambda f: bool(f.rel), RelatedFilterSpec)
+
+class ChoicesFilterSpec(FilterSpec):
+ def __init__(self, f, request, params):
+ super(ChoicesFilterSpec, self).__init__(f, request, params)
+ self.lookup_kwarg = '%s__exact' % f.name
+ self.lookup_val = request.GET.get(self.lookup_kwarg, None)
+
+ def choices(self, cl):
+ yield {'selected': self.lookup_val is None,
+ 'query_string': cl.get_query_string( {}, [self.lookup_kwarg]),
+ 'display': _('All')}
+ for k, v in self.field.choices:
+ yield {'selected': str(k) == self.lookup_val,
+ 'query_string': cl.get_query_string( {self.lookup_kwarg: k}),
+ 'display': v}
+
+FilterSpec.register(lambda f: bool(f.choices), ChoicesFilterSpec)
+
+class DateFieldFilterSpec(FilterSpec):
+ def __init__(self, f, request, params):
+ super(DateFieldFilterSpec, self).__init__(f, request, params)
+
+ self.field_generic = '%s__' % self.field.name
+
+ self.date_params = dict([(k, v) for k, v in params.items() if k.startswith(self.field_generic)])
+
+ today = datetime.date.today()
+ one_week_ago = today - datetime.timedelta(days=7)
+ today_str = isinstance(self.field, meta.DateTimeField) and today.strftime('%Y-%m-%d 23:59:59') or today.strftime('%Y-%m-%d')
+
+ self.links = (
+ (_('Any date'), {}),
+ (_('Today'), {'%s__year' % self.field.name: str(today.year),
+ '%s__month' % self.field.name: str(today.month),
+ '%s__day' % self.field.name: str(today.day)}),
+ (_('Past 7 days'), {'%s__gte' % self.field.name: one_week_ago.strftime('%Y-%m-%d'),
+ '%s__lte' % f.name: today_str}),
+ (_('This month'), {'%s__year' % self.field.name: str(today.year),
+ '%s__month' % f.name: str(today.month)}),
+ (_('This year'), {'%s__year' % self.field.name: str(today.year)})
+ )
+
+ def title(self):
+ return self.field.verbose_name
+
+ def choices(self, cl):
+ for title, param_dict in self.links:
+ yield {'selected': self.date_params == param_dict,
+ 'query_string': cl.get_query_string( param_dict, self.field_generic),
+ 'display': title}
+
+FilterSpec.register(lambda f: isinstance(f, meta.DateField), DateFieldFilterSpec)
+
+class BooleanFieldFilterSpec(FilterSpec):
+ def __init__(self, f, request, params):
+ super(BooleanFieldFilterSpec, self).__init__(f, request, params)
+ self.lookup_kwarg = '%s__exact' % f.name
+ self.lookup_kwarg2 = '%s__isnull' % f.name
+ self.lookup_val = request.GET.get(self.lookup_kwarg, None)
+ self.lookup_val2 = request.GET.get(self.lookup_kwarg2, None)
+
+ def title(self):
+ return self.field.verbose_name
+
+ def choices(self, cl):
+ for k, v in ((_('All'), None), (_('Yes'), '1'), (_('No'), '0')):
+ yield {'selected': self.lookup_val == v and not self.lookup_val2,
+ 'query_string': cl.get_query_string( {self.lookup_kwarg: v}, [self.lookup_kwarg2]),
+ 'display': k}
+ if isinstance(self.field, meta.NullBooleanField):
+ yield {'selected': self.lookup_val2 == 'True',
+ 'query_string': cl.get_query_string( {self.lookup_kwarg2: 'True'}, [self.lookup_kwarg]),
+ 'display': _('Unknown')}
+
+FilterSpec.register(lambda f: isinstance(f, meta.BooleanField) or isinstance(f, meta.NullBooleanField), BooleanFieldFilterSpec)
View
75 django/contrib/admin/templates/admin/change_form.html
@@ -0,0 +1,75 @@
+{% extends "admin/base_site" %}
+{% load i18n %}
+{% load admin_modify %}
+{% load adminmedia %}
+{% block extrahead %}
+{% for js in bound_manipulator.javascript_imports %}{% include_admin_script js %}{% endfor %}
+{% endblock %}
+{% block coltype %}{{ bound_manipulator.coltype }}{% endblock %}
+{% block bodyclass %}{{ app_label }}-{{ bound_manipulator.object_name.lower }} change-form{% endblock %}
+{% block breadcrumbs %}{% if not is_popup %}
+<div class="breadcrumbs">
+ <a href="../../../">{% trans "Home" %}</a> &rsaquo;
+ <a href="../">{{ bound_manipulator.verbose_name_plural|capfirst }}</a> &rsaquo;
+ {% if add %}{% trans "Add" %} {{ bound_manipulator.verbose_name }}{% else %}{{ bound_manipulator.original|striptags|truncatewords:"18" }}{% endif %}
+</div>
+{% endif %}{% endblock %}
+{% block content %}<div id="content-main">
+{% if change %}{% if not is_popup %}
+ <ul class="object-tools"><li><a href="history/" class="historylink">{% trans "History" %}</a></li>
+ {% if bound_manipulator.has_absolute_url %}<li><a href="/r/{{ bound_manipulator.content_type_id }}/{{ object_id }}/" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif%}
+ </ul>
+{% endif %}{% endif %}
+<form {{ bound_manipulator.form_enc_attrib }} action='{{ form_url }}' method="post">{% block form_top %}{% endblock %}
+{% if is_popup %}<input type="hidden" name="_popup" value="1">{% endif %}
+{% if bound_manipulator.save_on_top %}{% submit_row bound_manipulator %}{% endif %}
+{% if form.error_dict %}
+ <p class="errornote">
+ {% blocktrans count form.error_dict.items|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
+ </p>
+{% endif %}
+{% for bound_field_set in bound_manipulator.bound_field_sets %}
+ <fieldset class="module aligned {{ bound_field_set.classes }}">
+ {% if bound_field_set.name %}<h2>{{ bound_field_set.name }}</h2>{% endif %}
+ {% for bound_field_line in bound_field_set %}
+ {% admin_field_line bound_field_line %}
+ {% for bound_field in bound_field_line %}
+ {% filter_interface_script_maybe bound_field %}
+ {% endfor %}
+ {% endfor %}
+ </fieldset>
+{% endfor %}
+{% block after_field_sets %}{% endblock %}
+{% if change %}
+ {% if bound_manipulator.ordered_objects %}
+ <fieldset class="module"><h2>{% trans "Ordering" %}</h2>
+ <div class="form-row{% if form.order_.errors %} error{% endif %} ">
+ {% if form.order_.errors %}{{ form.order_.html_error_list }}{% endif %}
+ <p><label for="id_order_">{% trans "Order:" %}</label> {{ form.order_ }}</p>
+ </div></fieldset>
+ {% endif %}
+{% endif %}
+{% for related_object in bound_manipulator.inline_related_objects %}{% edit_inline related_object %}{% endfor %}
+{% block after_related_objects %}{% endblock %}
+{% submit_row bound_manipulator %}
+{% if add %}
+ <script type="text/javascript">document.getElementById("{{ bound_manipulator.first_form_field_id }}").focus();</script>
+{% endif %}
+{% if bound_manipulator.auto_populated_fields %}
+ <script type="text/javascript">
+ {% auto_populated_field_script bound_manipulator.auto_populated_fields change %}
+ </script>
+{% endif %}
+{% if change %}
+ {% if bound_manipulator.ordered_objects %}
+ {% if form.order_objects %}<ul id="orderthese">
+ {% for object in form.order_objects %}
+ <li id="p{% object_pk bound_manipulator object %}">
+ <span id="handlep{% object_pk bound_manipulator object %}">{{ object|truncatewords:"5" }}</span>
+ </li>
+ {% endfor %}
+ </ul>{% endif %}
+ {% endif %}
+{% endif %}
+</form></div>
+{% endblock %}
View
20 django/contrib/admin/templates/admin/change_list.html
@@ -0,0 +1,20 @@
+{% load admin_list %}
+{% load i18n %}
+{% extends "admin/base_site" %}
+{% block bodyclass %}change-list{% endblock %}
+{% if not is_popup %}{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../">{% trans "Home" %}</a> &rsaquo; {{ cl.opts.verbose_name_plural|capfirst }} </div>{% endblock %}{% endif %}
+{% block coltype %}flex{% endblock %}
+{% block content %}
+<div id="content-main">
+{% if has_add_permission %}
+<ul class="object-tools"><li><a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}</a></li></ul>
+{% endif %}
+<div class="module{% if cl.has_filters %} filtered{% endif %}" id="changelist">
+{% search_form cl %}
+{% date_hierarchy cl %}
+{% filters cl %}
+{% result_list cl %}
+{% pagination cl %}
+</div>
+</div>
+{% endblock %}
View
13 django/contrib/admin/templates/admin/change_list_results.html
@@ -0,0 +1,13 @@
+<table cellspacing="0">
+<thead>
+<tr>
+{% for header in result_headers %}<th{{ header.class_attrib }}>
+{% if header.sortable %}<a href="{{ header.url }}">{% endif %}
+{{ header.text|capfirst }}
+{% if header.sortable %}</a>{% endif %}</th>{% endfor %}
+</tr>
+</thead>
+{% for result in results %}
+<tr class="{% cycle row1,row2 %}">{% for item in result %}{{ item }}{% endfor %}</tr>
+{% endfor %}
+</table>
View
10 django/contrib/admin/templates/admin/date_hierarchy.html
@@ -0,0 +1,10 @@
+{% if show %}
+<div class="xfull">
+<ul class="toplinks">
+{% if back %}<li class="date-back"><a href="{{ back.link }}">&lsaquo; {{ back.title }}</a></li>{% endif %}
+{% for choice in choices %}
+<li> {% if choice.link %}<a href="{{ choice.link }}">{% endif %}{{ choice.title }}{% if choice.link %}</a>{% endif %}</li>
+{% endfor %}
+</ul><br class="clear" />
+</div>
+{% endif %}
View
15 django/contrib/admin/templates/admin/edit_inline_stacked.html
@@ -0,0 +1,15 @@
+<fieldset class="module aligned">
+ {% for fcw in bound_related_object.form_field_collection_wrappers %}
+ <h2>{{ bound_related_object.relation.opts.verbose_name|capfirst }}&nbsp;#{{ forloop.counter }}</h2>
+ {% if bound_related_object.show_url %}{% if fcw.obj.original %}
+ <p><a href="/r/{{ fcw.obj.original.content_type_id }}/{{ fcw.obj.original.id }}/">View on site</a></p>
+ {% endif %}{% endif %}
+ {% for bound_field in fcw.bound_fields %}
+ {% if bound_field.hidden %}
+ {% field_widget bound_field %}
+ {% else %}
+ {% admin_field_line bound_field %}
+ {% endif %}
+ {% endfor %}
+ {% endfor %}
+</fieldset>
View
42 django/contrib/admin/templates/admin/edit_inline_tabular.html
@@ -0,0 +1,42 @@
+<fieldset class="module">
+ <h2>{{ bound_related_object.relation.opts.verbose_name_plural|capfirst }}</h2><table>
+ <thead><tr>
+ {% for fw in bound_related_object.field_wrapper_list %}
+ {% if fw.needs_header %}
+ <th{{ fw.header_class_attribute }}>{{ fw.field.verbose_name|capfirst }}</th>
+ {% endif %}
+ {% endfor %}
+ {% for fcw in bound_related_object.form_field_collection_wrappers %}
+ {% if change %}{% if original_row_needed %}
+ {% if fcw.obj.original %}
+ <tr class="row-label {% cycle row1,row2 %}"><td colspan="{{ num_headers }}"><strong>{{ fcw.obj.original }}</strong></tr>
+ {% endif %}
+ {% endif %}{% endif %}
+ {% if fcw.obj.errors %}
+ <tr class="errorlist"><td colspan="{{ num_headers }}">
+ {{ fcw.obj.html_combined_error_list }}
+ </tr>
+ {% endif %}
+ <tr class="{% cycle row1,row2 %}">
+ {% for bound_field in fcw.bound_fields %}
+ {% if not bound_field.hidden %}
+ <td {{ bound_field.cell_class_attribute }}>
+ {% field_widget bound_field %}
+ </td>
+ {% endif %}
+ {% endfor %}
+ {% if bound_related_object.show_url %}<td>
+ {% if fcw.obj.original %}<a href="/r/{{ fcw.obj.original.content_type_id }}/{{ fcw.obj.original.id }}/">View on site</a>{% endif %}
+ </td>{% endif %}
+ </tr>
+
+ {% endfor %} </table>
+
+ {% for fcw in bound_related_object.form_field_collection_wrappers %}
+ {% for bound_field in fcw.bound_fields %}
+ {% if bound_field.hidden %}
+ {% field_widget bound_field %}
+ {% endif %}
+ {% endfor %}
+ {% endfor %}
+</fieldset>
View
21 django/contrib/admin/templates/admin/field_line.html
@@ -0,0 +1,21 @@
+<div class="{{ class_names }}" >
+{% for bound_field in bound_fields %}{{ bound_field.html_error_list }}{% endfor %}
+{% for bound_field in bound_fields %}
+ {% if bound_field.has_label_first %}
+ {% field_label bound_field %}
+ {% endif %}
+ {% field_widget bound_field %}
+ {% if not bound_field.has_label_first %}
+ {% field_label bound_field %}
+ {% endif %}
+ {% if change %}
+ {% if bound_field.field.primary_key %}
+ {{ bound_field.original_value }}
+ {% endif %}
+ {% if bound_field.raw_id_admin %}
+ {% if bound_field.existing_display %}&nbsp;<strong>{{ bound_field.existing_display|truncatewords:"14" }}</strong>{% endif %}
+ {% endif %}
+ {% endif %}
+ {% if bound_field.field.help_text %}<p class="help">{{ bound_field.field.help_text }}</p>{% endif %}
+{% endfor %}
+</div>
View
7 django/contrib/admin/templates/admin/filter.html
@@ -0,0 +1,7 @@
+<h3>{% blocktrans %} By {{ title }} {% endblocktrans %}</h3>
+<ul>
+{% for choice in choices %}
+ <li{% if choice.selected %} class="selected"{% endif %}>
+ <a href="{{ choice.query_string }}">{{ choice.display }}</a></li>
+{% endfor %}
+</ul>
View
5 django/contrib/admin/templates/admin/filters.html
@@ -0,0 +1,5 @@
+{% if cl.has_filters %}<div id="changelist-filter">
+<h2>Filter</h2>
+{% for spec in cl.filter_specs %}
+ {% filter cl spec %}
+{% endfor %}</div>{% endif %}
View
9 django/contrib/admin/templates/admin/pagination.html
@@ -0,0 +1,9 @@
+<p class="paginator">
+{% if pagination_required %}
+{% for i in page_range %}
+ {% paginator_number cl i %}
+{% endfor %}
+{% endif %}
+{{ cl.result_count }} {% ifequal cl.result_count 1 %}{{ cl.opts.verbose_name }}{% else %}{{ cl.opts.verbose_name_plural }}{% endifequal %}
+{% if show_all_url %}&nbsp;&nbsp;<a href="{{ show_all_url }}" class="showall">Show all</a>{% endif %}
+</p>
View
14 django/contrib/admin/templates/admin/search_form.html
@@ -0,0 +1,14 @@
+{% if cl.lookup_opts.admin.search_fields %}
+<div id="toolbar"><form id="changelist-search" action="" method="get">
+<label><img src="{% admin_media_prefix %}img/admin/icon_searchbox.png" /></label>
+<input type="text" size="40" name="{{ search_var }}" value="{{ cl.query|escape }}" id="searchbar" />
+<input type="submit" value="Go" />
+{% if show_result_count %}
+ <span class="small quiet">{{ cl.result_count }} result{{ cl.result_count|pluralize }} (<a href="?">{{ cl.full_result_count }} total</a>)</span>
+{% endif %}
+{% for pair in cl.params.items %}
+ {% ifnotequal pair.0 search_var %}<input type="hidden" name="{{ pair.0|escape }}" value="{{ pair.1|escape }}"/>{% endifnotequal %}
+{% endfor %}
+</form></div>
+<script type="text/javascript">document.getElementById("searchbar").focus();</script>
+{% endif %}
View
8 django/contrib/admin/templates/admin/submit_line.html
@@ -0,0 +1,8 @@
+{% load i18n %}
+<div class="submit-row">
+{% if show_delete_link %}<p class="float-left"><a href="delete/" class="deletelink">{% trans "Delete" %}</a></p>{% endif %}
+{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%}
+{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }} />{% endif %}
+{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" {{ onclick_attrib }}/>{% endif %}
+{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" {{ onclick_attrib }}/>{% endif %}
+</div>
View
22 django/contrib/admin/templates/admin_doc/bookmarklets.html
@@ -1,30 +1,32 @@
{% extends "admin/base_site" %}
-{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../">Home</a> &rsaquo; <a href="../">Documentation</a> &rsaquo; Bookmarklets</div>{% endblock %}
+{% block breadcrumbs %}<div class="breadcrumbs"><a href="../../">{% trans "Home" %}</a> &rsaquo; <a href="../">{% trans "Documentation" %}</a> &rsaquo; {% trans "Bookmarklets" %}</div>{% endblock %}
-{% block title %}Documentation bookmarklets{% endblock %}
+{% block title %}{% trans "Documentation bookmarklets" %}{% endblock %}
{% block content %}
+{% blocktrans %}
<p class="help">To install bookmarklets, drag the link to your bookmarks
toolbar, or right-click the link and add it to your bookmarks. Now you can
select the bookmarklet from any page in the site. Note that some of these
bookmarklets require you to be viewing the site from a computer designated
as "internal" (talk to your system administrator if you aren't sure if
your computer is "internal").</p>
+{% endblocktrans %}
<div id="content-main">
- <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){x=new XMLHttpRequest()}else{return;}x.open('HEAD',location.href,false);x.send(null);try{view=x.getResponseHeader('x-view');}catch(e){alert('No view found for this page');return;}if(view=="undefined"){alert("No view found for this page");}document.location='{{ admin_url }}/doc/views/'+view+'/';})()">Documentation for this page</a></h3>
- <p>Jumps you from any page to the documentation for the view that generates that page.</p>
+ <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){x=new XMLHttpRequest()}else{return;}x.open('HEAD',location.href,false);x.send(null);try{view=x.getResponseHeader('x-view');}catch(e){alert('No view found for this page');return;}if(view=="undefined"){alert("No view found for this page");}document.location='{{ admin_url }}/doc/views/'+view+'/';})()">{% trans "Documentation for this page" %}</a></h3>
+ <p>{% trans "Jumps you from any page to the documentation for the view that generates that page." %}</p>
- <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){x=new XMLHttpRequest()}else{return;}x.open('GET',location.href,false);x.send(null);try{type=x.getResponseHeader('x-object-type');id=x.getResponseHeader('x-object-id');}catch(e){type='(none)';id='(none)';}d=document;b=d.body;e=d.createElement('div');e.id='xxxhhh';s=e.style;s.position='absolute';s.left='10px';s.top='10px';s.font='10px monospace';s.border='1px black solid';s.padding='4px';s.backgroundColor='#eee';e.appendChild(d.createTextNode('Type: '+type));e.appendChild(d.createElement('br'));e.appendChild(d.createTextNode('ID: '+id));e.appendChild(d.createElement('br'));l=d.createElement('a');l.href='#';l.onclick=function(){b.removeChild(e);};l.appendChild(d.createTextNode('[close]'));l.style.textDecoration='none';e.appendChild(l);b.appendChild(e);})();">Show object ID</a></h3>
- <p>Shows the content-type and unique ID for pages that represent a single object.</p>
+ <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){x=new XMLHttpRequest()}else{return;}x.open('GET',location.href,false);x.send(null);try{type=x.getResponseHeader('x-object-type');id=x.getResponseHeader('x-object-id');}catch(e){type='(none)';id='(none)';}d=document;b=d.body;e=d.createElement('div');e.id='xxxhhh';s=e.style;s.position='absolute';s.left='10px';s.top='10px';s.font='10px monospace';s.border='1px black solid';s.padding='4px';s.backgroundColor='#eee';e.appendChild(d.createTextNode('Type: '+type));e.appendChild(d.createElement('br'));e.appendChild(d.createTextNode('ID: '+id));e.appendChild(d.createElement('br'));l=d.createElement('a');l.href='#';l.onclick=function(){b.removeChild(e);};l.appendChild(d.createTextNode('[close]'));l.style.textDecoration='none';e.appendChild(l);b.appendChild(e);})();">{% trans "Show object ID" %}</a></h3>
+ <p>{% trans "Shows the content-type and unique ID for pages that represent a single object." %}</p>
- <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){var x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){var x=new XMLHttpRequest()}else{return;}x.open('GET',location.href,false);x.send(null);try{var type=x.getResponseHeader('x-object-type');var id=x.getResponseHeader('x-object-id');}catch(e){return;}document.location='{{ admun_url }}/'+type.split('.').join('/')+'/'+id+'/';})()">Edit this object (current window)</a></h3>
- <p>Jumps to the admin page for pages that represent a single object.</p>
+ <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){var x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){var x=new XMLHttpRequest()}else{return;}x.open('GET',location.href,false);x.send(null);try{var type=x.getResponseHeader('x-object-type');var id=x.getResponseHeader('x-object-id');}catch(e){return;}document.location='{{ admun_url }}/'+type.split('.').join('/')+'/'+id+'/';})()">{% trans "Edit this object (current window)" %}</a></h3>
+ <p>{% trans "Jumps to the admin page for pages that represent a single object." %}</p>
- <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){var x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){var x=new XMLHttpRequest()}else{return;}x.open('GET',location.href,false);x.send(null);try{var type=x.getResponseHeader('x-object-type');var id=x.getResponseHeader('x-object-id');}catch(e){return;}window.open('{{ admun_url }}/'+type.split('.').join('/')+'/'+id+'/');})()">Edit this object (new window)</a></h3>
- <p>As above, but opens the admin page in a new window.</p>
+ <h3><a href="javascript:(function(){if(typeof ActiveXObject!='undefined'){var x=new ActiveXObject('Microsoft.XMLHTTP')}else if(typeof XMLHttpRequest!='undefined'){var x=new XMLHttpRequest()}else{return;}x.open('GET',location.href,false);x.send(null);try{var type=x.getResponseHeader('x-object-type');var id=x.getResponseHeader('x-object-id');}catch(e){return;}window.open('{{ admun_url }}/'+type.split('.').join('/')+'/'+id+'/');})()">{% trans "Edit this object (new window)" %}</a></h3>
+ <p>{% trans "As above, but opens the admin page in a new window." %}</p>
</div>
{% endblock %}
View
4 django/contrib/admin/templates/widget/date_time.html
@@ -0,0 +1,4 @@
+<p class="datetime">
+ Date: {{ bound_field.form_fields.0 }}<br />
+ Time: {{ bound_field.form_fields.1 }}
+</p>
View
1 django/contrib/admin/templates/widget/default.html
@@ -0,0 +1 @@
+{% output_all bound_field.form_fields %}
View
4 django/contrib/admin/templates/widget/file.html
@@ -0,0 +1,4 @@
+{% if bound_field.original_value %}
+Currently: <a href="{{ bound_field.original_url }}" > {{ bound_field.original_value }} </a><br />
+Change: {% output_all bound_field.form_fields %}
+{% else %} {% output_all bound_field.form_fields %} {% endif %}
View
7 django/contrib/admin/templates/widget/foreign.html
@@ -0,0 +1,7 @@
+{% output_all bound_field.form_fields %}
+{% if bound_field.raw_id_admin %}
+ <a href="../../../{{ bound_field.field.rel.to.app_label }}/{{ bound_field.field.rel.to.module_name }}/" class="related-lookup" id="lookup_{{bound_field.element_id}}" onclick="return showRelatedObjectLookupPopup(this);"> <img src="{% admin_media_prefix %}img/admin/selector-search.gif" width="16" height="16" alt="Lookup"></a>
+{% else %}
+{% if bound_field.needs_add_label %}
+ <a href="../../../{{ bound_field.field.rel.to.app_label }}/{{ bound_field.field.rel.to.module_name }}/add/" class="add-another" id="add_{{ bound_field.element_id}}" onclick="return showAddAnotherPopup(this);"> <img src="{% admin_media_prefix %}img/admin/icon_addlink.gif" width="10" height="10" alt="Add Another"/></a>
+{% endif %} {% endif %}
View
1 django/contrib/admin/templates/widget/many_to_many.html
@@ -0,0 +1 @@
+{% include "widget/foreign" %}
View
278 django/contrib/admin/templatetags/admin_list.py
@@ -0,0 +1,278 @@
+from django.contrib.admin.views.main import MAX_SHOW_ALL_ALLOWED, DEFAULT_RESULTS_PER_PAGE, ALL_VAR
+from django.contrib.admin.views.main import ORDER_VAR, ORDER_TYPE_VAR, PAGE_VAR, SEARCH_VAR
+from django.contrib.admin.views.main import IS_POPUP_VAR, EMPTY_CHANGELIST_VALUE, MONTHS
+from django.core import meta, template
+from django.core.exceptions import ObjectDoesNotExist
+from django.core.template.decorators import simple_tag, inclusion_tag
+from django.utils import dateformat
+from django.utils.html import strip_tags, escape
+from django.utils.text import capfirst
+from django.utils.translation import get_date_formats
+from django.conf.settings import ADMIN_MEDIA_PREFIX
+
+DOT = '.'
+
+#@simple_tag
+def paginator_number(cl,i):
+ if i == DOT:
+ return '... '
+ elif i == cl.page_num:
+ return '<span class="this-page">%d</span> ' % (i+1)
+ else:
+ return '<a href="%s"%s>%d</a> ' % (cl.get_query_string({PAGE_VAR: i}), (i == cl.paginator.pages-1 and ' class="end"' or ''), i+1)
+paginator_number = simple_tag(paginator_number)
+
+#@inclusion_tag('admin/pagination')
+def pagination(cl):
+ paginator, page_num = cl.paginator, cl.page_num
+
+ pagination_required = (not cl.show_all or not cl.can_show_all) and cl.multi_page
+ if not pagination_required:
+ page_range = []
+ else:
+ ON_EACH_SIDE = 3
+ ON_ENDS = 2
+
+ # If there are 10 or fewer pages, display links to every page.
+ # Otherwise, do some fancy
+ if paginator.pages <= 10:
+ page_range = range(paginator.pages)
+ else:
+ # Insert "smart" pagination links, so that there are always ON_ENDS
+ # links at either end of the list of pages, and there are always
+ # ON_EACH_SIDE links at either end of the "current page" link.
+ page_range = []
+ if page_num > (ON_EACH_SIDE + ON_ENDS):
+ page_range.extend(range(0, ON_EACH_SIDE - 1))
+ page_range.append(DOT)
+ page_range.extend(range(page_num - ON_EACH_SIDE, page_num + 1))
+ else:
+ page_range.extend(range(0, page_num + 1))
+ if page_num < (paginator.pages - ON_EACH_SIDE - ON_ENDS - 1):
+ page_range.extend(range(page_num + 1, page_num + ON_EACH_SIDE + 1))
+ page_range.append(DOT)
+ page_range.extend(range(paginator.pages - ON_ENDS, paginator.pages))
+ else:
+ page_range.extend(range(page_num + 1, paginator.pages))
+
+ need_show_all_link = cl.can_show_all and not cl.show_all and cl.multi_page
+ return {
+ 'cl': cl,
+ 'pagination_required': pagination_required,
+ 'show_all_url': need_show_all_link and cl.get_query_string({ALL_VAR: ''}),
+ 'page_range': page_range,
+ 'ALL_VAR': ALL_VAR,
+ '1': 1,
+ }
+pagination = inclusion_tag('admin/pagination')(pagination)
+
+def result_headers(cl):
+ lookup_opts = cl.lookup_opts
+
+ for i, field_name in enumerate(lookup_opts.admin.list_display):
+ try:
+ f = lookup_opts.get_field(field_name)
+ except meta.FieldDoesNotExist:
+ # For non-field list_display values, check for the function
+ # attribute "short_description". If that doesn't exist, fall
+ # back to the method name. And __repr__ is a special-case.
+ if field_name == '__repr__':
+ header = lookup_opts.verbose_name
+ else:
+ func = getattr(cl.mod.Klass, field_name) # Let AttributeErrors propogate.
+ try:
+ header = func.short_description
+ except AttributeError:
+ header = func.__name__.replace('_', ' ')
+ # Non-field list_display values don't get ordering capability.
+ yield {"text": header}
+ else:
+ if isinstance(f.rel, meta.ManyToOne) and f.null:
+ yield {"text": f.verbose_name}
+ else:
+ th_classes = []
+ new_order_type = 'asc'
+ if field_name == cl.order_field:
+ th_classes.append('sorted %sending' % cl.order_type.lower())
+ new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()]
+
+ yield {"text": f.verbose_name,
+ "sortable": True,
+ "url": cl.get_query_string({ORDER_VAR: i, ORDER_TYPE_VAR: new_order_type}),
+ "class_attrib": (th_classes and ' class="%s"' % ' '.join(th_classes) or '')}
+
+def items_for_result(cl, result):
+ first = True
+ pk = cl.lookup_opts.pk.attname
+ for field_name in cl.lookup_opts.admin.list_display:
+ row_class = ''
+ try:
+ f = cl.lookup_opts.get_field(field_name)
+ except meta.FieldDoesNotExist:
+ # For non-field list_display values, the value is a method
+ # name. Execute the method.
+ try:
+ func = getattr(result, field_name)
+ result_repr = str(func())
+ except AttributeError, ObjectDoesNotExist:
+ result_repr = EMPTY_CHANGELIST_VALUE
+ else:
+ # Strip HTML tags in the resulting text, except if the
+ # function has an "allow_tags" attribute set to True.
+ if not getattr(func, 'allow_tags', False):
+ result_repr = strip_tags(result_repr)
+ else:
+ field_val = getattr(result, f.attname)
+
+ if isinstance(f.rel, meta.ManyToOne):
+ if field_val is not None:
+ result_repr = getattr(result, 'get_%s' % f.name)()
+ else:
+ result_repr = EMPTY_CHANGELIST_VALUE
+ # Dates and times are special: They're formatted in a certain way.
+ elif isinstance(f, meta.DateField) or isinstance(f, meta.TimeField):
+ if field_val:
+ (date_format, datetime_format, time_format) = get_date_formats()
+ if isinstance(f, meta.DateTimeField):
+ result_repr = capfirst(dateformat.format(field_val, datetime_format))
+ elif isinstance(f, meta.TimeField):
+ result_repr = capfirst(dateformat.time_format(field_val, time_format))
+ else:
+ result_repr = capfirst(dateformat.format(field_val, date_format))
+ else:
+ result_repr = EMPTY_CHANGELIST_VALUE
+ row_class = ' class="nowrap"'
+ # Booleans are special: We use images.
+ elif isinstance(f, meta.BooleanField) or isinstance(f, meta.NullBooleanField):
+ BOOLEAN_MAPPING = {True: 'yes', False: 'no', None: 'unknown'}
+ result_repr = '<img src="%simg/admin/icon-%s.gif" alt="%s" />' % (ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val)
+ # ImageFields are special: Use a thumbnail.
+ elif isinstance(f, meta.ImageField):
+ from django.parts.media.photos import get_thumbnail_url
+ result_repr = '<img src="%s" alt="%s" title="%s" />' % (get_thumbnail_url(getattr(result, 'get_%s_url' % f.name)(), '120'), field_val, field_val)
+ # FloatFields are special: Zero-pad the decimals.
+ elif isinstance(f, meta.FloatField):
+ if field_val is not None:
+ result_repr = ('%%.%sf' % f.decimal_places) % field_val
+ else:
+ result_repr = EMPTY_CHANGELIST_VALUE
+ # Fields with choices are special: Use the representation
+ # of the choice.
+ elif f.choices:
+ result_repr = dict(f.choices).get(field_val, EMPTY_CHANGELIST_VALUE)
+ else:
+ result_repr = strip_tags(str(field_val))
+ if result_repr == '':
+ result_repr = '&nbsp;'
+ if first: # First column is a special case
+ first = False
+ url = cl.url_for_result(result)
+ result_id = getattr(result, pk)
+ yield ('<th%s><a href="%s"%s>%s</a></th>' % \
+ (row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %r); return false;"' % result_id or ''), result_repr))
+ else:
+ yield ('<td%s>%s</td>' % (row_class, result_repr))
+
+def results(cl):
+ for res in cl.result_list:
+ yield list(items_for_result(cl,res))
+
+#@inclusion_tag("admin/change_list_results")
+def result_list(cl):
+ res = list(results(cl))
+ return {'cl': cl,
+ 'result_headers': list(result_headers(cl)),
+ 'results': list(results(cl))}
+result_list = inclusion_tag("admin/change_list_results")(result_list)
+
+#@inclusion_tag("admin/date_hierarchy")
+def date_hierarchy(cl):
+ lookup_opts, params, lookup_params, lookup_mod = \
+ cl.lookup_opts, cl.params, cl.lookup_params, cl.lookup_mod
+
+ if lookup_opts.admin.date_hierarchy:
+ field_name = lookup_opts.admin.date_hierarchy
+
+ year_field = '%s__year' % field_name
+ month_field = '%s__month' % field_name
+ day_field = '%s__day' % field_name
+ field_generic = '%s__' % field_name
+ year_lookup = params.get(year_field)
+ month_lookup = params.get(month_field)
+ day_lookup = params.get(day_field)
+
+ def link(d):
+ return cl.get_query_string(d, [field_generic])
+
+ def get_dates(unit, params):
+ return getattr(lookup_mod, 'get_%s_list' % field_name)(unit, **params)
+
+ if year_lookup and month_lookup and day_lookup:
+ month_name = MONTHS[int(month_lookup)]
+ return {
+ 'show': True,
+ 'back': {
+ 'link': link({year_field: year_lookup, month_field: month_lookup}),
+ 'title': "%s %s" % (month_name, year_lookup)
+ },
+ 'choices': [{'title': "%s %s" % (month_name, day_lookup)}]
+ }
+ elif year_lookup and month_lookup:
+ date_lookup_params = lookup_params.copy()
+ date_lookup_params.update({year_field: year_lookup, month_field: month_lookup})
+ days = get_dates('day', date_lookup_params)
+ return {
+ 'show': True,
+ 'back': {
+ 'link': link({year_field: year_lookup}),
+ 'title': year_lookup
+ },
+ 'choices': [{
+ 'link': link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}),
+ 'title': day.strftime('%B %d')
+ } for day in days]
+ }
+ elif year_lookup:
+ date_lookup_params = lookup_params.copy()
+ date_lookup_params.update({year_field: year_lookup})
+ months = get_dates('month', date_lookup_params)
+ return {
+ 'show' : True,
+ 'back': {
+ 'link' : link({}),
+ 'title': _('All dates')
+ },
+ 'choices': [{
+ 'link': link( {year_field: year_lookup, month_field: month.month}),
+ 'title': "%s %s" % (month.strftime('%B') , month.year)
+ } for month in months]
+ }
+ else:
+ years = get_dates('year', lookup_params)
+ return {
+ 'show': True,
+ 'choices': [{
+ 'link': link({year_field: year.year}),
+ 'title': year.year
+ } for year in years ]
+ }
+date_hierarchy = inclusion_tag('admin/date_hierarchy')(date_hierarchy)
+
+#@inclusion_tag('admin/search_form')
+def search_form(cl):
+ return {
+ 'cl': cl,
+ 'show_result_count': cl.result_count != cl.full_result_count and not cl.opts.one_to_one_field,
+ 'search_var': SEARCH_VAR
+ }
+search_form = inclusion_tag('admin/search_form')(search_form)
+
+#@inclusion_tag('admin/filter')
+def filter(cl, spec):
+ return {'title': spec.title(), 'choices' : list(spec.choices(cl))}
+filter = inclusion_tag('admin/filter')(filter)
+
+#@inclusion_tag('admin/filters')
+def filters(cl):
+ return {'cl': cl}
+filters = inclusion_tag('admin/filters')(filters)
View
258 django/contrib/admin/templatetags/admin_modify.py
@@ -0,0 +1,258 @@
+from django.core import template, template_loader, meta
+from django.utils.html import escape
+from django.utils.text import capfirst
+from django.utils.functional import curry
+from django.core.template.decorators import simple_tag, inclusion_tag
+from django.contrib.admin.views.main import AdminBoundField
+from django.core.meta.fields import BoundField, Field
+from django.core.meta import BoundRelatedObject, TABULAR, STACKED
+from django.conf.settings import ADMIN_MEDIA_PREFIX
+import re
+
+word_re = re.compile('[A-Z][a-z]+')
+
+def class_name_to_underscored(name):
+ return '_'.join([s.lower() for s in word_re.findall(name)[:-1]])
+
+#@simple_tag
+def include_admin_script(script_path):
+ return '<script type="text/javascript" src="%s%s"></script>' % (ADMIN_MEDIA_PREFIX, script_path)
+include_admin_script = simple_tag(include_admin_script)
+
+#@inclusion_tag('admin/submit_line', takes_context=True)
+def submit_row(context, bound_manipulator):
+ change = context['change']
+ add = context['add']
+ show_delete = context['show_delete']
+ has_delete_permission = context['has_delete_permission']
+ is_popup = context['is_popup']
+ return {
+ 'onclick_attrib': (bound_manipulator.ordered_objects and change
+ and 'onclick="submitOrderForm();"' or ''),
+ 'show_delete_link': (not is_popup and has_delete_permission
+ and (change or show_delete)),
+ 'show_save_as_new': not is_popup and change and bound_manipulator.save_as,
+ 'show_save_and_add_another': not is_popup and (not bound_manipulator.save_as or add),
+ 'show_save_and_continue': not is_popup,
+ 'show_save': True
+ }
+submit_row = inclusion_tag('admin/submit_line', takes_context=True)(submit_row)
+
+#@simple_tag
+def field_label(bound_field):
+ class_names = []
+ if isinstance(bound_field.field, meta.BooleanField):
+ class_names.append("vCheckboxLabel")
+ colon = ""
+ else:
+ if not bound_field.field.blank:
+ class_names.append('required')
+ if not bound_field.first:
+ class_names.append('inline')
+ colon = ":"
+ class_str = class_names and ' class="%s"' % ' '.join(class_names) or ''
+ return '<label for="%s"%s>%s%s</label> ' % (bound_field.element_id, class_str, \
+ capfirst(bound_field.field.verbose_name), colon)
+field_label = simple_tag(field_label)
+
+class FieldWidgetNode(template.Node):
+ nodelists = {}
+ default = None
+
+ def __init__(self, bound_field_var):
+ self.bound_field_var = bound_field_var
+
+ def get_nodelist(cls, klass):
+ if not cls.nodelists.has_key(klass):
+ try:
+ field_class_name = klass.__name__
+ template_name = "widget/%s" % \
+ class_name_to_underscored(field_class_name)
+ nodelist = template_loader.get_template(template_name).nodelist
+ except template.TemplateDoesNotExist:
+ super_klass = bool(klass.__bases__) and klass.__bases__[0] or None
+ if super_klass and super_klass != Field:
+ nodelist = cls.get_nodelist(super_klass)
+ else:
+ if not cls.default:
+ cls.default = template_loader.get_template("widget/default").nodelist
+ nodelist = cls.default
+
+ cls.nodelists[klass] = nodelist
+ return nodelist
+ else:
+ return cls.nodelists[klass]
+ get_nodelist = classmethod(get_nodelist)
+
+ def render(self, context):
+ bound_field = template.resolve_variable(self.bound_field_var, context)
+
+ context.push()
+ context['bound_field'] = bound_field
+
+ output = self.get_nodelist(bound_field.field.__class__).render(context)
+ context.pop()
+ return output
+
+class FieldWrapper(object):
+ def __init__(self, field ):
+ self.field = field
+
+ def needs_header(self):
+ return not isinstance(self.field, meta.AutoField)
+
+ def header_class_attribute(self):
+ return self.field.blank and ' class="optional"' or ''
+
+ def use_raw_id_admin(self):
+ return isinstance(self.field.rel, (meta.ManyToOne, meta.ManyToMany)) \
+ and self.field.rel.raw_id_admin
+
+class FormFieldCollectionWrapper(object):
+ def __init__(self, field_mapping, fields):
+ self.field_mapping = field_mapping
+ self.fields = fields
+ self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mapping['original'])
+ for field in self.fields]
+
+class TabularBoundRelatedObject(BoundRelatedObject):
+ def __init__(self, related_object, field_mapping, original):
+ super(TabularBoundRelatedObject, self).__init__(related_object, field_mapping, original)
+ self.field_wrapper_list = [FieldWrapper(field) for field in self.relation.editable_fields()]
+
+ fields = self.relation.editable_fields()
+
+ self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping, fields)
+ for field_mapping in self.field_mappings]
+ self.original_row_needed = max([fw.use_raw_id_admin() for fw in self.field_wrapper_list])
+ self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
+
+ def template_name(self):
+ return "admin/edit_inline_tabular"
+
+class StackedBoundRelatedObject(BoundRelatedObject):
+ def __init__(self, related_object, field_mapping, original):
+ super(StackedBoundRelatedObject, self).__init__(related_object, field_mapping, original)
+ fields = self.relation.editable_fields()
+ self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping ,fields)
+ for field_mapping in self.field_mappings]
+ self.show_url = original and hasattr(self.relation.opts, 'get_absolute_url')
+
+ def template_name(self):
+ return "admin/edit_inline_stacked"
+
+bound_related_object_overrides = {
+ TABULAR: TabularBoundRelatedObject,
+ STACKED: StackedBoundRelatedObject,
+}
+
+class EditInlineNode(template.Node):
+ def __init__(self, rel_var):
+ self.rel_var = rel_var
+
+ def render(self, context):
+ relation = template.resolve_variable(self.rel_var, context)
+
+ context.push()
+
+ klass = relation.field.rel.edit_inline
+ bound_related_object_class = bound_related_object_overrides.get(klass, klass)
+
+ original = context.get('original', None)
+
+ bound_related_object = relation.bind(context['form'], original, bound_related_object_class)
+ context['bound_related_object'] = bound_related_object
+
+ t = template_loader.get_template(bound_related_object.template_name())
+
+ output = t.render(context)
+
+ context.pop()
+ return output
+
+#@simple_tag
+def output_all(form_fields):
+ return ''.join([str(f) for f in form_fields])
+output_all = simple_tag(output_all)
+
+#@simple_tag
+def auto_populated_field_script(auto_pop_fields, change = False):
+ for field in auto_pop_fields:
+ t = []
+ if change:
+ t.append('document.getElementById("id_%s")._changed = true;' % field.name)
+ else:
+ t.append('document.getElementById("id_%s").onchange = function() { this._changed = true; };' % field.name)
+
+ add_values = ' + " " + '.join(['document.getElementById("id_%s").value' % g for g in field.prepopulate_from])
+ for f in field.prepopulate_from:
+ t.append('document.getElementById("id_%s").onkeyup = function() {' \
+ ' var e = document.getElementById("id_%s");' \
+ ' if(!e._changed) { e.value = URLify(%s, %s);} }; ' % (
+ f, field.name, add_values, field.maxlength))
+ return ''.join(t)
+auto_populated_field_script = simple_tag(auto_populated_field_script)
+
+#@simple_tag
+def filter_interface_script_maybe(bound_field):
+ f = bound_field.field
+ if f.rel and isinstance(f.rel, meta.ManyToMany) and f.rel.filter_interface:
+ return '<script type="text/javascript">addEvent(window, "load", function(e) {' \
+ ' SelectFilter.init("id_%s", "%s", %s, "%s"); });</script>\n' % (
+ f.name, f.verbose_name, f.rel.filter_interface-1, ADMIN_MEDIA_PREFIX)
+ else:
+ return ''
+filter_interface_script_maybe = simple_tag(filter_interface_script_maybe)
+
+def do_one_arg_tag(node_factory, parser,token):
+ tokens = token.contents.split()
+ if len(tokens) != 2:
+ raise template.TemplateSyntaxError("%s takes 1 argument" % tokens[0])
+ return node_factory(tokens[1])
+
+def register_one_arg_tag(node):
+ tag_name = class_name_to_underscored(node.__name__)
+ parse_func = curry(do_one_arg_tag, node)
+ template.register_tag(tag_name, parse_func)
+
+one_arg_tag_nodes = (
+ FieldWidgetNode,
+ EditInlineNode,
+)
+
+for node in one_arg_tag_nodes:
+ register_one_arg_tag(node)
+
+#@inclusion_tag('admin/field_line', takes_context=True)
+def admin_field_line(context, argument_val):
+ if (isinstance(argument_val, BoundField)):
+ bound_fields = [argument_val]
+ else:
+ bound_fields = [bf for bf in argument_val]
+ add = context['add']
+ change = context['change']
+
+ class_names = ['form-row']
+ for bound_field in bound_fields:
+ for f in bound_field.form_fields:
+ if f.errors():
+ class_names.append('errors')
+ break
+
+ # Assumes BooleanFields won't be stacked next to each other!
+ if isinstance(bound_fields[0].field, meta.BooleanField):
+ class_names.append('checkbox-row')
+
+ return {
+ 'add': context['add'],
+ 'change': context['change'],
+ 'bound_fields': bound_fields,
+ 'class_names': " ".join(class_names),
+ }
+admin_field_line = inclusion_tag('admin/field_line', takes_context=True)(admin_field_line)
+
+#@simple_tag
+def object_pk(bound_manip, ordered_obj):
+ return bound_manip.get_ordered_object_pk(ordered_obj)
+
+object_pk = simple_tag(object_pk)
View
1,307 django/contrib/admin/views/main.py
431 additions, 876 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
154 django/core/formfields.py
@@ -90,15 +90,7 @@ def do_html2python(self, new_data):
expected to deal with invalid input.
"""
for field in self.fields:
- if new_data.has_key(field.field_name):
- new_data.setlist(field.field_name,
- [field.__class__.html2python(data) for data in new_data.getlist(field.field_name)])
- else:
- try:
- # individual fields deal with None values themselves
- new_data.setlist(field.field_name, [field.__class__.html2python(None)])
- except EmptyValue:
- new_data.setlist(field.field_name, [])
+ field.convert_post_data(new_data)
class FormWrapper:
"""
@@ -106,24 +98,36 @@ class FormWrapper:
This allows dictionary-style lookups of formfields. It also handles feeding
prepopulated data and validation error messages to the formfield objects.
"""
- def __init__(self, manipulator, data, error_dict):
+ def __init__(self, manipulator, data, error_dict, edit_inline=True):
self.manipulator, self.data = manipulator, data
self.error_dict = error_dict
+ self._inline_collections = None
+ self.edit_inline = edit_inline
def __repr__(self):
- return repr(self.data)
+ return repr(self.__dict__)
def __getitem__(self, key):
for field in self.manipulator.fields:
if field.field_name == key:
- if hasattr(field, 'requires_data_list') and hasattr(self.data, 'getlist'):
- data = self.data.getlist(field.field_name)
- else:
- data = self.data.get(field.field_name, None)
- if data is None:
- data = ''
+ data = field.extract_data(self.data)
return FormFieldWrapper(field, data, self.error_dict.get(field.field_name, []))
- raise KeyError
+ if self.edit_inline:
+ self.fill_inline_collections()
+ for inline_collection in self._inline_collections:
+ if inline_collection.name == key:
+ return inline_collection
+ raise KeyError, "Could not find Formfield or InlineObjectCollection named %r" % key
+
+ def fill_inline_collections(self):
+ if not self._inline_collections:
+ ic = []
+ related_objects = self.manipulator.get_related_objects()
+ for rel_obj in related_objects:
+ data = rel_obj.extract_data(self.data)
+ inline_collection = InlineObjectCollection(self.manipulator, rel_obj, data, self.error_dict)
+ ic.append(inline_collection)
+ self._inline_collections = ic
def has_errors(self):
return self.error_dict != {}
@@ -166,6 +170,9 @@ def html_error_list(self):
else:
return ''
+ def get_id(self):
+ return self.formfield.get_id()
+
class FormFieldCollection(FormFieldWrapper):
"A utility class that gives the template access to a dict of FormFieldWrappers"
def __init__(self, formfield_dict):
@@ -185,9 +192,66 @@ def errors(self):
"Returns list of all errors in this collection's formfields"
errors = []
for field in self.formfield_dict.values():
- errors.extend(field.errors())
+ if hasattr(field, 'errors'):
+ errors.extend(field.errors())
return errors
+ def has_errors(self):
+ return bool(len(self.errors()))
+
+ def html_combined_error_list(self):
+ return ''.join([field.html_error_list() for field in self.formfield_dict.values() if hasattr(field, 'errors')])
+
+class InlineObjectCollection:
+ "An object that acts like a list of form field collections."
+ def __init__(self, parent_manipulator, rel_obj, data, errors):
+ self.parent_manipulator = parent_manipulator
+ self.rel_obj = rel_obj
+ self.data = data
+ self.errors = errors
+ self._collections = None
+ self.name = rel_obj.name
+
+ def __len__(self):
+ self.fill()
+ return self._collections.__len__()
+
+ def __getitem__(self, k):
+ self.fill()
+ return self._collections.__getitem__(k)
+
+ def __setitem__(self, k, v):
+ self.fill()
+ return self._collections.__setitem__(k,v)
+
+ def __delitem__(self, k):
+ self.fill()
+ return self._collections.__delitem__(k)
+
+ def __iter__(self):
+ self.fill()
+ return self._collections.__iter__()
+
+ def fill(self):
+ if self._collections:
+ return
+ else:
+ var_name = self.rel_obj.opts.object_name.lower()
+ wrapper = []
+ orig = hasattr(self.parent_manipulator, 'original_object') and self.parent_manipulator.original_object or None
+ orig_list = self.rel_obj.get_list(orig)
+ for i, instance in enumerate(orig_list):
+ collection = {'original': instance}
+ for f in self.rel_obj.editable_fields():
+ for field_name in f.get_manipulator_field_names(''):
+ full_field_name = '%s.%d.%s' % (var_name, i, field_name)
+ field = self.parent_manipulator[full_field_name]
+ data = field.extract_data(self.data)
+ errors = self.errors.get(full_field_name, [])
+ collection[field_name] = FormFieldWrapper(field, data, errors)
+ wrapper.append(FormFieldCollection(collection))
+ self._collections = wrapper
+
class FormField:
"""Abstract class representing a form field.
@@ -220,6 +284,37 @@ def html2python(data):
def render(self, data):
raise NotImplementedError
+ def get_member_name(self):
+ if hasattr(self, 'member_name'):
+ return self.member_name
+ else:
+ return self.field_name
+
+ def extract_data(self, data_dict):
+ if hasattr(self, 'requires_data_list') and hasattr(data_dict, 'getlist'):
+ data = data_dict.getlist(self.get_member_name())
+ else:
+ data = data_dict.get(self.get_member_name(), None)
+ if data is None:
+ data = ''
+ return data
+
+ def convert_post_data(self, new_data):
+ name = self.get_member_name()
+ if new_data.has_key(self.field_name):
+ d = new_data.getlist(self.field_name)
+ try:
+ converted_data = [self.__class__.html2python(data) for data in d]
+ except ValueError:
+ converted_data = d
+ new_data.setlist(name, converted_data)
+ else:
+ try:
+ # individual fields deal with None values themselves
+ new_data.setlist(name, [self.__class__.html2python(None)])
+ except EmptyValue:
+ new_data.setlist(name, [])
+
def get_id(self):
"Returns the HTML 'id' attribute for this form field."
return FORM_FIELD_ID_PREFIX + self.field_name
@@ -313,11 +408,13 @@ def html2python(data):
html2python = staticmethod(html2python)
class SelectField(FormField):
- def __init__(self, field_name, choices=[], size=1, is_required=False, validator_list=[]):
+ def __init__(self, field_name, choices=[], size=1, is_required=False, validator_list=[], member_name=None):
self.field_name = field_name
# choices is a list of (value, human-readable key) tuples because order matters
self.choices, self.size, self.is_required = choices, size, is_required
self.validator_list = [self.isValidChoice] + validator_list
+ if member_name != None:
+ self.member_name = member_name
def render(self, data):
output = ['<select id="%s" class="v%s%s" name="%s" size="%s">' % \
@@ -347,12 +444,14 @@ def html2python(data):
html2python = staticmethod(html2python)
class RadioSelectField(FormField):
- def __init__(self, field_name, choices=[], ul_class='', is_required=False, validator_list=[]):
+ def __init__(self, field_name, choices=[], ul_class='', is_required=False, validator_list=[], member_name=None):
self.field_name = field_name
# choices is a list of (value, human-readable key) tuples because order matters
self.choices, self.is_required = choices, is_required
self.validator_list = [self.isValidChoice] + validator_list
self.ul_class = ul_class
+ if member_name != None:
+ self.member_name = member_name
def render(self, data):
"""
@@ -483,8 +582,8 @@ def render(self, data):
checked_html = ' checked="checked"'
field_name = '%s%s' % (self.field_name, value)
output.append('<li><input type="checkbox" id="%s" class="v%s" name="%s"%s /> <label for="%s">%s</label></li>' % \
- (self.get_id(), self.__class__.__name__, field_name, checked_html,
- self.get_id(), choice))
+ (self.get_id() + value , self.__class__.__name__, field_name, checked_html,
+ self.get_id() + value, choice))
output.append('</ul>')
return '\n'.join(output)
@@ -528,8 +627,10 @@ def isValidImage(self, field_data, all_data):
####################
class IntegerField(TextField):
- def __init__(self, field_name, length=10, maxlength=None, is_required=False, validator_list=[]):
+ def __init__(self, field_name, length=10, maxlength=None, is_required=False, validator_list=[], member_name=None):
validator_list = [self.isInteger] + validator_list
+ if member_name is not None:
+ self.member_name = member_name
TextField.__init__(self, field_name, length, maxlength, is_required, validator_list)
def isInteger(self, field_data, all_data):
@@ -784,6 +885,11 @@ def isCommaSeparatedIntegerList(self, field_data, all_data):
except validators.ValidationError, e:
raise validators.CriticalValidationError, e.messages
+class RawIdAdminField(CommaSeparatedIntegerField):
+ def html2python(data):
+ return data.split(',');
+ html2python = classmethod(html2python)
+
class XMLLargeTextField(LargeTextField):
"""
A LargeTextField with an XML validator. The schema_path argument is the
View
6 django/core/management.py
@@ -670,12 +670,12 @@ def get_validation_errors(outfile):
e.add(opts, '"ordering" refers to "%s", a field that doesn\'t exist.' % field_name)
# Check core=True, if needed.
- for rel_opts, rel_field in opts.get_inline_related_objects():
+ for related in opts.get_followed_related_objects():
try:
- for f in rel_opts.fields:
+ for f in related.opts.fields:
if f.core:
raise StopIteration
- e.add(rel_opts, "At least one field in %s should have core=True, because it's being edited inline by %s.%s." % (rel_opts.object_name, opts.module_name, opts.object_name))
+ e.add(related.opts, "At least one field in %s should have core=True, because it's being edited inline by %s.%s." % (related.opts.object_name, opts.module_name, opts.object_name))
except StopIteration:
pass
View
452 django/core/meta/__init__.py
@@ -148,6 +148,140 @@ class FieldDoesNotExist(Exception):
class BadKeywordArguments(Exception):
pass
+class BoundRelatedObject(object):
+ def __init__(self, related_object, field_mapping, original):
+ self.relation = related_object
+ self.field_mappings = field_mapping[related_object.opts.module_name]
+
+ def template_name(self):
+ raise NotImplementedError
+
+ def __repr__(self):
+ return repr(self.__dict__)
+
+class RelatedObject(object):
+ def __init__(self, parent_opts, opts, field):
+ self.parent_opts = parent_opts
+ self.opts = opts
+ self.field = field
+ self.edit_inline = field.rel.edit_inline
+ self.name = opts.module_name
+ self.var_name = opts.object_name.lower()
+
+ def flatten_data(self, follow, obj=None):
+ new_data = {}
+ rel_instances = self.get_list(obj)
+ for i, rel_instance in enumerate(rel_instances):
+ instance_data = {}
+ for f in self.opts.fields + self.opts.many_to_many:
+ # TODO: Fix for recursive manipulators.
+ fol = follow.get(f.name, None)
+ if fol:
+ field_data = f.flatten_data(fol, rel_instance)
+ for name, value in field_data.items():
+ instance_data['%s.%d.%s' % (self.var_name, i, name)] = value
+ new_data.update(instance_data)
+ return new_data
+
+ def extract_data(self, data):
+ """
+ Pull out the data meant for inline objects of this class,
+ i.e. anything starting with our module name.
+ """
+ return data # TODO
+
+ def get_list(self, parent_instance=None):
+ "Get the list of this type of object from an instance of the parent class."
+ if parent_instance != None:
+ func_name = 'get_%s_list' % self.get_method_name_part()
+ func = getattr(parent_instance, func_name)
+ list = func()
+
+ count = len(list) + self.field.rel.num_extra_on_change
+ if self.field.rel.min_num_in_admin:
+ count = max(count, self.field.rel.min_num_in_admin)
+ if self.field.rel.max_num_in_admin:
+ count = min(count, self.field.rel.max_num_in_admin)
+
+ change = count - len(list)
+ if change > 0:
+ return list + [None for _ in range(change)]
+ if change < 0:
+ return list[:change]
+ else: # Just right
+ return list
+ else:
+ return [None for _ in range(self.field.rel.num_in_admin)]
+
+
+ def editable_fields(self):
+ "Get the fields in this class that should be edited inline."
+ return [f for f in self.opts.fields + self.opts.many_to_many if f.editable and f != self.field]
+
+ def get_follow(self, override=None):
+ if isinstance(override, bool):
+ if override:
+ over = {}
+ else:
+ return None
+ else:
+ if override:
+ over = override.copy()
+ elif self.edit_inline:
+ over = {}
+ else:
+ return None
+
+ over[self.field.name] = False
+ return self.opts.get_follow(over)
+
+ def __repr__(self):
+ return "<RelatedObject: %s related to %s>" % ( self.name, self.field.name)
+
+ def get_manipulator_fields(self, opts, manipulator, change, follow):
+ # TODO: Remove core fields stuff.
+ if change:
+ meth_name = 'get_%s_count' % self.get_method_name_part()
+ count = getattr(manipulator.original_object, meth_name)()
+ count += self.field.rel.num_extra_on_change
+ if self.field.rel.min_num_in_admin:
+ count = max(count, self.field.rel.min_num_in_admin)
+ if self.field.rel.max_num_in_admin:
+ count = min(count, self.field.rel.max_num_in_admin)
+ else:
+ count = self.field.rel.num_in_admin
+
+ fields = []
+ for i in range(count):
+ for f in self.opts.fields + self.opts.many_to_many:
+ if follow.get(f.name, False):
+ prefix = '%s.%d.' % (self.var_name, i)
+ fields.extend(f.get_manipulator_fields(self.opts, manipulator, change, name_prefix=prefix, rel=True))
+ return fields
+
+ def bind(self, field_mapping, original, bound_related_object_class=BoundRelatedObject):
+ return bound_related_object_class(self, field_mapping, original)
+
+ def get_method_name_part(self):
+ # This method encapsulates the logic that decides what name to give a
+ # method that retrieves related many-to-one objects. Usually it just
+ # uses the lower-cased object_name, but if the related object is in
+ # another app, its app_label is appended.
+ #
+ # Examples:
+ #
+ # # Normal case -- a related object in the same app.
+ # # This method returns "choice".
+ # Poll.get_choice_list()
+ #
+ # # A related object in a different app.
+ # # This method returns "lcom_bestofaward".
+ # Place.get_lcom_bestofaward_list() # "lcom_bestofaward"
+ rel_obj_name = self.field.rel.related_name or self.opts.object_name.lower()
+ if self.parent_opts.app_label != self.opts.app_label:
+ rel_obj_name = '%s_%s' % (self.opts.app_label, rel_obj_name)
+ return rel_obj_name
+
class Options:
def __init__(self, module_name='', verbose_name='', verbose_name_plural='', db_table='',
fields=None, ordering=None, unique_together=None, admin=None, has_related_links=False,
@@ -268,26 +402,6 @@ def get_change_permission(self):
def get_delete_permission(self):
return 'delete_%s' % self.object_name.lower()
- def get_rel_object_method_name(self, rel_opts, rel_field):
- # This method encapsulates the logic that decides what name to give a
- # method that retrieves related many-to-one objects. Usually it just
- # uses the lower-cased object_name, but if the related object is in
- # another app, its app_label is appended.
- #
- # Examples:
- #
- # # Normal case -- a related object in the same app.
- # # This method returns "choice".
- # Poll.get_choice_list()
- #
- # # A related object in a different app.
- # # This method returns "lcom_bestofaward".
- # Place.get_lcom_bestofaward_list() # "lcom_bestofaward"
- rel_obj_name = rel_field.rel.related_name or rel_opts.object_name.lower()
- if self.app_label != rel_opts.app_label:
- rel_obj_name = '%s_%s' % (rel_opts.app_label, rel_obj_name)
- return rel_obj_name
-
def get_all_related_objects(self):
try: # Try the cache first.
return self._all_related_objects
@@ -298,7 +412,7 @@ def get_all_related_objects(self):
for klass in mod._MODELS:
for f in klass._meta.fields:
if f.rel and self == f.rel.to:
- rel_objs.append((klass._meta, f))
+ rel_objs.append(RelatedObject(self, klass._meta, f))
if self.has_related_links:
# Manually add RelatedLink objects, which are a special case.
relatedlinks = get_module('relatedlinks', 'relatedlinks')
@@ -312,12 +426,31 @@ def get_all_related_objects(self):
'content_type__package__label__exact': self.app_label,
'content_type__python_module_name__exact': self.module_name,
})
- rel_objs.append((relatedlinks.RelatedLink._meta, link_field))
+ rel_objs.append(RelatedObject(self, relatedlinks.RelatedLink._meta, link_field))
self._all_related_objects = rel_objs
return rel_objs
- def get_inline_related_objects(self):
- return [(a, b) for a, b in self.get_all_related_objects() if b.rel.edit_inline]
+ def get_followed_related_objects(self, follow=None):
+ if follow == None:
+ follow = self.get_follow()
+ return [f for f in self.get_all_related_objects() if follow.get(f.name, None)]
+
+ def get_data_holders(self, follow=None):
+ if follow == None:
+ follow = self.get_follow()
+ return [f for f in self.fields + self.many_to_many + self.get_all_related_objects() if follow.get(f.name, None)]
+
+ def get_follow(self, override=None):
+ follow = {}
+ for f in self.fields + self.many_to_many + self.get_all_related_objects():
+ if override and override.has_key(f.name):
+ child_override = override[f.name]
+ else:
+ child_override = None
+ fol = f.get_follow(child_override)
+ if fol:
+ follow[f.name] = fol
+ return follow
def get_all_related_many_to_many_objects(self):
module_list = get_installed_model_modules()
@@ -327,7 +460,7 @@ def get_all_related_many_to_many_objects(self):
try:
for f in klass._meta.many_to_many:
if f.rel and self == f.rel.to:
- rel_objs.append((klass._meta, f))
+ rel_objs.append(RelatedObject(self, klass._meta, f))
raise StopIteration
except StopIteration:
continue
@@ -345,11 +478,12 @@ def get_ordered_objects(self):
self._ordered_objects = objects
return self._ordered_objects
- def has_field_type(self, field_type):
+ def has_field_type(self, field_type, follow=None):
"""
Returns True if this object's admin form has at least one of the given
field_type (e.g. FileField).
"""
+ # TODO: follow
if not hasattr(self, '_field_types'):
self._field_types = {}
if not self._field_types.has_key(field_type):
@@ -359,8 +493,8 @@ def has_field_type(self, field_type):
if isinstance(f, field_type):
raise StopIteration
# Failing that, check related fields.
- for rel_obj, rel_field in self.get_inline_related_objects():
- for f in rel_obj.fields:
+ for related in self.get_followed_related_objects(follow):
+ for f in related.opts.fields:
if isinstance(f, field_type):
raise StopIteration
except StopIteration:
@@ -597,6 +731,7 @@ def __new__(cls, name, bases, attrs):
new_mod.get_latest = curry(function_get_latest, opts, new_class, does_not_exist_exception)
for f in opts.fields:
+ #TODO : change this into a virtual function so that user defined fields will be able to add methods to module or class.
if f.choices:
# Add "get_thingie_display" method to get human-readable value.
func = curry(method_get_display_value, f)
@@ -720,12 +855,9 @@ def __new__(cls, name, bases, attrs):
old_app._MODELS[i] = new_class
# Replace all relationships to the old class with
# relationships to the new one.
- for rel_opts, rel_field in model._meta.get_all_related_objects():
- rel_field.rel.to = opts
- for rel_opts, rel_field in model._meta.get_all_related_many_to_many_objects():
- rel_field.rel.to = opts
+ for related in model._meta.get_all_related_objects() + model._meta.get_all_related_many_to_many_objects():
+ related.field.rel.to = opts
break
-
return new_class
class Model:
@@ -826,9 +958,9 @@ def method_delete(opts, self):
if hasattr(self, '_pre_delete'):
self._pre_delete()
cursor = db.db.cursor()
- for rel_opts, rel_field in opts.get_all_related_objects():
- rel_opts_name = opts.get_rel_object_method_name(rel_opts, rel_field)
- if isinstance(rel_field.rel, OneToOne):
+ for related in opts.get_all_related_objects():
+ rel_opts_name = related.get_method_name_part()
+ if isinstance(related.field.rel, OneToOne):
try:
sub_obj = getattr(self, 'get_%s' % rel_opts_name)()
except ObjectDoesNotExist:
@@ -838,9 +970,9 @@ def method_delete(opts, self):
else:
for sub_obj in getattr(self, 'get_%s_list' % rel_opts_name)():
sub_obj.delete()
- for rel_opts, rel_field in opts.get_all_related_many_to_many_objects():
+ for related in opts.get_all_related_many_to_many_objects():
cursor.execute("DELETE FROM %s WHERE %s=%%s" % \
- (db.db.quote_name(rel_field.get_m2m_db_table(rel_opts)),
+ (db.db.quote_name(related.field.get_m2m_db_table(related.opts)),
db.db.quote_name(self._meta.object_name.lower() + '_id')), [getattr(self, opts.pk.attname)])
for f in opts.many_to_many:
cursor.execute("DELETE FROM %s WHERE %s=%%s" % \
@@ -1474,6 +1606,8 @@ def get_manipulator(opts, klass, extra_methods, add=False, change=False):
man.__module__ = MODEL_PREFIX + '.' + opts.module_name # Set this explicitly, as above.
man.__init__ = curry(manipulator_init, opts, add, change)
man.save = curry(manipulator_save, opts, klass, add, change)
+ man.get_related_objects = curry(manipulator_get_related_objects, opts, klass, add, change)
+ man.flatten_data = curry(manipulator_flatten_data, opts, klass, add, change)
for field_name_list in opts.unique_together:
setattr(man, 'isUnique%s' % '_'.join(field_name_list), curry(manipulator_validator_unique_together, field_name_list, opts))
for f in opts.fields:
@@ -1487,7 +1621,9 @@ def get_manipulator(opts, klass, extra_methods, add=False, change=False):
setattr(man, k, v)
return man
-def manipulator_init(opts, add, change, self, obj_key=None):
+def manipulator_init(opts, add, change, self, obj_key=None, follow=None):
+ self.follow = opts.get_follow(follow)
+
if change:
assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter."
self.obj_key = obj_key
@@ -1511,40 +1647,37 @@ def manipulator_init(opts, add, change, self, obj_key=None):
else:
raise
self.fields = []
+
for f in opts.fields + opts.many_to_many:
- if f.editable and not (f.primary_key and change) and (not f.rel or not f.rel.edit_inline):
+ if self.follow.get(f.name, False):
self.fields.extend(f.get_manipulator_fields(opts, self, change))
# Add fields for related objects.
- for rel_opts, rel_field in opts.get_inline_related_objects():
- if change:
- count = getattr(self.original_object, 'get_%s_count' % opts.get_rel_object_method_name(rel_opts, rel_field))()
- count += rel_field.rel.num_extra_on_change
- if rel_field.rel.min_num_in_admin:
- count = max(count, rel_field.rel.min_num_in_admin)
- if rel_field.rel.max_num_in_admin:
- count = min(count, rel_field.rel.max_num_in_admin)
- else:
- count = rel_field.rel.num_in_admin
- for f in rel_opts.fields + rel_opts.many_to_many:
- if f.editable and f != rel_field and (not f.primary_key or (f.primary_key and change)):
- for i in range(count):
- self.fields.extend(f.get_manipulator_fields(rel_opts, self, change, name_prefix='%s.%d.' % (rel_opts.object_name.lower(), i), rel=True))
+ for f in opts.get_all_related_objects():
+ if self.follow.get(f.name, False):
+ fol = self.follow[f.name]
+ self.fields.extend(f.get_manipulator_fields(opts, self, change, fol))
# Add field for ordering.
if change and opts.get_ordered_objects():
self.fields.append(formfields.CommaSeparatedIntegerField(field_name="order_"))
def manipulator_save(opts, klass, add, change, self, new_data):
+ # TODO: big cleanup when core fields go -> use recursive manipulators.
from django.utils.datastructures import DotExpandedDict
params = {}
for f in opts.fields:
- # Fields with auto_now_add are another special case; they should keep
- # their original value in the change stage.
- if change and getattr(f, 'auto_now_add', False):
- params[f.attname] = getattr(self.original_object, f.attname)
+ # Fields with auto_now_add should keep their original value in the change stage.
+ auto_now_add = change and getattr(f, 'auto_now_add', False)
+ if self.follow.get(f.name, None) and not auto_now_add:
+ param = f.get_manipulator_new_data(new_data)
else:
- params[f.attname] = f.get_manipulator_new_data(new_data)
+ if change:
+ param = getattr(self.original_object, f.attname)
+ else:
+ param = f.get_default()
+ params[f.attname] = param
+
if change:
params[opts.pk.attname] = self.obj_key
@@ -1567,101 +1700,116 @@ def manipulator_save(opts, klass, add, change, self, new_data):
# Save many-to-many objects. Example: Poll.set_sites()
for f in opts.many_to_many:
- if not f.rel.edit_inline:
- was_changed = getattr(new_object, 'set_%s' % f.name)(new_data.getlist(f.name))
- if change and was_changed:
- self.fields_changed.append(f.verbose_name)
+ if self.follow.get(f.name, None):
+ if not f.rel.edit_inline:
+ was_changed = getattr(new_object, 'set_%s' % f.name)(new_data.getlist(f.name))
+ if change and was_changed:
+ self.fields_changed.append(f.verbose_name)
+ expanded_data = DotExpandedDict(new_data.data)
# Save many-to-one objects. Example: Add the Choice objects for a Poll.
- for rel_opts, rel_field in opts.get_inline_related_objects():
+ for related in opts.get_all_related_objects():
# Create obj_list, which is a DotExpandedDict such as this:
# [('0', {'id': ['940'], 'choice': ['This is the first choice']}),
# ('1', {'id': ['941'], 'choice': ['This is the second choice']}),
# ('2', {'id': [''], 'choice': ['']})]
- obj_list = DotExpandedDict(new_data.data)[rel_opts.object_name.lower()].items()
- obj_list.sort(lambda x, y: cmp(int(x[0]), int(y[0])))
- params = {}
-
- # For each related item...
- for _, rel_new_data in obj_list:
-
- # Keep track of which core=True fields were provided.
- # If all core fields were given, the related object will be saved.
- # If none of the core fields were given, the object will be deleted.
- # If some, but not all, of the fields were given, the validator would
- # have caught that.
- all_cores_given, all_cores_blank = True, True
-
- # Get a reference to the old object. We'll use it to compare the
- # old to the new, to see which fields have changed.
- if change:
+ child_follow = self.follow.get(related.name, None)
+
+ if child_follow:
+ obj_list = expanded_data[related.var_name].items()
+ obj_list.sort(lambda x, y: cmp(int(x[0]), int(y[0])))
+ params = {}
+
+
+ # For each related item...
+ for _, rel_new_data in obj_list:
+
+ # Keep track of which core=True fields were provided.
+ # If all core fields were given, the related object will be saved.
+ # If none of the core fields were given, the object will be deleted.
+ # If some, but not all, of the fields were given, the validator would
+ # have caught that.
+ all_cores_given, all_cores_blank = True, True
+
+ # Get a reference to the old object. We'll use it to compare the
+ # old to the new, to see which fields have changed.
old_rel_obj = None
- if rel_new_data[rel_opts.pk.name][0]:
- try:
- old_rel_obj = getattr(self.original_object, 'get_%s' % opts.get_rel_object_method_name(rel_opts, rel_field))(**{'%s__exact' % rel_opts.pk.name: rel_new_data[rel_opts.pk.attname][0]})
- except ObjectDoesNotExist:
- pass
-
- for f in rel_opts.fields:
- if f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True) in (None, ''):
- all_cores_given = False
- elif f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True) not in (None, ''):
- all_cores_blank = False
- # If this field isn't editable, give it the same value it had
- # previously, according to the given ID. If the ID wasn't
- # given, use a default value. FileFields are also a special
- # case, because they'll be dealt with later.
- if change and (isinstance(f, FileField) or not f.editable):
- if rel_new_data.get(rel_opts.pk.attname, False) and rel_new_data[rel_opts.pk.attname][0]:
- params[f.attname] = getattr(old_rel_obj, f.attname)
- else:
- params[f.attname] = f.get_default()
- elif f == rel_field:
- params[f.attname] = getattr(new_object, rel_field.rel.field_name)
- elif add and isinstance(f, AutoField):
- params[f.attname] = None
- else:
- params[f.attname] = f.get_manipulator_new_data(rel_new_data, rel=True)
- # Related links are a special case, because we have to
- # manually set the "content_type_id" and "object_id" fields.
- if opts.has_related_links and rel_opts.module_name == 'relatedlinks':
- contenttypes_mod = get_module('core', 'contenttypes')
- params['content_type_id'] = contenttypes_mod.get_object(package__label__exact=opts.app_label, python_module_name__exact=opts.module_name).id
- params['object_id'] = new_object.id
-
- # Create the related item.
- new_rel_obj = rel_opts.get_model_module().Klass(**params)
-
- # If all the core fields were provided (non-empty), save the item.
- if all_cores_given:
- new_rel_obj.save()
-
- # Save any uploaded files.
- for f in rel_opts.fields:
- if isinstance(f, FileField) and rel_new_data.get(f.attname, False):
- f.save_file(rel_new_data, new_rel_obj, change and old_rel_obj or None, old_rel_obj is not None, rel=True)
-
- # Calculate whether any fields have changed.
if change:
- if not old_rel_obj: # This object didn't exist before.
- self.fields_added.append('%s "%r"' % (rel_opts.verbose_name, new_rel_obj))
+ if rel_new_data[related.opts.pk.name][0]:
+ try:
+ old_rel_obj = getattr(self.original_object, 'get_%s' % related.get_method_name_part() )(**{'%s__exact' % related.opts.pk.name: rel_new_data[related.opts.pk.attname][0]})
+ except ObjectDoesNotExist:
+ pass
+
+ for f in related.opts.fields:
+ if f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True) in (None, ''):
+ all_cores_given = False
+ elif f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True) not in (None, ''):
+ all_cores_blank = False
+ # If this field isn't editable, give it the same value it had
+ # previously, according to the given ID. If the ID wasn't
+ # given, use a default value. FileFields are also a special
+ # case, because they'll be dealt with later.
+
+ if f == related.field:
+ param = getattr(new_object, related.field.rel.field_name)
+ elif add and isinstance(f, AutoField):
+ param = None
+ elif change and (isinstance(f, FileField) or not child_follow.get(f.name, None)):
+ if old_rel_obj:
+ param = getattr(old_rel_obj, f.column)
+ else:
+ param = f.get_default()
else:
- for f in rel_opts.fields:
- if not f.primary_key and f != rel_field and str(getattr(old_rel_obj, f.attname)) != str(getattr(new_rel_obj, f.attname)):
- self.fields_changed.append('%s for %s "%r"' % (f.verbose_name, rel_opts.verbose_name, new_rel_obj))
-
- # Save many-to-many objects.
- for f in rel_opts.many_to_many:
- if not f.rel.edit_inline:
- was_changed = getattr(new_rel_obj, 'set_%s' % f.name)(rel_new_data[f.attname])
- if change and was_changed:
- self.fields_changed.append('%s for %s "%s"' % (f.verbose_name, rel_opts.verbose_name, new_rel_obj))
-
- # If, in the change stage, all of the core fields were blank and
- # the primary key (ID) was provided, delete the item.
- if change and all_cores_blank and rel_new_data.has_key(rel_opts.pk.attname) and rel_new_data[rel_opts.pk.attname][0]:
- new_rel_obj.delete()
- self.fields_deleted.append('%s "%r"' % (rel_opts.verbose_name, old_rel_obj))
+ param = f.get_manipulator_new_data(rel_new_data, rel=True)
+ if param != None:
+ params[f.attname] = param
+
+
+ # Related links are a special case, because we have to
+ # manually set the "content_type_id" and "object_id" fields.
+ if opts.has_related_links and related.opts.module_name == 'relatedlinks':
+ contenttypes_mod = get_module('core', 'contenttypes')
+ params['content_type_id'] = contenttypes_mod.get_object(package__label__exact=opts.app_label, python_module_name__exact=opts.module_name).id
+ params['object_id'] = new_object.id
+
+ # Create the related item.
+ new_rel_obj = related.opts.get_model_module().Klass(**params)
+
+
+
+ # If all the core fields were provided (non-empty), save the item.
+ if all_cores_given:
+ new_rel_obj.save()
+
+ # Save any uploaded files.
+ for f in related.opts.fields:
+ if child_follow.get(f.name, None):
+ if isinstance(f, FileField) and rel_new_data.get(f.name, False):
+ f.save_file(rel_new_data, new_rel_obj, change and old_rel_obj or None, old_rel_obj is not None, rel=True)
+
+ # Calculate whether any fields have changed.
+ if change:
+ if not old_rel_obj: # This object didn't exist before.
+ self.fields_added.append('%s "%s"' % (related.opts.verbose_name, new_rel_obj))
+ else:
+ for f in related.opts.fields:
+ if not f.primary_key and f != related.field and str(getattr(old_rel_obj, f.attname)) != str(getattr(new_rel_obj, f.attname)):
+ self.fields_changed.append('%s for %s "%s"' % (f.verbose_name, related.opts.verbose_name, new_rel_obj))
+
+ # Save many-to-many objects.
+ for f in related.opts.many_to_many:
+ if child_follow.get(f.name, None) and not f.rel.edit_inline:
+ was_changed = getattr(new_rel_obj, 'set_%s' % f.name)(rel_new_data[f.attname])
+ if change and was_changed:
+ self.fields_changed.append('%s for %s "%s"' % (f.verbose_name, related.opts.verbose_name, new_rel_obj))
+
+ # If, in the change stage, all of the core fields were blank and
+ # the primary key (ID) was provided, delete the item.
+ if change and all_cores_blank and old_rel_obj:
+ new_rel_obj.delete()
+ self.fields_deleted.append('%s "%s"' % (related.opts.verbose_name, old_rel_obj))
+
# Save the order, if applicable.
if change and opts.get_ordered_objects():
@@ -1670,6 +1818,17 @@ def manipulator_save(opts, klass, add, change, self, new_data):
getattr(new_object, 'set_%s_order' % rel_opts.object_name.lower())(order)
return new_object
+def manipulator_get_related_objects(opts, klass, add, change, self):
+ return opts.get_followed_related_objects(self.follow)
+
+def manipulator_flatten_data(opts, klass, add, change, self):
+ new_data = {}
+ obj = change and self.original_object or None
+ for f in opts.get_data_holders(self.follow):
+ fol = self.follow.get(f.name)
+ new_data.update(f.flatten_data(fol, obj))
+ return new_data
+
def manipulator_validator_unique_together(field_name_list, opts, self, field_data, all_data):
from django.utils.text import get_text_list
field_list = [opts.get_field(field_name) for field_name in field_name_list]
@@ -1678,6 +1837,9 @@ def manipulator_validator_unique_together(field_name_list, opts, self, field_dat
else:
kwargs = {'%s__iexact' % field_name_list[0]: field_data}
for f in field_list[1:]:
+ # This is really not going to work for fields that have different
+ # form fields, e.g. DateTime.
+ # This validation needs to occur after html2python to be effective.
field_val = all_data.get(f.attname, None)
if field_val is None:
# This will be caught by another validator, assuming the field
View
213 django/core/meta/fields.py
@@ -59,6 +59,24 @@ def manipulator_validator_unique(f, opts, self, field_data, all_data):
return
raise validators.ValidationError, _("%(optname)s with this %(fieldname)s already exists.") % {'optname': capfirst(opts.verbose_name), 'fieldname': f.verbose_name}
+class BoundField(object):
+ def __init__(self, field, field_mapping, original):
+ self.field = field
+ self.original = original
+ self.form_fields = self.resolve_form_fields(field_mapping)
+
+ def resolve_form_fields(self, field_mapping):
+ return [field_mapping[name] for name in self.field.get_manipulator_field_names('')]
+
+ def as_field_list(self):
+ return [self.field]
+
+ def original_value(self):
+ if self.original:
+ return self.original.__dict__[self.field.column]
+
+ def __repr__(self):
+ return "BoundField:(%s, %s)" % (self.field.name, self.form_fields)
# A guide to Field parameters:
#
@@ -185,7 +203,7 @@ def get_default(self):
if hasattr(self.default, '__get_value__'):
return self.default.__get_value__()
return self.default
- if self.null:
+ if not self.empty_strings_allowed or self.null:
return None
return ""
@@ -207,28 +225,28 @@ def get_manipulator_fields(self, opts, manipulator, change, name_prefix='', rel=
if self.maxlength and not self.choices: # Don't give SelectFields a maxlength parameter.
params['maxlength'] = self.maxlength
if isinstance(self.rel, ManyToOne):
+ params['member_name'] = name_prefix + self.attname
if self.rel.raw_id_admin:
field_objs = self.get_manipulator_field_objs()
params['validator_list'].append(curry(manipulator_valid_rel_key, self, manipulator))
else:
if self.radio_admin:
field_objs = [formfields.RadioSelectField]
- params['choices'] = self.get_choices(include_blank=self.blank, blank_choice=BLANK_CHOICE_NONE)
params['ul_class'] = get_ul_class(self.radio_admin)
else:
if self.null:
field_objs = [formfields.NullSelectField]
else:
field_objs = [formfields.SelectField]
- params['choices'] = self.get_choices()
+ params['choices'] = self.get_choices_default()
elif self.choices:
if self.radio_admin:
field_objs = [formfields.RadioSelectField]
- params['choices'] = self.get_choices(include_blank=self.blank, blank_choice=BLANK_CHOICE_NONE)
params['ul_class'] = get_ul_class(self.radio_admin)
else:
field_objs = [formfields.SelectField]
- params['choices'] = self.get_choices()
+
+ params['choices'] = self.get_choices_default()
else:
field_objs = self.get_manipulator_field_objs()
@@ -294,7 +312,37 @@ def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH):
if self.choices:
return first_choice + list(self.choices)
rel_obj = self.rel.to
- return first_choice + [(getattr(x, rel_obj.pk.attname), repr(x)) for x in rel_obj.get_model_module().get_list(**self.rel.limit_choices_to)]
+ return first_choice + [(getattr(x, rel_obj.pk.attname), str(x))
+ for x in rel_obj.get_model_module().get_list(**self.rel.limit_choices_to)]
+
+ def get_choices_default(self):
+ if(self.radio_admin):
+ return self.get_choices(include_blank=self.blank, blank_choice=BLANK_CHOICE_NONE)
+ else:
+ return self.get_choices()
+
+ def _get_val_from_obj(self, obj):
+ if obj:
+ return getattr(obj, self.attname)
+ else:
+ return self.get_default()
+
+ def flatten_data(self, follow, obj = None):
+ """
+ Returns a dictionary mapping the field's manipulator field names to its
+ "flattened" string values for the admin view. obj is the instance to
+ extract the values from.
+ """