Skip to content

Commit

Permalink
django-extra-views friendly mixings
Browse files Browse the repository at this point in the history
  • Loading branch information
kmmbvnr committed Jun 10, 2014
1 parent 3e85efa commit d8c835e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 21 deletions.
10 changes: 5 additions & 5 deletions tests/examples/shipment/views.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.views.generic import CreateView, UpdateView
from viewflow.flow.start import StartViewMixin
from viewflow.flow.view import TaskViewMixin
from viewflow.flow.start import StartFormViewMixin
from viewflow.flow.view import TaskFormViewMixin
from examples.shipment.models import Shipment, Insurance


class StartView(StartViewMixin, CreateView):
class StartView(StartFormViewMixin, CreateView):
model = Shipment
fields = ['goods_tag']

Expand All @@ -15,12 +15,12 @@ def activation_done(self, form):
self.activation.done()


class ShipmentView(TaskViewMixin, UpdateView):
class ShipmentView(TaskFormViewMixin, UpdateView):
def get_object(self):
return self.activation.process.shipment


class InsuranceView(TaskViewMixin, CreateView):
class InsuranceView(TaskFormViewMixin, CreateView):
model = Insurance
fields = ['company_name', 'cost']

Expand Down
57 changes: 48 additions & 9 deletions viewflow/flow/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@ def get_template_names(self):
return ('{}/flow/start.html'.format(self.activation.flow_cls._meta.app_label),
'viewflow/flow/start.html')

@flow_start_view()
def dispatch(self, request, activation, **kwargs):
"""
Check user permissions, and prepare flow to execution
"""
self.activation = activation
if not self.activation.flow_task.has_perm(request.user):
raise PermissionDenied

self.activation.assign(user=request.user)
self.activation.prepare(request.POST or None)
return super(StartViewMixin, self).dispatch(request, **kwargs)


class StartFormViewMixin(StartViewMixin):
"""
Mixing for form based views
"""
def activation_done(self, form):
"""
Finish activation. Subclasses could override this
Expand All @@ -124,18 +142,39 @@ def form_valid(self, form):
self.activation_done(form)
return HttpResponseRedirect(self.get_success_url())

@flow_start_view()
def dispatch(self, request, activation, **kwargs):

class StartFormsetViewMixin(StartViewMixin):
"""
Mixin for formset based views
"""
def activation_done(self, formset):
"""
Check user permissions, and prepare flow to execution
Finish activation. Subclasses could override this
"""
self.activation = activation
if not self.activation.flow_task.has_perm(request.user):
raise PermissionDenied
self.object_list = formset.save()
self.activation.done()

self.activation.assign(user=request.user)
self.activation.prepare(request.POST or None)
return super(StartViewMixin, self).dispatch(request, **kwargs)
def formset_valid(self, formset):
self.activation_done(formset)
return HttpResponseRedirect(self.get_success_url())


class StartInlinesViewMixin(StartViewMixin):
"""
Mixin for forms with inlines view
"""
def activation_done(self, form, inlines):
"""
Finish activation. Subclasses could override this
"""
self.object = form.save()
for formset in inlines:
formset.save()
self.activation.done()

def forms_valid(self, form, inlines):
self.activation_done(form, inlines)
return HttpResponseRedirect(self.get_success_url())


class StartView(StartViewActivation, UpdateView):
Expand Down
53 changes: 46 additions & 7 deletions viewflow/flow/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ def get_template_names(self):
'{}/flow/task.html'.format(flow_cls._meta.app_label),
'viewflow/flow/task.html')

@flow_view()
def dispatch(self, request, activation, **kwargs):
self.activation = activation
if not self.activation.flow_task.has_perm(request.user, self.activation.task):
raise PermissionDenied

self.activation.prepare(request.POST or None)
return super(TaskViewMixin, self).dispatch(request, **kwargs)


class TaskFormViewMixin(TaskViewMixin):
"""
Mixing for form based views
"""
def activation_done(self, form):
"""
Finish activation. Subclasses could override this
Expand All @@ -127,14 +141,39 @@ def form_valid(self, form):
self.activation_done(form)
return HttpResponseRedirect(self.get_success_url())

@flow_view()
def dispatch(self, request, activation, **kwargs):
self.activation = activation
if not self.activation.flow_task.has_perm(request.user, self.activation.task):
raise PermissionDenied

self.activation.prepare(request.POST or None)
return super(TaskViewMixin, self).dispatch(request, **kwargs)
class TaskFormsetViewMixin(TaskViewMixin):
"""
Mixin for formset based views
"""
def activation_done(self, formset):
"""
Finish activation. Subclasses could override this
"""
self.object_list = formset.save()
self.activation.done()

def formset_valid(self, formset):
self.activation_done(formset)
return HttpResponseRedirect(self.get_success_url())


class TaskInlinesViewMixin(TaskViewMixin):
"""
Mixin for forms with inlines view
"""
def activation_done(self, form, inlines):
"""
Finish activation. Subclasses could override this
"""
self.object = form.save()
for formset in inlines:
formset.save()
self.activation.done()

def forms_valid(self, form, inlines):
self.activation_done(form, inlines)
return HttpResponseRedirect(self.get_success_url())


class ProcessView(TaskViewActivation, UpdateView):
Expand Down

0 comments on commit d8c835e

Please sign in to comment.