Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

beginnings of form views

  • Loading branch information...
commit 6f058a139dbf7b5a33be160c003e7c7b76b73ca4 1 parent d801e24
@bendavis78 bendavis78 authored
View
18 impositions/forms.py
@@ -0,0 +1,18 @@
+from django import forms
+from django.forms.models import inlineformset_factory
+from impositions import models
+
+class TemplateForm(forms.ModelForm):
+ class Meta:
+ model = models.Template
+
+class RegionForm(forms.ModelForm):
+ class Meta:
+ model = models.TemplateRegion
+ # these fields aren't used yet
+ exclude = ['description', 'allowed_images']
+
+TemplateRegionFormSet = inlineformset_factory(models.Template,
+ models.TemplateRegion,
+ form=RegionForm,
+ extra=0)
View
2  impositions/models.py
@@ -24,7 +24,7 @@ def __unicode__(self):
class Template(models.Model):
name = models.CharField(max_length=100)
- file = models.FileField(upload_to='impositions/templates')
+ file = models.FileField('Template file', upload_to='impositions/templates')
def __unicode__(self):
return self.name
View
9 impositions/urls.py
@@ -0,0 +1,9 @@
+from django.conf.urls.defaults import patterns, url
+
+urlpatterns = patterns('impositions.views',
+ url('^templates/$', 'template_list', name='impositions-template-list'),
+ url('^templates/create/$', 'template_create', name='impositions-template-create'),
+ url('^templates/(?P<pk>\d+)/$', 'template_edit', name='impositions-template-edit'),
+ #url('^comps/create/$', 'comp_editor', name='impositions-comp-create'),
+ #url('^comps/(?P<id>\d+)/$', 'comp_editor', name='impositions-comp-edit'),
+)
View
44 impositions/views.py
@@ -1 +1,43 @@
-# Create your views here.
+from django import http
+from django.views.generic import list, edit
+from impositions import models, forms
+
+class TemplateListView(list.ListView):
+ model = models.Template
+ context_object_name = 'templates'
+
+class TemplateCreateView(edit.CreateView):
+ model = models.Template
+ context_object_name = 'template'
+
+class TemplateUpdateView(edit.UpdateView):
+ model = models.Template
+ context_object_name = 'template'
+ form_class = forms.TemplateForm
+
+ def form_valid(self, form):
+ context = self.get_context_data()
+ region_formset = context['region_formset']
+ if region_formset.is_valid():
+ self.object = form.save()
+ region_formset.instance = self.object
+ region_formset.save()
+ return http.HttpResponseRedirect('.')
+ return super(TemplateUpdateView, self).form_valid(form)
+
+ def get_context_data(self, **kwargs):
+ context = super(TemplateUpdateView, self).get_context_data(**kwargs)
+ context['formset_form_tpl'] = 'impositions/region_form.html'
+
+ if self.request.POST:
+ region_formset = forms.TemplateRegionFormSet(self.request.POST,
+ self.request.FILES, instance=self.object)
+ else:
+ region_formset = forms.TemplateRegionFormSet(instance=self.object)
+
+ context.update(region_formset=region_formset)
+ return context
+
+template_list = TemplateListView.as_view()
+template_create = TemplateCreateView.as_view()
+template_edit = TemplateUpdateView.as_view()
Please sign in to comment.
Something went wrong with that request. Please try again.