From 76d192f9bbedf4b57786bf08bc05fe41d7ec6eb9 Mon Sep 17 00:00:00 2001 From: jwokaty Date: Tue, 20 Oct 2020 16:42:36 -0400 Subject: [PATCH 1/2] Add ProfileAddView --- member_manager/views/__init__.py | 4 ++-- member_manager/views/profile_add.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 member_manager/views/profile_add.py diff --git a/member_manager/views/__init__.py b/member_manager/views/__init__.py index 91ea44a..39f1473 100644 --- a/member_manager/views/__init__.py +++ b/member_manager/views/__init__.py @@ -1,3 +1,3 @@ -from django.shortcuts import render +"""MemberManager views""" -# Create your views here. +from member_manager.views.profile_add import ProfileAddView diff --git a/member_manager/views/profile_add.py b/member_manager/views/profile_add.py new file mode 100644 index 0000000..9f8c311 --- /dev/null +++ b/member_manager/views/profile_add.py @@ -0,0 +1,24 @@ +"""ProfileAddView class""" + +from django.forms import modelform_factory + +from common_files.views.timestamp import TimestampCreateView +from member_manager.forms.profile import ProfileForm +from member_manager.models.profile import Profile + + +class ProfileAddView(TimestampCreateView): + """ProfileAddView""" + + form_class = modelform_factory(Profile, form=ProfileForm, + fields=['first_name', 'last_name', + 'pronouns', 'email', 'phone', + 'join_reason', 'how_heard', + 'how_heard_other']) + template_name = 'member_manager/profile_add_form.html' + + def form_valid(self, form): + """Save valid form and add profile_id to session.""" + redirect = super().form_valid(form) + self.request.session['profile_id'] = self.object.id + return redirect From 8cc60d43c1040d4b00c207cc81d5e4031d362728 Mon Sep 17 00:00:00 2001 From: jwokaty Date: Tue, 20 Oct 2020 16:43:14 -0400 Subject: [PATCH 2/2] Add ProfileAddViewTest closes #10 --- member_manager/tests/__init__.py | 1 + member_manager/tests/profile_add.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 member_manager/tests/profile_add.py diff --git a/member_manager/tests/__init__.py b/member_manager/tests/__init__.py index b39b6ec..0348e2b 100644 --- a/member_manager/tests/__init__.py +++ b/member_manager/tests/__init__.py @@ -7,6 +7,7 @@ ) from member_manager.tests.page import PageTest from member_manager.tests.profile import ProfileTest +from member_manager.tests.profile_add import ProfileAddViewTest from member_manager.tests.profile_form import ProfileFormTest from member_manager.tests.profile_skill import ProfileSkillTest from member_manager.tests.published import PublishedListFilterTest diff --git a/member_manager/tests/profile_add.py b/member_manager/tests/profile_add.py new file mode 100644 index 0000000..fa0a22a --- /dev/null +++ b/member_manager/tests/profile_add.py @@ -0,0 +1,28 @@ +"""ProfileAddViewTest class""" + +from django.contrib.auth.models import AnonymousUser +from django.test import RequestFactory, TestCase + +from member_manager.views.profile_add import ProfileAddView + + +class ProfileAddViewTest(TestCase): + """ProfileAddViewTest tests the ProfileAddView class""" + + @classmethod + def setUpTestData(cls): + request_factory = RequestFactory() + cls.request = request_factory.get('/member_manager/profile') + cls.request.user = AnonymousUser() + cls.request.session = {} + cls.data = {'first_name': 'foo', 'last_name': 'bar', + 'email': 'fb@example.com', 'phone': '0987654321'} + + def test_form_valid(self): + """Tests form_valid""" + view_i = ProfileAddView(success_url='/availability', + **{'request': self.request}) + form_class = view_i.get_form_class() + form = form_class(data=self.data) + self.assertTrue(view_i.form_valid(form)) + self.assertEqual(self.request.session['profile_id'], 1)