forms.py
1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# coding=utf-8
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.utils.html import strip_tags
from django.db.models import Q
from resubmit.widgets import ResubmitFileWidget
from .models import TalkMessages
class Validation(forms.ModelForm):
MAX_UPLOAD_SIZE = 5*1024*1024
def clean_text(self):
text = self.cleaned_data.get('text', '')
cleaned_text = strip_tags(text)
if cleaned_text == '':
self._errors['text'] = [_('This field is required.')]
return ValueError
return text
def clean_image(self):
image = self.cleaned_data.get('image', False)
if image:
if hasattr(image, '_size'):
if image._size > self.MAX_UPLOAD_SIZE:
self._errors['image'] = [_("The image is too large. It should have less than 5MB.")]
return ValueError
return image
class ChatMessageForm(Validation):
class Meta:
model = TalkMessages
fields = ['text', 'image']
widgets = {
'text': forms.Textarea,
'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
}