Commit 78197505 authored by Patrick's avatar Patrick

Code clean up and use a boolean if user is admin

parent 92861859
# -*- coding: utf-8
from __future__ import unicode_literals
import re
from django import forms
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import Permission
from django.contrib.auth.models import User
from django.db.models import F, Q
from django.utils import translation
......@@ -13,8 +11,6 @@ from django.utils.translation import ugettext_lazy as _
from const import DECIMAL_ZERO, DECIMAL_ONE, DECIMAL_THREE
from models import Customer, Staff, Configuration
ACTION_RE = re.compile(r"(?<=\.)(.*?)(?=\_)")
class RepanierCustomBackend(ModelBackend):
user = None
......@@ -25,7 +21,8 @@ class RepanierCustomBackend(ModelBackend):
def authenticate(self, username=None, password=None, confirm=None, **kwargs):
self.user = None
user_username = User.objects.filter(Q(username=username[:30]) | Q(email=username)).order_by('?').first()
admin = staff = customer = None
is_admin = False
staff = customer = None
login_attempt_counter = DECIMAL_THREE
if user_username is not None:
username = user_username.username
......@@ -37,13 +34,13 @@ class RepanierCustomBackend(ModelBackend):
user=user_username, is_active=True
).order_by('?').first()
if staff is None:
is_admin = True
login_attempt_counter = Configuration.objects.filter(
id=DECIMAL_ONE
).only(
'login_attempt_counter'
).first().login_attempt_counter
else:
admin = True
login_attempt_counter = staff.login_attempt_counter
else:
login_attempt_counter = customer.login_attempt_counter
......@@ -64,7 +61,7 @@ class RepanierCustomBackend(ModelBackend):
login_attempt_counter=F('login_attempt_counter') +
DECIMAL_ONE
)
elif admin is not None:
elif is_admin:
Configuration.objects.filter(id=DECIMAL_ONE).update(
login_attempt_counter=F('login_attempt_counter') +
DECIMAL_ONE
......@@ -100,7 +97,7 @@ class RepanierCustomBackend(ModelBackend):
Staff.objects.filter(id=staff.id).update(
login_attempt_counter=DECIMAL_ZERO
)
elif admin is not None:
elif is_admin:
if login_attempt_counter > DECIMAL_ZERO:
Configuration.objects.filter(id=DECIMAL_ONE).update(
login_attempt_counter=DECIMAL_ZERO
......@@ -114,14 +111,12 @@ class RepanierCustomBackend(ModelBackend):
return self.user
user_or_none = User.objects.filter(pk=user_id).only("id", "is_superuser").order_by('?').first()
if user_or_none is not None and not user_or_none.is_superuser:
a = Customer.objects.filter(user_id=user_or_none.id) \
.only("is_active").order_by('?').first()
a = Customer.objects.filter(user_id=user_or_none.id).only("is_active").order_by('?').first()
if a is not None:
if not a.is_active:
user_or_none = None
else:
a = Staff.objects.filter(user_id=user_or_none.id) \
.only("is_active").order_by('?').first()
a = Staff.objects.filter(user_id=user_or_none.id).only("is_active").order_by('?').first()
if a is not None:
if not a.is_active:
user_or_none = None
......@@ -130,14 +125,3 @@ class RepanierCustomBackend(ModelBackend):
self.user = user_or_none
return user_or_none
# def has_perm(self, user_obj, perm, obj=None):
# # if perm.startswith('dummy_cmsplugin_cascade'):
# if perm.startswith('cascade_dummy'):
# codename = perm.split('.', 1)[1]
# if not Permission.objects.filter(codename=codename).exists():
# perm_splitted = codename.split('_', 1)
# # codename = '%s_bootstrapcontainerpluginmodel' % perm_splitted[0]
# codename = '%s_cascadeelement' % perm_splitted[0]
# perm = 'cmsplugin_cascade.%s' % codename
# return super(RepanierCustomBackend, self).has_perm(user_obj, perm, obj)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment