def display_for_field(value, field): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance( field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateField) or isinstance( field, models.TimeField): if value: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(field, models.DateTimeField): return capfirst(dateformat.format(value, datetime_format)) elif isinstance(field, models.TimeField): return capfirst(dateformat.time_format(value, time_format)) else: return capfirst(dateformat.format(value, date_format)) else: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DecimalField): if value is not None: return ('%%.%sf' % field.decimal_places) % value else: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.FloatField): return escape(value) else: return smart_unicode(value)
def values(self): """ Returns a list of values for this field for this instance. It's a list so we can accomodate many-to-many fields. """ # This import is deliberately inside the function because it causes # some settings to be imported, and we don't want to do that at the # module level. if self.field.rel: if isinstance(self.field.rel, models.ManyToOneRel): objs = getattr(self.instance.instance, self.field.name) elif isinstance(self.field.rel, models.ManyToManyRel): # ManyToManyRel return list(getattr(self.instance.instance, self.field.name).all()) elif self.field.choices: objs = dict(self.field.choices).get(self.raw_value, EMPTY_VALUE) elif isinstance(self.field, models.DateField) or isinstance(self.field, models.TimeField): if self.raw_value: date_format, datetime_format, time_format = get_date_formats() if isinstance(self.field, models.DateTimeField): objs = capfirst(dateformat.format(self.raw_value, datetime_format)) elif isinstance(self.field, models.TimeField): objs = capfirst(dateformat.time_format(self.raw_value, time_format)) else: objs = capfirst(dateformat.format(self.raw_value, date_format)) else: objs = EMPTY_VALUE elif isinstance(self.field, models.BooleanField) or isinstance(self.field, models.NullBooleanField): objs = {True: 'Yes', False: 'No', None: 'Unknown'}[self.raw_value] else: objs = self.raw_value return [objs]
def display_for_field(value, field): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateField) or isinstance(field, models.TimeField): if value: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(field, models.DateTimeField): return capfirst(dateformat.format(value, datetime_format)) elif isinstance(field, models.TimeField): return capfirst(dateformat.time_format(value, time_format)) else: return capfirst(dateformat.format(value, date_format)) else: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DecimalField): if value is not None: return ('%%.%sf' % field.decimal_places) % value else: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.FloatField): return escape(value) else: return smart_unicode(value)
def local_dateformat(dt): """ Returns a string representation of the given ``datetime`` ``dt``. """ if formats: try: return formats.localize(dt, use_l10n=True) except TypeError: # Django 1.2 return formats.localize(dt) return dateformat.format(dt, get_date_formats()[1])
def last_run_with_link(self, obj): format = get_date_formats()[1] value = capfirst(dateformat.format(obj.last_run, format)) log_id = obj.log_set.latest('run_date').id try: # Old way url = reverse('chronograph_log_change', args=(log_id, )) except: # New way url = reverse('admin:chronograph_log_change', args=(log_id, )) return '<a href="%s">%s</a>' % (url, value)
def last_run_with_link(self, obj): format = get_date_formats()[1] value = capfirst(dateformat.format(obj.last_run, format)) log_id = obj.log_set.latest('run_date').id try: # Old way url = reverse('chronograph_log_change', args=(log_id,)) except: # New way url = reverse('admin:chronograph_log_change', args=(log_id,)) return '<a href="%s">%s</a>' % (url, value)
def localize(f_value): date_format, datetime_format, time_format = get_date_formats() if isinstance(f_value, datetime): return dateformat.format(f_value, datetime_format) if isinstance(f_value, time): return dateformat.time_format(f_value, time_format) if isinstance(f_value, date): return dateformat.format(f_value, date_format) if isinstance(f_value, decimal.Decimal): return moneyformat(f_value, None, app_settings.THOUSANDS_SEPARATOR) return value
def values(self): """ Returns a list of values for this field for this instance. It's a list so we can accomodate many-to-many fields. """ # This import is deliberately inside the function because it causes # some settings to be imported, and we don't want to do that at the # module level. if self.field.rel: if isinstance(self.field.rel, models.ManyToOneRel): objs = getattr(self.instance.instance, self.field.name) elif isinstance(self.field.rel, models.ManyToManyRel): # ManyToManyRel return list( getattr(self.instance.instance, self.field.name).all()) elif self.field.choices: objs = dict(self.field.choices).get(self.raw_value, EMPTY_VALUE) elif isinstance(self.field, models.DateField) or isinstance( self.field, models.TimeField): if self.raw_value: date_format, datetime_format, time_format = get_date_formats() if isinstance(self.field, models.DateTimeField): objs = capfirst( dateformat.format(self.raw_value, datetime_format)) elif isinstance(self.field, models.TimeField): objs = capfirst( dateformat.time_format(self.raw_value, time_format)) else: objs = capfirst( dateformat.format(self.raw_value, date_format)) else: objs = EMPTY_VALUE elif isinstance(self.field, models.BooleanField) or isinstance( self.field, models.NullBooleanField): objs = {True: 'Yes', False: 'No', None: 'Unknown'}[self.raw_value] else: objs = self.raw_value return [objs]
def items_for_result(cl, result): first = True pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: f = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: # For non-field list_display values, the value is either a method, # property or returned via a callable. try: if callable(field_name): attr = field_name value = attr(result) elif hasattr(cl.model_admin, field_name) and \ not field_name == '__str__' and not field_name == '__unicode__': attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) 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 allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance(f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst(dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance(f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # DecimalFields are special: Zero-pad the decimals. elif isinstance(f, models.DecimalField): 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 = escape(field_val) if force_unicode(result_repr) == '': result_repr = mark_safe(' ') # If list_display_links not defined, add the link tag to the first field if (first and not cl.list_display_links) or field_name in cl.list_display_links: table_tag = {True:'th', False:'td'}[first] first = False # #RYW # #original: #url = cl.url_for_result(result) # # a hack: I'm using this file to stash a global variable for the # HTTP request, which is currently used by admin_list.py to exclude # a link to a page for editing details. # req = dsh_common_django_request.get() if not dsh_common_django_request.deny_it(req): url = cl.url_for_result(result) else: url = dsh_common_django_request.no_permission_reply() # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = str(cl.to_field) else: attr = pk result_id = repr(force_unicode(getattr(result, attr)))[1:] yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag)) else: yield mark_safe(u'<td%s>%s</td>' % (row_class, conditional_escape(result_repr)))
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 models.FieldDoesNotExist: # For non-field list_display values, the value is either a method # or a property. try: attr = getattr(result, field_name) allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if callable(attr): attr = attr() if boolean: allow_tags = True result_repr = _boolean_icon(attr) else: result_repr = str(attr) 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 allow_tags: result_repr = escape(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance( f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst( dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance( f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # FloatFields are special: Zero-pad the decimals. elif isinstance(f, models.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 = escape(str(field_val)) if result_repr == '': result_repr = ' ' # If list_display_links not defined, add the link tag to the first field if (first and not cl.lookup_opts.admin.list_display_links ) or field_name in cl.lookup_opts.admin.list_display_links: table_tag = {True: 'th', False: 'td'}[first] first = False url = cl.url_for_result(result) result_id = str(getattr( result, pk)) # str() is needed in case of 23L (long ints) yield ('<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %r); return false;"' % result_id or ''), result_repr, table_tag)) else: yield ('<td%s>%s</td>' % (row_class, result_repr))
def items_for_result(cl, result): first = True for field_name in cl.list_display: row_class = "" try: f = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: # For non-field list_display values, the value is either a method, # property or returned via a callable. try: if callable(field_name): attr = field_name value = attr(result) elif ( hasattr(cl.model_admin, field_name) and not field_name == "__str__" and not field_name == "__unicode__" ): attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, "allow_tags", False) boolean = getattr(attr, "boolean", False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) 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 allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance(f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst(dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance(f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # DecimalFields are special: Zero-pad the decimals. elif isinstance(f, models.DecimalField): 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 = escape(field_val) if force_unicode(result_repr) == "": result_repr = mark_safe(" ") # If list_display_links not defined, add the link tag to the first field if (first and not cl.list_display_links) or field_name in cl.list_display_links: table_tag = {True: "th", False: "td"}[first] first = False url = cl.url_for_result(result) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = tuple(cl.to_field) else: attr = cl.pk.attnames result_id = repr(PRIMARY_KEY_URL_SEPARATOR.join([force_unicode(getattr(result, pk)) for pk in attr]))[1:] yield mark_safe( u'<%s%s><a href="%s"%s>%s</a></%s>' % ( table_tag, row_class, url, ( cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or "" ), conditional_escape(result_repr), table_tag, ) ) else: yield mark_safe(u"<td%s>%s</td>" % (row_class, conditional_escape(result_repr)))
def thumbnails_for_result(cl, result, form): """ Basically does the same thing as django's items_for_result, but makes all the items fit in one <td> element instead of separate elements for each field """ first = True pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: f = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: # For non-field list_display values, the value is either a method, # property or returned via a callable. try: if callable(field_name): attr = field_name value = attr(result) elif hasattr(cl.model_admin, field_name) and \ not field_name == '__str__' and not field_name == '__unicode__': attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) 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 allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance(f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst(dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance(f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # DecimalFields are special: Zero-pad the decimals. elif isinstance(f, models.DecimalField): 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.flatchoices: result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE) else: result_repr = escape(field_val) if force_unicode(result_repr) == '': result_repr = mark_safe(' ') # If list_display_links not defined, add the link tag to the first field if (first and not cl.list_display_links) or field_name in cl.list_display_links: table_tag = {True:'th', False:'td'}[first] first = False url = cl.url_for_result(result) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = str(cl.to_field) else: attr = pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] if cl.is_popup and cl.params['pop'] == u'2': yield mark_safe(u'<a href="%s"%s>%s</a>' % \ ('#', (cl.is_popup and ' onclick="FileBrowserDialogue.fileSubmit(\'%s\', \'%s\', \'%s\'); return false;"' % (result.media_url or '', quo_esc(result.caption) or '', result.get_absolute_url())), conditional_escape(result_repr))) #(url, (cl.is_popup and ' onclick="FileBrowserDialogue.fileSubmit(\'%s\'); return false;"' % result.get_absolute_url() or ''), conditional_escape(result_repr))) else: yield mark_safe(u'<a href="%s"%s>%s</a>' % \ (url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr))) else: # By default the fields come from ModelAdmin.list_editable, but if we pull # the fields out of the form instead of list_editable custom admins # can provide fields on a per request basis if form and field_name in form.fields: bf = form[field_name] result_repr = mark_safe(force_unicode(bf.errors) + force_unicode(bf)) else: result_repr = conditional_escape(result_repr) yield mark_safe(u'%s' % (result_repr)) if form: yield mark_safe(force_unicode(form[cl.model._meta.pk.name]))
# python import datetime # django from django.contrib import admin from django.utils.timesince import timeuntil from django.utils.translation import get_date_formats from django.utils import dateformat # app from models import SentLog, Newsletter (date_format, datetime_format, time_format) = get_date_formats() class NewsletterAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): if request.user.is_superuser: obj.author = request.user obj.modify_date = datetime.datetime.now() obj.save() list_display = ( 'send_date_plus', 'sent', 'subject_template', 'author', 'modify_date', 'send_count', ) list_filter = ('language', )
def my_items_for_result(cl, result, form): ''' defines onclick function in book's change_list (returns _unicode_() instead of id ) ''' first = True cl_pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: field = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: try: if callable(field_name): attr = field_name value = attr(result) elif hasattr(cl.model_admin, field_name) and \ not field_name == '__str__' and not field_name == '__unicode__': attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) except (AttributeError, ObjectDoesNotExist): result_repr = EMPTY_CHANGELIST_VALUE else: if not allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, field.attname) if isinstance(field.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, field.name)) else: result_repr = EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateField) or isinstance( field, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(field, models.DateTimeField): result_repr = capfirst( dateformat.format(field_val, datetime_format)) elif isinstance(field, models.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"' elif isinstance(field, models.BooleanField) or isinstance( field, models.NullBooleanField): result_repr = _boolean_icon(field_val) elif isinstance(field, models.DecimalField): if field_val is not None: result_repr = ('%%.%sf' % field.decimal_places) % field_val else: result_repr = EMPTY_CHANGELIST_VALUE elif field.flatchoices: result_repr = dict(field.flatchoices).get( field_val, EMPTY_CHANGELIST_VALUE) else: result_repr = escape(field_val) if force_unicode(result_repr) == '': result_repr = mark_safe(' ') if (first and not cl.list_display_links ) or field_name in cl.list_display_links: table_tag = {True: 'th', False: 'td'}[first] first = False url = cl.url_for_result(result) if cl.to_field: attr = str(cl.to_field) else: attr = cl_pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] ######### my representation for books in author if isinstance(result, Author): result_name = repr(force_unicode(result.name))[1:] yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.checkboxDismissRelatedLookupPopup(window, %s, %s); return false;"' %(result_id, result_name) or ('','')), conditional_escape(result_repr), table_tag)) else: yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.checkboxDismissRelatedLookupPopup(window, %s); return false;"' %result_id or ''), conditional_escape(result_repr), table_tag)) else: if form and field_name in form.fields: form_field_name = form[field_name] result_repr = mark_safe( force_unicode(form_field_name.errors) + force_unicode(form_field_name)) else: result_repr = conditional_escape(result_repr) yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr)) if form: yield mark_safe(force_unicode(form[cl.model._meta.pk.name]))
def get_timeuntil(self, obj): format = get_date_formats()[1] value = capfirst(dateformat.format(obj.next_run, format)) return "%s<br /><span class='mini'>(%s)</span>" % (value, obj.get_timeuntil())
def _formatted_datetime(self): (date_format, datetime_format, time_format) = get_date_formats() return capfirst(dateformat.format(self.datetime, datetime_format))
def items_for_result(cl, result): first = True pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: f = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: # For non-field list_display values, the value is either a method, # property or returned via a callable. try: if callable(field_name): attr = field_name value = attr(result) elif hasattr(cl.model_admin, field_name) and \ not field_name == '__str__' and not field_name == '__unicode__': attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) 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 allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance( f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst( dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance( f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # DecimalFields are special: Zero-pad the decimals. elif isinstance(f, models.DecimalField): 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 = escape(field_val) if force_unicode(result_repr) == '': result_repr = mark_safe(' ') # If list_display_links not defined, add the link tag to the first field if (first and not cl.list_display_links ) or field_name in cl.list_display_links: table_tag = {True: 'th', False: 'td'}[first] first = False url = cl.url_for_result(result) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = str(cl.to_field) else: attr = pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag)) else: yield mark_safe(u'<td%s>%s</td>' % (row_class, conditional_escape(result_repr)))
def my_items_for_result(cl, result, form): ''' defines onclick function in book's change_list (returns _unicode_() instead of id ) ''' first = True cl_pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: field = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: try: if callable(field_name): attr = field_name value = attr(result) elif hasattr(cl.model_admin, field_name) and \ not field_name == '__str__' and not field_name == '__unicode__': attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) except (AttributeError, ObjectDoesNotExist): result_repr = EMPTY_CHANGELIST_VALUE else: if not allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, field.attname) if isinstance(field.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, field.name)) else: result_repr = EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateField) or isinstance(field, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(field, models.DateTimeField): result_repr = capfirst(dateformat.format(field_val, datetime_format)) elif isinstance(field, models.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"' elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): result_repr = _boolean_icon(field_val) elif isinstance(field, models.DecimalField): if field_val is not None: result_repr = ('%%.%sf' % field.decimal_places) % field_val else: result_repr = EMPTY_CHANGELIST_VALUE elif field.flatchoices: result_repr = dict(field.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE) else: result_repr = escape(field_val) if force_unicode(result_repr) == '': result_repr = mark_safe(' ') if (first and not cl.list_display_links) or field_name in cl.list_display_links: table_tag = {True:'th', False:'td'}[first] first = False url = cl.url_for_result(result) if cl.to_field: attr = str(cl.to_field) else: attr = cl_pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] ######### my representation for books in author if isinstance(result, Author): result_name = repr(force_unicode(result.name))[1:] yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.checkboxDismissRelatedLookupPopup(window, %s, %s); return false;"' %(result_id, result_name) or ('','')), conditional_escape(result_repr), table_tag)) else: yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.checkboxDismissRelatedLookupPopup(window, %s); return false;"' %result_id or ''), conditional_escape(result_repr), table_tag)) else: if form and field_name in form.fields: form_field_name = form[field_name] result_repr = mark_safe(force_unicode(form_field_name.errors) + force_unicode(form_field_name)) else: result_repr = conditional_escape(result_repr) yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr)) if form: yield mark_safe(force_unicode(form[cl.model._meta.pk.name]))
# python import datetime # django from django.contrib import admin from django.utils.timesince import timeuntil from django.utils.translation import get_date_formats from django.utils import dateformat # app from models import SentLog, Newsletter (date_format, datetime_format, time_format) = get_date_formats() class NewsletterAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): if request.user.is_superuser: obj.author = request.user obj.modify_date = datetime.datetime.now() obj.save() list_display = ("send_date_plus", "sent", "subject_template", "author", "modify_date", "send_count") list_filter = ("language",) ordering = ("-send_date",) exclude = ("date_added", "author", "sent_date") def sent(self, obj): return obj.sent_date is not None
def items_for_result(cl, result, form): first = True pk = cl.lookup_opts.pk.attname for field_name in cl.list_display: row_class = '' try: f = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: # For non-field list_display values, the value is either a method, # property or returned via a callable. try: if callable(field_name): attr = field_name value = attr(result) elif hasattr(cl.model_admin, field_name) and \ not field_name == '__str__' and not field_name == '__unicode__': attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) 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 allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance(f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst(dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance(f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # DecimalFields are special: Zero-pad the decimals. elif isinstance(f, models.DecimalField): 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.flatchoices: result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE) else: result_repr = escape(field_val) if force_unicode(result_repr) == '': result_repr = mark_safe(' ') # If list_display_links not defined, add the link tag to the first field if (first and not cl.list_display_links) or field_name in cl.list_display_links: table_tag = {True:'th', False:'td'}[first] first = False url = cl.url_for_result(result) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = str(cl.to_field) else: attr = pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] yield mark_safe(u'<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag)) else: # By default the fields come from ModelAdmin.list_editable, but if we pull # the fields out of the form instead of list_editable custom admins # can provide fields on a per request basis if form and field_name in form.fields: bf = form[field_name] result_repr = mark_safe(force_unicode(bf.errors) + force_unicode(bf)) else: result_repr = conditional_escape(result_repr) yield mark_safe(u'<td%s>%s</td>' % (row_class, result_repr)) if form: yield mark_safe(force_unicode(form[cl.model._meta.pk.name]))
def mptt_items_for_result(cl, result, form): first = True pk = cl.lookup_opts.pk.attname # figure out which field to indent mptt_indent_field = None for field_name in cl.list_display: try: f = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: if mptt_indent_field is None: attr = getattr(result, field_name, None) if callable(attr): # first callable field, use this if we can't find any model fields mptt_indent_field = field_name else: # first model field, use this one mptt_indent_field = field_name break for field_name in cl.list_display: row_class = '' f = None try: f = cl.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: # For non-field list_display values, the value is either a method, # property or returned via a callable. try: if callable(field_name): attr = field_name value = attr(result) elif hasattr(cl.model_admin, field_name) and \ not field_name == '__str__' and not field_name == '__unicode__': attr = getattr(cl.model_admin, field_name) value = attr(result) else: attr = getattr(result, field_name) if callable(attr): value = attr() else: value = attr allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True result_repr = _boolean_icon(value) else: result_repr = smart_unicode(value) 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 allow_tags: result_repr = escape(result_repr) else: result_repr = mark_safe(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance( f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst( dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance( f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # DecimalFields are special: Zero-pad the decimals. elif isinstance(f, models.DecimalField): 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.flatchoices: result_repr = dict(f.flatchoices).get(field_val, EMPTY_CHANGELIST_VALUE) else: result_repr = escape(field_val) if force_unicode(result_repr) == '': result_repr = mark_safe(' ') if field_name == mptt_indent_field: level = getattr(result, result._mptt_meta.level_attr) padding_attr = ' style="padding-left:%spx"' % ( 5 + MPTT_ADMIN_LEVEL_INDENT * level) else: padding_attr = '' # If list_display_links not defined, add the link tag to the first field if (first and not cl.list_display_links ) or field_name in cl.list_display_links: table_tag = {True: 'th', False: 'td'}[first] first = False url = cl.url_for_result(result) # Convert the pk to something that can be used in Javascript. # Problem cases are long ints (23L) and non-ASCII strings. if cl.to_field: attr = str(cl.to_field) else: attr = pk value = result.serializable_value(attr) result_id = repr(force_unicode(value))[1:] yield mark_safe(u'<%s%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, padding_attr, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %s); return false;"' % result_id or ''), conditional_escape(result_repr), table_tag)) else: # By default the fields come from ModelAdmin.list_editable, but if we pull # the fields out of the form instead of list_editable custom admins # can provide fields on a per request basis if form and field_name in form.fields: bf = form[field_name] result_repr = mark_safe( force_unicode(bf.errors) + force_unicode(bf)) else: result_repr = conditional_escape(result_repr) yield mark_safe(u'<td%s%s>%s</td>' % (row_class, padding_attr, result_repr)) if form: yield mark_safe(u'<td>%s</td>' % force_unicode(form[cl.model._meta.pk.name]))
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 models.FieldDoesNotExist: # For non-field list_display values, the value is either a method # or a property. try: attr = getattr(result, field_name) allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if callable(attr): attr = attr() if boolean: allow_tags = True result_repr = _boolean_icon(attr) else: result_repr = str(attr) 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 allow_tags: result_repr = escape(result_repr) else: field_val = getattr(result, f.attname) if isinstance(f.rel, models.ManyToOneRel): if field_val is not None: result_repr = escape(getattr(result, f.name)) else: result_repr = EMPTY_CHANGELIST_VALUE # Dates and times are special: They're formatted in a certain way. elif isinstance(f, models.DateField) or isinstance(f, models.TimeField): if field_val: (date_format, datetime_format, time_format) = get_date_formats() if isinstance(f, models.DateTimeField): result_repr = capfirst(dateformat.format(field_val, datetime_format)) elif isinstance(f, models.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, models.BooleanField) or isinstance(f, models.NullBooleanField): result_repr = _boolean_icon(field_val) # DecimalFields are special: Zero-pad the decimals. elif isinstance(f, models.DecimalField): 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 = escape(str(field_val)) if result_repr == '': result_repr = ' ' # If list_display_links not defined, add the link tag to the first field if (first and not cl.lookup_opts.admin.list_display_links) or field_name in cl.lookup_opts.admin.list_display_links: table_tag = {True:'th', False:'td'}[first] first = False url = cl.url_for_result(result) result_id = str(getattr(result, pk)) # str() is needed in case of 23L (long ints) yield ('<%s%s><a href="%s"%s>%s</a></%s>' % \ (table_tag, row_class, url, (cl.is_popup and ' onclick="opener.dismissRelatedLookupPopup(window, %r); return false;"' % result_id or ''), result_repr, table_tag)) else: yield ('<td%s>%s</td>' % (row_class, result_repr))
def fecha_formateada(self): (_date_format, datetime_format, _time_format) = get_date_formats() result_repr = capfirst(dateformat.format(self.fecha, datetime_format)) return result_repr