mirror of
https://github.com/jazzband/django-avatar.git
synced 2026-03-16 22:20:30 +00:00
Add avatar_deleted signal
This commit is contained in:
parent
9e1bb44c98
commit
fb195a570f
3 changed files with 11 additions and 1 deletions
|
|
@ -2,3 +2,4 @@ import django.dispatch
|
|||
|
||||
|
||||
avatar_updated = django.dispatch.Signal(providing_args=["user", "avatar"])
|
||||
avatar_deleted = django.dispatch.Signal(providing_args=["user", "avatar"])
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ from django.contrib.auth.decorators import login_required
|
|||
from avatar.conf import settings
|
||||
from avatar.forms import PrimaryAvatarForm, DeleteAvatarForm, UploadAvatarForm
|
||||
from avatar.models import Avatar
|
||||
from avatar.signals import avatar_updated
|
||||
from avatar.signals import avatar_updated, avatar_deleted
|
||||
from avatar.utils import (get_primary_avatar, get_default_avatar_url,
|
||||
invalidate_cache)
|
||||
|
||||
|
|
@ -136,6 +136,10 @@ def delete(request, extra_context=None, next_override=None, *args, **kwargs):
|
|||
if request.method == 'POST':
|
||||
if delete_avatar_form.is_valid():
|
||||
ids = delete_avatar_form.cleaned_data['choices']
|
||||
for a in avatars:
|
||||
if six.text_type(a.id) in ids:
|
||||
avatar_deleted.send(sender=Avatar, user=request.user,
|
||||
avatar=a)
|
||||
if six.text_type(avatar.id) in ids and avatars.count() > len(ids):
|
||||
# Find the next best avatar, and set it as the new primary
|
||||
for a in avatars:
|
||||
|
|
|
|||
|
|
@ -10,7 +10,9 @@ from avatar.conf import settings
|
|||
from avatar.utils import get_primary_avatar, get_user_model
|
||||
from avatar.models import Avatar
|
||||
from avatar.templatetags import avatar_tags
|
||||
from avatar.signals import avatar_deleted
|
||||
from PIL import Image
|
||||
from unittest.mock import Mock
|
||||
|
||||
|
||||
def upload_helper(o, filename):
|
||||
|
|
@ -106,6 +108,8 @@ class AvatarTests(TestCase):
|
|||
self.test_normal_image_upload()
|
||||
avatar = Avatar.objects.filter(user=self.user)
|
||||
self.assertEqual(len(avatar), 1)
|
||||
receiver = Mock()
|
||||
avatar_deleted.connect(receiver)
|
||||
response = self.client.post(reverse('avatar_delete'), {
|
||||
'choices': [avatar[0].id],
|
||||
}, follow=True)
|
||||
|
|
@ -113,6 +117,7 @@ class AvatarTests(TestCase):
|
|||
self.assertEqual(len(response.redirect_chain), 1)
|
||||
count = Avatar.objects.filter(user=self.user).count()
|
||||
self.assertEqual(count, 0)
|
||||
self.assertEqual(receiver.call_count, 1)
|
||||
|
||||
def test_delete_primary_avatar_and_new_primary(self):
|
||||
self.test_there_can_be_only_one_primary_avatar()
|
||||
|
|
|
|||
Loading…
Reference in a new issue