def new_mandat(request): aidant = request.user form = MandatForm() if request.method == "GET": return render( request, "aidants_connect_web/new_mandat/new_mandat.html", { "aidant": aidant, "form": form }, ) else: form = MandatForm(request.POST) if form.is_valid(): data = form.cleaned_data duree = 1 if data["duree"] == "short" else 365 connection = Connection.objects.create(demarches=data["perimeter"], duree=duree) request.session["connection"] = connection.pk return redirect("fc_authorize") else: return render( request, "aidants_connect_web/new_mandat/new_mandat.html", { "aidant": aidant, "form": form }, )
def test_non_integer_duree_triggers_error(self): form = MandatForm(data={"demarche": ["argent"], "duree": "test"}) self.assertFalse(form.is_valid()) self.assertEqual( form.errors["duree"], ["Sélectionnez un choix valide. test n’en fait pas partie."], )
def test_non_existing_perimeter_triggers_error(self): form = MandatForm(data={"perimeter": ["test"], "duree": "16"}) self.assertFalse(form.is_valid()) self.assertEqual( form.errors["perimeter"], ["Sélectionnez un choix valide. test n'en fait pas partie."], )
def new_mandat(request): aidant: Aidant = request.user try: connection = Connection.objects.get( pk=request.session.get("connection")) except Connection.DoesNotExist: connection = None if request.method == "GET": inital = ({ "duree": connection.duree_keyword, "is_remote": connection.mandat_is_remote, "demarche": connection.demarches, "user_phone": connection.user_phone, } if connection is not None else None) form = MandatForm(initial=inital) return render( request, "aidants_connect_web/new_mandat/new_mandat.html", { "aidant": aidant, "form": form }, ) else: form = MandatForm(request.POST, error_class=PatchedErrorList) if form.is_valid(): data = form.cleaned_data kwargs = { "aidant": aidant, "organisation": aidant.organisation, "demarches": data["demarche"], "duree_keyword": data["duree"], "mandat_is_remote": data["is_remote"], } if kwargs["mandat_is_remote"] is True: kwargs["user_phone"] = data["user_phone"] connection = Connection.objects.create(**kwargs) request.session["connection"] = connection.pk return redirect("fc_authorize") else: return render( request, "aidants_connect_web/new_mandat/new_mandat.html", { "aidant": aidant, "form": form }, )
def renew_mandat(request, usager_id): aidant: Aidant = request.user usager: Usager = aidant.get_usager(usager_id) if not usager: django_messages.error(request, "Cet usager est introuvable ou inaccessible.") return redirect("espace_aidant_home") form = MandatForm() if request.method == "GET": return render( request, "aidants_connect_web/new_mandat/renew_mandat.html", {"aidant": aidant, "form": form}, ) else: form = MandatForm(request.POST) if form.is_valid(): data = form.cleaned_data access_token = make_password(token_urlsafe(64), settings.FC_AS_FI_HASH_SALT) connection = Connection.objects.create( aidant=aidant, organisation=aidant.organisation, connection_type="FS", access_token=access_token, usager=usager, demarches=data["demarche"], duree_keyword=data["duree"], mandat_is_remote=data["is_remote"], ) duree = AuthorizationDurations.duration(connection.duree_keyword) Journal.log_init_renew_mandat( aidant=aidant, usager=usager, demarches=connection.demarches, duree=duree, is_remote_mandat=connection.mandat_is_remote, access_token=connection.access_token, ) request.session["connection"] = connection.pk return redirect("new_mandat_recap") else: return render( request, "aidants_connect_web/new_mandat/renew_mandat.html", {"aidant": aidant, "form": form}, )
def test_validation_for_blank_items(self): form = MandatForm(data={"demarche": ["argent"], "duree": "SHORT"}) self.assertTrue(form.is_valid()) form_2 = MandatForm(data={"demarche": [], "duree": "SHORT"}) self.assertFalse(form_2.is_valid()) self.assertEqual(form_2.errors["demarche"], ["Ce champ est obligatoire."]) form_3 = MandatForm(data={"demarche": ["travail"], "duree": ""}) self.assertFalse(form_3.is_valid()) self.assertEqual(form_3.errors["duree"], ["Ce champ est obligatoire."])
def setUp(self): self.aidant_thierry = factories.UserFactory() self.client = Client() self.test_usager = Usager.objects.create( given_name="Fabrice", family_name="MERCIER", sub= "46df505a40508b9fa620767c73dc1d7ad8c30f66fa6ae5ae963bf9cccc885e8dv1", preferred_username="******", birthdate="1981-07-27", gender="female", birthplace="95277", birthcountry="99100", email="*****@*****.**", ) self.mandat_form = MandatForm(data={ "perimeter": ["papiers", "logement"], "duree": "short" }) Connection.objects.create( id=1, state="test_another_state", connection_type="FS", nonce="test_another_nonce", demarches=["papiers", "logement"], duree=1, usager=self.test_usager, )
def test_validation_for_blank_items(self): form = MandatForm(data={"demarche": ["argent"], "duree": "SHORT"}) self.assertTrue(form.is_valid()) form_2 = MandatForm(data={"demarche": [], "duree": "SHORT"}) self.assertFalse(form_2.is_valid()) self.assertEqual( form_2.errors["demarche"], ["Vous devez sélectionner au moins une démarche."], ) form_3 = MandatForm(data={"demarche": ["travail"], "duree": ""}) self.assertFalse(form_3.is_valid()) self.assertEqual(form_3.errors["duree"], ["Veuillez sélectionner la durée du mandat."])
def new_mandat(request): aidant = request.user form = MandatForm() if request.method == "GET": return render( request, "aidants_connect_web/new_mandat/new_mandat.html", { "aidant": aidant, "form": form }, ) else: form = MandatForm(request.POST) if form.is_valid(): data = form.cleaned_data connection = Connection.objects.create( aidant=request.user, demarches=data["demarche"], duree_keyword=data["duree"], mandat_is_remote=data["is_remote"], ) request.session["connection"] = connection.pk return redirect("fc_authorize") else: return render( request, "aidants_connect_web/new_mandat/new_mandat.html", { "aidant": aidant, "form": form }, )
def setUp(self): self.aidant_thierry = AidantFactory() self.client = Client() self.test_usager = UsagerFactory( given_name="Fabrice", family_name="MERCIER", sub="46df505a40508b9fa620767c73dc1d7ad8c30f66fa6ae5ae963bf9cccc885e8dv1", ) self.autorisation_form = MandatForm( data={"demarche": ["papiers", "logement"], "duree": "short"} ) Connection.objects.create( id=1, state="test_another_state", connection_type="FS", nonce="test_another_nonce", demarches=["papiers", "logement"], duree_keyword="SHORT", usager=self.test_usager, )
def test_form_renders_item_text_input(self): form = MandatForm() self.assertIn("argent", form.as_p())