Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚗 Added auto-login for staff #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 49 additions & 42 deletions langate/langate/insalan_auth/insalan_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,48 +107,55 @@ def authenticate(self, request: HttpRequest, username: str = None,
try:
json_result = request_result.json()
paid_status = json_result["err"]
if paid_status == "registration_not_found":
# The insalan.fr account exists, but the player
# is not registered during the event
raise ValidationError(
"The account exists, but this player is not registered.")
elif paid_status == "no_paid_place":
# The player is registered but has not paid
raise ValidationError(
"This player is registered but has not paid.")
else:
# The player has paid, we can return the object
email = json_result["user"]["email"]
first_name = json_result["user"]["first_name"]
last_name = json_result["user"]["last_name"]

with transaction.atomic():

# If the user is already registered, return the existing entry
if User.objects.filter(username=username).exists():
return User.objects.get(username=username)

user = User.objects.create(
username=username,
email=email,
password=password,
first_name=first_name,
last_name=last_name)

for tournament in json_result["tournaments"]:
if tournament["has_paid"]:
short_name = tournament["shortname"] if "shortname" in tournament else None
is_manager = tournament["manager"] if "manager" in tournament else False

user.profile.tournament = self.short_name_to_tournament_enum(short_name)
user.profile.team = tournament["team"] if "team" in tournament else None

if is_manager:
user.profile.role = Role.M.value

break
user.save()
return user
is_staff = "is_staff" in json_result and json_result["is_staff"]

if not is_staff:
if paid_status == "registration_not_found":
# The insalan.fr account exists, but the player
# is not registered during the event
raise ValidationError(
"The account exists, but this player is not registered.")
elif paid_status == "no_paid_place":
# The player is registered but has not paid
raise ValidationError(
"This player is registered but has not paid.")

# The player has paid, we can return the object
email = json_result["user"]["email"]
first_name = json_result["user"]["first_name"]
last_name = json_result["user"]["last_name"]

with transaction.atomic():

# If the user is already registered, return the existing entry
if User.objects.filter(username=username).exists():
return User.objects.get(username=username)

user = User.objects.create(
username=username,
email=email,
password=password,
first_name=first_name,
last_name=last_name)

for tournament in json_result["tournaments"]:
if tournament["has_paid"]:
short_name = tournament["shortname"] if "shortname" in tournament else None
is_manager = tournament["manager"] if "manager" in tournament else False

user.profile.tournament = self.short_name_to_tournament_enum(short_name)
user.profile.team = tournament["team"] if "team" in tournament else None

if is_manager:
user.profile.role = Role.M.value

break

if is_staff:
user.profile.role = Role.S.value

user.save()
return user

except ValidationError as e:
# Any validation error is rethrown
Expand Down