Merge branch 'Kerl/fix_generic_team_ath' into 'supportBDS'

Change CofProfile to Profile in kfet/backends.py

- K-Fêt accounts are now linked to profiles
- There is no need to perform the `get_or_create` as long as the profile
  creation has been automated.
- This file is now PEP8 compliant

See merge request !180
This commit is contained in:
Martin Pepin 2017-02-23 12:31:55 +01:00
commit 4b4d570e07

View file

@ -1,15 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import (absolute_import, division,
print_function, unicode_literals)
from builtins import *
import hashlib import hashlib
from django.contrib.auth.models import User, Permission from django.contrib.auth.models import User, Permission
from cof.models import CofProfile
from kfet.models import Account, GenericTeamToken from kfet.models import Account, GenericTeamToken
class KFetBackend(object): class KFetBackend(object):
def authenticate(self, request): def authenticate(self, request):
password = request.POST.get('KFETPASSWORD', '') password = request.POST.get('KFETPASSWORD', '')
@ -17,8 +13,8 @@ class KFetBackend(object):
if not password: if not password:
return None return None
password_sha256 = hashlib.sha256(password.encode('utf-8')).hexdigest()
try: try:
password_sha256 = hashlib.sha256(password.encode('utf-8')).hexdigest()
account = Account.objects.get(password=password_sha256) account = Account.objects.get(password=password_sha256)
user = account.profile.user user = account.profile.user
except Account.DoesNotExist: except Account.DoesNotExist:
@ -26,16 +22,20 @@ class KFetBackend(object):
return user return user
class GenericTeamBackend(object): class GenericTeamBackend(object):
"""
Authenticate using the generic_team user.
"""
def authenticate(self, username=None, token=None): def authenticate(self, username=None, token=None):
valid_token = GenericTeamToken.objects.get(token=token) valid_token = GenericTeamToken.objects.get(token=token)
if username == 'kfet_genericteam' and valid_token: if username == 'kfet_genericteam' and valid_token:
# Création du user s'il n'existe pas déjà # Création du user s'il n'existe pas déjà
user, _ = User.objects.get_or_create(username='kfet_genericteam') user, _ = User.objects.get_or_create(username='kfet_genericteam')
profile, _ = CofProfile.objects.get_or_create(user=user)
account, _ = Account.objects.get_or_create( account, _ = Account.objects.get_or_create(
profile=profile, profile=user.profile,
trigramme='GNR') trigramme='GNR'
)
# Ajoute la permission kfet.is_team à ce user # Ajoute la permission kfet.is_team à ce user
perm_is_team = Permission.objects.get(codename='is_team') perm_is_team = Permission.objects.get(codename='is_team')