Commit 5c06bf58344cb4e05a60ae4c1e83ca82e1a5b4c9

Authored by Gustavo
1 parent 2358e15a

Some adjusts in the bulletin forms.py

Showing 1 changed file with 21 additions and 10 deletions   Show diff stats
bulletin/forms.py
... ... @@ -11,6 +11,7 @@ from resubmit.widgets import ResubmitFileWidget
11 11  
12 12 class BulletinForm(forms.ModelForm):
13 13 subject = None
  14 + MAX_UPLOAD_SIZE = 1024*1024
14 15  
15 16 def __init__(self, *args, **kwargs):
16 17 super(BulletinForm, self).__init__(*args, **kwargs)
... ... @@ -21,14 +22,13 @@ class BulletinForm(forms.ModelForm):
21 22 self.subject = self.instance.topic.subject
22 23 self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True))
23 24  
24   - self.fields['students'].queryset = self.subject.students.all()
25 25 self.fields['groups'].queryset = self.subject.group_subject.all()
26 26  
27 27 tags = forms.CharField(label = _('Tags'), required = False)
28 28  
29 29 class Meta:
30 30 model = Bulletin
31   - fields = ['name', 'content', 'brief_description', 'all_students', 'students', 'groups', 'show_window', 'visible']
  31 + fields = ['name', 'content', 'brief_description', 'all_students', 'groups', 'show_window', 'visible','file_content']
32 32 labels = {
33 33 'name': _('Bulletin name'),
34 34 'content': _('Bulletin content'),
... ... @@ -36,8 +36,8 @@ class BulletinForm(forms.ModelForm):
36 36 widgets = {
37 37 'content': forms.Textarea,
38 38 'brief_description': forms.Textarea,
39   - 'students': forms.SelectMultiple,
40 39 'groups': forms.SelectMultiple,
  40 + 'all_students': forms.HiddenInput(),
41 41 }
42 42  
43 43 def clean_name(self):
... ... @@ -58,16 +58,27 @@ class BulletinForm(forms.ModelForm):
58 58  
59 59 return name
60 60  
61   - def clean_content(self):
62   - content = self.cleaned_data.get('content', '')
63   - cleaned_content = strip_tags(content)
  61 + def clean_all_students(self):
  62 + all_students = True
  63 + return all_students
  64 +
64 65  
65   - if cleaned_content == '':
66   - self._errors['content'] = [_('This field is required.')]
  66 + def clean_file_content(self):
  67 + file_content = self.cleaned_data.get('file_content', False)
67 68  
68   - return ValueError
  69 + if file_content:
  70 + if hasattr(file_content, '_size'):
  71 + if file_content._size > self.MAX_UPLOAD_SIZE:
  72 + self._errors['file_content'] = [_("The file is too large. It should have less than 1MB.")]
  73 +
  74 + return ValueError
  75 +
  76 + elif not self.instance.pk:
  77 + self._errors['file_content'] = [_('This field is required.')]
  78 +
  79 + return ValueError
69 80  
70   - return content
  81 + return file_content
71 82  
72 83 def save(self, commit = True):
73 84 super(BulletinForm, self).save(commit = True)
... ...