Browse Source

refactoring

devel
bashmak 8 years ago
parent
commit
2c6a79b26a
  1. 2
      clientsideapp/templates/clientsideapp/ext.html
  2. 14
      djing/auth_backends.py
  3. 5
      djing/settings.py
  4. 12
      global_context_processors.py

2
clientsideapp/templates/clientsideapp/ext.html

@ -71,7 +71,7 @@
</ul> </ul>
</li> </li>
</ul> </ul>
<span class="navbar-text">Ваш балланс <b>{{ subscriber.ballance|floatformat:2 }}</b> руб.</span>
<span class="navbar-text">Ваш балланс <b>{{ request.user.ballance|floatformat:2 }}</b> руб.</span>
</div><!--/.nav-collapse --> </div><!--/.nav-collapse -->
</div> </div>
</div> </div>

14
djing/auth_backends.py

@ -7,25 +7,16 @@ class CustomAuthBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs): def authenticate(self, request, username=None, password=None, **kwargs):
if username is None: if username is None:
username = kwargs.get(BaseAccount.USERNAME_FIELD) username = kwargs.get(BaseAccount.USERNAME_FIELD)
print('username', username)
try: try:
user = BaseAccount._default_manager.get_by_natural_key(username) user = BaseAccount._default_manager.get_by_natural_key(username)
print('user', user)
if user.check_password(password): if user.check_password(password):
if user.is_admin:
print('is adm')
if user.is_staff:
auser = UserProfile.objects.get_by_natural_key(username) auser = UserProfile.objects.get_by_natural_key(username)
else: else:
print('no adm')
auser = Abon.objects.get_by_natural_key(username) auser = Abon.objects.get_by_natural_key(username)
if self.user_can_authenticate(auser): if self.user_can_authenticate(auser):
print('can auth')
return auser return auser
print('no can auth')
else:
print('wrong password')
except BaseAccount.DoesNotExist: except BaseAccount.DoesNotExist:
print('does not exist')
# Run the default password hasher once to reduce the timing # Run the default password hasher once to reduce the timing
# difference between an existing and a non-existing user (#20760). # difference between an existing and a non-existing user (#20760).
BaseAccount().set_password(password) BaseAccount().set_password(password)
@ -33,11 +24,10 @@ class CustomAuthBackend(ModelBackend):
def get_user(self, user_id): def get_user(self, user_id):
try: try:
user = BaseAccount._default_manager.get(pk=user_id) user = BaseAccount._default_manager.get(pk=user_id)
if user.is_admin:
if user.is_staff:
user = UserProfile._default_manager.get(pk=user_id) user = UserProfile._default_manager.get(pk=user_id)
else: else:
user = Abon._default_manager.get(pk=user_id) user = Abon._default_manager.get(pk=user_id)
except BaseAccount.DoesNotExist: except BaseAccount.DoesNotExist:
return None return None
return user if self.user_can_authenticate(user) else None return user if self.user_can_authenticate(user) else None

5
djing/settings.py

@ -79,12 +79,11 @@ TEMPLATES = [
'APP_DIRS': True, 'APP_DIRS': True,
'OPTIONS': { 'OPTIONS': {
'context_processors': [ 'context_processors': [
'django.template.context_processors.debug',
#'django.template.context_processors.debug',
'django.template.context_processors.request', 'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth', 'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages', 'django.contrib.messages.context_processors.messages',
'taskapp.context_proc.get_active_tasks_count', 'taskapp.context_proc.get_active_tasks_count',
'global_context_processors.context_processor_additional_profile',
'msg_app.context_processors.get_new_messages_count' 'msg_app.context_processors.get_new_messages_count'
], ],
}, },
@ -118,7 +117,7 @@ AUTH_PASSWORD_VALIDATORS = [
}, },
] ]
SESSION_ENGINE = 'django.contrib.sessions.backends.file'
SESSION_ENGINE = 'django.contrib.sessions.backends.db'
SESSION_COOKIE_HTTPONLY = True SESSION_COOKIE_HTTPONLY = True

12
global_context_processors.py

@ -1,12 +0,0 @@
# -*- coding: utf-8 -*-
from django.shortcuts import get_object_or_404
from abonapp.models import Abon
from django.conf import settings
# От сюда можно получать на клиентской стороне профиль абонента
def context_processor_additional_profile(request):
if request.user.is_staff or request.user.is_anonymous():
return {'subscriber': request.user, 'FILE_UPLOAD_MAX_MEMORY_SIZE': settings.FILE_UPLOAD_MAX_MEMORY_SIZE}
else:
return {'subscriber': get_object_or_404(Abon, id=request.user.pk), 'FILE_UPLOAD_MAX_MEMORY_SIZE': settings.FILE_UPLOAD_MAX_MEMORY_SIZE}
Loading…
Cancel
Save