示例#1
0
 def test_invalid_form(self, groups):
     form_data = {
         'name': 'Test Group',
         # 'description': 'still not very interesting',
         'members': []
     }
     form = ManageRecipientGroupForm(data=form_data)
     form.is_valid()
     with pytest.raises(ArchivedItemException):
         exists_and_archived(form, RecipientGroup, 'group')
示例#2
0
 def test_invalid_form(self, groups):
     form_data = {
         'name': 'Test Group',
         # 'description': 'still not very interesting',
         'members': []
     }
     form = ManageRecipientGroupForm(data=form_data)
     form.is_valid()
     with pytest.raises(ArchivedItemException):
         exists_and_archived(form, RecipientGroup, 'group')
示例#3
0
    def post(self, request, *args, **kwargs):
        try:
            instance = self.model_class.objects.get(pk=kwargs['pk'])  # original instance
            form = self.form_class(request.POST, instance=instance)
        except KeyError:
            form = self.form_class(request.POST)

        if form.is_valid():
            # if form is valid, save, otherwise handle different type of errors
            form.save()
            return redirect(self.redirect_url)
        else:
            new_instance = exists_and_archived(form, self.model_class, self.identifier)
            if new_instance is not None:
                # if we have a clash with existing object and it is archived,
                # redirect there, otherwise return form with errors
                messages.info(
                    request,
                    "'{0}' already exists. Click the button to bring it back from the archive.".format(
                        str(new_instance)
                    )
                )
                return redirect(new_instance.get_absolute_url())
            else:
                return render(request, "apostello/item.html",
                              dict(form=form,
                                   redirect_url=self.redirect_url,
                                   submit_text="Submit",
                                   identifier=self.identifier,
                                   object=new_instance)
                              )
示例#4
0
 def test_group_archived(self, groups):
     form_data = {
         'name': 'Archived Group',
         'description': 'still not very interesting',
         'members': []
     }
     form = ManageRecipientGroupForm(data=form_data)
     form.is_valid()
     assert exists_and_archived(form, RecipientGroup,
                                'group').name == 'Archived Group'
示例#5
0
 def test_group_archived(self, groups):
     form_data = {
         'name': 'Archived Group',
         'description': 'still not very interesting',
         'members': []
     }
     form = ManageRecipientGroupForm(data=form_data)
     form.is_valid()
     assert exists_and_archived(
         form, RecipientGroup, 'group'
     ).name == 'Archived Group'
示例#6
0
    def post(self, request, *args, **kwargs):
        """
        Handle form post.

        If an object is created that matches an existing, archived, object, the
        user will be redirected to the existing object and told how to restore
        the archived object.
        """
        try:
            instance = self.model_class.objects.get(
                pk=kwargs['pk']
            )  # original instance
            form = self.form_class(request.POST, instance=instance)
        except KeyError:
            form = self.form_class(request.POST)

        if form.is_valid():
            # if form is valid, save, otherwise handle different type of errors
            form.save()
            return redirect(self.redirect_url)
        else:
            try:
                # if we have a clash with existing object and it is archived,
                # redirect there, otherwise return form with errors
                new_instance = exists_and_archived(
                    form, self.model_class, self.identifier
                )
                messages.info(
                    request,
                    "'{0}' already exists. You can open the menu to restore it.".format(
                        str(new_instance)
                    )
                )
                return redirect(new_instance.get_absolute_url)
            except ArchivedItemException:
                return render(
                    request,
                    "apostello/item.html",
                    dict(
                        form=form,
                        redirect_url=self.redirect_url,
                        submit_text="Submit",
                        identifier=self.identifier,
                        object=None
                    )
                )
示例#7
0
    def post(self, request, *args, **kwargs):
        """
        Handle form post.

        If an object is created that matches an existing, archived, object, the
        user will be redirected to the existing object and told how to restore
        the archived object.
        """
        try:
            instance = self.model_class.objects.get(
                pk=kwargs['pk'])  # original instance
            form = self.form_class(request.POST, instance=instance)
        except KeyError:
            form = self.form_class(request.POST)

        if form.is_valid():
            # if form is valid, save, otherwise handle different type of errors
            form.save()
            return redirect(self.redirect_url)
        else:
            try:
                # if we have a clash with existing object and it is archived,
                # redirect there, otherwise return form with errors
                new_instance = exists_and_archived(form, self.model_class,
                                                   self.identifier)
                messages.info(
                    request, "'{0}' already exists."
                    " You can open the menu to restore it.".format(
                        str(new_instance)))
                return redirect(new_instance.get_absolute_url)
            except ArchivedItemException:
                return TemplateResponse(
                    request, "apostello/item.html", {
                        'form': form,
                        'redirect_url': self.redirect_url,
                        'submit_text': "Submit",
                        'identifier': self.identifier,
                        'object': None
                    })