def post(self, request, *args, **kwargs): form = AddProjectForm(request.POST) # Let add crawl model work normally if it is not dealing with an xmlhttprequest. if request.is_ajax(): if form.is_valid(): self.object = form.save() return HttpResponse( json.dumps({ "url": self.object.get_absolute_url(), "id": self.object.id, "name": self.object.name, "slug": self.object.slug, "description": self.object.description, }), status=200, content_type="application/json" ) else: return HttpResponse( json.dumps({ "form_errors": form.errors, }), status=500, content_type="application/json", ) else: return super(AddProjectView, self).post(request, *args, **kwargs)
def post(self, request, *args, **kwargs): form = AddProjectForm(request.POST) # Let add crawl model work normally if it is not dealing with an xmlhttprequest. if request.is_ajax(): if form.is_valid(): self.object = form.save() return HttpResponse(json.dumps({ "url": self.object.get_absolute_url(), "id": self.object.id, "name": self.object.name, "slug": self.object.slug, "description": self.object.description, }), status=200, content_type="application/json") else: return HttpResponse( json.dumps({ "form_errors": form.errors, }), status=500, content_type="application/json", ) else: return super(AddProjectView, self).post(request, *args, **kwargs)
def test_existing_project_error(self): form_data = { 'name': 'Bicycles for sale', 'description': 'cats cats cats'} form = AddProjectForm(data=form_data) assert form.is_valid() is False assert 'Project with this Name already exists.' in form.errors['name']
def test_project_form_no_name(self): form_data = {'description': 'cats cats cats'} form = AddProjectForm(data=form_data) assert form.is_valid() is False assert 'This field is required.' in form.errors['name']
def test_project_form(self): form_data = {'name': 'CATS', 'description': 'cats cats cats'} form = AddProjectForm(data=form_data) assert form.is_valid()
def test_project_form_no_name(self): form_data = { 'description': 'cats cats cats'} form = AddProjectForm(data=form_data) assert form.is_valid() is False assert 'This field is required.' in form.errors['name']
def test_project_form(self): form_data = { 'name': 'CATS', 'description': 'cats cats cats'} form = AddProjectForm(data=form_data) assert form.is_valid()