Skip to content
This repository has been archived by the owner on Jun 29, 2020. It is now read-only.

Commit

Permalink
Contact Infoの初期値が自動設定されるようになった。テストも書いた
Browse files Browse the repository at this point in the history
  • Loading branch information
giginet committed Nov 14, 2014
1 parent 736286a commit 021aeac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/kawaz/apps/products/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ def test_non_members_cannot_create_product(self):
r = self.client.post('/products/create/', self.product_kwargs)
self.assertRedirects(r, login_url)

def test_contact_info_initial_value(self):
"""
Contact infoの初期値が
ニックネーム: メールアドレス
になっている
"""
for i, user in enumerate(self.members):
self.prefer_login(user)
r = self.client.get('/products/create/')
form = r.context['form']
contact_info = "{}: {}".format(user.nickname, user.email)
self.assertEqual(form.fields['contact_info'].initial, contact_info)

def test_members_can_create_product(self):
"""
メンバーはプロダクトを作成することが出来る
Expand Down
8 changes: 8 additions & 0 deletions src/kawaz/apps/products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ class ProductCreateView(ProductFormMixin, CreateView):
model = Product
form_class = ProductCreateForm

def get_form(self, form_class):
form = super().get_form(form_class)
# Contact Infoの初期値としてニックネームとメールアドレスを入れている
user = getattr(self.request, 'user')
contact_info = "{}: {}".format(user.nickname, user.email)
form.fields['contact_info'].initial = contact_info
return form

def form_valid(self, *args, **kwargs):
response = super().form_valid(*args, **kwargs)
if self.object:
Expand Down

0 comments on commit 021aeac

Please sign in to comment.