Added support for DateTime information being stored in a JSONAttribute. Switched...
[philo.git] / contrib / cowell / admin.py
1 from django import forms
2 from django.contrib import admin
3 from philo.contrib.cowell.fields import ForeignKeyAttribute, ManyToManyAttribute
4 from philo.contrib.cowell.forms import ProxyFieldForm, proxy_fields_for_entity_model
5 from philo.contrib.cowell.widgets import ForeignKeyAttributeRawIdWidget, ManyToManyAttributeRawIdWidget
6 from philo.admin import EntityAdmin
7
8
9 def hide_proxy_fields(hidden, attrs, attname, attvalue, proxy_fields):
10         attvalue = set(attvalue)
11         proxy_fields = set(proxy_fields)
12         if proxy_fields & attvalue:
13                 hidden[attname] = list(attvalue)
14                 attrs[attname] = list(attvalue - proxy_fields)
15
16
17 class ProxyFieldAdminMetaclass(EntityAdmin.__metaclass__):
18         def __new__(cls, name, bases, attrs):
19                 # HACK to bypass model validation for proxy fields by masking them as readonly fields
20                 form = attrs.get('form')
21                 if form:
22                         opts = form._meta
23                         if issubclass(form, ProxyFieldForm) and opts.model:
24                                 proxy_fields = proxy_fields_for_entity_model(opts.model).keys()
25                                 readonly_fields = attrs.pop('readonly_fields', ())
26                                 cls._real_readonly_fields = readonly_fields
27                                 attrs['readonly_fields'] = list(readonly_fields) + proxy_fields
28                                 
29                                 # Additional HACKS to handle raw_id_fields and other attributes that the admin
30                                 # uses model._meta.get_field to validate.
31                                 hidden_attributes = {}
32                                 hide_proxy_fields(hidden_attributes, attrs, 'raw_id_fields', attrs.pop('raw_id_fields', ()), proxy_fields)
33                                 attrs['_hidden_attributes'] = hidden_attributes
34                 #END HACK
35                 return EntityAdmin.__metaclass__.__new__(cls, name, bases, attrs)
36
37
38 class ProxyFieldAdmin(EntityAdmin):
39         __metaclass__ = ProxyFieldAdminMetaclass
40         #form = ProxyFieldForm
41         
42         def __init__(self, *args, **kwargs):
43                 # HACK PART 2 restores the actual readonly fields etc. on __init__.
44                 self.readonly_fields = self.__class__._real_readonly_fields
45                 if hasattr(self, '_hidden_attributes'):
46                         for name, value in self._hidden_attributes.items():
47                                 setattr(self, name, value)
48                 # END HACK
49                 super(ProxyFieldAdmin, self).__init__(*args, **kwargs)
50         
51         def formfield_for_dbfield(self, db_field, **kwargs):
52                 """
53                 Override the default behavior to provide special formfields for EntityProxyFields.
54                 Essentially clones the ForeignKey/ManyToManyField special behavior for the Attribute versions.
55                 """
56                 if not db_field.choices and isinstance(db_field, (ForeignKeyAttribute, ManyToManyAttribute)):
57                         request = kwargs.pop("request", None)
58                         # Combine the field kwargs with any options for formfield_overrides.
59                         # Make sure the passed in **kwargs override anything in
60                         # formfield_overrides because **kwargs is more specific, and should
61                         # always win.
62                         if db_field.__class__ in self.formfield_overrides:
63                                 kwargs = dict(self.formfield_overrides[db_field.__class__], **kwargs)
64                         
65                         # Get the correct formfield.
66                         if isinstance(db_field, ManyToManyAttribute):
67                                 formfield = self.formfield_for_manytomanyattribute(db_field, request, **kwargs)
68                         elif isinstance(db_field, ForeignKeyAttribute):
69                                 formfield = self.formfield_for_foreignkeyattribute(db_field, request, **kwargs)
70                         
71                         # For non-raw_id fields, wrap the widget with a wrapper that adds
72                         # extra HTML -- the "add other" interface -- to the end of the
73                         # rendered output. formfield can be None if it came from a
74                         # OneToOneField with parent_link=True or a M2M intermediary.
75                         # TODO: Implement this.
76                         #if formfield and db_field.name not in self.raw_id_fields:
77                         #       formfield.widget = admin.widgets.RelatedFieldWidgetWrapper(formfield.widget, db_field, self.admin_site)
78                         
79                         return formfield
80                 return super(ProxyFieldAdmin, self).formfield_for_dbfield(db_field, **kwargs)
81         
82         def formfield_for_foreignkeyattribute(self, db_field, request=None, **kwargs):
83                 """Get a form field for a ForeignKeyAttribute field."""
84                 db = kwargs.get('using')
85                 if db_field.name in self.raw_id_fields:
86                         kwargs['widget'] = ForeignKeyAttributeRawIdWidget(db_field, db)
87                 #TODO: Add support for radio fields
88                 #elif db_field.name in self.radio_fields:
89                 #       kwargs['widget'] = widgets.AdminRadioSelect(attrs={
90                 #               'class': get_ul_class(self.radio_fields[db_field.name]),
91                 #       })
92                 #       kwargs['empty_label'] = db_field.blank and _('None') or None
93                 
94                 return db_field.formfield(**kwargs)
95         
96         def formfield_for_manytomanyattribute(self, db_field, request=None, **kwargs):
97                 """Get a form field for a ManyToManyAttribute field."""
98                 db = kwargs.get('using')
99                 
100                 if db_field.name in self.raw_id_fields:
101                         kwargs['widget'] = ManyToManyAttributeRawIdWidget(db_field, using=db)
102                         kwargs['help_text'] = ''
103                 #TODO: Add support for filtered fields.
104                 #elif db_field.name in (list(self.filter_vertical) + list(self.filter_horizontal)):
105                 #       kwargs['widget'] = widgets.FilteredSelectMultiple(db_field.verbose_name, (db_field.name in self.filter_vertical))
106                 
107                 return db_field.formfield(**kwargs)