Reaching a point where I can query /k-fet.

Edit forms and views in app kfet to make the depend on gestion.Profile and not
on cof.CofProfile.
This commit is contained in:
Michele Orrù 2017-02-11 15:07:45 +01:00
parent f50ef1d51a
commit 376e829502
6 changed files with 54 additions and 40 deletions

View file

@ -237,7 +237,7 @@ class SurveyAnswer(models.Model):
self.survey.title) self.survey.title)
@python_2_unicode_compatible #XXX. this needs to be removed according to Martin
class Clipper(models.Model): class Clipper(models.Model):
username = models.CharField("Identifiant", max_length=20) username = models.CharField("Identifiant", max_length=20)
fullname = models.CharField("Nom complet", max_length=200) fullname = models.CharField("Nom complet", max_length=200)

View file

@ -5,8 +5,10 @@ when you run "manage.py test".
Replace this with more appropriate tests for your application. Replace this with more appropriate tests for your application.
""" """
from unittest.mock import patch
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
from django.test import TestCase from django.test import Client, TestCase
from gestion.models import Profile, User from gestion.models import Profile, User
@ -58,3 +60,12 @@ class SimpleTest(TestCase):
pkfet2 = Account.objects.create(profile=p2, trigramme='BAZ') pkfet2 = Account.objects.create(profile=p2, trigramme='BAZ')
self.assertRaises(IntegrityError, self.assertRaises(IntegrityError,
CofProfile.objects.create, profile=p2) CofProfile.objects.create, profile=p2)
class TestProfile(TestCase):
@patch('kfet.signals.messages')
def test_profile(self, mock_messages):
root = User.objects.create_superuser('root', 'foo@bar.com', 'root')
c = Client()
c.login(username='root', password='root')
c.get('/k-fet')

View file

@ -26,8 +26,8 @@ def account_create(request):
search_words = q.split() search_words = q.split()
queries['kfet'] = Account.objects queries['kfet'] = Account.objects
queries['users_cof'] = User.objects.filter(Q(profile__is_cof = True)) queries['users_cof'] = User.objects.filter(Q(profile__cof__is_cof = True))
queries['users_notcof'] = User.objects.filter(Q(profile__is_cof = False)) queries['users_notcof'] = User.objects.filter(Q(profile__cof__is_cof = False))
queries['clippers'] = Clipper.objects queries['clippers'] = Clipper.objects
for word in search_words: for word in search_words:

View file

@ -16,7 +16,7 @@ from django.utils import timezone
from kfet.models import (Account, Checkout, Article, OperationGroup, Operation, from kfet.models import (Account, Checkout, Article, OperationGroup, Operation,
CheckoutStatement, ArticleCategory, Settings, AccountNegative, Transfer, CheckoutStatement, ArticleCategory, Settings, AccountNegative, Transfer,
TransferGroup, Supplier, Inventory, InventoryArticle) TransferGroup, Supplier, Inventory, InventoryArticle)
from cof.models import CofProfile from gestion.models import Profile
# ----- # -----
# Widgets # Widgets
@ -91,19 +91,21 @@ class AccountPwdForm(forms.Form):
raise ValidationError("Les mots de passes sont différents") raise ValidationError("Les mots de passes sont différents")
super(AccountPwdForm, self).clean() super(AccountPwdForm, self).clean()
class CofForm(forms.ModelForm): class ProfileForm(forms.ModelForm):
def clean_is_cof(self): # def clean_is_cof(self):
instance = getattr(self, 'instance', None) # instance = getattr(self, 'instance', None)
if instance and instance.pk: # if instance and instance.pk:
return instance.is_cof # return instance.is_cof
else: # else:
return False # return False
class Meta:
model = CofProfile
fields = ['login_clipper', 'is_cof', 'departement']
class CofRestrictForm(CofForm): class Meta:
class Meta(CofForm.Meta): model = Profile
# XXX. I am removing is_cof from here.
fields = ['login_clipper', 'departement']
class ProfileRestrictForm(ProfileForm):
class Meta(ProfileForm.Meta):
fields = ['departement'] fields = ['departement']
class UserForm(forms.ModelForm): class UserForm(forms.ModelForm):

View file

@ -81,7 +81,7 @@ class Account(models.Model):
return self.profile.departement return self.profile.departement
@property @property
def is_cof(self): def is_cof(self):
return self.profile.is_cof return self.profile.cof.is_cof
# Propriétés supplémentaires # Propriétés supplémentaires
@property @property

View file

@ -3,6 +3,11 @@
from __future__ import (absolute_import, division, from __future__ import (absolute_import, division,
print_function, unicode_literals) print_function, unicode_literals)
from builtins import * from builtins import *
from collections import defaultdict
from datetime import timedelta
import hashlib
import heapq
import statistics
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
from django.core.exceptions import PermissionDenied, ValidationError from django.core.exceptions import PermissionDenied, ValidationError
@ -22,19 +27,15 @@ from django.db.models import F, Sum, Prefetch, Count, Func
from django.db.models.functions import Coalesce from django.db.models.functions import Coalesce
from django.utils import timezone from django.utils import timezone
from django.utils.crypto import get_random_string from django.utils.crypto import get_random_string
from cof.models import CofProfile, Clipper from gestion.models import Profile
from cof.models import Clipper
from kfet.decorators import teamkfet_required from kfet.decorators import teamkfet_required
from kfet.models import (Account, Checkout, Article, Settings, AccountNegative, from kfet.models import (Account, Checkout, Article, Settings, AccountNegative,
CheckoutStatement, GenericTeamToken, Supplier, SupplierArticle, Inventory, CheckoutStatement, GenericTeamToken, Supplier, SupplierArticle, Inventory,
InventoryArticle, Order, OrderArticle) InventoryArticle, Order, OrderArticle)
from kfet.forms import * from kfet.forms import *
from collections import defaultdict
from kfet import consumers from kfet import consumers
from datetime import timedelta
import django_cas_ng import django_cas_ng
import hashlib
import heapq
import statistics
@login_required @login_required
def home(request): def home(request):
@ -43,7 +44,7 @@ def home(request):
@teamkfet_required @teamkfet_required
def login_genericteam(request): def login_genericteam(request):
# Check si besoin de déconnecter l'utilisateur de CAS # Check si besoin de déconnecter l'utilisateur de CAS
profile, _ = CofProfile.objects.get_or_create(user=request.user) profile, _ = Profile.objects.get_or_create(user=request.user)
need_cas_logout = False need_cas_logout = False
if profile.login_clipper: if profile.login_clipper:
need_cas_logout = True need_cas_logout = True
@ -250,34 +251,34 @@ def get_account_create_forms(request=None, username=None, login_clipper=None):
if len(names) > 1: if len(names) > 1:
# Si d'autres noms -> tous dans le nom de famille # Si d'autres noms -> tous dans le nom de famille
user_initial['last_name'] = " ".join(names[1:]) user_initial['last_name'] = " ".join(names[1:])
# CofForm - Prefill # ProfileForm - Prefill
cof_initial = { 'login_clipper': login_clipper } profile_initial = { 'login_clipper': login_clipper }
# Form créations # Form créations
if request: if request:
user_form = UserForm(request.POST, initial=user_initial, from_clipper=True) user_form = UserForm(request.POST, initial=user_initial, from_clipper=True)
cof_form = CofForm(request.POST, initial=cof_initial) profile_form = ProfileForm(request.POST, initial=profile_initial)
else: else:
user_form = UserForm(initial=user_initial, from_clipper=True) user_form = UserForm(initial=user_initial, from_clipper=True)
cof_form = CofForm(initial=cof_initial) profile_form = ProfileForm(initial=cof_initial)
# Protection (read-only) des champs username et login_clipper # Protection (read-only) des champs username et login_clipper
account_form_set_readonly_fields(user_form, cof_form) account_form_set_readonly_fields(user_form, profile_form)
if username and not clipper: if username and not clipper:
try: try:
user = User.objects.get(username=username) user = User.objects.get(username=username)
# le user existe déjà # le user existe déjà
# récupération du profil cof # récupération du profil cof
(cof, _) = CofProfile.objects.get_or_create(user=user) profile, _ = Profile.objects.get_or_create(user=user)
# UserForm + CofForm - Création à partir des instances existantes # UserForm + ProfileForm - Création à partir des instances existantes
if request: if request:
user_form = UserForm(request.POST, instance = user) user_form = UserForm(request.POST, instance = user)
cof_form = CofForm(request.POST, instance = cof) profile_form = ProfileForm(request.POST, instance = profile)
else: else:
user_form = UserForm(instance=user) user_form = UserForm(instance=user)
cof_form = CofForm(instance=cof) profile_form = ProfileForm(instance=profile)
# Protection (read-only) des champs username, login_clipper et is_cof # Protection (read-only) des champs username, login_clipper et is_cof
account_form_set_readonly_fields(user_form, cof_form) account_form_set_readonly_fields(user_form, profile_form)
except User.DoesNotExist: except User.DoesNotExist:
# le username donnée n'existe pas -> Création depuis rien # le username donnée n'existe pas -> Création depuis rien
# (éventuellement en cours avec erreurs précédemment) # (éventuellement en cours avec erreurs précédemment)
@ -286,13 +287,13 @@ def get_account_create_forms(request=None, username=None, login_clipper=None):
# connaît pas du tout, faut tout remplir # connaît pas du tout, faut tout remplir
if request: if request:
user_form = UserForm(request.POST) user_form = UserForm(request.POST)
cof_form = CofForm(request.POST) profile_form = ProfileForm(request.POST)
else: else:
user_form = UserForm() user_form = UserForm()
cof_form = CofForm() profile_form = ProfileForm()
# mais on laisse le username en écriture # mais on laisse le username en écriture
cof_form.fields['login_clipper'].widget.attrs['readonly'] = True profile_form.fields['login_clipper'].widget.attrs['readonly'] = True
cof_form.fields['is_cof'].widget.attrs['disabled'] = True # cof_form.fields['is_cof'].widget.attrs['disabled'] = True
if request: if request:
account_form = AccountNoTriForm(request.POST) account_form = AccountNoTriForm(request.POST)
@ -301,7 +302,7 @@ def get_account_create_forms(request=None, username=None, login_clipper=None):
return { return {
'account_form': account_form, 'account_form': account_form,
'cof_form': cof_form, 'cof_form': profile_form,
'user_form': user_form, 'user_form': user_form,
} }