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
13 'ContentletInlineFormSet',
14 'ContentReferenceForm',
15 'ContentReferenceInlineFormSet'
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('_', ' ')
25 class ContentletForm(ContainerForm):
26 content = forms.CharField(required=False, widget=AdminTextareaWidget, label='Content')
28 def should_delete(self):
29 return not bool(self.cleaned_data['content'])
33 fields = ['name', 'content']
36 class ContentReferenceForm(ContainerForm):
37 def __init__(self, *args, **kwargs):
38 super(ContentReferenceForm, self).__init__(*args, **kwargs)
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.
45 def should_delete(self):
46 return (self.cleaned_data['content_id'] is None)
49 model = ContentReference
50 fields = ['name', 'content_id']
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.
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
66 backlink_value = getattr(self.instance, self.fk.rel.field_name)
68 queryset = self.model._default_manager
69 qs = queryset.filter(**{self.fk.name: backlink_value})
70 # End cribbed from BaseInline
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)
77 def get_container_instances(self, containers, qs):
78 raise NotImplementedError
80 def total_form_count(self):
81 if self.data or self.files:
82 return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
84 return self.initial_form_count() + self.extra
86 def save_existing_objects(self, commit=True):
87 self.changed_objects = []
88 self.deleted_objects = []
89 if not self.get_queryset():
93 for form in self.initial_forms:
94 pk_name = self._pk_field.name
95 raw_pk_value = form._raw_value(pk_name)
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)
102 obj = self._existing_object(pk_value)
103 if form.should_delete():
104 self.deleted_objects.append(obj)
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))
111 self.saved_forms.append(form)
112 return saved_instances
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():
119 # If someone has marked an add form for deletion, don't save the
121 if form.should_delete():
123 self.new_objects.append(self.save_new(form, commit=commit))
125 self.saved_forms.append(form)
126 return self.new_objects
129 class ContentletInlineFormSet(ContainerInlineFormSet):
130 def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
132 self.instance = self.fk.rel.to()
134 self.instance = instance
137 containers = list(self.instance.containers[0])
138 except ObjectDoesNotExist:
141 super(ContentletInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
143 def get_container_instances(self, containers, qs):
144 qs = qs.filter(name__in=containers)
145 container_instances = []
147 container_instances.append(container)
148 containers.remove(container.name)
149 return container_instances, qs
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])
155 return super(ContentletInlineFormSet, self)._construct_form(i, **kwargs)
158 class ContentReferenceInlineFormSet(ContainerInlineFormSet):
159 def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None):
161 self.instance = self.fk.rel.to()
163 self.instance = instance
166 containers = list(self.instance.containers[1])
167 except ObjectDoesNotExist:
170 super(ContentReferenceInlineFormSet, self).__init__(containers, data, files, instance, save_as_new, prefix, queryset)
172 def get_container_instances(self, containers, qs):
175 for name, ct in containers:
176 filter |= Q(name=name, content_type=ct)
178 qs = qs.filter(filter)
179 container_instances = []
181 container_instances.append(container)
182 containers.remove((container.name, container.content_type))
183 return container_instances, qs
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)
190 return super(ContentReferenceInlineFormSet, self)._construct_form(i, **kwargs)