Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class CategoryCRUDL(SmartCRUDL):
model = Category
class Create(SmartCreateView):
form_class = CategoryForm
class PostCRUDL(SmartCRUDL):
model = Post
actions = (
'create', 'read', 'update', 'delete', 'list', 'author', 'exclude', 'exclude2', 'readonly', 'readonly2',
'messages', 'csv_import', 'by_uuid', 'refresh', 'no_refresh', 'list_no_pagination'
)
class Read(SmartReadView):
permission = None
class List(SmartListView):
fields = ('title', 'tags', 'created_on', 'created_by')
search_fields = ('title__icontains', 'body__icontains')
default_order = 'title'
def as_json(self, context):
items = []
for obj in self.object_list:
items.append(dict(title=obj.title,
body=obj.body,
tags=obj.tags))
return items
class Readonly(SmartUpdateView):
readonly = ('tags',)
class Readonly2(SmartUpdateView):
form_class = ExcludeForm
readonly = ('tags',)
class Messages(SmartListView):
def pre_process(self, request, *args, **kwargs):
messages.error(request, "Error Messages")
messages.success(request, "Success Messages")
messages.info(request, "Info Messages")
messages.warning(request, "Warning Messages")
messages.debug(request, "Debug Messages")
class ByUuid(SmartReadView):
slug_url_kwarg = 'uuid'
class Refresh(SmartReadView):
permission = None
def derive_refresh(self):
return 123
class NoRefresh(SmartReadView):
permission = None
def derive_refresh(self):
return 0
Endpoint for creating a Pinned Comment
"""
permission = "msg_board.messageboardcomment_pin"
fields = ["comment", "pinned_on"]
http_method_names = ["post"]
def get_object(self, queryset=None):
return get_object_or_404(MessageBoardComment.get_all(self.request.org), pk=self.kwargs.get("pk"))
def post(self, request, *args, **kwargs):
comment = self.get_object()
comment.pin()
return HttpResponse(status=204)
class Unpin(OrgObjPermsMixin, SmartReadView):
"""
Endpoint for deleting a Pinned Comment
"""
permission = "msg_board.messageboardcomment_unpin"
fields = ["comment", "pinned_on"]
http_method_names = ["post"]
def get_object(self, queryset=None):
return get_object_or_404(MessageBoardComment.get_all(self.request.org), pk=self.kwargs.get("pk"))
def post(self, request, *args, **kwargs):
comment = self.get_object()
comment.unpin()
return HttpResponse(status=204)
def render_to_response(self, context, **response_kwargs):
return JsonResponse({"results": context["timeline"], "max_time": context["max_time"]}, encoder=JSONEncoder)
class Watch(OrgObjPermsMixin, SmartReadView):
"""
Endpoint for watching a case
"""
permission = "cases.case_read"
def post(self, request, *args, **kwargs):
self.get_object().watch(request.user)
return HttpResponse(status=204)
class Unwatch(OrgObjPermsMixin, SmartReadView):
"""
Endpoint for unwatching a case
"""
permission = "cases.case_read"
def post(self, request, *args, **kwargs):
self.get_object().unwatch(request.user)
return HttpResponse(status=204)
class CaseExportCRUDL(SmartCRUDL):
model = CaseExport
actions = ("create", "read")
class Create(NonAtomicMixin, OrgPermsMixin, CaseSearchMixin, SmartCreateView):
search = self.derive_search()
cases = Case.search(org, user, search)
paginator = LazyPaginator(cases, 50)
context["object_list"] = paginator.page(page)
context["has_more"] = paginator.num_pages > page
return context
def render_to_response(self, context, **response_kwargs):
return JsonResponse(
{"results": [c.as_json() for c in context["object_list"]], "has_more": context["has_more"]},
encoder=JSONEncoder,
)
class Timeline(OrgObjPermsMixin, SmartReadView):
"""
JSON endpoint for fetching case actions and messages
"""
permission = "cases.case_read"
def get_context_data(self, **kwargs):
context = super(CaseCRUDL.Timeline, self).get_context_data(**kwargs)
dt_now = now()
empty = False
after = self.request.GET.get("after", None)
if after:
after = microseconds_to_datetime(int(after))
merge_from_backend = False
else:
def render_search(self, book, search): # pragma: no cover
pass
def get_search(self):
search = json.loads(self.search)
if "after" in search:
search["after"] = parse_iso8601(search["after"])
if "before" in search:
search["before"] = parse_iso8601(search["before"])
return search
class Meta:
abstract = True
class BaseDownloadView(OrgObjPermsMixin, SmartReadView):
"""
Download view for exports
"""
filename = None
template_name = "download.haml"
@classmethod
def derive_url_pattern(cls, path, action):
return r"%s/download/(?P\d+)/" % path
def has_permission(self, request, *args, **kwargs):
if not super(BaseDownloadView, self).has_permission(request, *args, **kwargs):
return False
obj = self.get_object()
return obj
return JsonResponse({"results": [as_json(l) for l in labels]})
class Watch(OrgObjPermsMixin, SmartReadView):
"""
Endpoint for watching a label
"""
permission = "msgs.label_read"
def post(self, request, *args, **kwargs):
self.get_object().watch(request.user)
return HttpResponse(status=204)
class Unwatch(OrgObjPermsMixin, SmartReadView):
"""
Endpoint for unwatching a label
"""
permission = "msgs.label_read"
def post(self, request, *args, **kwargs):
self.get_object().unwatch(request.user)
return HttpResponse(status=204)
class MessageSearchMixin(object):
def derive_search(self):
"""
Collects and prepares message search parameters into JSON serializable dict
"""
class ContactCRUDL(SmartCRUDL):
"""
Simple contact CRUDL for debugging by superusers, i.e. not exposed to regular users for now
"""
model = Contact
actions = ("list", "read", "fetch", "cases")
class List(OrgPermsMixin, SmartListView):
fields = ("uuid", "name", "language", "created_on")
def get_queryset(self, **kwargs):
return self.model.objects.filter(org=self.request.org)
class Read(OrgObjPermsMixin, SmartReadView):
def get_context_data(self, **kwargs):
context = super(ContactCRUDL.Read, self).get_context_data(**kwargs)
fields = Field.get_all(self.object.org, visible=True).order_by("label")
context["context_data_json"] = {"contact": self.object.as_json(full=True), "fields": [f.as_json() for f in fields]}
context["backend_url"] = settings.SITE_EXTERNAL_CONTACT_URL % self.object.uuid
return context
class Fetch(OrgObjPermsMixin, SmartReadView):
"""
JSON endpoint for fetching a single contact
"""
permission = "contacts.contact_read"
"open",
"update_summary",
"reply",
"fetch",
"search",
"timeline",
"note",
"reassign",
"close",
"reopen",
"label",
"watch",
"unwatch",
)
class Read(OrgObjPermsMixin, SmartReadView):
fields = ()
def has_permission(self, request, *args, **kwargs):
has_perm = super(CaseCRUDL.Read, self).has_permission(request, *args, **kwargs)
return has_perm and self.get_object().access_level(self.request.user) >= AccessLevel.read
def derive_queryset(self, **kwargs):
return Case.get_all(self.request.org).select_related("org", "assignee")
def get_context_data(self, **kwargs):
context = super(CaseCRUDL.Read, self).get_context_data(**kwargs)
case = self.get_object()
can_update = case.access_level(self.request.user) == AccessLevel.update
labels = Label.get_all(self.request.org).order_by("name")
fields = Field.get_all(self.object.org, visible=True).order_by("label")
fields = ("uuid", "name", "language", "created_on")
def get_queryset(self, **kwargs):
return self.model.objects.filter(org=self.request.org)
class Read(OrgObjPermsMixin, SmartReadView):
def get_context_data(self, **kwargs):
context = super(ContactCRUDL.Read, self).get_context_data(**kwargs)
fields = Field.get_all(self.object.org, visible=True).order_by("label")
context["context_data_json"] = {"contact": self.object.as_json(full=True), "fields": [f.as_json() for f in fields]}
context["backend_url"] = settings.SITE_EXTERNAL_CONTACT_URL % self.object.uuid
return context
class Fetch(OrgObjPermsMixin, SmartReadView):
"""
JSON endpoint for fetching a single contact
"""
permission = "contacts.contact_read"
def render_to_response(self, context, **response_kwargs):
return JsonResponse(self.object.as_json(full=True), encoder=JSONEncoder)
class Cases(OrgObjPermsMixin, SmartReadView):
"""
JSON endpoint for fetching a contact's cases
"""
permission = "contacts.contact_read"