示例#1
0
def project_step_create_edit(request, project_slug, project_step_number=None):
    user = request.user
    project = get_object_or_404(Project, slug=project_slug)
    if not project.is_editable(request):
        return HttpResponseForbidden('You are not the owner of this project.')
    if 'add' in request.path and project.steps.count() >= 20:
        messages.add_message(request, messages.WARNING,
                    'This project already contains 20 steps.')
        return redirect('project-detail', project.slug)
    if project_step_number is not None:
        project_step = get_object_or_404(
            ProjectStep, project=project, _order=project_step_number
        )
    else:
        project_step = ProjectStep(project=project)
    project_step_form = ProjectStepForm(
        request.POST or None, instance=project_step
    )
    if project_step_form.is_valid():
        project_step_form.save()
        if project.is_published():
            action.send(user, verb='updated', action_object=project_step, target=project)
        # User clicked "save and add another"
        if 'save-add' in request.POST:
            redirect_args = ('project-step-create-edit', project.slug)
        # User clicked "save" (catch anything else)
        else:
            redirect_args = (project,)
        return redirect(*redirect_args)
    return render(request, 'project/project_step_create_edit.html', {
        'project': project,
        'project_step': project_step,
        'project_step_form': project_step_form,
    })
示例#2
0
 def test_images_empty(self):
     post_data = {
         'title': self.get_random_string(),
         'content': self.get_random_string(),
         'images': []
     }
     request = self.request_factory.post('/', post_data)
     form = ProjectStepForm(request.POST, instance=self.project_step)
     if form.is_valid():
         project_step = form.save()
         self.assertEqual(project_step.gallery.count(), 0)
     else:
         self.fail('Form should be valid')
示例#3
0
 def test_images_empty(self):
     post_data = {
         'title': self.get_random_string(),
         'content': self.get_random_string(),
         'images': []
     }
     request = self.request_factory.post('/', post_data)
     form = ProjectStepForm(request.POST, instance=self.project_step)
     if form.is_valid():
         project_step = form.save()
         self.assertEqual(project_step.gallery.count(), 0)
     else:
         self.fail('Form should be valid')
示例#4
0
 def test_images_valid(self):
     project_image = self.create_project_image()
     post_data = {
         'title': self.get_random_string(),
         'content': self.get_random_string(),
         'images': [str(project_image.id)]
     }
     request = self.request_factory.post('/', post_data)
     form = ProjectStepForm(request.POST, instance=self.project_step)
     if form.is_valid():
         project_step = form.save()
         self.assertEqual(list(project_step.gallery.all()), [project_image])
     else:
         self.fail('Form should be valid')
示例#5
0
 def test_images_valid(self):
     project_image = self.create_project_image()
     post_data = {
         'title': self.get_random_string(),
         'content': self.get_random_string(),
         'images': [str(project_image.id)]
     }
     request = self.request_factory.post('/', post_data)
     form = ProjectStepForm(request.POST, instance=self.project_step)
     if form.is_valid():
         project_step = form.save()
         self.assertEqual(list(project_step.gallery.all()), [project_image])
     else:
         self.fail('Form should be valid')
示例#6
0
def project_step_create_edit(request, project_slug, project_step_number=None):
    user = request.user
    project = get_object_or_404(Project, slug=project_slug)
    if not project.is_editable(request):
        return HttpResponseForbidden('You are not the owner of this project.')
    if 'add' in request.path and project.steps.count() >= 20:
        messages.add_message(request, messages.WARNING,
                             'This project already contains 20 steps.')
        return redirect('project-detail', project.slug)
    if project_step_number is not None:
        project_step = get_object_or_404(ProjectStep,
                                         project=project,
                                         _order=project_step_number)
    else:
        project_step = ProjectStep(project=project)
    project_step_form = ProjectStepForm(request.POST or None,
                                        instance=project_step)
    if project_step_form.is_valid():
        project_step_form.save()
        if project.is_published():
            action.send(user,
                        verb='updated',
                        action_object=project_step,
                        target=project)
        # User clicked "save and add another"
        if 'save-add' in request.POST:
            redirect_args = ('project-step-create-edit', project.slug)
        # User clicked "save" (catch anything else)
        else:
            redirect_args = (project, )
        return redirect(*redirect_args)
    return render(
        request, 'project/project_step_create_edit.html', {
            'project': project,
            'project_step': project_step,
            'project_step_form': project_step_form,
        })
示例#7
0
 def test_multiple_images_valid(self):
     project_image1 = self.create_project_image()
     project_image2 = self.create_project_image()
     project_images = (project_image1, project_image2)
     post_data = {
         'title': self.get_random_string(),
         'content': self.get_random_string(),
         'images': ','.join([str(image.id) for image in project_images])
     }
     request = self.request_factory.post('/', post_data)
     form = ProjectStepForm(request.POST, instance=self.project_step)
     if form.is_valid():
         project_step = form.save()
         self.assertEqual(tuple(project_step.gallery.all()), project_images)
     else:
         self.fail('Form should be valid')
示例#8
0
 def test_multiple_images_valid(self):
     project_image1 = self.create_project_image()
     project_image2 = self.create_project_image()
     project_images = (project_image1, project_image2)
     post_data = {
         'title': self.get_random_string(),
         'content': self.get_random_string(),
         'images': ','.join([str(image.id) for image in project_images])
     }
     request = self.request_factory.post('/', post_data)
     form = ProjectStepForm(request.POST, instance=self.project_step)
     if form.is_valid():
         project_step = form.save()
         self.assertEqual(tuple(project_step.gallery.all()), project_images)
     else:
         self.fail('Form should be valid')