Changeset 998
- Timestamp:
- 10/23/05 10:45:02 (3 years ago)
- Files:
-
- django/branches/new-admin/django/contrib/admin/templates/admin/change_form.html (modified) (3 diffs)
- django/branches/new-admin/django/contrib/admin/views/main.py (modified) (9 diffs)
- django/branches/new-admin/django/core/meta/__init__.py (modified) (1 diff)
- django/branches/new-admin/django/utils/dateformat.py (modified) (7 diffs)
- django/branches/new-admin/django/utils/timesince.py (modified) (3 diffs)
- django/branches/new-admin/django/utils/tzinfo.py (added)
- django/branches/new-admin/django/views/decorators/auth.py (modified) (1 diff)
- django/branches/new-admin/docs/authentication.txt (added)
- django/branches/new-admin/docs/templates.txt (modified) (3 diffs)
- django/branches/new-admin/tests/othertests/dateformat.py (added)
- django/branches/new-admin/tests/testapp/models/custom_pk.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/new-admin/django/contrib/admin/templates/admin/change_form.html
r982 r998 20 20 </ul> 21 21 {% endif %}{% endif %} 22 <form {{ form_enc_attrib }} action='{{ form_url }}' method="post"> 23 22 <form {{ form_enc_attrib }} action='{{ form_url }}' method="post">{% block form_top %}{%endblock%} 24 23 {% if is_popup %}<input type="hidden" name="_popup" value="1">{% endif %} 25 24 {% if save_on_top %}{% submit_row %}{% endif %} … … 36 35 </fieldset> 37 36 {% endfor %} 37 {% block after_field_sets %}{% endblock %} 38 38 {% if change %} 39 39 {% if ordered_objects %} … … 45 45 {% endif %} 46 46 {% endif %} 47 47 48 {% for related_object in inline_related_objects %}{% edit_inline related_object %}{% endfor %} 49 {% block after_related_objects%}{%endblock%} 48 50 {% submit_row %} 49 51 {% if add %} django/branches/new-admin/django/contrib/admin/views/main.py
r982 r998 608 608 609 609 610 def fill_extra_context(opts, app_label, context, add=False, change=False, show_delete=False, form_url=''):610 def render_change_form(opts, app_label, context, add=False, change=False, show_delete=False, form_url=''): 611 611 ordered_objects = opts.get_ordered_objects()[:] 612 612 auto_populated_fields = [f for f in opts.fields if f.prepopulate_from] … … 651 651 } 652 652 653 context.update(extra_context) 653 context.update(extra_context) 654 655 return render_to_response(["admin/%s/%s/change_form" % (app_label, opts.object_name.lower() ), 656 "admin/%s/change_form" % app_label , 657 "admin/change_form"], 658 context_instance=context) 654 659 655 660 def log_add_message(user, opts,manipulator,new_object): 661 pk_value = getattr(new_object, opts.pk.column) 662 log.log_action(user.id, opts.get_content_type_id(), pk_value, repr(new_object), log.ADDITION) 663 656 664 def add_stage(request, app_label, module_name, show_delete=False, form_url='', post_url='../', post_url_continue='../%s/', object_id_override=None): 657 665 mod, opts = _get_mod_opts(app_label, module_name) … … 667 675 668 676 if not errors and not request.POST.has_key("_preview"): 669 for f in opts.many_to_many:670 if f.rel.raw_id_admin:671 new_data.setlist(f.name, new_data[f.name].split(","))672 677 new_object = manipulator.save(new_data) 673 pk_value = getattr(new_object, opts.pk.column) 674 log.log_action(request.user.id, opts.get_content_type_id(), pk_value, repr(new_object), log.ADDITION) 678 log_add_message(request.user, opts,manipulator,new_object) 675 679 msg = 'The %s "%s" was added successfully.' % (opts.verbose_name, new_object) 680 676 681 # Here, we distinguish between different save types by checking for 677 682 # the presence of keys in request.POST. … … 690 695 request.user.add_message(msg) 691 696 return HttpResponseRedirect(post_url) 692 # if request.POST.has_key("_preview"): # Always happens anyway.693 # manipulator.do_html2python(new_data)694 697 else: 695 698 # Add default data. … … 711 714 c['object_id'] = object_id_override 712 715 716 return render_change_form(opts, app_label, c, add=True) 717 add_stage = staff_member_required(add_stage) 718 719 def log_change_message(user, opts,manipulator,new_object): 720 pk_value = getattr(new_object, opts.pk.column) 721 # Construct the change message. 722 change_message = [] 723 if manipulator.fields_added: 724 change_message.append('Added %s.' % get_text_list(manipulator.fields_added, 'and')) 725 if manipulator.fields_changed: 726 change_message.append('Changed %s.' % get_text_list(manipulator.fields_changed, 'and')) 727 if manipulator.fields_deleted: 728 change_message.append('Deleted %s.' % get_text_list(manipulator.fields_deleted, 'and')) 729 change_message = ' '.join(change_message) 730 if not change_message: 731 change_message = 'No fields changed.' 732 log.log_action(user.id, opts.get_content_type_id(), pk_value, repr(new_object), log.CHANGE, change_message) 713 733 714 fill_extra_context(opts, app_label, c, add=True)715 716 return render_to_response("admin/change_form", context_instance=c)717 add_stage = staff_member_required(add_stage)718 719 720 734 def change_stage(request, app_label, module_name, object_id): 721 735 mod, opts = _get_mod_opts(app_label, module_name) … … 738 752 manipulator.do_html2python(new_data) 739 753 if not errors and not request.POST.has_key("_preview"): 740 # Now done in commaseparatedint741 # for f in opts.many_to_many:742 # if f.rel.raw_id_admin:743 # new_data.setlist(f.name, new_data[f.name].split(","))744 754 new_object = manipulator.save(new_data) 745 pk_value = getattr(new_object, opts.pk.column) 746 747 # Construct the change message. 748 change_message = [] 749 if manipulator.fields_added: 750 change_message.append('Added %s.' % get_text_list(manipulator.fields_added, 'and')) 751 if manipulator.fields_changed: 752 change_message.append('Changed %s.' % get_text_list(manipulator.fields_changed, 'and')) 753 if manipulator.fields_deleted: 754 change_message.append('Deleted %s.' % get_text_list(manipulator.fields_deleted, 'and')) 755 change_message = ' '.join(change_message) 756 if not change_message: 757 change_message = 'No fields changed.' 758 759 log.log_action(request.user.id, opts.get_content_type_id(), pk_value, repr(new_object), log.CHANGE, change_message) 755 log_change_message(request.user,opts,manipulator,new_object) 760 756 msg = 'The %s "%s" was changed successfully.' % (opts.verbose_name, new_object) 761 757 if request.POST.has_key("_continue"): … … 774 770 request.user.add_message(msg) 775 771 return HttpResponseRedirect("../") 776 # if request.POST.has_key("_preview"): # always happens777 # manipulator.do_html2python(new_data)778 772 else: 779 773 # Populate new_data with a "flattened" version of the current data. 780 774 new_data = manipulator.flatten_data() 781 775 782 776 # TODO: do this in flatten_data... 783 777 # If the object has ordered objects on its admin page, get the existing 784 778 # order and flatten it into a comma-separated list of IDs. 779 785 780 id_order_list = [] 786 781 for rel_obj in opts.get_ordered_objects(): … … 795 790 form.order_objects = [] 796 791 792 #TODO Should be done in flatten_data / FormWrapper construction 797 793 for related in opts.get_followed_related_objects(): 798 794 wrt = related.opts.order_with_respect_to … … 811 807 }) 812 808 813 fill_extra_context(opts, app_label, c, change=True)809 return render_change_form(opts, app_label, c, change=True) 814 810 815 return render_to_response('admin/change_form', context_instance=c) 816 change_stage = staff_member_required(change_stage) 811 817 812 818 813 def _nest_help(obj, depth, val): django/branches/new-admin/django/core/meta/__init__.py
r986 r998 1499 1499 id_list = args and args[0] or kwargs['id_list'] 1500 1500 assert id_list != [], "get_in_bulk() cannot be passed an empty list." 1501 kwargs['where'] = ["%s.id IN (%s)" % (opts.db_table, ",".join(map(str, id_list)))] 1501 kwargs['where'] = ["%s.%s IN (%s)" % (opts.db_table, opts.pk.column, ",".join(['%s'] * len(id_list)))] 1502 kwargs['params'] = id_list 1502 1503 obj_list = function_get_list(opts, klass, **kwargs) 1503 return dict([( o.id, o) for o in obj_list])1504 return dict([(getattr(o, opts.pk.column), o) for o in obj_list]) 1504 1505 1505 1506 def function_get_latest(opts, klass, does_not_exist_exception, **kwargs): django/branches/new-admin/django/utils/dateformat.py
r980 r998 13 13 14 14 from django.utils.dates import MONTHS, MONTHS_AP, WEEKDAYS 15 from django.utils.tzinfo import LocalTimezone 15 16 from calendar import isleap 16 import re 17 import re, time 17 18 18 19 re_formatchars = re.compile(r'(?<!\\)([aABdDfFgGhHiIjlLmMnNOPrsStTUwWyYzZ])') … … 41 42 def A(self): 42 43 "'AM' or 'PM'" 43 return self.a().upper() 44 if self.data.hour > 11: 45 return 'PM' 46 return 'AM' 44 47 45 48 def B(self): … … 101 104 year_days = [None, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334] 102 105 103 def __init__(self, d): 104 self.data = d 106 def __init__(self, dt): 107 # Accepts either a datetime or date object. 108 self.data = dt 109 self.timezone = getattr(dt, 'tzinfo', None) 110 if hasattr(self.data, 'hour') and not self.timezone: 111 self.timezone = LocalTimezone(dt) 105 112 106 113 def d(self): … … 120 127 raise NotImplementedError 121 128 129 def I(self): 130 "'1' if Daylight Savings Time, '0' otherwise." 131 if self.timezone.dst(self.data): 132 return '1' 133 else: 134 return '0' 135 122 136 def j(self): 123 137 "Day of the month without leading zeros; i.e. '1' to '31'" … … 150 164 def O(self): 151 165 "Difference to Greenwich time in hours; e.g. '+0200'" 152 raise NotImplementedError 166 tz = self.timezone.utcoffset(self.data) 167 return "%+03d%02d" % (tz.seconds // 3600, (tz.seconds // 60) % 60) 153 168 154 169 def r(self): 155 170 "RFC 822 formatted date; e.g. 'Thu, 21 Dec 2000 16:01:07 +0200'" 156 r aise NotImplementedError171 return self.format('D, j M Y H:i:s O') 157 172 158 173 def S(self): … … 175 190 def T(self): 176 191 "Time zone of this machine; e.g. 'EST' or 'MDT'" 177 raise NotImplementedError 192 name = self.timezone.tzname(self.data) 193 if name is None: 194 name = self.format('O') 195 return name 178 196 179 197 def U(self): 180 198 "Seconds since the Unix epoch (January 1 1970 00:00:00 GMT)" 181 raise NotImplementedError 199 off = self.timezone.utcoffset(self.data) 200 return int(time.mktime(self.data.timetuple())) + off.seconds * 60 182 201 183 202 def w(self): … … 230 249 for timezones west of UTC is always negative, and for those east of UTC 231 250 is always positive.""" 232 r aise NotImplementedError251 return self.timezone.utcoffset(self.data).seconds 233 252 234 253 def format(value, format_string): django/branches/new-admin/django/utils/timesince.py
r3 r998 1 import time, math, datetime 1 import datetime, math, time 2 from django.utils.tzinfo import LocalTimezone 2 3 3 4 def timesince(d, now=None): … … 7 8 Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since 8 9 """ 9 original = time.mktime(d.timetuple())10 10 chunks = ( 11 11 (60 * 60 * 24 * 365, 'year'), … … 15 15 (60, 'minute') 16 16 ) 17 if not now: 18 now = time.time() 19 since = now - original 17 if now: 18 t = time.mktime(now) 19 else: 20 t = time.localtime() 21 if d.tzinfo: 22 tz = LocalTimezone() 23 else: 24 tz = None 25 now = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], tzinfo=tz) 26 delta = now - d 27 since = delta.days * 24 * 60 * 60 + delta.seconds 20 28 # Crazy iteration syntax because we need i to be current index 21 29 for i, (seconds, name) in zip(range(len(chunks)), chunks): django/branches/new-admin/django/views/decorators/auth.py
r3 r998 1 def login_required(view_func): 1 def user_passes_test(test_func): 2 """ 3 Decorator for views that checks that the user passes the given test, 4 redirecting to the log-in page if necessary. The test should be a callable 5 that takes the user object and returns True if the user passes. 6 """ 7 8 def _dec(view_func): 9 def _checklogin(request, *args, **kwargs): 10 from django.views.auth.login import redirect_to_login 11 if test_func(request.user): 12 return view_func(request, *args, **kwargs) 13 return redirect_to_login(request.path) 14 return _checklogin 15 return _dec 16 17 18 login_required = user_passes_test(lambda u: not u.is_anonymous()) 19 login_required.__doc__ = ( 2 20 """ 3 21 Decorator for views that checks that the user is logged in, redirecting 4 22 to the log-in page if necessary. 5 23 """ 6 from django.views.auth.login import redirect_to_login 7 def _checklogin(request, *args, **kwargs): 8 if request.user.is_anonymous(): 9 return redirect_to_login(request.path) 10 else: 11 return view_func(request, *args, **kwargs) 12 return _checklogin 24 ) 25 django/branches/new-admin/docs/templates.txt
r987 r998 518 518 N Month abbreviation in Associated Press ``'Jan.'``, ``'Feb.'``, ``'March'``, ``'May'`` 519 519 style. Proprietary extension. 520 O Not implemented.520 O Difference to Greenwich time in hours. ``'+0200'`` 521 521 P Time, in 12-hour hours, minutes and ``'1 a.m.'``, ``'1:30 p.m.'``, ``'midnight'``, ``'noon'``, ``'12:30 p.m.'`` 522 522 'a.m.'/'p.m.', with minutes left off … … 524 524 strings 'midnight' and 'noon' if 525 525 appropriate. Proprietary extension. 526 r Not implemented.526 r RFC 822 formatted date. ``'Thu, 21 Dec 2000 16:01:07 +0200'`` 527 527 s Seconds, 2 digits with leading zeros. ``'00'`` to ``'59'`` 528 528 S English ordinal suffix for day of the ``'st'``, ``'nd'``, ``'rd'`` or ``'th'`` 529 529 month, 2 characters. 530 530 t Not implemented. 531 T Not implemented.531 T Time zone of this machine. ``'EST'``, ``'MDT'`` 532 532 U Not implemented. 533 533 w Day of the week, digits without ``'0'`` (Sunday) to ``'6'`` (Saturday) … … 538 538 Y Year, 4 digits. ``'1999'`` 539 539 z Day of the year. ``0`` to ``365`` 540 Z Not implemented. 540 Z Time zone offset in seconds. The ``-43200`` to ``43200`` 541 offset for timezones west of UTC is 542 always negative, and for those east of 543 UTC is always positive. 541 544 ================ ====================================== ===================== 542 545 django/branches/new-admin/tests/testapp/models/custom_pk.py
r682 r998 54 54 >>> employees.get_list(last_name__exact='Jones') 55 55 [Dan Jones, Fran Jones] 56 >>> employees.get_in_bulk(['ABC123', 'XYZ456']) 57 {'XYZ456': Fran Jones, 'ABC123': Dan Jones} 56 58 57 59 >>> b = businesses.Business(name='Sears') … … 63 65 >>> fran.get_business_list() 64 66 [Sears] 67 >>> businesses.get_in_bulk(['Sears']) 68 {'Sears': Sears} 65 69 """
