diff --git a/kfet/autocomplete.py b/kfet/autocomplete.py index 70293475..eaee1fbd 100644 --- a/kfet/autocomplete.py +++ b/kfet/autocomplete.py @@ -2,43 +2,66 @@ from django.shortcuts import render from django.http import Http404 from django.db.models import Q from gestioncof.models import User, Clipper +from kfet.models import Account -def account_new(request): +def account_create(request): if "q" not in request.GET: raise Http404 q = request.GET.get("q") if (len(q) == 0): - return render(request, "kfet/account_new_autocomplete.html") + return render(request, "kfet/account_create_autocomplete.html") data = {'q': q} queries = {} search_words = q.split() + queries['kfet'] = Account.objects queries['users_cof'] = User.objects.filter(Q(profile__is_cof = True)) queries['users_notcof'] = User.objects.filter(Q(profile__is_cof = False)) queries['clippers'] = Clipper.objects for word in search_words: - queries['users_cof'] = queries['users_cof'].filter( + queries['kfet'] = queries['kfet'].filter( + Q(cofprofile__user__username__icontains = word) + | Q(cofprofile__user__first_name__icontains = word) + | Q(cofprofile__user__last_name__icontains = word) + ) + queries['users_cof'] = queries['users_cof'].filter( Q(username__icontains = word) | Q(first_name__icontains = word) - | Q(last_name__icontains = word)) + | Q(last_name__icontains = word) + ) queries['users_notcof'] = queries['users_notcof'].filter( Q(username__icontains = word) | Q(first_name__icontains = word) - | Q(last_name__icontains = word)) + | Q(last_name__icontains = word) + ) queries['clippers'] = queries['clippers'].filter( Q(username__icontains = word) - | Q(fullname__icontains = word)) + | Q(fullname__icontains = word) + ) - queries['users_cof'] = queries['users_cof'].distinct() - queries['users_notcof'] = queries['users_notcof'].distinct() + queries['kfet'] = queries['kfet'].distinct() + print(queries['kfet']) + + usernames = list( \ + queries['kfet'].values_list('cofprofile__user__username', flat=True)) + + queries['kfet'] = [ (account, account.cofprofile.user) \ + for account in queries['kfet'] ] + + queries['users_cof'] = \ + queries['users_cof'].exclude(username__in=usernames).distinct() + queries['users_notcof'] = \ + queries['users_notcof'].exclude(username__in=usernames).distinct() + + usernames += list( \ + queries['users_cof'].values_list('username', flat=True)) + usernames += list( \ + queries['users_notcof'].values_list('username', flat=True)) - usernames = list(queries['users_cof'].values_list('username', flat=True)) - usernames += list(queries['users_notcof'] \ - .values_list('username', flat=True)) queries['clippers'] = \ queries['clippers'].exclude(username__in=usernames).distinct() @@ -49,4 +72,4 @@ def account_new(request): options += len(query) data['options'] = options - return render(request, "kfet/account_new_autocomplete.html", data) + return render(request, "kfet/account_create_autocomplete.html", data) diff --git a/kfet/forms.py b/kfet/forms.py index 2173e5a2..5328a5f2 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -3,18 +3,32 @@ from django.contrib.auth.models import User from kfet.models import Account from gestioncof.models import CofProfile -class AccountTrigrammeForm(forms.ModelForm): +class AccountForm(forms.ModelForm): + + # Surcharge pour passer data à Account.save() + def save(self, data = {}, *args, **kwargs): + obj = super(AccountForm, self).save(commit = False, *args, **kwargs) + obj.save(data = data) + return obj + class Meta: model = Account - fields = ['trigramme'] + fields = ['trigramme', 'promo', 'nickname'] widgets = { 'trigramme': forms.TextInput(attrs={'autocomplete': 'off'}), } -class AccountForm(forms.ModelForm): - class Meta: - model = Account - fields = ['promo', 'nickname'] +class AccountTriForm(AccountForm): + class Meta(AccountForm.Meta): + fields = ['trigramme'] + +class AccountNoTriForm(AccountForm): + class Meta(AccountForm.Meta): + exclude = ['trigramme'] + +class AccountRestrictForm(AccountForm): + class Meta(AccountForm.Meta): + fields = ['promo'] class CofForm(forms.ModelForm): def clean_is_cof(self): @@ -27,6 +41,10 @@ class CofForm(forms.ModelForm): model = CofProfile fields = ['login_clipper', 'is_cof', 'departement'] +class CofRestrictForm(CofForm): + class Meta(CofForm.Meta): + fields = ['departement'] + class UserForm(forms.ModelForm): class Meta: model = User @@ -34,3 +52,7 @@ class UserForm(forms.ModelForm): help_texts = { 'username': '' } + +class UserRestrictForm(UserForm): + class Meta(UserForm.Meta): + fields = ['first_name', 'last_name', 'email'] diff --git a/kfet/migrations/0002_auto_20160802_2139.py b/kfet/migrations/0002_auto_20160802_2139.py new file mode 100644 index 00000000..0a59de44 --- /dev/null +++ b/kfet/migrations/0002_auto_20160802_2139.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +import datetime + + +class Migration(migrations.Migration): + + dependencies = [ + ('kfet', '0001_initial'), + ] + + operations = [ + migrations.AlterModelOptions( + name='account', + options={'permissions': (('is_team', 'Is part of the team'),)}, + ), + migrations.AlterField( + model_name='accountnegative', + name='start', + field=models.DateTimeField(default=datetime.datetime(2016, 8, 2, 21, 39, 30, 52279)), + ), + ] diff --git a/kfet/migrations/0003_auto_20160802_2142.py b/kfet/migrations/0003_auto_20160802_2142.py new file mode 100644 index 00000000..586146de --- /dev/null +++ b/kfet/migrations/0003_auto_20160802_2142.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +import datetime + + +class Migration(migrations.Migration): + + dependencies = [ + ('kfet', '0002_auto_20160802_2139'), + ] + + operations = [ + migrations.AlterField( + model_name='accountnegative', + name='start', + field=models.DateTimeField(default=datetime.datetime.now), + ), + ] diff --git a/kfet/migrations/0004_auto_20160802_2144.py b/kfet/migrations/0004_auto_20160802_2144.py new file mode 100644 index 00000000..b9e9d0d3 --- /dev/null +++ b/kfet/migrations/0004_auto_20160802_2144.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('kfet', '0003_auto_20160802_2142'), + ] + + operations = [ + migrations.AlterField( + model_name='accountnegative', + name='balance_offset', + field=models.DecimalField(decimal_places=2, max_digits=6, default=0), + ), + ] diff --git a/kfet/migrations/0005_auto_20160802_2154.py b/kfet/migrations/0005_auto_20160802_2154.py new file mode 100644 index 00000000..a01419fc --- /dev/null +++ b/kfet/migrations/0005_auto_20160802_2154.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('kfet', '0004_auto_20160802_2144'), + ] + + operations = [ + migrations.CreateModel( + name='GlobalPermissions', + fields=[ + ('id', models.AutoField(serialize=False, primary_key=True, verbose_name='ID', auto_created=True)), + ], + options={ + 'permissions': (('is_team', 'Is part of the team'),), + 'managed': False, + }, + ), + migrations.AlterModelOptions( + name='account', + options={}, + ), + ] diff --git a/kfet/models.py b/kfet/models.py index 2b2750ab..76310337 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -1,10 +1,11 @@ from django.db import models from django.core.exceptions import PermissionDenied -from django.contrib.auth.models import User +from django.contrib.auth.models import User, AnonymousUser from django.core.validators import RegexValidator from gestioncof.models import CofProfile from django.utils.six.moves import reduce import datetime +import re def choices_length(choices): return reduce(lambda m, choice: max(m, len(choice[0])), choices, 0) @@ -42,36 +43,36 @@ class Account(models.Model): return self.trigramme @staticmethod - def is_free(trigramme): + def is_validandfree(trigramme): + data = { 'is_valid' : False, 'is_free' : False } + pattern = re.compile("^[^a-z]{3}$") + data['is_valid'] = pattern.match(trigramme) and True or False try: - account = Account.objects.filter(trigramme=trigramme).get() - return False + account = Account.objects.get(trigramme=trigramme) except Account.DoesNotExist: - return True + data['is_free'] = True + return data - # Méthode save() avec auth + + def real_balance(self): + if (hasattr(self, 'negative')): + return self.balance + self.negative.balance_offset + return self.balance + + def read(self, auth_user = AnonymousUser()): + user = self.cofprofile.user + + # Surcharge Méthode save() avec gestions de User et CofProfile # Args: - # - auth_user : request.user # - data : datas pour User et CofProfile # Action: # - Enregistre User, CofProfile à partir de "data" # - Enregistre Account - # Permissions - # - Edition si request.user: - # - modifie son compte (ne peut pas modifier nickname) - # ou - a la perm kfet.change_account - # - Ajout si request.user a la perm kfet.add_account - def save_api(self, auth_user, data = None): + def save(self, data = {}, *args, **kwargs): if self.pk: # Account update - # Checking permissions - user = self.cofprofile.user - if not auth_user.has_perm('kfet.change_account') \ - and request.user != user: - raise PermissionDenied - # Updating User with data + user = self.cofprofile.user user.first_name = data.get("first_name", user.first_name) user.last_name = data.get("last_name", user.last_name) user.email = data.get("email", user.email) @@ -81,18 +82,25 @@ class Account(models.Model): cof.departement = data.get("departement", cof.departement) cof.save() # Nickname is not editable by the user + """ if not auth_user.has_perm('kfet.change_account'): account_old = Account.objects.get(pk=self.pk) self.nickname = account_old.nickname + """ else: # New account - # Checking permissions - if not auth_user.has_perm('kfet.add_account'): - raise PermissionDenied + # Checking if user has already an account + username = data.get("username") + try: + user = User.objects.get(username=username) + if hasattr(user.profile, "account_kfet"): + trigramme = user.profile.account_kfet.trigramme + raise Account.UserHasAccount(trigramme) + except User.DoesNotExist: + pass # Creating or updating User instance - username = data.get("username") (user, _) = User.objects.get_or_create(username=username) if "first_name" in data: user.first_name = data['first_name'] @@ -108,11 +116,8 @@ class Account(models.Model): if "departement" in data: cof.departement = data['departement'] cof.save() - # Check if cof is linked to an account - if hasattr(cof, 'account_kfet'): - raise Account.UserHasAccount(cof.account_kfet.trigramme) self.cofprofile = cof - self.save() + super(Account, self).save(*args, **kwargs) # Surcharge de delete # Pas de suppression possible @@ -128,8 +133,10 @@ class AccountNegative(models.Model): account = models.OneToOneField( Account, on_delete = models.PROTECT, related_name = "negative") - start = models.DateTimeField(default = datetime.datetime.now()) - balance_offset = models.DecimalField(max_digits = 6, decimal_places = 2) + start = models.DateTimeField(default = datetime.datetime.now) + balance_offset = models.DecimalField( + max_digits = 6, decimal_places = 2, + default = 0) authorized_overdraft = models.DecimalField( max_digits = 6, decimal_places = 2, default = 0) @@ -346,3 +353,10 @@ class Operation(models.Model): related_name = "addcosts", blank = True, null = True, default = None) addcost_amount = models.DecimalField(max_digits = 6, decimal_places = 2) + +class GlobalPermissions(models.Model): + class Meta: + managed = False + permissions = ( + ('is_team', 'Is part of the team'), + ) diff --git a/kfet/templates/kfet/account.html b/kfet/templates/kfet/account.html new file mode 100644 index 00000000..450d6a94 --- /dev/null +++ b/kfet/templates/kfet/account.html @@ -0,0 +1,17 @@ +{% extends "kfet/base.html" %} + +{% block title %}Liste des comptes{% endblock %} + +{% block content %} + + + +{% endblock %} diff --git a/kfet/templates/kfet/account_create.html b/kfet/templates/kfet/account_create.html new file mode 100644 index 00000000..a130c9c6 --- /dev/null +++ b/kfet/templates/kfet/account_create.html @@ -0,0 +1,82 @@ +{% extends "kfet/base.html" %} +{% load static %} + +{% block title %}Création d'un nouveau compte{% endblock %} + +{% block extra_head %} + + +{% endblock %} + +{% block content %} +

Création d'un nouveau compte

+ +{% if post %} + {% if success %} + Nouveau compte créé : {{ trigramme }} + {% else %} + Echec de la création du compte + {{ errors }} + {% endif %} +
+{% endif %} + +
+ {{ account_trigramme_form }} +
+ +
+
+
+ + +{% endblock %} diff --git a/kfet/templates/kfet/account_new_autocomplete.html b/kfet/templates/kfet/account_create_autocomplete.html similarity index 65% rename from kfet/templates/kfet/account_new_autocomplete.html rename to kfet/templates/kfet/account_create_autocomplete.html index 21700838..70a75f34 100644 --- a/kfet/templates/kfet/account_new_autocomplete.html +++ b/kfet/templates/kfet/account_create_autocomplete.html @@ -2,15 +2,21 @@