Skip to content

Commit

Permalink
Addressing Egene's review requests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick committed Jan 3, 2017
1 parent 385f8f5 commit 136b191
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
6 changes: 3 additions & 3 deletions cadasta/organization/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,8 @@ def clean_questionnaire(self):
new_form = self.data.get('questionnaire')
current_form = self.initial.get('questionnaire')

if (self.instance.has_records and new_form is not None and
new_form != current_form):
if (new_form is not None and new_form != current_form and
self.instance.has_records):
raise ValidationError(
_("Data has already been contributed to this project. To "
"ensure data integrity, uploading a new questionnaire is "
Expand All @@ -368,7 +368,7 @@ def save(self, *args, **kwargs):
original_file=original_file,
project=self.instance
)
elif not self.instance.has_records:
elif new_form is not None and not self.instance.has_records:
self.instance.current_questionnaire = ''

return super().save(*args, **kwargs)
Expand Down
35 changes: 27 additions & 8 deletions cadasta/organization/tests/test_views_default_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,8 @@ class ProjectEditDetailsTest(ViewTestCase, UserTestCase,

def setup_models(self):
self.project = ProjectFactory.create(current_questionnaire='abc')
self.questionnaire = QuestionnaireFactory.create(project=self.project,
id='abc')

def setup_url_kwargs(self):
return {
Expand All @@ -945,7 +947,11 @@ def setup_url_kwargs(self):
def setup_template_context(self):
return {'project': self.project,
'object': self.project,
'form': forms.ProjectEditDetails(instance=self.project)}
'form': forms.ProjectEditDetails(
instance=self.project,
initial={'questionnaire': self.questionnaire.xls_form.url,
'original_file': self.questionnaire.original_file}
)}

def test_get_with_authorized_user(self):
user = UserFactory.create()
Expand All @@ -956,6 +962,20 @@ def test_get_with_authorized_user(self):
assert response.content == self.expected_content
assert 'Select the questionnaire' in self.expected_content

def test_get_empty_questionnaire_with_authorized_user(self):
user = UserFactory.create()
assign_policies(user)

self.project.current_questionnaire = ''
self.project.save()

form = forms.ProjectEditDetails(instance=self.project)

response = self.request(user=user)
assert response.status_code == 200
assert response.content == self.render_content(form=form)
assert 'Select the questionnaire' in self.expected_content

def test_get_with_blocked_questionnaire_upload(self):
user = UserFactory.create()
assign_policies(user)
Expand Down Expand Up @@ -1028,15 +1048,14 @@ def test_post_empty_questionnaire_with_blocked_questionnaire_upload(self):
SpatialUnitFactory.create(project=self.project)
user = UserFactory.create()
assign_policies(user)
post_data = self.post_data.copy()
del post_data['questionnaire']
assert 'questionnaire' not in post_data.keys()
response = self.request(user=user, method='POST')
response = self.request(user=user, method='POST',
post_data={'questionnaire': None})

assert response.status_code == 200
assert response.status_code == 302
assert self.expected_success_url in response.location
self.project.refresh_from_db()
assert self.project.name != self.post_data['name']
assert self.project.description != self.post_data['description']
assert self.project.name == self.post_data['name']
assert self.project.description == self.post_data['description']
assert self.project.current_questionnaire == 'abc'

def test_post_invalid_form(self):
Expand Down

0 comments on commit 136b191

Please sign in to comment.