Search code examples
djangodjango-permissionsdjango-viewflow

Django viewflow extend task view


Am trying out an idea to use both django-viewflow and django-permission in an app where there would be some complicated permission rules. The way that I have understood the way django-permission works is by adding a decorator to the view that you want to apply permission rules.

I have managed to get this working on the process views by extending the base viewflow views and pointing the urls.py to the extended view. When I try to follow the same idea for tasks I bump into the error listed below

type object 'CBVTask' has no attribute 'flow_class'

models.py

class CBVArticle(models.Model):
    created_by = models.ForeignKey(User)
    title = models.CharField(max_length=100)
    content = models.TextField()

class CBVArticleProcess(Process):
    article = models.ForeignKey(CBVArticle, blank=True, null=True)

class CBVTask(Task):

    class Meta:
        proxy = True

flow.py

class CBVArticleFlow(Flow):
    process_class = models.CBVArticleProcess
    task_class = models.CBVTask

    start = (
        flow.Start(views.ArticleCreate).Next(this.end)
    )

    end =flow.End()

urls.py

myflow_urls = FlowViewSet(CBVArticleFlow).urls  

urlpatterns = [
    url(r'^process/(?P<process_pk>\d+)/$', views.TestDetailProcessView.as_view(), kwargs = dict(flow_class=CBVArticleFlow), name='detail'),
    url(r'^process/(?P<process_pk>\d+)/start/(?P<task_pk>\d+)/detail/$', views.DetailTaskView.as_view(), kwargs = dict(flow_class=CBVArticleFlow, flow_task=CBVTask), name='start__detail'),
]

views.py

from viewflow.flow.views import DetailProcessView as BaseDetailProcessView, DetailTaskView as BaseDetailTaskView

@permission_required('test_app_cbv.view_cbvarticleprocess')
class TestDetailProcessView(BaseDetailProcessView):
    template_name = 'test_app_cbv/detail.html'

    def get_queryset(self):
        pk = self.kwargs['process_pk']

        return models.CBVArticleProcess.objects.filter(process_ptr_id = pk)

class DetailTaskView(BaseDetailTaskView):
    template_name = 'test_app_cbv/task_detail.html'

Thanks in advance for any pointers!


Solution

  • Turns out to be a bit of a rookie mistake - the urls.py file should have read as follows:

    urls.py

    myflow_urls = FlowViewSet(CBVArticleFlow).urls  
    
    urlpatterns = [
        url(r'^process/(?P<process_pk>\d+)/$', views.TestDetailProcessView.as_view(), kwargs = dict(flow_class=CBVArticleFlow), name='detail'),
        url(r'^process/(?P<process_pk>\d+)/start/(?P<task_pk>\d+)/detail/$', views.DetailTaskView.as_view(), kwargs = dict(flow_class=CBVArticleFlow, flow_task=CBVArticleFlow.start), name='start__detail'),
    ]