Commit 600590deeb462a4a59d571afee48b631e5d59346

Authored by Jailson Dias
1 parent 413e0a5d

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 usuario
Showing 2 changed files with 36 additions and 18 deletions   Show diff stats
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
... ...
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 = []
... ...