Commit ec6c233a9e59857bc9c927036e2ae9806e785036
1 parent
44a958fb
Exists in
master
and in
2 other branches
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 | 6 | from django.contrib.auth import update_session_auth_hash |
7 | 7 | from django.core.validators import validate_email |
8 | 8 | from django.core.exceptions import ValidationError |
9 | - | |
9 | +from os.path import join | |
10 | 10 | from resubmit.widgets import ResubmitFileWidget |
11 | 11 | from PIL import Image |
12 | - | |
12 | +import os | |
13 | +from amadeus import settings | |
13 | 14 | |
14 | 15 | from .models import User |
15 | 16 | |
... | ... | @@ -71,6 +72,7 @@ class RegisterUserForm(Validation): |
71 | 72 | |
72 | 73 | def save(self, commit=True): |
73 | 74 | super(RegisterUserForm, self).save(commit=False) |
75 | + self.deletepath = "" | |
74 | 76 | |
75 | 77 | x = self.cleaned_data.get('x') |
76 | 78 | y = self.cleaned_data.get('y') |
... | ... | @@ -81,12 +83,22 @@ class RegisterUserForm(Validation): |
81 | 83 | image = Image.open(self.instance.image) |
82 | 84 | cropped_image = image.crop((x, y, w+x, h+y)) |
83 | 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 | 95 | resized_image.save(self.instance.image.path) |
85 | 96 | |
86 | 97 | self.instance.set_password(self.cleaned_data['new_password']) |
87 | 98 | |
88 | 99 | self.instance.save() |
89 | - | |
100 | + if (self.deletepath): | |
101 | + os.remove(self.deletepath) | |
90 | 102 | return self.instance |
91 | 103 | |
92 | 104 | class Meta: |
... | ... | @@ -111,6 +123,7 @@ class ProfileForm(Validation): |
111 | 123 | |
112 | 124 | def save(self, commit=True): |
113 | 125 | super(ProfileForm, self).save(commit=False) |
126 | + self.deletepath = "" | |
114 | 127 | x = self.cleaned_data.get('x') |
115 | 128 | y = self.cleaned_data.get('y') |
116 | 129 | w = self.cleaned_data.get('width') |
... | ... | @@ -120,10 +133,20 @@ class ProfileForm(Validation): |
120 | 133 | image = Image.open(self.instance.image) |
121 | 134 | cropped_image = image.crop((x, y, w+x, h+y)) |
122 | 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 | 145 | resized_image.save(self.instance.image.path) |
124 | 146 | |
125 | 147 | self.instance.save() |
126 | - | |
148 | + if (self.deletepath): | |
149 | + os.remove(self.deletepath) | |
127 | 150 | return self.instance |
128 | 151 | |
129 | 152 | class Meta: |
... | ... | @@ -158,6 +181,7 @@ class UserForm(Validation): |
158 | 181 | |
159 | 182 | def save(self, commit=True): |
160 | 183 | super(UserForm, self).save(commit=False) |
184 | + self.deletepath = "" | |
161 | 185 | |
162 | 186 | x = self.cleaned_data.get('x') |
163 | 187 | y = self.cleaned_data.get('y') |
... | ... | @@ -168,6 +192,15 @@ class UserForm(Validation): |
168 | 192 | image = Image.open(self.instance.image) |
169 | 193 | cropped_image = image.crop((x, y, w+x, h+y)) |
170 | 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 | 204 | resized_image.save(self.instance.image.path) |
172 | 205 | |
173 | 206 | |
... | ... | @@ -175,7 +208,8 @@ class UserForm(Validation): |
175 | 208 | self.instance.set_password(self.cleaned_data['new_password']) |
176 | 209 | |
177 | 210 | self.instance.save() |
178 | - | |
211 | + if (self.deletepath): | |
212 | + os.remove(self.deletepath) | |
179 | 213 | return self.instance |
180 | 214 | |
181 | 215 | class Meta: | ... | ... |