2018-01-19 17:48:00 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
|
2018-10-21 20:19:31 +02:00
|
|
|
from gestioncof.petits_cours_models import (
|
|
|
|
PetitCoursAbility,
|
|
|
|
PetitCoursAttributionCounter,
|
|
|
|
PetitCoursDemande,
|
|
|
|
PetitCoursSubject,
|
|
|
|
)
|
|
|
|
|
2018-01-19 17:48:00 +01:00
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
def _create_user(username, is_cof=False, is_staff=False, attrs=None):
|
|
|
|
if attrs is None:
|
|
|
|
attrs = {}
|
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
password = attrs.pop("password", username)
|
2018-01-19 17:48:00 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
user_keys = ["first_name", "last_name", "email", "is_staff", "is_superuser"]
|
2018-01-19 17:48:00 +01:00
|
|
|
user_attrs = {k: v for k, v in attrs.items() if k in user_keys}
|
|
|
|
|
|
|
|
profile_keys = [
|
2018-10-06 12:35:49 +02:00
|
|
|
"is_cof",
|
|
|
|
"login_clipper",
|
|
|
|
"phone",
|
|
|
|
"occupation",
|
|
|
|
"departement",
|
|
|
|
"type_cotiz",
|
|
|
|
"mailing_cof",
|
|
|
|
"mailing_bda",
|
|
|
|
"mailing_bda_revente",
|
|
|
|
"comments",
|
|
|
|
"is_buro",
|
|
|
|
"petit_cours_accept",
|
|
|
|
"petit_cours_remarques",
|
2018-01-19 17:48:00 +01:00
|
|
|
]
|
|
|
|
profile_attrs = {k: v for k, v in attrs.items() if k in profile_keys}
|
|
|
|
|
|
|
|
if is_cof:
|
2018-10-06 12:35:49 +02:00
|
|
|
profile_attrs["is_cof"] = True
|
2018-01-19 17:48:00 +01:00
|
|
|
|
|
|
|
if is_staff:
|
|
|
|
# At the moment, admin is accessible by COF staff.
|
2018-10-06 12:35:49 +02:00
|
|
|
user_attrs["is_staff"] = True
|
|
|
|
profile_attrs["is_buro"] = True
|
2018-01-19 17:48:00 +01:00
|
|
|
|
|
|
|
user = User(username=username, **user_attrs)
|
|
|
|
user.set_password(password)
|
|
|
|
user.save()
|
|
|
|
|
|
|
|
for k, v in profile_attrs.items():
|
|
|
|
setattr(user.profile, k, v)
|
|
|
|
user.profile.save()
|
|
|
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def create_user(username, attrs=None):
|
|
|
|
return _create_user(username, attrs=attrs)
|
|
|
|
|
|
|
|
|
|
|
|
def create_member(username, attrs=None):
|
|
|
|
return _create_user(username, is_cof=True, attrs=attrs)
|
|
|
|
|
|
|
|
|
|
|
|
def create_staff(username, attrs=None):
|
|
|
|
return _create_user(username, is_cof=True, is_staff=True, attrs=attrs)
|
2018-01-22 21:38:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_root(username, attrs=None):
|
|
|
|
if attrs is None:
|
|
|
|
attrs = {}
|
2018-10-06 12:35:49 +02:00
|
|
|
attrs.setdefault("is_staff", True)
|
|
|
|
attrs.setdefault("is_superuser", True)
|
2018-01-22 21:38:01 +01:00
|
|
|
return _create_user(username, attrs=attrs)
|
2018-10-21 20:19:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def create_petitcours_ability(**kwargs):
|
|
|
|
if "user" not in kwargs:
|
|
|
|
kwargs["user"] = create_user()
|
|
|
|
if "matiere" not in kwargs:
|
|
|
|
kwargs["matiere"] = create_petitcours_subject()
|
|
|
|
if "niveau" not in kwargs:
|
|
|
|
kwargs["niveau"] = "college"
|
|
|
|
ability = PetitCoursAbility.objects.create(**kwargs)
|
|
|
|
PetitCoursAttributionCounter.get_uptodate(ability.user, ability.matiere)
|
|
|
|
return ability
|
|
|
|
|
|
|
|
|
|
|
|
def create_petitcours_demande(**kwargs):
|
|
|
|
return PetitCoursDemande.objects.create(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def create_petitcours_subject(**kwargs):
|
|
|
|
return PetitCoursSubject.objects.create(**kwargs)
|