Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
body=obj.body,
tags=obj.tags))
return items
class Author(SmartListView):
fields = ('title', 'tags', 'created_on', 'created_by')
default_order = ('created_by__username', 'order')
class Update(SmartUpdateView):
success_message = "Your blog post has been updated."
class Create(SmartCreateView):
submit_button_name = "Create New Post"
class Exclude(SmartUpdateView):
exclude = ('tags',)
class Exclude2(SmartUpdateView):
form_class = ExcludeForm
exclude = ('tags',)
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")
success_message = "Your blog post has been updated."
class Create(SmartCreateView):
submit_button_name = "Create New Post"
class Exclude(SmartUpdateView):
exclude = ('tags',)
class Exclude2(SmartUpdateView):
form_class = ExcludeForm
exclude = ('tags',)
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
default_order = ('created_by__username', 'order')
class Update(SmartUpdateView):
success_message = "Your blog post has been updated."
class Create(SmartCreateView):
submit_button_name = "Create New Post"
class Exclude(SmartUpdateView):
exclude = ('tags',)
class Exclude2(SmartUpdateView):
form_class = ExcludeForm
exclude = ('tags',)
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'
form_class = PartnerCreateForm
def save(self, obj):
data = self.form.cleaned_data
org = self.request.user.get_org()
restricted = data["is_restricted"]
labels = data["labels"] if restricted else []
self.object = Partner.create(
org, data["name"], data["description"], None, restricted, labels, data["logo"]
)
def get_success_url(self):
return reverse("cases.partner_read", args=[self.object.pk])
class Update(OrgObjPermsMixin, PartnerFormMixin, SmartUpdateView):
form_class = PartnerUpdateForm
success_url = "id@cases.partner_read"
def has_permission(self, request, *args, **kwargs):
return request.user.can_manage(self.get_object())
class Read(OrgObjPermsMixin, SmartReadView):
def get_queryset(self):
return Partner.get_all(self.request.org)
def get_context_data(self, **kwargs):
context = super(PartnerCRUDL.Read, self).get_context_data(**kwargs)
fields = Field.get_all(self.object.org, visible=True).order_by("label")
# angular app requires context data in JSON format
def post(self, request, *args, **kwargs):
case = self.get_object()
user = request.user
user_labels = Label.get_all(self.org, user)
label_ids = request.json["labels"]
specified_labels = list(user_labels.filter(pk__in=label_ids))
# user can't remove labels that they can't see
unseen_labels = [l for l in case.labels.all() if l not in user_labels]
case.update_labels(user, specified_labels + unseen_labels)
return HttpResponse(status=204)
class UpdateSummary(OrgObjPermsMixin, SmartUpdateView):
"""
JSON endpoint for updating a case summary
"""
permission = "cases.case_update"
def post(self, request, *args, **kwargs):
case = self.get_object()
summary = request.json["summary"]
case.update_summary(request.user, summary)
return HttpResponse(status=204)
class Reply(OrgObjPermsMixin, SmartUpdateView):
"""
JSON endpoint for replying in a case
"""
return initial
def save(self, obj):
data = self.form.cleaned_data
org = self.request.org
name = data["name"]
description = data["description"]
tests = self.construct_tests()
is_synced = data["is_synced"]
self.object = Label.create(org, name, description, tests, is_synced)
def get_success_url(self):
return reverse("msgs.label_read", args=[self.object.pk])
class Update(RuleFormMixin, OrgObjPermsMixin, SmartUpdateView):
form_class = LabelForm
success_url = "id@msgs.label_read"
def get_form_kwargs(self):
kwargs = super(LabelCRUDL.Update, self).get_form_kwargs()
kwargs["org"] = self.request.org
kwargs["is_create"] = False
return kwargs
def post_save(self, obj):
obj = super(LabelCRUDL.Update, self).post_save(obj)
tests = self.construct_tests()
obj.update_tests(tests)
return obj
class Close(OrgObjPermsMixin, SmartUpdateView):
"""
JSON endpoint for closing a case
"""
permission = "cases.case_update"
def post(self, request, *args, **kwargs):
case = self.get_object()
note = request.json.get("note")
case.close(request.user, note)
return HttpResponse(status=204)
class Reopen(OrgObjPermsMixin, SmartUpdateView):
"""
JSON endpoint for re-opening a case
"""
permission = "cases.case_update"
def post(self, request, *args, **kwargs):
case = self.get_object()
note = request.json.get("note")
case.reopen(request.user, note)
return HttpResponse(status=204)
class Label(OrgObjPermsMixin, SmartUpdateView):
"""
JSON endpoint for labelling a case
"""
Make sure our groups are up to date
"""
if 'groups' in self.form.cleaned_data:
obj.groups.clear()
for group in self.form.cleaned_data['groups']:
obj.groups.add(group)
# if a new password was set, reset our failed logins
if 'new_password' in self.form.cleaned_data and self.form.cleaned_data['new_password']:
FailedLogin.objects.filter(user=self.object).delete()
PasswordHistory.objects.create(user=obj, password=obj.password)
return obj
class Profile(SmartUpdateView):
form_class = UserProfileForm
success_message = "User profile saved successfully."
fields = ('username', 'old_password', 'new_password', 'confirm_new_password',
'first_name', 'last_name', 'email')
field_config = {
'username': dict(readonly=True, label=_("Username")),
'old_password': dict(label=_("Password"), help=_("Your password")),
'new_password': dict(label=_("New Password"), help=_("If you want to set a new password, enter it here")),
'confirm_new_password': dict(label=_("Confirm New Password"), help=_("Confirm your new password")),
}
def post_save(self, obj):
obj = super(UserCRUDL.Profile, self).post_save(obj)
if 'new_password' in self.form.cleaned_data and self.form.cleaned_data['new_password']:
FailedLogin.objects.filter(user=self.object).delete()
PasswordHistory.objects.create(user=obj, password=obj.password)
return FAQ.get_all(self.request.org)
def get_context_data(self, **kwargs):
context = super(FaqCRUDL.Read, self).get_context_data(**kwargs)
edit_button_url = reverse("msgs.faq_update", args=[self.object.pk])
context["context_data_json"] = {"faq": self.object.as_json()}
context["edit_button_url"] = edit_button_url
context["can_delete"] = True
labels = []
for label in self.object.labels.all():
labels.append(label.name)
context["labels"] = ", ".join(labels)
return context
class Update(OrgPermsMixin, SmartUpdateView):
form_class = FaqForm
def get_form_kwargs(self):
kwargs = super(FaqCRUDL.Update, self).get_form_kwargs()
# Get the data for post requests that didn't come through a form
if self.request.method == "POST" and not self.request.POST and hasattr(self.request, "json"):
kwargs["data"] = self.request.json
kwargs["org"] = self.request.org
return kwargs
def derive_initial(self):
initial = super(FaqCRUDL.Update, self).derive_initial()
initial["labels"] = self.object.labels.all()
return initial
def derive_fields(self):
def has_permission(self, request, *args, **kwargs):
return request.user.is_authenticated
def get_object(self, queryset=None):
return self.request.user
def post_save(self, obj):
obj = super(UserCRUDL.Newpassword, self).post_save(obj)
PasswordHistory.objects.create(user=obj, password=obj.password)
return obj
def get_success_url(self):
return settings.LOGIN_REDIRECT_URL
class Mimic(SmartUpdateView):
fields = ('id',)
def derive_success_message(self):
return _("You are now logged in as %s") % self.object.username
def pre_process(self, request, *args, **kwargs):
user = self.get_object()
Login.as_view()(request)
# After logging in it is important to change the user stored in the session
# otherwise the user will remain the same
request.session[auth.SESSION_KEY] = user.id
request.session[auth.HASH_SESSION_KEY] = user.get_session_auth_hash()
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)