Skip to content

Commit

Permalink
Merge pull request #5656 from nyaruka/flows_base_views
Browse files Browse the repository at this point in the history
Use base views in flows app
  • Loading branch information
rowanseymour authored Nov 13, 2024
2 parents 20e2a87 + 5a4721e commit b571748
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion temba/flows/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ def test_views(self):

# also shouldn't be able to view other flow
response = self.client.get(reverse("flows.flow_editor", args=[other_flow.uuid]))
self.assertEqual(302, response.status_code)
self.assertEqual(404, response.status_code)

# get our create page
response = self.client.get(create_url)
Expand Down
48 changes: 24 additions & 24 deletions temba/flows/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@
from temba import mailroom
from temba.channels.models import Channel
from temba.contacts.models import URN
from temba.flows.models import Flow, FlowRevision, FlowRun, FlowSession, FlowStart
from temba.flows.models import Flow, FlowPathCount, FlowRevision, FlowRun, FlowSession, FlowStart
from temba.flows.tasks import update_session_wait_expires
from temba.ivr.models import Call
from temba.orgs.models import IntegrationType, Org
from temba.orgs.views.base import BaseDependencyDeleteModal, BaseExportModal, BaseListView, BaseMenuView
from temba.orgs.views.base import (
BaseDependencyDeleteModal,
BaseExportModal,
BaseListView,
BaseMenuView,
BaseReadView,
BaseUpdateModal,
)
from temba.orgs.views.mixins import BulkActionMixin, OrgObjPermsMixin, OrgPermsMixin
from temba.triggers.models import Trigger
from temba.utils import analytics, gettext, json, languages, on_transaction_commit
Expand Down Expand Up @@ -158,6 +165,7 @@ def post(self, request, *args, **kwargs):


class FlowCRUDL(SmartCRUDL):
model = Flow
actions = (
"list",
"archived",
Expand Down Expand Up @@ -186,13 +194,6 @@ class FlowCRUDL(SmartCRUDL):
"assets",
)

model = Flow

class AllowOnlyActiveFlowMixin:
def get_queryset(self):
initial_queryset = super().get_queryset()
return initial_queryset.filter(is_active=True)

class Menu(BaseMenuView):
@classmethod
def derive_url_pattern(cls, path, action):
Expand Down Expand Up @@ -249,7 +250,7 @@ def derive_menu(self):

return menu

class RecentContacts(OrgObjPermsMixin, SmartReadView):
class RecentContacts(BaseReadView):
"""
Used by the editor for the rollover of recent contacts coming out of a split
"""
Expand All @@ -266,7 +267,7 @@ def render_to_response(self, context, **response_kwargs):

return JsonResponse(self.object.get_recent_contacts(exit_uuid, dest_uuid), safe=False)

class Revisions(AllowOnlyActiveFlowMixin, OrgObjPermsMixin, SmartReadView):
class Revisions(BaseReadView):
"""
Used by the editor for fetching and saving flow definitions
"""
Expand Down Expand Up @@ -486,7 +487,7 @@ def form_valid(self, form):
# redirect to the newly created flow
return HttpResponseRedirect(reverse("flows.flow_editor", args=[copy.uuid]))

class Update(AllowOnlyActiveFlowMixin, ModalFormMixin, OrgObjPermsMixin, SmartUpdateView):
class Update(BaseUpdateModal):
class BaseForm(BaseFlowForm):
class Meta:
model = Flow
Expand Down Expand Up @@ -784,7 +785,7 @@ def get_queryset(self, **kwargs):
qs = super().get_queryset(**kwargs)
return qs.filter(org=self.request.org, labels=self.label, is_archived=False).order_by("-created_on")

class Editor(SpaMixin, OrgObjPermsMixin, ContextMenuMixin, SmartReadView):
class Editor(SpaMixin, ContextMenuMixin, BaseReadView):
slug_url_kwarg = "uuid"

def derive_menu_path(self):
Expand Down Expand Up @@ -1144,7 +1145,7 @@ def create_export(self, org, user, form):
extra_urns=form.cleaned_data.get("extra_urns", []),
)

class ActivityData(OrgObjPermsMixin, SmartReadView):
class ActivityData(BaseReadView):
# the min number of responses to show a histogram
HISTOGRAM_MIN = 0

Expand All @@ -1155,8 +1156,7 @@ class ActivityData(OrgObjPermsMixin, SmartReadView):

def render_to_response(self, context, **response_kwargs):
total_responses = 0
flow = self.get_object()
from temba.flows.models import FlowPathCount
flow = self.object

from_uuids = flow.metadata["waiting_exit_uuids"]
dates = FlowPathCount.objects.filter(flow=flow, from_uuid__in=from_uuids).aggregate(
Expand Down Expand Up @@ -1286,10 +1286,10 @@ def render_to_response(self, context, **response_kwargs):
encoder=json.EpochEncoder,
)

class ActivityChart(SpaMixin, AllowOnlyActiveFlowMixin, OrgObjPermsMixin, SmartReadView):
class ActivityChart(SpaMixin, BaseReadView):
permission = "flows.flow_results"

class CategoryCounts(AllowOnlyActiveFlowMixin, OrgObjPermsMixin, SmartReadView):
class CategoryCounts(BaseReadView):
"""
Used by the editor for the counts on split exits
"""
Expand All @@ -1298,9 +1298,9 @@ class CategoryCounts(AllowOnlyActiveFlowMixin, OrgObjPermsMixin, SmartReadView):
slug_url_kwarg = "uuid"

def render_to_response(self, context, **response_kwargs):
return JsonResponse({"counts": self.get_object().get_category_counts()})
return JsonResponse({"counts": self.object.get_category_counts()})

class Results(SpaMixin, AllowOnlyActiveFlowMixin, OrgObjPermsMixin, ContextMenuMixin, SmartReadView):
class Results(SpaMixin, ContextMenuMixin, BaseReadView):
slug_url_kwarg = "uuid"

def build_context_menu(self, menu):
Expand All @@ -1319,7 +1319,7 @@ def build_context_menu(self, menu):

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
flow = self.get_object()
flow = self.object

result_fields = []
for result_field in flow.metadata[Flow.METADATA_RESULTS]:
Expand All @@ -1333,7 +1333,7 @@ def get_context_data(self, *args, **kwargs):
context["utcoffset"] = int(datetime.now(flow.org.timezone).utcoffset().total_seconds() // 60)
return context

class Activity(AllowOnlyActiveFlowMixin, OrgObjPermsMixin, SmartReadView):
class Activity(BaseReadView):
"""
Used by the editor for the counts on paths between nodes
"""
Expand All @@ -1346,7 +1346,7 @@ def get(self, request, *args, **kwargs):
(active, visited) = flow.get_activity()
return JsonResponse(dict(nodes=active, segments=visited))

class Simulate(OrgObjPermsMixin, SmartReadView):
class Simulate(BaseReadView):
permission = "flows.flow_editor"

@csrf_exempt
Expand Down Expand Up @@ -1425,7 +1425,7 @@ def post(self, request, *args, **kwargs):
except mailroom.RequestException:
return JsonResponse(dict(status="error", description="mailroom error"), status=500)

class PreviewStart(OrgObjPermsMixin, SmartReadView):
class PreviewStart(BaseReadView):
permission = "flows.flow_start"

blockers = {
Expand Down

0 comments on commit b571748

Please sign in to comment.