Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return super(UserCRUDL.Recover, self).pre_process(request, args, kwargs)
def get_object(self, queryset=None):
token = self.kwargs.get('token')
recovery_token = RecoveryToken.objects.get(token=token)
return recovery_token.user
def post_save(self, obj):
obj = super(UserCRUDL.Recover, self).post_save(obj)
validity_time = timezone.now() - timedelta(hours=48)
RecoveryToken.objects.filter(user=obj).delete()
RecoveryToken.objects.filter(created_on__lt=validity_time).delete()
PasswordHistory.objects.create(user=obj, password=obj.password)
return obj
class Expired(SmartView, TemplateView):
permission = None
template_name = 'smartmin/users/user_expired.html'
class Failed(SmartView, TemplateView):
permission = None
template_name = 'smartmin/users/user_failed.html'
def get_context_data(self, *args, **kwargs):
context = super(UserCRUDL.Failed, self).get_context_data(*args, **kwargs)
lockout_timeout = getattr(settings, 'USER_LOCKOUT_TIMEOUT', 10)
failed_login_limit = getattr(settings, 'USER_FAILED_LOGIN_LIMIT', 5)
allow_email_recovery = getattr(settings, 'USER_ALLOW_EMAIL_RECOVERY', True)
context['lockout_timeout'] = lockout_timeout
context['failed_login_limit'] = failed_login_limit
def form_valid(self, form):
try:
self.execute_action()
except forms.ValidationError as e:
# turns out we aren't valid after all, stuff our error into our form
self.form.add_error(None, e)
return self.form_invalid(form)
# all went well, stuff our success message in and return
messages.success(self.request, self.derive_success_message())
return super(SmartModelActionView, self).form_valid(form)
class SmartMultiFormView(SmartView, TemplateView):
default_template = 'smartmin/multi_form.html'
forms = {}
# allows you to specify the name of URL to use for a remove link that will automatically be shown
delete_url = None
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
page_forms = []
for prefix, form in self.forms.items():
f = form(prefix=prefix)
page_forms.append(f)
context['forms'] = page_forms
return context
def render_to_response(self, context, **response_kwargs):
"""
Overloaded to deal with _format arguments.
"""
# should we actually render in json?
if '_format' in self.request.GET and self.request.GET['_format'] == 'json':
return JsonResponse(self.as_json(context), safe=False)
# otherwise, return normally
else:
return super(SmartView, self).render_to_response(context)
class SmartTemplateView(SmartView, TemplateView):
pass
def derive_single_object_url_pattern(slug_url_kwarg, path, action):
"""
Utility function called by class methods for single object views
"""
if slug_url_kwarg:
return r'^%s/%s/(?P<%s>[^/]+)/$' % (path, action, slug_url_kwarg)
else:
return r'^%s/%s/(?P\d+)/$' % (path, action)
class SmartSingleObjectView(SmartView):
slug_field = None
slug_url_kwarg = None
kwargs['initial'] = self.derive_initial()
return kwargs
def derive_submit_button_name(self):
"""
Returns the name for our button
"""
return self.submit_button_name
def get_context_data(self, **kwargs):
context = super(SmartFormMixin, self).get_context_data(**kwargs)
context['submit_button_name'] = self.derive_submit_button_name()
return context
class SmartFormView(SmartFormMixin, SmartView, FormView):
default_template = 'smartmin/form.html'
def form_valid(self, form):
# plug in our success message
messages.success(self.request, self.derive_success_message())
return super(SmartFormView, self).form_valid(form)
class SmartModelFormView(SmartFormMixin, SmartSingleObjectView, ModelFormMixin):
javascript_submit = None
field_config = {'modified_blurb': dict(label="Modified"), 'created_blurb': dict(label="Created")}
def derive_title(self):
"""
Derives our title from our object
recovery_token = RecoveryToken.objects.get(token=token)
return recovery_token.user
def post_save(self, obj):
obj = super(UserCRUDL.Recover, self).post_save(obj)
validity_time = timezone.now() - timedelta(hours=48)
RecoveryToken.objects.filter(user=obj).delete()
RecoveryToken.objects.filter(created_on__lt=validity_time).delete()
PasswordHistory.objects.create(user=obj, password=obj.password)
return obj
class Expired(SmartView, TemplateView):
permission = None
template_name = 'smartmin/users/user_expired.html'
class Failed(SmartView, TemplateView):
permission = None
template_name = 'smartmin/users/user_failed.html'
def get_context_data(self, *args, **kwargs):
context = super(UserCRUDL.Failed, self).get_context_data(*args, **kwargs)
lockout_timeout = getattr(settings, 'USER_LOCKOUT_TIMEOUT', 10)
failed_login_limit = getattr(settings, 'USER_FAILED_LOGIN_LIMIT', 5)
allow_email_recovery = getattr(settings, 'USER_ALLOW_EMAIL_RECOVERY', True)
context['lockout_timeout'] = lockout_timeout
context['failed_login_limit'] = failed_login_limit
context['allow_email_recovery'] = allow_email_recovery
return context
def render_to_response(self, context, **response_kwargs):
"""
Overloaded to deal with _format arguments.
"""
# should we actually render in json?
if '_format' in self.request.GET and self.request.GET['_format'] == 'json':
return JsonResponse(self.as_json(context), safe=False)
# otherwise, return normally
else:
return super(SmartView, self).render_to_response(context)
def get_redirect_url(self, **kwargs):
if not self.redirect_url:
raise ImproperlyConfigured("DeleteView must define a redirect_url")
return smart_url(self.redirect_url)
def get_context_data(self, **kwargs):
""" Add in the field to use for the name field """
context = super(SmartDeleteView, self).get_context_data(**kwargs)
context['name_field'] = self.name_field
context['cancel_url'] = self.get_cancel_url()
return context
class SmartListView(SmartView, ListView):
default_template = 'smartmin/list.html'
link_url = None
link_fields = None
add_button = None
search_fields = None
paginate_by = 25
field_config = {'is_active': dict(label='')}
default_order = None
select_related = None
@classmethod
def derive_url_pattern(cls, path, action):
if action == 'list':
return r'^%s/$' % (path)
else:
class SmartTemplateView(SmartView, TemplateView):
pass
def derive_single_object_url_pattern(slug_url_kwarg, path, action):
"""
Utility function called by class methods for single object views
"""
if slug_url_kwarg:
return r'^%s/%s/(?P<%s>[^/]+)/$' % (path, action, slug_url_kwarg)
else:
return r'^%s/%s/(?P\d+)/$' % (path, action)
class SmartSingleObjectView(SmartView):
slug_field = None
slug_url_kwarg = None
def get_slug_field(self):
"""
If `slug_field` isn't specified it defaults to `slug_url_kwarg`
"""
return self.slug_field if self.slug_field else self.slug_url_kwarg
class SmartReadView(SmartSingleObjectView, DetailView):
default_template = 'smartmin/read.html'
edit_button = None
field_config = {'modified_blurb': dict(label="Modified"),
'created_blurb': dict(label="Created")}