Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# We overload a normal CategoryForm to not call the super's clean method. By default
# model forms will check for integrity checks. We want to force a DB thrown IntegrityError
# so we don't call the super, instead letting smartmin wrap the error
class CategoryForm(forms.ModelForm):
def clean(self):
return self.cleaned_data
class Meta:
model = Category
fields = ('name',)
# just tests that our reverse and permissions are based on the view.py app, not
# the model app, the template should also be /blog/user_list.html for the List view
class UserCRUDL(SmartCRUDL):
model = User
permissions = False
actions = ('list',)
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',
# just tests that our reverse and permissions are based on the view.py app, not
# the model app, the template should also be /blog/user_list.html for the List view
class UserCRUDL(SmartCRUDL):
model = User
permissions = False
actions = ('list',)
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 = []
text = self.request.GET.get("text", None)
contact_id = self.request.GET.get("contact", None)
after = parse_iso8601(self.request.GET.get("after", None))
before = parse_iso8601(self.request.GET.get("before", None))
return {
"folder": folder,
"label": label_id,
"text": text,
"contact": contact_id,
"after": after,
"before": before,
}
class MessageCRUDL(SmartCRUDL):
actions = ("search", "lock", "action", "label", "bulk_reply", "forward", "history")
model = Message
class Search(OrgPermsMixin, MessageSearchMixin, SmartTemplateView):
"""
JSON endpoint for fetching incoming messages
"""
page_size = 50
def get_messages(self, search, last_refresh=None):
org = self.request.org
user = self.request.user
queryset = Message.search(org, user, search, modified_after=last_refresh, all=False)
return queryset.prefetch_related("contact", "labels", "case__assignee", "case__user_assignee")
folder = CaseFolder[params["folder"]]
assignee = params.get("assignee")
user_assignee = params.get("user_assignee")
after = parse_iso8601(params.get("after"))
before = parse_iso8601(params.get("before"))
return {
"folder": folder,
"assignee": assignee,
"user_assignee": user_assignee,
"after": after,
"before": before,
}
class CaseCRUDL(SmartCRUDL):
model = Case
actions = (
"read",
"open",
"update_summary",
"reply",
"fetch",
"search",
"timeline",
"note",
"reassign",
"close",
"reopen",
"label",
"watch",
"unwatch",
def get_form_kwargs(self):
kwargs = super(GroupCRUDL.Select, self).get_form_kwargs()
kwargs["org"] = self.request.user.get_org()
return kwargs
def form_valid(self, form):
selected_ids = form.cleaned_data["groups"]
org_groups = Group.objects.filter(org=self.request.org)
org_groups.filter(pk__in=selected_ids).update(is_visible=True)
org_groups.exclude(pk__in=selected_ids).update(is_visible=False)
return HttpResponseRedirect(self.get_success_url())
class FieldCRUDL(SmartCRUDL):
model = Field
actions = ("list",)
class List(OrgPermsMixin, SmartListView):
"""
Basic list view mostly for debugging
"""
fields = ("key", "label", "value_type", "is_visible")
default_order = "key"
def get_queryset(self, **kwargs):
queryset = super(FieldCRUDL.List, self).get_queryset(**kwargs)
return queryset.filter(org=self.request.org)
from dash.orgs.views import OrgObjPermsMixin, OrgPermsMixin
from smartmin.views import SmartCRUDL, SmartFormView, SmartListView, SmartReadView
from django import forms
from django.conf import settings
from django.http import HttpResponseRedirect, JsonResponse
from django.utils.translation import ugettext_lazy as _
from casepro.cases.models import Case
from casepro.utils import JSONEncoder, json_encode
from .models import Contact, Field, Group
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)
filename = "reply_export.xls"
class FaqSearchMixin(object):
def derive_search(self):
"""
Collects and prepares FAQ search parameters into JSON serializable dict
"""
label = self.request.GET.get("label", None)
text = self.request.GET.get("text", None)
language = self.request.GET.get("language", None)
return {"label": label, "text": text, "language": language}
class FaqCRUDL(SmartCRUDL):
model = FAQ
actions = ("list", "create", "read", "update", "delete", "search", "import", "languages")
class List(OrgPermsMixin, SmartListView):
fields = ("question", "answer", "language", "parent")
default_order = ("-parent", "question")
def derive_queryset(self, **kwargs):
return FAQ.get_all(self.request.org)
class Create(OrgPermsMixin, SmartCreateView):
form_class = FaqForm
def get_form_kwargs(self):
kwargs = super(FaqCRUDL.Create, self).get_form_kwargs()
# Get the data for post requests that didn't come through a form
from dash.orgs.views import OrgPermsMixin
from smartmin.views import SmartCRUDL, SmartListView
from .models import Rule
class RuleCRUDL(SmartCRUDL):
"""
Simple CRUDL for debugging by superusers, i.e. not exposed to regular users for now
"""
model = Rule
actions = ("list",)
class List(OrgPermsMixin, SmartListView):
fields = ("tests", "actions")
def get_queryset(self, **kwargs):
return self.model.objects.filter(org=self.request.org).order_by("id")
if self.cleaned_data['new_password'] != self.cleaned_data['confirm_new_password']:
raise forms.ValidationError(_("Mismatch between your new password and confirmation, try again"))
password = self.cleaned_data['new_password']
if password and not is_password_complex(password):
raise forms.ValidationError(_("Passwords must have at least 8 characters, including one uppercase, "
"one lowercase and one number"))
if password and PasswordHistory.is_password_repeat(self.instance, password):
raise forms.ValidationError(_("You have used this password before in the past year, "
"please use a new password."))
return self.cleaned_data['new_password']
class UserCRUDL(SmartCRUDL):
model = get_user_model()
permissions = True
actions = ('create', 'list', 'update', 'profile', 'forget', 'recover', 'expired', 'failed', 'newpassword', 'mimic')
class List(SmartListView):
search_fields = ('username__icontains', 'first_name__icontains', 'last_name__icontains')
fields = ('username', 'name', 'group', 'last_login')
link_fields = ('username', 'name')
default_order = 'username'
add_button = True
template_name = "smartmin/users/user_list.html"
def get_context_data(self, **kwargs):
context = super(UserCRUDL.List, self).get_context_data(**kwargs)
context['groups'] = Group.objects.all()
group_id = self.request.POST.get('group_id', self.request.GET.get('group_id', 0))
class ReplySearchMixin(object):
def derive_search(self):
"""
Collects and prepares reply search parameters into JSON serializable dict
"""
params = self.request.GET
partner = params.get("partner")
after = parse_iso8601(params.get("after"))
before = parse_iso8601(params.get("before"))
return {"partner": partner, "after": after, "before": before}
class OutgoingCRUDL(SmartCRUDL):
actions = ("search", "search_replies")
model = Outgoing
class Search(OrgPermsMixin, SmartTemplateView):
"""
JSON endpoint for fetching outgoing messages
"""
def derive_search(self):
folder = OutgoingFolder[self.request.GET["folder"]]
text = self.request.GET.get("text", None)
contact = self.request.GET.get("contact", None)
return {"folder": folder, "text": text, "contact": contact}
def get_context_data(self, **kwargs):