From 376e829502edfcaa290caaf93fa65c8aadfaf808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michele=20Orr=C3=B9?= Date: Sat, 11 Feb 2017 15:07:45 +0100 Subject: [PATCH] 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. --- cof/models.py | 2 +- gestion/tests.py | 13 ++++++++++++- kfet/autocomplete.py | 4 ++-- kfet/forms.py | 28 ++++++++++++++------------- kfet/models.py | 2 +- kfet/views.py | 45 ++++++++++++++++++++++---------------------- 6 files changed, 54 insertions(+), 40 deletions(-) diff --git a/cof/models.py b/cof/models.py index 81a57015..3ffdf365 100644 --- a/cof/models.py +++ b/cof/models.py @@ -237,7 +237,7 @@ class SurveyAnswer(models.Model): self.survey.title) -@python_2_unicode_compatible +#XXX. this needs to be removed according to Martin class Clipper(models.Model): username = models.CharField("Identifiant", max_length=20) fullname = models.CharField("Nom complet", max_length=200) diff --git a/gestion/tests.py b/gestion/tests.py index d12725c4..49f5736e 100644 --- a/gestion/tests.py +++ b/gestion/tests.py @@ -5,8 +5,10 @@ when you run "manage.py test". Replace this with more appropriate tests for your application. """ +from unittest.mock import patch + from django.db.utils import IntegrityError -from django.test import TestCase +from django.test import Client, TestCase from gestion.models import Profile, User @@ -58,3 +60,12 @@ class SimpleTest(TestCase): pkfet2 = Account.objects.create(profile=p2, trigramme='BAZ') self.assertRaises(IntegrityError, 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') diff --git a/kfet/autocomplete.py b/kfet/autocomplete.py index 768da4b5..ec8edd5f 100644 --- a/kfet/autocomplete.py +++ b/kfet/autocomplete.py @@ -26,8 +26,8 @@ def account_create(request): 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['users_cof'] = User.objects.filter(Q(profile__cof__is_cof = True)) + queries['users_notcof'] = User.objects.filter(Q(profile__cof__is_cof = False)) queries['clippers'] = Clipper.objects for word in search_words: diff --git a/kfet/forms.py b/kfet/forms.py index e68a0d20..1146d2fd 100644 --- a/kfet/forms.py +++ b/kfet/forms.py @@ -16,7 +16,7 @@ from django.utils import timezone from kfet.models import (Account, Checkout, Article, OperationGroup, Operation, CheckoutStatement, ArticleCategory, Settings, AccountNegative, Transfer, TransferGroup, Supplier, Inventory, InventoryArticle) -from cof.models import CofProfile +from gestion.models import Profile # ----- # Widgets @@ -91,19 +91,21 @@ class AccountPwdForm(forms.Form): raise ValidationError("Les mots de passes sont différents") super(AccountPwdForm, self).clean() -class CofForm(forms.ModelForm): - def clean_is_cof(self): - instance = getattr(self, 'instance', None) - if instance and instance.pk: - return instance.is_cof - else: - return False - class Meta: - model = CofProfile - fields = ['login_clipper', 'is_cof', 'departement'] +class ProfileForm(forms.ModelForm): + # def clean_is_cof(self): + # instance = getattr(self, 'instance', None) + # if instance and instance.pk: + # return instance.is_cof + # else: + # return False -class CofRestrictForm(CofForm): - class Meta(CofForm.Meta): + class Meta: + model = Profile + # XXX. I am removing is_cof from here. + fields = ['login_clipper', 'departement'] + +class ProfileRestrictForm(ProfileForm): + class Meta(ProfileForm.Meta): fields = ['departement'] class UserForm(forms.ModelForm): diff --git a/kfet/models.py b/kfet/models.py index cff60e84..3c68da59 100644 --- a/kfet/models.py +++ b/kfet/models.py @@ -81,7 +81,7 @@ class Account(models.Model): return self.profile.departement @property def is_cof(self): - return self.profile.is_cof + return self.profile.cof.is_cof # Propriétés supplémentaires @property diff --git a/kfet/views.py b/kfet/views.py index 3534373f..f45ab124 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -3,6 +3,11 @@ from __future__ import (absolute_import, division, print_function, unicode_literals) 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.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.utils import timezone 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.models import (Account, Checkout, Article, Settings, AccountNegative, CheckoutStatement, GenericTeamToken, Supplier, SupplierArticle, Inventory, InventoryArticle, Order, OrderArticle) from kfet.forms import * -from collections import defaultdict from kfet import consumers -from datetime import timedelta import django_cas_ng -import hashlib -import heapq -import statistics @login_required def home(request): @@ -43,7 +44,7 @@ def home(request): @teamkfet_required def login_genericteam(request): # 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 if profile.login_clipper: need_cas_logout = True @@ -250,34 +251,34 @@ def get_account_create_forms(request=None, username=None, login_clipper=None): if len(names) > 1: # Si d'autres noms -> tous dans le nom de famille user_initial['last_name'] = " ".join(names[1:]) - # CofForm - Prefill - cof_initial = { 'login_clipper': login_clipper } + # ProfileForm - Prefill + profile_initial = { 'login_clipper': login_clipper } # Form créations if request: 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: 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 - account_form_set_readonly_fields(user_form, cof_form) + account_form_set_readonly_fields(user_form, profile_form) if username and not clipper: try: user = User.objects.get(username=username) # le user existe déjà # récupération du profil cof - (cof, _) = CofProfile.objects.get_or_create(user=user) - # UserForm + CofForm - Création à partir des instances existantes + profile, _ = Profile.objects.get_or_create(user=user) + # UserForm + ProfileForm - Création à partir des instances existantes if request: user_form = UserForm(request.POST, instance = user) - cof_form = CofForm(request.POST, instance = cof) + profile_form = ProfileForm(request.POST, instance = profile) else: 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 - account_form_set_readonly_fields(user_form, cof_form) + account_form_set_readonly_fields(user_form, profile_form) except User.DoesNotExist: # le username donnée n'existe pas -> Création depuis rien # (é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 if request: user_form = UserForm(request.POST) - cof_form = CofForm(request.POST) + profile_form = ProfileForm(request.POST) else: user_form = UserForm() - cof_form = CofForm() + profile_form = ProfileForm() # mais on laisse le username en écriture - cof_form.fields['login_clipper'].widget.attrs['readonly'] = True - cof_form.fields['is_cof'].widget.attrs['disabled'] = True + profile_form.fields['login_clipper'].widget.attrs['readonly'] = True + # cof_form.fields['is_cof'].widget.attrs['disabled'] = True if request: account_form = AccountNoTriForm(request.POST) @@ -301,7 +302,7 @@ def get_account_create_forms(request=None, username=None, login_clipper=None): return { 'account_form': account_form, - 'cof_form': cof_form, + 'cof_form': profile_form, 'user_form': user_form, }