forked from DGNum/gestioCOF
150 lines
6.1 KiB
Python
150 lines
6.1 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
from django.core.exceptions import PermissionDenied
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
|
from django.contrib.auth.models import User
|
|
from django.http import HttpResponse, Http404
|
|
import json
|
|
from gestioncof.models import CofProfile, Clipper
|
|
from kfet.models import Account
|
|
from kfet.forms import AccountTrigrammeForm, AccountForm, CofForm, UserForm
|
|
|
|
@login_required
|
|
def home(request):
|
|
return render(request, "kfet/base.html")
|
|
|
|
def put_cleaned_data_in_dict(dict, form):
|
|
for field in form.cleaned_data:
|
|
dict[field] = form.cleaned_data[field]
|
|
|
|
@login_required
|
|
@permission_required('kfet.add_account')
|
|
def account_new(request):
|
|
# A envoyer au template
|
|
data_template = {
|
|
'account_trigramme_form': AccountTrigrammeForm(),
|
|
'post' : False,
|
|
'success' : False,
|
|
'trigramme' : '',
|
|
'errors' : {},
|
|
}
|
|
|
|
# Enregistrement
|
|
if request.method == "POST":
|
|
# Pour indiquer la tentative d'enregistrement au template
|
|
data_template['post'] = True
|
|
|
|
# Peuplement des forms
|
|
username = request.POST.get('username')
|
|
try:
|
|
user = User.objects.filter(username=username).get()
|
|
(cof, _) = CofProfile.objects.get_or_create(user=user)
|
|
user_form = UserForm(request.POST, instance=user)
|
|
cof_form = CofForm(request.POST, instance=cof)
|
|
except User.DoesNotExist:
|
|
user_form = UserForm(request.POST)
|
|
cof_form = CofForm(request.POST)
|
|
trigramme_form = AccountTrigrammeForm(request.POST)
|
|
account_form = AccountForm(request.POST)
|
|
# Ajout des erreurs pour le template
|
|
data_template['errors']['user_form'] = user_form.errors
|
|
data_template['errors']['cof_form'] = cof_form.errors
|
|
data_template['errors']['trigramme_form'] = trigramme_form.errors
|
|
data_template['errors']['account_form'] = account_form.errors
|
|
|
|
if all((user_form.is_valid(), cof_form.is_valid(),
|
|
trigramme_form.is_valid(), account_form.is_valid())):
|
|
print(user_form.cleaned_data)
|
|
data = {}
|
|
# Fill data
|
|
put_cleaned_data_in_dict(data, user_form)
|
|
put_cleaned_data_in_dict(data, cof_form)
|
|
|
|
account = Account()
|
|
account.trigramme = trigramme_form.cleaned_data['trigramme']
|
|
account.promo = account_form.cleaned_data['promo']
|
|
account.nickname = account_form.cleaned_data['nickname']
|
|
try:
|
|
account.save_api(auth_user = request.user, data = data)
|
|
data_template['success'] = True
|
|
data_template['trigramme'] = account.trigramme
|
|
except Account.UserHasAccount as e:
|
|
data_template['errors']['global'] = \
|
|
"Cet utilisateur a déjà un compte K-Fêt : " + e.trigramme
|
|
except PermissionDenied:
|
|
print("perm")
|
|
|
|
return render(request, "kfet/account_new.html", data_template)
|
|
|
|
def account_new_set_readonly_fields(user_form, cof_form):
|
|
user_form.fields['username'].widget.attrs['readonly'] = True
|
|
cof_form.fields['login_clipper'].widget.attrs['readonly'] = True
|
|
cof_form.fields['is_cof'].widget.attrs['disabled'] = True
|
|
|
|
def account_new_ajax(request, username=None, login_clipper=None):
|
|
user = None
|
|
if login_clipper:
|
|
# à partir d'un clipper
|
|
# le user associé à ce clipper ne devrait pas encore existé
|
|
clipper = get_object_or_404(Clipper, username = login_clipper)
|
|
try:
|
|
# Vérification que clipper ne soit pas déjà dans User
|
|
user = User.objects.filter(username=login_clipper).get()
|
|
# Ici, on nous a menti, le user existe déjà
|
|
username = user.username
|
|
login_clipper = None
|
|
except User.DoesNotExist:
|
|
# Clipper (sans user déjà existant)
|
|
|
|
# UserForm - Prefill + Création
|
|
user_initial_data = {
|
|
'username' : login_clipper,
|
|
'email' : login_clipper + "@clipper.ens.fr"}
|
|
if clipper.fullname:
|
|
# Prefill du nom et prénom
|
|
names = clipper.fullname.split()
|
|
# Le premier, c'est le prénom
|
|
user_initial_data['first_name'] = names[0]
|
|
if len(names) > 1:
|
|
# Si d'autres noms -> tous dans le nom de famille
|
|
user_initial_data['last_name'] = " ".join(names[1:])
|
|
user_form = UserForm(initial = user_initial_data)
|
|
|
|
# CofForm - Prefill + Création
|
|
cof_initial_data = { 'login_clipper': login_clipper }
|
|
cof_form = CofForm(initial = cof_initial_data)
|
|
|
|
# AccountForm
|
|
account_form = AccountForm()
|
|
|
|
# Protection (read-only) des champs username et login_clipper
|
|
account_new_set_readonly_fields(user_form, cof_form)
|
|
if username:
|
|
# le user existe déjà
|
|
user = get_object_or_404(User, username=username)
|
|
# récupération du profil cof
|
|
(cof, _) = CofProfile.objects.get_or_create(user=user)
|
|
# UserForm + CofForm - Création à partir des instances existantes
|
|
user_form = UserForm(instance = user)
|
|
cof_form = CofForm(instance = cof)
|
|
# AccountForm
|
|
account_form = AccountForm()
|
|
# Protection (read-only) des champs username et login_clipper
|
|
account_new_set_readonly_fields(user_form, cof_form)
|
|
elif not login_clipper:
|
|
# connaît pas du tout, faut tout remplir
|
|
user_form = UserForm()
|
|
cof_form = CofForm()
|
|
account_form = AccountForm()
|
|
|
|
return render(request, "kfet/account_new_form.html", {
|
|
'account_form' : account_form,
|
|
'cof_form' : cof_form,
|
|
'user_form' : user_form,
|
|
})
|
|
|
|
def account_is_free_ajax(request):
|
|
if not request.GET.get("trigramme"):
|
|
raise Http404
|
|
trigramme = request.GET.get("trigramme")
|
|
data = { 'is_free': Account.is_free(trigramme) }
|
|
return HttpResponse(json.dumps(data), content_type = 'application/json')
|