Skip to content

Commit

Permalink
avatar delete
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Sep 20, 2020
1 parent fc6b4c3 commit d671fcf
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions spirit/core/static/spirit/stylesheets/src/_base.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,14 @@ del.diff {
margin: 0 auto;
}
}
.profile__form {
display: flex;
flex-direction: column;
// avatar
.field:nth-child(3) {
order: -1;
}
}
// Profile_end
// Profile_settings_start
.profile__settings {
Expand Down
17 changes: 15 additions & 2 deletions spirit/user/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,37 @@ class Meta:
widgets = {'avatar': ImageWidget}

def __init__(self, *args, **kwargs):
super(UserProfileForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
now = timezone.localtime(timezone.now())
self.fields['timezone'].help_text = _('Current time is: %(date)s %(time)s') % {
'date': defaultfilters.date(now),
'time': defaultfilters.time(now)}
self.fields['avatar'].widget.clear_checkbox_label = _('Remove avatar')
self.fields['avatar'].widget.attrs['accept'] = (
", ".join(
'.%s' % ext
for ext in sorted(settings.ST_ALLOWED_AVATAR_FORMAT)))

def clean_avatar(self):
file = self.cleaned_data['avatar']
ext = os.path.splitext(file.name)[1].lstrip('.').lower()
# can be bool (clear) or not an image (empty)
if not hasattr(file, 'image'):
return file

ext = os.path.splitext(file.name)[1].lstrip('.').lower()
if (ext not in settings.ST_ALLOWED_AVATAR_FORMAT or
file.image.format.lower() not in settings.ST_ALLOWED_AVATAR_FORMAT):
raise forms.ValidationError(
_("Unsupported file format. Supported formats are %s.") %
", ".join(settings.ST_ALLOWED_AVATAR_FORMAT))

return file

def clean(self):
cleaned_data = super().clean()
if self.instance.pk is not None:
if self.instance.avatar != cleaned_data['avatar']:
# XXX maybe add setting.AVATAR_STORAGE
# to support overwrite and prevent race conditions
self.instance.avatar.delete()
return cleaned_data
2 changes: 1 addition & 1 deletion spirit/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def save(self, *args, **kwargs):
if self.is_administrator:
self.is_moderator = True

super(UserProfile, self).save(*args, **kwargs)
super().save(*args, **kwargs)

def get_absolute_url(self):
return reverse(
Expand Down

0 comments on commit d671fcf

Please sign in to comment.