Commit 50c09a62cb14169a240a1af708dae83e0c1ea519

Authored by fbormann
2 parents 495dbd8a 6f3a606b

Merge branch 'refactoring' of https://github.com/amadeusproject/amadeuslms into refactoring

.gitignore
... ... @@ -51,5 +51,6 @@ coverage.xml
51 51 angular_api
52 52 logs/
53 53 amadeus/uploads/
  54 +data/
54 55 links/static/images/
55 56 uploads/
... ...
amadeus/settings.py
... ... @@ -56,6 +56,7 @@ INSTALLED_APPS = [
56 56 'django_crontab',
57 57 'django_cron',
58 58 'channels',
  59 + 'resubmit', # Utilizado para salvar arquivos na cache, para caso o formulario não seja preenchido corretamente o usuário não precise fazer o upload outra vez dos arquivos
59 60  
60 61 'amadeus',
61 62 'users',
... ... @@ -120,6 +121,17 @@ TEMPLATES = [
120 121 },
121 122 ]
122 123  
  124 +
  125 +CACHES = {
  126 + 'default': {
  127 + 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
  128 + },
  129 + "resubmit": {
  130 + 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
  131 + "LOCATION": os.path.join(BASE_DIR, 'data/cache/resubmit'),
  132 + },
  133 +}
  134 +
123 135 WSGI_APPLICATION = 'amadeus.wsgi.application'
124 136  
125 137 SESSION_SECURITY_WARN_AFTER = 1140
... ... @@ -357,11 +369,11 @@ SUMMERNOTE_CONFIG = {
357 369 # Set `upload_to` function for attachments.
358 370 #'attachment_upload_to': my_custom_upload_to_func(),
359 371  
360   -
  372 +
361 373  
362 374 }
363 375  
364 376 try:
365 377 from .local_settings import *
366 378 except ImportError:
367   - pass
368 379 \ No newline at end of file
  380 + pass
... ...
mural/forms.py
... ... @@ -4,6 +4,8 @@ from django.utils.translation import ugettext_lazy as _
4 4 from django.utils.html import strip_tags
5 5 from django.db.models import Q
6 6  
  7 +from resubmit.widgets import ResubmitFileWidget
  8 +
7 9 from topics.models import Resource
8 10  
9 11 from .models import GeneralPost, CategoryPost, SubjectPost, Comment
... ... @@ -14,7 +16,7 @@ class Validation(forms.ModelForm):
14 16 def clean_post(self):
15 17 post = self.cleaned_data.get('post', '')
16 18 cleaned_post = strip_tags(post)
17   -
  19 +
18 20 if cleaned_post == '':
19 21 self._errors['post'] = [_('This field is required.')]
20 22  
... ... @@ -34,13 +36,15 @@ class Validation(forms.ModelForm):
34 36  
35 37 return image
36 38  
  39 +
37 40 class GeneralPostForm(Validation):
38 41 class Meta:
39 42 model = GeneralPost
40 43 fields = ['action', 'post', 'image']
41 44 widgets = {
42 45 'action': forms.RadioSelect,
43   - 'post': forms.Textarea
  46 + 'post': forms.Textarea,
  47 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
44 48 }
45 49  
46 50 class CategoryPostForm(Validation):
... ... @@ -49,7 +53,8 @@ class CategoryPostForm(Validation):
49 53 fields = ['action', 'post', 'image']
50 54 widgets = {
51 55 'action': forms.RadioSelect,
52   - 'post': forms.Textarea
  56 + 'post': forms.Textarea,
  57 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
53 58 }
54 59  
55 60 class SubjectPostForm(Validation):
... ... @@ -60,7 +65,7 @@ class SubjectPostForm(Validation):
60 65 subject = kwargs['initial'].get('subject', None)
61 66  
62 67 if not kwargs['instance'] is None:
63   - subject = self.instance.space
  68 + subject = self.instance.space
64 69  
65 70 if user.is_staff:
66 71 self.fields['resource'].choices = [(r.id, str(r)) for r in Resource.objects.filter(Q(topic__subject = subject))]
... ... @@ -74,7 +79,8 @@ class SubjectPostForm(Validation):
74 79 fields = ['action', 'resource', 'post', 'image']
75 80 widgets = {
76 81 'action': forms.RadioSelect,
77   - 'post': forms.Textarea
  82 + 'post': forms.Textarea,
  83 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
78 84 }
79 85  
80 86 class ResourcePostForm(Validation):
... ... @@ -83,7 +89,8 @@ class ResourcePostForm(Validation):
83 89 fields = ['action', 'post', 'image']
84 90 widgets = {
85 91 'action': forms.RadioSelect,
86   - 'post': forms.Textarea
  92 + 'post': forms.Textarea,
  93 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
87 94 }
88 95  
89 96 class CommentForm(forms.ModelForm):
... ... @@ -92,7 +99,7 @@ class CommentForm(forms.ModelForm):
92 99 def clean_comment(self):
93 100 comment = self.cleaned_data.get('comment', '')
94 101 cleaned_comment = strip_tags(comment)
95   -
  102 +
96 103 if cleaned_comment == '':
97 104 self._errors['comment'] = [_('This field is required.')]
98 105  
... ... @@ -114,4 +121,7 @@ class CommentForm(forms.ModelForm):
114 121  
115 122 class Meta:
116 123 model = Comment
117   - fields = ['comment', 'image']
118 124 \ No newline at end of file
  125 + fields = ['comment', 'image']
  126 + widgets = {
  127 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
  128 + }
... ...
mural/templates/mural/_form.html
... ... @@ -33,7 +33,7 @@
33 33 {% render_field form.resource class='form-control' %}
34 34  
35 35 <span id="helpBlock" class="help-block">{{ form.resource.help_text }}</span>
36   -
  36 +
37 37 {% if form.resource.errors %}
38 38 <div class="alert alert-danger alert-dismissible" role="alert">
39 39 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
... ... @@ -54,7 +54,7 @@
54 54 {% render_field form.post class='form-control text_simple_wysiwyg' %}
55 55  
56 56 <span id="helpBlock" class="help-block">{{ form.post.help_text }}</span>
57   -
  57 +
58 58 {% if form.post.errors %}
59 59 <div class="alert alert-danger alert-dismissible" role="alert">
60 60 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
... ... @@ -71,24 +71,15 @@
71 71  
72 72 <div class="form-group{% if form.has_error %} has-error {% endif %} is-fileinput">
73 73 {% render_field form.image %}
74   -
75   - <div class="input-group common-file-input">
76   - <input type="text" readonly="" class="form-control" placeholder="{% trans 'Choose your photo...' %}">
77   - <span class="input-group-btn input-group-sm">
78   - <button type="button" class="btn btn-fab btn-fab-mini">
79   - <i class="material-icons">attach_file</i>
80   - </button>
81   - </span>
82   - </div>
83 74  
84 75 <div class="filedrag">
85 76 {% trans 'Click or drop the file here' %}<br />
86   -
  77 +
87 78 <small>{% trans 'The file could not exceed 5MB.' %}</small>
88 79 </div>
89 80  
90 81 <span id="helpBlock" class="help-block">{{ form.image.help_text }}</span>
91   -
  82 +
92 83 {% if form.image.errors %}
93 84 <div class="alert alert-danger alert-dismissible" role="alert">
94 85 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
... ... @@ -137,7 +128,7 @@
137 128 var small = $("#id_image"),
138 129 filedrag = $(".filedrag"),
139 130 common = $(".common-file-input");
140   -
  131 +
141 132 // file select
142 133 small.on("change", FileSelectHandler);
143 134  
... ...
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
... ...
requirements.txt
... ... @@ -37,4 +37,5 @@ django-cron==0.5.0
37 37 django-crontab==0.7.1
38 38 python-dateutil==2.6.0
39 39 channels==1.0.3
40   -asgi_redis==1.0.0
41 40 \ No newline at end of file
  41 +asgi_redis==1.0.0
  42 +file-resubmit==0.1.0
... ...
users/forms.py
... ... @@ -5,6 +5,9 @@ from rolepermissions.shortcuts import assign_role
5 5 from django.contrib.auth import update_session_auth_hash
6 6 from django.core.validators import validate_email
7 7 from django.core.exceptions import ValidationError
  8 +
  9 +from resubmit.widgets import ResubmitFileWidget
  10 +
8 11 from .models import User
9 12  
10 13 class Validation(forms.ModelForm):
... ... @@ -19,7 +22,7 @@ class Validation(forms.ModelForm):
19 22 return email
20 23 except ValidationError:
21 24 self._errors['email'] = [_('You must insert an email address')]
22   -
  25 +
23 26 return ValueError
24 27  
25 28 def clean_image(self):
... ... @@ -37,13 +40,13 @@ class Validation(forms.ModelForm):
37 40 def clean_password2(self):
38 41 password = self.cleaned_data.get("new_password")
39 42 password2 = self.cleaned_data.get("password2")
40   -
  43 +
41 44 if password and password2 and password != password2:
42 45 self._errors['password2'] = [_('The confirmation password is incorrect.')]
43 46  
44 47 return ValueError
45   -
46   - return password2
  48 +
  49 + return password2
47 50  
48 51 class RegisterUserForm(Validation):
49 52 new_password = forms.CharField(label=_('Password'), widget = forms.PasswordInput(render_value = True, attrs = {'placeholder': _('Password') + ' *'}))
... ... @@ -53,11 +56,11 @@ class RegisterUserForm(Validation):
53 56  
54 57 def save(self, commit=True):
55 58 super(RegisterUserForm, self).save(commit=False)
56   -
  59 +
57 60 self.instance.set_password(self.cleaned_data['new_password'])
58 61  
59 62 self.instance.save()
60   -
  63 +
61 64 return self.instance
62 65  
63 66 class Meta:
... ... @@ -68,6 +71,7 @@ class RegisterUserForm(Validation):
68 71 'username': forms.TextInput(attrs = {'placeholder': _('Name') + ' *'}),
69 72 'last_name': forms.TextInput(attrs = {'placeholder': _('Last Name') + ' *'}),
70 73 'social_name': forms.TextInput(attrs = {'placeholder': _('Social Name')}),
  74 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
71 75 }
72 76  
73 77 class ProfileForm(Validation):
... ... @@ -75,9 +79,9 @@ class ProfileForm(Validation):
75 79  
76 80 def save(self, commit=True):
77 81 super(ProfileForm, self).save(commit=False)
78   -
  82 +
79 83 self.instance.save()
80   -
  84 +
81 85 return self.instance
82 86  
83 87 class Meta:
... ... @@ -86,7 +90,8 @@ class ProfileForm(Validation):
86 90 widgets = {
87 91 'description': forms.Textarea,
88 92 'username': forms.TextInput(attrs = {'readonly': 'readonly'}),
89   - 'last_name': forms.TextInput(attrs = {'readonly': 'readonly'})
  93 + 'last_name': forms.TextInput(attrs = {'readonly': 'readonly'}),
  94 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
90 95 }
91 96  
92 97 class UserForm(Validation):
... ... @@ -98,19 +103,19 @@ class UserForm(Validation):
98 103 super(UserForm, self).__init__(*args, **kwargs)
99 104  
100 105 self.is_edit = is_update
101   -
  106 +
102 107 new_password = forms.CharField(label = _('Password'), widget = forms.PasswordInput(render_value = True), required = False)
103 108 password2 = forms.CharField(label = _('Confirm Password'), widget = forms.PasswordInput(render_value = True), required = False)
104 109  
105 110  
106 111 def save(self, commit=True):
107 112 super(UserForm, self).save(commit=False)
108   -
  113 +
109 114 if not self.is_edit or self.cleaned_data['new_password'] != '':
110 115 self.instance.set_password(self.cleaned_data['new_password'])
111 116  
112 117 self.instance.save()
113   -
  118 +
114 119 return self.instance
115 120  
116 121 class Meta:
... ... @@ -118,6 +123,7 @@ class UserForm(Validation):
118 123 fields = ['email', 'username', 'last_name', 'social_name', 'description', 'show_email', 'image', 'is_staff', 'is_active',]
119 124 widgets = {
120 125 'description': forms.Textarea,
  126 + 'image': ResubmitFileWidget(attrs={'accept':'image/*'}),
121 127 }
122 128  
123 129 class ChangePassForm(Validation):
... ... @@ -143,13 +149,13 @@ class ChangePassForm(Validation):
143 149  
144 150 def save(self, commit=True):
145 151 super(ChangePassForm, self).save(commit=False)
146   -
  152 +
147 153 self.instance.set_password(self.cleaned_data['new_password'])
148 154  
149 155 update_session_auth_hash(self.request, self.instance)
150 156  
151 157 self.instance.save()
152   -
  158 +
153 159 return self.instance
154 160  
155 161 class Meta:
... ... @@ -173,7 +179,7 @@ class PassResetRequest(forms.Form):
173 179 return email
174 180 except ValidationError:
175 181 self._errors['email'] = [_('You must insert a valid email address')]
176   -
  182 +
177 183 return ValueError
178 184  
179 185 class SetPasswordForm(Validation):
... ... @@ -184,4 +190,4 @@ class SetPasswordForm(Validation):
184 190  
185 191 class Meta:
186 192 model = User
187   - fields = []
188 193 \ No newline at end of file
  194 + fields = []
... ...