Skip to content

Commit

Permalink
Merge pull request #80 from ccnmtl/contactus-refactor
Browse files Browse the repository at this point in the history
Correcting FormView.get_initial usage
  • Loading branch information
thraxil committed Jan 20, 2015
2 parents af38256 + c615c42 commit 70fb4e0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
28 changes: 18 additions & 10 deletions mediathread/main/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,12 @@ def test_get_initial_anonymous(self):
view.request = RequestFactory().get('/contact/')
view.request.session = {}
view.request.user = AnonymousUser()
view.get_initial()
initial = view.get_initial()

self.assertIsNotNone(view.initial['issue_date'])
self.assertFalse('name' in view.initial)
self.assertFalse('email' in view.initial)
self.assertFalse('username' in view.initial)
self.assertIsNotNone(initial['issue_date'])
self.assertFalse('name' in initial)
self.assertFalse('email' in initial)
self.assertFalse('username' in initial)

def test_get_initial_not_anonymous(self):
view = ContactUsView()
Expand All @@ -450,12 +450,20 @@ def test_get_initial_not_anonymous(self):
last_name='Bar',
email='[email protected]')

view.get_initial()
initial = view.get_initial()
self.assertIsNotNone(initial['issue_date'])
self.assertEquals(initial['name'], 'Foo Bar')
self.assertEquals(initial['email'], '[email protected]')
self.assertEquals(initial['username'], view.request.user.username)

self.assertIsNotNone(view.initial['issue_date'])
self.assertEquals(view.initial['name'], 'Foo Bar')
self.assertEquals(view.initial['email'], '[email protected]')
self.assertEquals(view.initial['username'], view.request.user.username)
# a subsequent call using an anonymous session returns a clean initial
view.request.session = {}
view.request.user = AnonymousUser()
initial = view.get_initial()
self.assertIsNotNone(initial['issue_date'])
self.assertFalse('name' in initial)
self.assertFalse('email' in initial)
self.assertFalse('username' in initial)

def test_form_valid(self):
view = ContactUsView()
Expand Down
13 changes: 7 additions & 6 deletions mediathread/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,17 +417,18 @@ def get_initial(self):
"""
Returns the initial data to use for forms on this view.
"""
initial = super(ContactUsView, self).get_initial()
if not self.request.user.is_anonymous():
self.initial['name'] = self.request.user.get_full_name()
self.initial['email'] = self.request.user.email
self.initial['username'] = self.request.user.username
initial['name'] = self.request.user.get_full_name()
initial['email'] = self.request.user.email
initial['username'] = self.request.user.username

self.initial['issue_date'] = datetime.now()
initial['issue_date'] = datetime.now()

if SESSION_KEY in self.request.session:
self.initial['course'] = self.request.session[SESSION_KEY].title
initial['course'] = self.request.session[SESSION_KEY].title

return super(ContactUsView, self).get_initial()
return initial

def form_valid(self, form):
subject = "Mediathread Contact Us Request"
Expand Down

0 comments on commit 70fb4e0

Please sign in to comment.