Example #1
0
 def test_errors(self):
     form = SpotForm({})
     errors = form.errors
     self.assertTrue("name" in errors,
                     "Default spot form requires a spot name")
     self.assertFalse("capacity" in errors,
                      "Default spot form doesn't require a spot name")
Example #2
0
 def test_errors(self):
     """Assert that an empty form produces validation errors"""
     form = SpotForm({})
     errors = form.errors
     self.assertIn('name', errors)
     # Capacity is not required, so it should not have an error
     self.assertNotIn('capacity', errors)
Example #3
0
 def test_default(self):
     form = SpotForm({})
     self.assertEqual(
         form.__class__,
         DefaultSpotForm({}).__class__,
         "Tests shouldn't be run with a defined SPOTSEEKER_SPOT_FORM")
Example #4
0
    def build_and_save_from_input(self, request, spot):
        body = request.read()
        try:
            json_values = json.loads(body)
        except Exception as e:
            raise RESTException("Unable to parse JSON", status_code=400)

        partial_update = False
        stash = {}
        is_new = spot is None

        spot_pre_build.send(sender=SpotForm.implementation(),
                            request=request,
                            json_values=json_values,
                            spot=spot,
                            partial_update=partial_update,
                            stash=stash)

        self._build_spot_types(json_values, spot, partial_update)
        self._build_spot_location(json_values)

        spot_pre_save.send(sender=SpotForm.implementation(),
                           request=request,
                           json_values=json_values,
                           spot=spot,
                           partial_update=partial_update,
                           stash=stash)

        # Remve excluded fields
        excludefields = set(SpotForm.implementation().Meta.exclude)
        for fieldname in excludefields:
            if fieldname in json_values:
                del json_values[fieldname]

        if spot is not None and partial_update:
            # Copy over the existing values
            for field in spot._meta.fields:
                if field.name in excludefields:
                    continue
                if not field.name in json_values:
                    json_values[field.name] = getattr(spot, field.name)

            # spottypes is not included in the above copy, do it manually
            if not 'spottypes' in json_values:
                json_values['spottypes'] = [t.pk for t in spot.spottypes.all()]

        form = SpotForm(json_values, instance=spot)
        if not form.is_valid():
            raise RESTFormInvalidError(form)

        spot = form.save()

        spot_post_save.send(sender=SpotForm.implementation(),
                            request=request,
                            spot=spot,
                            partial_update=partial_update,
                            stash=stash)

        # gets the current etag
        spot = Spot.get_with_external(spot.pk)

        if is_new:
            response = HttpResponse(status=201)
            response['Location'] = spot.rest_url()
        else:
            response = JSONResponse(spot.json_data_structure(), status=200)
        response["ETag"] = spot.etag

        spot_post_build.send(sender=SpotForm.implementation(),
                             request=request,
                             response=response,
                             spot=spot,
                             partial_update=partial_update,
                             stash=stash)

        return response
Example #5
0
 def test_default(self):
     form = SpotForm({})
     self.assertIs(
         type(form), DefaultSpotForm,
         "Tests shouldn't be run with a defined "
         "SPOTSEEKER_SPOT_FORM")