-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathviewset.py
105 lines (86 loc) · 2.87 KB
/
viewset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from django.urls import include, path, reverse_lazy
from django.views.generic import TemplateView
from django.shortcuts import redirect
from rest_framework import routers
from viewflow.fsm import FlowViewsMixin
from viewflow.forms import TrixEditorWidget
from viewflow.views import CreateModelView
from viewflow.workflow.rest.views import get_schema_view
from viewflow.urls import Application, CreateViewMixin, ReadonlyModelViewset, route
from .flows import ReviewFlow
from .models import Review, ReviewState
from .views import reject_view
from .rest import ReviewViewSet
router = routers.DefaultRouter()
router.register(r"", ReviewViewSet)
class AddReviewView(CreateModelView):
model = Review
fields = ["title", "text"]
form_widgets = {
"text": TrixEditorWidget,
}
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.author = self.request.user
self.object.stage = ReviewState.NEW
self.object.save()
return redirect(self.get_success_url())
class ReviewViewset(FlowViewsMixin, CreateViewMixin, ReadonlyModelViewset):
icon = "menu_book"
model = Review
flow_state = ReviewFlow.stage
list_columns = (
"__str__",
"author",
"published",
"approver",
"stage",
)
list_filter_fields = ("stage",)
create_view_class = AddReviewView
def get_object_flow(self, request, obj):
return ReviewFlow(
obj, user=request.user, ip_address=request.META.get("REMOTE_ADDR")
)
def get_transition_fields(self, request, obj, slug):
if slug == "approve":
return ["text", "comment"]
else:
return []
@property
def remove_path(self):
return path(
"<path:pk>/transition/reject/", reject_view, name="transition_remove"
)
class ReviewApplication(Application):
title = "FSM Flow Demo"
icon = "fact_check"
menu_template_name = "review/app_menu.html"
permission = (lambda user: user.is_staff,)
reviews_path = route("review/", ReviewViewset())
swagger_path = path(
"api/swagger/",
TemplateView.as_view(
template_name="viewflow/contrib/swagger.html",
extra_context={"api_url": reverse_lazy("review:schema")},
),
name="swagger",
)
# schema_path = path("api/schema/", get_schema_view(title="FSM 101"), name="schema")
# schema_path = path(
# "api/schema/", SpectacularAPIView.as_view(schema=AutoSchema), name="schema"
# )
"""
Schema view
"""
@property
def schema_path(self):
def patterns():
prefix = self.reverse("index")
yield path(prefix, self.urls)
return path(
"api/schema/",
get_schema_view(patterns=patterns()),
name="schema",
)
api_path = path("api/", include(router.urls))