Esempio n. 1
0
 def form_valid(self, form):
     updated_content = form.save(commit=False)
     if "save_draft" in self.request.POST:
         updated_content.is_draft = True
     elif "publish" in self.request.POST:
         updated_content.is_draft = False
     elif "reset" in self.request.POST:
         current = DisclaimerContent.current()
         updated_content.disclaimer_terms = current.disclaimer_terms
         updated_content.form = current.form
     else:
         raise ValidationError(
             "Action (save draft/publish/reset) cannot be determined")
     updated_content.save()
     ActivityLog.objects.create(
         log=f"Disclaimer content ({updated_content.status}) "
         f"version {updated_content.version} updated by admin user {full_name(self.request.user)}"
     )
     if "reset" in self.request.POST:
         messages.success(
             self.request,
             f"Content reset to Version {DisclaimerContent.current().version}"
         )
         return HttpResponseRedirect(
             reverse('studioadmin:edit_disclaimer_content',
                     args=(updated_content.version, )))
     return HttpResponseRedirect(self.get_success_url())
 def test_list_view_current_version(self):
     make_disclaimer_content(disclaimer_terms='Foo')
     make_disclaimer_content(disclaimer_terms='Bar')
     latest = make_disclaimer_content(disclaimer_terms='Foobar')
     assert DisclaimerContent.current() == latest
     resp = self.client.get(self.list_url)
     assert resp.context_data["current_version"] == latest.version
 def test_add_disclaimer_content(self):
     DisclaimerContent.objects.all().delete()
     self.client.post(
         reverse("studioadmin:add_disclaimer_content"), {
             **self.form_default, "disclaimer_terms": "A new policy",
             "publish": "Publish"
         })
     assert DisclaimerContent.objects.count() == 1
     assert DisclaimerContent.current().disclaimer_terms == "A new policy"
Esempio n. 4
0
 def clean_version(self):
     version = self.cleaned_data.get('version')
     current_version = DisclaimerContent.current_version()
     if version is None or version > current_version:
         return version
     self.add_error(
         'version',
         f'New version must increment current version (must be greater than {current_version})'
     )
Esempio n. 5
0
    def test_disclaimer_content_first_version(self):
        DisclaimerContent.objects.all().delete()
        assert DisclaimerContent.objects.exists() is False
        assert DisclaimerContent.current_version() == 0

        content = make_disclaimer_content()
        assert content.version == 1.0

        content1 = make_disclaimer_content()
        assert content1.version == 2.0
 def test_add_disclaimer_content_publish(self):
     DisclaimerContent.objects.all().delete()
     self.client.post(reverse("studioadmin:add_disclaimer_content"), {
         **self.form_default, "disclaimer_terms": "test",
         "publish": "Publish"
     })
     assert DisclaimerContent.objects.count() == 1
     disclaimer_content = DisclaimerContent.objects.first()
     assert disclaimer_content.is_draft is False
     assert DisclaimerContent.current_version(
     ) == disclaimer_content.version
 def test_add_disclaimer_content_save_as_draft(self):
     DisclaimerContent.objects.all().delete()
     self.client.post(
         reverse("studioadmin:add_disclaimer_content"), {
             **self.form_default, "disclaimer_terms": "test",
             "save_draft": "Save as draft"
         })
     assert DisclaimerContent.objects.count() == 1
     disclaimer_content = DisclaimerContent.objects.first()
     assert disclaimer_content.is_draft is True
     assert DisclaimerContent.current_version() == 0
Esempio n. 8
0
 def clean(self):
     new_disclaimer_terms = self.cleaned_data.get('disclaimer_terms')
     new_health_questionnaire = self.cleaned_data.get('form')
     # check content has changed
     current_content = DisclaimerContent.current()
     if current_content and current_content.disclaimer_terms == new_disclaimer_terms:
         if current_content.form == json.loads(new_health_questionnaire):
             self.add_error(
                 None,
                 'No changes made from previous version; new version must update disclaimer content'
             )
Esempio n. 9
0
 def __init__(self, *args, **kwargs):
     super().__init__(*args, **kwargs)
     self.fields['version'].required = False
     self.fields['disclaimer_terms'].widget = CKEditorWidget(
         config_name='default')
     self.fields['form_info'].widget = CKEditorWidget(config_name='default')
     if not self.instance.id:
         current_content = DisclaimerContent.current()
         if current_content:
             self.fields[
                 'disclaimer_terms'].initial = current_content.disclaimer_terms
             self.fields['form'].initial = current_content.form
             self.fields['form_title'].initial = current_content.form_title
             self.fields['form_info'].initial = current_content.form_info
             next_default_version = Decimal(
                 floor((DisclaimerContent.current_version() + 1)))
             self.fields['version'].help_text = f'Current version is {current_content.version}.  Leave ' \
                                        f'blank for next major version ({next_default_version:.1f})'
         else:
             self.fields['version'].initial = 1.0
Esempio n. 10
0
 def get_form_kwargs(self):
     form_kwargs = super().get_form_kwargs()
     obj = self.get_object()
     current = DisclaimerContent.current()
     if current:
         form_kwargs["hide_reset_button"] = (obj.disclaimer_terms
                                             == current.disclaimer_terms
                                             and obj.form == current.form)
     else:
         form_kwargs["hide_reset_button"] = True
     return form_kwargs
Esempio n. 11
0
 def form_invalid(self, form):
     # The formbuilder doesn't display properly if we simply return the POST data in the form. Mostly we'll
     # get here if we made an error with the version or tried to save with no changes, so just instantiate a
     # new form and add the errors manually to the context
     current = DisclaimerContent.current()
     hide_reset_button = (form.instance.disclaimer_terms
                          == current.disclaimer_terms
                          and form.instance.form == current.form)
     context = self.get_context_data()
     context["form_errors"] = form.errors
     context["form"] = StudioadminDisclaimerContentForm(
         hide_reset_button=hide_reset_button)
     return TemplateResponse(self.request, self.template_name, context)
 def test_edit_disclaimer_content_save_as_draft(self):
     draft_disclaimer_content = make_disclaimer_content(
         is_draft=True, disclaimer_terms="Bar", version=None)
     url = reverse("studioadmin:edit_disclaimer_content",
                   args=(draft_disclaimer_content.version, ))
     self.client.post(
         url, {
             **self.form_default, "disclaimer_terms": "test",
             "save_draft": "Save as draft",
             "version": draft_disclaimer_content.version
         })
     draft_disclaimer_content.refresh_from_db()
     assert draft_disclaimer_content.is_draft is True
     assert DisclaimerContent.current_version() == 0
 def test_publish_draft(self):
     draft_disclaimer_content = make_disclaimer_content(
         is_draft=True, disclaimer_terms="Bar", version=None)
     url = reverse("studioadmin:edit_disclaimer_content",
                   args=(draft_disclaimer_content.version, ))
     self.client.post(
         url, {
             **self.form_default,
             "publish": "Publish",
             "disclaimer_terms": draft_disclaimer_content.disclaimer_terms,
             "form": json.dumps(draft_disclaimer_content.form),
             "version": draft_disclaimer_content.version,
         })
     draft_disclaimer_content.refresh_from_db()
     assert draft_disclaimer_content.is_draft is False
     assert DisclaimerContent.current() == draft_disclaimer_content
Esempio n. 14
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context["current_version"] = DisclaimerContent.current_version()
     return context