Commit ca2bad4026ff69b95ed7ee65e5fc53089e7fcae0

Authored by Jailson Dias
1 parent ced53701

Resolvendo bug do tipo Retrabalho com upload de arquivo em caso de erro no preen…

…chimento de um formulário para as UC's de pdf
Showing 1 changed file with 9 additions and 7 deletions   Show diff stats
pdf_file/forms.py
... ... @@ -3,6 +3,7 @@ from django.utils.translation import ugettext_lazy as _
3 3 from django.utils.html import strip_tags
4 4  
5 5 from subjects.models import Tag
  6 +from resubmit.widgets import ResubmitFileWidget
6 7  
7 8 from .models import PDFFile
8 9  
... ... @@ -17,7 +18,7 @@ class PDFFileForm(forms.ModelForm):
17 18 if self.instance.id:
18 19 self.subject = self.instance.topic.subject
19 20 self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True))
20   -
  21 +
21 22 self.fields['students'].queryset = self.subject.students.all()
22 23 self.fields['groups'].queryset = self.subject.group_subject.all()
23 24  
... ... @@ -32,11 +33,12 @@ class PDFFileForm(forms.ModelForm):
32 33 'brief_description': forms.Textarea,
33 34 'students': forms.SelectMultiple,
34 35 'groups': forms.SelectMultiple,
  36 + 'file': ResubmitFileWidget(attrs={'accept':'application/pdf, application/x-pdf, application/x-bzpdf, application/x-gzpdf'}),
35 37 }
36 38  
37 39 def clean_name(self):
38 40 name = self.cleaned_data.get('name', '')
39   -
  41 +
40 42 topics = self.subject.topic_subject.all()
41 43  
42 44 for topic in topics:
... ... @@ -44,7 +46,7 @@ class PDFFileForm(forms.ModelForm):
44 46 same_name = topic.resource_topic.filter(name__unaccent__iexact = name).exclude(id = self.instance.id).count()
45 47 else:
46 48 same_name = topic.resource_topic.filter(name__unaccent__iexact = name).count()
47   -
  49 +
48 50 if same_name > 0:
49 51 self._errors['name'] = [_('This subject already has a pdf file with this name')]
50 52  
... ... @@ -54,14 +56,14 @@ class PDFFileForm(forms.ModelForm):
54 56  
55 57 def clean_file(self):
56 58 file = self.cleaned_data.get('file', False)
57   -
  59 +
58 60 if file:
59 61 if hasattr(file, '_size'):
60 62 if file._size > self.MAX_UPLOAD_SIZE:
61 63 self._errors['file'] = [_("The file is too large. It should have less than 10MB.")]
62 64  
63 65 return ValueError
64   -
  66 +
65 67 elif not self.instance.pk:
66 68 self._errors['file'] = [_('This field is required.')]
67 69  
... ... @@ -82,7 +84,7 @@ class PDFFileForm(forms.ModelForm):
82 84 for prev in previous_tags:
83 85 if not prev.name in tags:
84 86 self.instance.tags.remove(prev)
85   -
  87 +
86 88 for tag in tags:
87 89 tag = tag.strip()
88 90  
... ... @@ -96,4 +98,4 @@ class PDFFileForm(forms.ModelForm):
96 98 if not new_tag in self.instance.tags.all():
97 99 self.instance.tags.add(new_tag)
98 100  
99   - return self.instance
100 101 \ No newline at end of file
  102 + return self.instance
... ...