forked from rapidpro/rapidpro
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5523 from nyaruka/cleaner_views_pt3
Start adding base classes for org level views
- Loading branch information
Showing
29 changed files
with
179 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from smartmin.views import SmartCreateView, SmartCRUDL, SmartDeleteView, SmartListView, SmartReadView, SmartUpdateView | ||
from smartmin.views import SmartCreateView, SmartCRUDL, SmartDeleteView, SmartReadView, SmartUpdateView | ||
|
||
from django import forms | ||
from django.contrib import messages | ||
|
@@ -11,8 +11,9 @@ | |
from temba.contacts.models import ContactField, ContactGroup | ||
from temba.flows.models import Flow | ||
from temba.msgs.models import Msg | ||
from temba.orgs.mixins import OrgFilterMixin, OrgObjPermsMixin, OrgPermsMixin | ||
from temba.orgs.views import BaseMenuView, ModalMixin | ||
from temba.orgs.views import ModalMixin | ||
from temba.orgs.views.base import BaseListView, BaseMenuView | ||
from temba.orgs.views.mixins import OrgObjPermsMixin, OrgPermsMixin | ||
from temba.utils import languages | ||
from temba.utils.fields import CompletionTextarea, InputWidget, SelectWidget, TembaChoiceField | ||
from temba.utils.views import BulkActionMixin, ContentMenuMixin, SpaMixin | ||
|
@@ -161,7 +162,7 @@ def get_form_kwargs(self): | |
kwargs["org"] = self.request.org | ||
return kwargs | ||
|
||
class BaseList(SpaMixin, ContentMenuMixin, OrgFilterMixin, OrgPermsMixin, BulkActionMixin, SmartListView): | ||
class BaseList(SpaMixin, ContentMenuMixin, BulkActionMixin, BaseListView): | ||
fields = ("name", "group") | ||
default_template = "campaigns/campaign_list.html" | ||
default_order = ("-modified_on",) | ||
|
@@ -205,7 +206,7 @@ def get_queryset(self, *args, **kwargs): | |
qs = qs.filter(is_active=True, is_archived=True) | ||
return qs | ||
|
||
class Archive(OrgFilterMixin, OrgPermsMixin, SmartUpdateView): | ||
class Archive(OrgObjPermsMixin, SmartUpdateView): | ||
fields = () | ||
success_url = "[email protected]_read" | ||
success_message = _("Campaign archived") | ||
|
@@ -214,7 +215,7 @@ def save(self, obj): | |
obj.apply_action_archive(self.request.user, Campaign.objects.filter(id=obj.id)) | ||
return obj | ||
|
||
class Activate(OrgFilterMixin, OrgPermsMixin, SmartUpdateView): | ||
class Activate(OrgObjPermsMixin, SmartUpdateView): | ||
fields = () | ||
success_url = "[email protected]_read" | ||
success_message = _("Campaign activated") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .views import * # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
from smartmin.views import SmartListView, SmartTemplateView | ||
|
||
from django.http import JsonResponse | ||
from django.urls import reverse | ||
from django.utils.text import slugify | ||
|
||
from .mixins import OrgPermsMixin | ||
|
||
|
||
class BaseListView(OrgPermsMixin, SmartListView): | ||
""" | ||
Base list view for objects that belong to the current org | ||
""" | ||
|
||
def derive_queryset(self, *args, **kwargs): | ||
queryset = super().derive_queryset(*args, **kwargs) | ||
|
||
if not self.request.user.is_authenticated: | ||
return queryset.none() # pragma: no cover | ||
else: | ||
return queryset.filter(org=self.request.org) | ||
|
||
|
||
class BaseMenuView(OrgPermsMixin, SmartTemplateView): | ||
""" | ||
Base view for the section menus | ||
""" | ||
|
||
def create_divider(self): | ||
return {"type": "divider"} | ||
|
||
def create_space(self): # pragma: no cover | ||
return {"type": "space"} | ||
|
||
def create_section(self, name, items=()): # pragma: no cover | ||
return {"id": slugify(name), "name": name, "type": "section", "items": items} | ||
|
||
def create_list(self, name, href, type): | ||
return {"id": name, "href": href, "type": type} | ||
|
||
def create_modax_button(self, name, href, icon=None, on_submit=None): # pragma: no cover | ||
menu_item = {"id": slugify(name), "name": name, "type": "modax-button"} | ||
if href: | ||
if href[0] == "/": # pragma: no cover | ||
menu_item["href"] = href | ||
elif self.has_org_perm(href): | ||
menu_item["href"] = reverse(href) | ||
|
||
if on_submit: | ||
menu_item["on_submit"] = on_submit | ||
|
||
if icon: # pragma: no cover | ||
menu_item["icon"] = icon | ||
|
||
if "href" not in menu_item: # pragma: no cover | ||
return None | ||
|
||
return menu_item | ||
|
||
def create_menu_item( | ||
self, | ||
menu_id=None, | ||
name=None, | ||
icon=None, | ||
avatar=None, | ||
endpoint=None, | ||
href=None, | ||
count=None, | ||
perm=None, | ||
items=[], | ||
inline=False, | ||
bottom=False, | ||
popup=False, | ||
event=None, | ||
posterize=False, | ||
bubble=None, | ||
mobile=False, | ||
): | ||
if perm and not self.has_org_perm(perm): # pragma: no cover | ||
return | ||
|
||
menu_item = {"name": name, "inline": inline} | ||
menu_item["id"] = menu_id if menu_id else slugify(name) | ||
menu_item["bottom"] = bottom | ||
menu_item["popup"] = popup | ||
menu_item["avatar"] = avatar | ||
menu_item["posterize"] = posterize | ||
menu_item["event"] = event | ||
menu_item["mobile"] = mobile | ||
|
||
if bubble: | ||
menu_item["bubble"] = bubble | ||
|
||
if icon: | ||
menu_item["icon"] = icon | ||
|
||
if count is not None: | ||
menu_item["count"] = count | ||
|
||
if endpoint: | ||
if endpoint[0] == "/": # pragma: no cover | ||
menu_item["endpoint"] = endpoint | ||
elif perm or self.has_org_perm(endpoint): | ||
menu_item["endpoint"] = reverse(endpoint) | ||
|
||
if href: | ||
if href[0] == "/": | ||
menu_item["href"] = href | ||
elif perm or self.has_org_perm(href): | ||
menu_item["href"] = reverse(href) | ||
|
||
if items: # pragma: no cover | ||
menu_item["items"] = [item for item in items if item is not None] | ||
|
||
# only include the menu item if we have somewhere to go | ||
if "href" not in menu_item and "endpoint" not in menu_item and not inline and not popup and not event: | ||
return None | ||
|
||
return menu_item | ||
|
||
def get_menu(self): | ||
return [item for item in self.derive_menu() if item is not None] | ||
|
||
def render_to_response(self, context, **response_kwargs): | ||
return JsonResponse({"results": self.get_menu()}) |
Oops, something went wrong.