Added support for DateTime information being stored in a JSONAttribute. Switched...
[philo.git] / admin / forms / containers.py
1 from django import forms
2 from django.contrib.admin.widgets import AdminTextareaWidget
3 from django.core.exceptions import ObjectDoesNotExist
4 from django.db.models import Q
5 from django.forms.models import ModelForm, BaseInlineFormSet
6 from django.forms.formsets import TOTAL_FORM_COUNT
7 from philo.admin.widgets import ModelLookupWidget
8 from philo.models import Contentlet, ContentReference
9
10
11 __all__ = (
12         'ContentletForm',
13         'ContentletInlineFormSet',
14         'ContentReferenceForm',
15         'ContentReferenceInlineFormSet'
16 )
17
18
19 class ContainerForm(ModelForm):
20         def __init__(self, *args, **kwargs):
21                 super(ContainerForm, self).__init__(*args, **kwargs)
22                 self.verbose_name = self.instance.name.replace('_', ' ')
23
24
25 class ContentletForm(ContainerForm):
26         content = forms.CharField(required=False, widget=AdminTextareaWidget, label='Content')
27         
28         def should_delete(self):
29                 return not bool(self.cleaned_data['content'])
30         
31         class Meta:
32                 model = Contentlet
33                 fields = ['name', 'content']
34
35
36 class ContentReferenceForm(ContainerForm):
37         def __init__(self, *args, **kwargs):
38                 super(ContentReferenceForm, self).__init__(*args, **kwargs)
39                 try:
40                         self.fields['content_id'].widget = ModelLookupWidget(self.instance.content_type)
41                 except ObjectDoesNotExist:
42                         # This will happen when an empty form (which we will never use) gets instantiated.
43                         pass
44         
45         def should_delete(self):
46                 return (self.cleaned_data['content_id'] is None)
47         
48         class Meta:
49                 model = ContentReference
50                 fields = ['name', 'content_id']
51
52
53 class ContainerInlineFormSet(BaseInlineFormSet):
54         def __init__(self, containers, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
55                 # Unfortunately, I need to add some things to BaseInline between its __init__ and its
56                 # super call, so a lot of this is repetition.
57                 
58                 # Start cribbed from BaseInline
59                 from django.db.models.fields.related import RelatedObject
60                 self.save_as_new = save_as_new
61                 # is there a better way to get the object descriptor?
62                 self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
63                 if self.fk.rel.field_name == self.fk.rel.to._meta.pk.name:
64                         backlink_value = self.instance
65                 else:
66                         backlink_value = getattr(self.instance, self.fk.rel.field_name)
67                 if queryset is None:
68                         queryset = self.model._default_manager
69                 qs = queryset.filter(**{self.fk.name: backlink_value})
70                 # End cribbed from BaseInline
71                 
72                 self.container_instances, qs = self.get_container_instances(containers, qs)
73                 self.extra_containers = containers
74                 self.extra = len(self.extra_containers)
75                 super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix, queryset=qs)
76         
77         def get_container_instances(self, containers, qs):
78                 raise NotImplementedError
79         
80         def total_form_count(self):
81                 if self.data or self.files:
82                         return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
83                 else:
84                         return self.initial_form_count() + self.extra
85         
86         def save_existing_objects(self, commit=True):
87                 self.changed_objects = []
88                 self.deleted_objects = []
89                 if not self.get_queryset():
90                         return []
91
92                 saved_instances = []
93                 for form in self.initial_forms:
94                         pk_name = self._pk_field.name
95                         raw_pk_value = form._raw_value(pk_name)
96
97                         # clean() for different types of PK fields can sometimes return
98                         # the model instance, and sometimes the PK. Handle either.
99                         pk_value = form.fields[pk_name].clean(raw_pk_value)
100                         pk_value = getattr(pk_value, 'pk', pk_value)
101
102                         obj = self._existing_object(pk_value)
103                         if form.should_delete():
104                                 self.deleted_objects.append(obj)
105                                 obj.delete()
106                                 continue
107                         if form.has_changed():
108                                 self.changed_objects.append((obj, form.changed_data))
109                                 saved_instances.append(self.save_existing(form, obj, commit=commit))
110                                 if not commit:
111                                         self.saved_forms.append(form)
112                 return saved_instances
113
114         def save_new_objects(self, commit=True):
115                 self.new_objects = []
116                 for form in self.extra_forms:
117                         if not form.has_changed():
118                                 continue
119                         # If someone has marked an add form for deletion, don't save the
120                         # object.
121                         if form.should_delete():
122                                 continue
123                         self.new_objects.append(self.save_new(form, commit=commit))
124                         if not commit:
125                                 self.saved_forms.append(form)
126                 return self.new_objects
127
128
129 class ContentletInlineFormSet(ContainerInlineFormSet):
130         def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
131                 if instance is None:
132                         self.instance = self.fk.rel.to()
133                 else:
134                         self.instance = instance
135                 
136                 try:
137                         containers = list(self.instance.containers[0])
138                 except ObjectDoesNotExist:
139                         containers = []
140         
141                 super(ContentletInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
142         
143         def get_container_instances(self, containers, qs):
144                 qs = qs.filter(name__in=containers)
145                 container_instances = []
146                 for container in qs:
147                         container_instances.append(container)
148                         containers.remove(container.name)
149                 return container_instances, qs
150         
151         def _construct_form(self, i, **kwargs):
152                 if i >= self.initial_form_count(): # and not kwargs.get('instance'):
153                         kwargs['instance'] = self.model(name=self.extra_containers[i - self.initial_form_count() - 1])
154                 
155                 return super(ContentletInlineFormSet, self)._construct_form(i, **kwargs)
156
157
158 class ContentReferenceInlineFormSet(ContainerInlineFormSet):
159         def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
160                 if instance is None:
161                         self.instance = self.fk.rel.to()
162                 else:
163                         self.instance = instance
164                 
165                 try:
166                         containers = list(self.instance.containers[1])
167                 except ObjectDoesNotExist:
168                         containers = []
169         
170                 super(ContentReferenceInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
171         
172         def get_container_instances(self, containers, qs):
173                 filter = Q()
174                 
175                 for name, ct in containers:
176                         filter |= Q(name=name, content_type=ct)
177                 
178                 qs = qs.filter(filter)
179                 container_instances = []
180                 for container in qs:
181                         container_instances.append(container)
182                         containers.remove((container.name, container.content_type))
183                 return container_instances, qs
184
185         def _construct_form(self, i, **kwargs):
186                 if i >= self.initial_form_count(): # and not kwargs.get('instance'):
187                         name, content_type = self.extra_containers[i - self.initial_form_count() - 1]
188                         kwargs['instance'] = self.model(name=name, content_type=content_type)
189
190                 return super(ContentReferenceInlineFormSet, self)._construct_form(i, **kwargs)