forms.py
3.51 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# coding=utf-8
from django import forms
from django.utils.translation import ugettext_lazy as _, pgettext_lazy
from django.utils.html import strip_tags
from django.db.models import Q
from resubmit.widgets import ResubmitFileWidget
from topics.models import Resource
from .models import GeneralPost, CategoryPost, SubjectPost, Comment
class Validation(forms.ModelForm):
MAX_UPLOAD_SIZE = 5*1024*1024
def __init__(self, *args, **kwargs):
super(Validation, self).__init__(*args, **kwargs)
CHOICES = (("comment", pgettext_lazy("form","Comment")), ("help", pgettext_lazy("form","Ask for Help")))
self.fields['action'].choices = CHOICES
def clean_post(self):
post = self.cleaned_data.get('post', '')
cleaned_post = strip_tags(post)
if cleaned_post == '':
self._errors['post'] = [_('This field is required.')]
return ValueError
return post
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 GeneralPostForm(Validation):
class Meta:
model = GeneralPost
fields = ['action', 'post', 'image']
widgets = {
'action': forms.RadioSelect,
'post': forms.Textarea,
'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
}
class CategoryPostForm(Validation):
class Meta:
model = CategoryPost
fields = ['action', 'post', 'image']
widgets = {
'action': forms.RadioSelect,
'post': forms.Textarea,
'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
}
class SubjectPostForm(Validation):
def __init__(self, *args, **kwargs):
super(SubjectPostForm, self).__init__(*args, **kwargs)
user = kwargs['initial'].get('user', None)
subject = kwargs['initial'].get('subject', None)
if not kwargs['instance'] is None:
subject = self.instance.space
if user.is_staff:
self.fields['resource'].choices = [(r.id, str(r)) for r in Resource.objects.filter(Q(topic__subject = subject))]
else:
self.fields['resource'].choices = [(r.id, str(r)) for r in Resource.objects.filter(Q(topic__subject = subject) & (Q(all_students = True) | Q(students = user) | Q(groups__participants = user)))]
self.fields['resource'].choices.append(("", _("Choose an especific resource")))
class Meta:
model = SubjectPost
fields = ['action', 'resource', 'post', 'image']
widgets = {
'action': forms.RadioSelect,
'post': forms.Textarea,
'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
}
class ResourcePostForm(Validation):
class Meta:
model = SubjectPost
fields = ['action', 'post', 'image']
widgets = {
'action': forms.RadioSelect,
'post': forms.Textarea,
'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
}
class CommentForm(forms.ModelForm):
MAX_UPLOAD_SIZE = 5*1024*1024
def clean_comment(self):
comment = self.cleaned_data.get('comment', '')
cleaned_comment = strip_tags(comment)
if cleaned_comment == '':
self._errors['comment'] = [_('This field is required.')]
return ValueError
return comment
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 Meta:
model = Comment
fields = ['comment', 'image']
widgets = {
'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
}