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)
@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)

View file

@ -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')

View file

@ -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:

View file

@ -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):

View file

@ -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

View file

@ -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,
}