Skip to content

Commit

Permalink
Moving Crop clean method into model, not the form
Browse files Browse the repository at this point in the history
  • Loading branch information
ortsed committed Sep 5, 2012
1 parent 085b50b commit 45d964f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
14 changes: 13 additions & 1 deletion cropduster/models.py
Expand Up @@ -9,7 +9,9 @@

CROPDUSTER_UPLOAD_PATH = getattr(settings, "CROPDUSTER_UPLOAD_PATH", settings.MEDIA_ROOT)

IMAGE_SAVE_PARAMS = {"quality" :95}
IMAGE_SAVE_PARAMS = {
"quality" :95
}

MANUALLY_CROP = 0
AUTO_CROP = 1
Expand Down Expand Up @@ -180,6 +182,16 @@ def save(self, *args, **kwargs):
# loop through the other sizes of the same aspect ratio, and create those crops
for size in sizes:
self.image.rescale(cropped_image, size=size)
def clean(self):

if not self.crop_x or not self.crop_y:
raise ValidationError("Missing crop values")

if self.crop_x < 0 or self.crop_y < 0:
raise ValidationError("Crop positions must be non-negative")

if self.crop_w <= 0 or self.crop_h <= 0:
raise ValidationError("Crop measurements must be greater than zero")


class Image(CachingMixin, models.Model):
Expand Down
16 changes: 0 additions & 16 deletions cropduster/views.py
Expand Up @@ -37,23 +37,7 @@ class Meta:
widgets = {
"image": TextInput(),
}
def clean(self):

cleaned_data = super(CropForm, self).clean()

if "crop_x" not in cleaned_data or "crop_y" not in cleaned_data:
self._errors.clear()
raise ValidationError("Missing crop values")

if cleaned_data["crop_x"] < 0 or cleaned_data["crop_y"] < 0:
self._errors.clear()
raise ValidationError("Crop positions must be non-negative")

if cleaned_data["crop_w"] <= 0 or cleaned_data["crop_h"] <= 0:
self._errors.clear()
raise ValidationError("Crop measurements must be greater than zero")

return self.cleaned_data

@csrf_exempt
def upload(request):
Expand Down

0 comments on commit 45d964f

Please sign in to comment.