Commit 66990953 authored by Julia Santos's avatar Julia Santos

Added image validation

parent 5635366c
from django import forms
from django.core.validators import FileExtensionValidator
class LoginForm(forms.Form):
user = forms.CharField(label = 'username', max_length = 100)
......@@ -9,4 +10,4 @@ class ProductTypeForm(forms.Form):
Product_Category = forms.CharField(max_length = 255)
Product_Description = forms.CharField(max_length = 255, required = False)
To_Buy = forms.BooleanField(initial = False, required = False)
Icon = forms.ImageField()
\ No newline at end of file
Icon = forms.ImageField(validators=[FileExtensionValidator( ['png','jpg'] ) ])
\ No newline at end of file
......@@ -3,6 +3,7 @@ from django.db.models import Q
from .forms import LoginForm, ProductTypeForm
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.forms import ValidationError
from django.views.generic.edit import DeleteView
from .models import User, Product_Type, Item, Recipe
......@@ -58,9 +59,17 @@ def add_product_type(request):
product_description = form.cleaned_data.get("Product_Description")
to_buy = form.cleaned_data.get("To_Buy")
icon = form.cleaned_data.get("Icon")
if icon:
if icon.size > 4*1024*1024: #( > 4mb )
raise ValidationError("Image file too large")
return icon
else:
raise ValidationError("Couldn't read uploaded image")
#item = Product_Type.objects.create(Product_Name = product_name, Product_Category = product_category, Product_Description = product_description, To_Buy = to_buy, User_id = self.request.user.User_ID, Icon = icon)
instance = Product_Type.objects.create(Product_Name = product_name, Product_Category = product_category, Product_Description = product_description, To_Buy = to_buy, User_ID = request.user, Icon = icon)
instance.save()
return HttpResponseRedirect('/fridge/list/')
else:
form = ProductTypeForm()
context['form'] = form
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment