Commit ec6c233a9e59857bc9c927036e2ae9806e785036

Authored by Gustavo Bernardo
1 parent 44a958fb

solved some bugs when trying to create a user without having the uploads folder,…

… and removing duplicated user images
Showing 1 changed file with 39 additions and 5 deletions   Show diff stats
users/forms.py
@@ -6,10 +6,11 @@ from rolepermissions.shortcuts import assign_role @@ -6,10 +6,11 @@ from rolepermissions.shortcuts import assign_role
6 from django.contrib.auth import update_session_auth_hash 6 from django.contrib.auth import update_session_auth_hash
7 from django.core.validators import validate_email 7 from django.core.validators import validate_email
8 from django.core.exceptions import ValidationError 8 from django.core.exceptions import ValidationError
9 - 9 +from os.path import join
10 from resubmit.widgets import ResubmitFileWidget 10 from resubmit.widgets import ResubmitFileWidget
11 from PIL import Image 11 from PIL import Image
12 - 12 +import os
  13 +from amadeus import settings
13 14
14 from .models import User 15 from .models import User
15 16
@@ -71,6 +72,7 @@ class RegisterUserForm(Validation): @@ -71,6 +72,7 @@ class RegisterUserForm(Validation):
71 72
72 def save(self, commit=True): 73 def save(self, commit=True):
73 super(RegisterUserForm, self).save(commit=False) 74 super(RegisterUserForm, self).save(commit=False)
  75 + self.deletepath = ""
74 76
75 x = self.cleaned_data.get('x') 77 x = self.cleaned_data.get('x')
76 y = self.cleaned_data.get('y') 78 y = self.cleaned_data.get('y')
@@ -81,12 +83,22 @@ class RegisterUserForm(Validation): @@ -81,12 +83,22 @@ class RegisterUserForm(Validation):
81 image = Image.open(self.instance.image) 83 image = Image.open(self.instance.image)
82 cropped_image = image.crop((x, y, w+x, h+y)) 84 cropped_image = image.crop((x, y, w+x, h+y))
83 resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS) 85 resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
  86 +
  87 + folder_path = join(settings.MEDIA_ROOT, 'users')
  88 + #check if the folder already exists
  89 + if not os.path.isdir(folder_path):
  90 + os.makedirs(folder_path)
  91 +
  92 + if ("users" not in self.instance.image.path):
  93 + self.deletepath = self.instance.image.path
  94 +
84 resized_image.save(self.instance.image.path) 95 resized_image.save(self.instance.image.path)
85 96
86 self.instance.set_password(self.cleaned_data['new_password']) 97 self.instance.set_password(self.cleaned_data['new_password'])
87 98
88 self.instance.save() 99 self.instance.save()
89 - 100 + if (self.deletepath):
  101 + os.remove(self.deletepath)
90 return self.instance 102 return self.instance
91 103
92 class Meta: 104 class Meta:
@@ -111,6 +123,7 @@ class ProfileForm(Validation): @@ -111,6 +123,7 @@ class ProfileForm(Validation):
111 123
112 def save(self, commit=True): 124 def save(self, commit=True):
113 super(ProfileForm, self).save(commit=False) 125 super(ProfileForm, self).save(commit=False)
  126 + self.deletepath = ""
114 x = self.cleaned_data.get('x') 127 x = self.cleaned_data.get('x')
115 y = self.cleaned_data.get('y') 128 y = self.cleaned_data.get('y')
116 w = self.cleaned_data.get('width') 129 w = self.cleaned_data.get('width')
@@ -120,10 +133,20 @@ class ProfileForm(Validation): @@ -120,10 +133,20 @@ class ProfileForm(Validation):
120 image = Image.open(self.instance.image) 133 image = Image.open(self.instance.image)
121 cropped_image = image.crop((x, y, w+x, h+y)) 134 cropped_image = image.crop((x, y, w+x, h+y))
122 resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS) 135 resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
  136 +
  137 + folder_path = join(settings.MEDIA_ROOT, 'users')
  138 + #check if the folder already exists
  139 + if not os.path.isdir(folder_path):
  140 + os.makedirs(folder_path)
  141 +
  142 + if ("users" not in self.instance.image.path):
  143 + self.deletepath = self.instance.image.path
  144 +
123 resized_image.save(self.instance.image.path) 145 resized_image.save(self.instance.image.path)
124 146
125 self.instance.save() 147 self.instance.save()
126 - 148 + if (self.deletepath):
  149 + os.remove(self.deletepath)
127 return self.instance 150 return self.instance
128 151
129 class Meta: 152 class Meta:
@@ -158,6 +181,7 @@ class UserForm(Validation): @@ -158,6 +181,7 @@ class UserForm(Validation):
158 181
159 def save(self, commit=True): 182 def save(self, commit=True):
160 super(UserForm, self).save(commit=False) 183 super(UserForm, self).save(commit=False)
  184 + self.deletepath = ""
161 185
162 x = self.cleaned_data.get('x') 186 x = self.cleaned_data.get('x')
163 y = self.cleaned_data.get('y') 187 y = self.cleaned_data.get('y')
@@ -168,6 +192,15 @@ class UserForm(Validation): @@ -168,6 +192,15 @@ class UserForm(Validation):
168 image = Image.open(self.instance.image) 192 image = Image.open(self.instance.image)
169 cropped_image = image.crop((x, y, w+x, h+y)) 193 cropped_image = image.crop((x, y, w+x, h+y))
170 resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS) 194 resized_image = cropped_image.resize((200, 200), Image.ANTIALIAS)
  195 +
  196 + folder_path = join(settings.MEDIA_ROOT, 'users')
  197 + #check if the folder already exists
  198 + if not os.path.isdir(folder_path):
  199 + os.makedirs(folder_path)
  200 +
  201 + if ("users" not in self.instance.image.path):
  202 + self.deletepath = self.instance.image.path
  203 +
171 resized_image.save(self.instance.image.path) 204 resized_image.save(self.instance.image.path)
172 205
173 206
@@ -175,7 +208,8 @@ class UserForm(Validation): @@ -175,7 +208,8 @@ class UserForm(Validation):
175 self.instance.set_password(self.cleaned_data['new_password']) 208 self.instance.set_password(self.cleaned_data['new_password'])
176 209
177 self.instance.save() 210 self.instance.save()
178 - 211 + if (self.deletepath):
  212 + os.remove(self.deletepath)
179 return self.instance 213 return self.instance
180 214
181 class Meta: 215 class Meta: