Skip to content

Commit

Permalink
Merge pull request #179 from colab/delete_user
Browse files Browse the repository at this point in the history
Delete user
  • Loading branch information
simiaosimis committed May 12, 2016
2 parents a0b13d6 + 4d66a5b commit a8c52fd
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 24 deletions.
10 changes: 9 additions & 1 deletion colab/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.utils.translation import ugettext_lazy as _

from .signals import (user_created, user_password_changed,
user_basic_info_updated)
user_basic_info_updated, delete_user)
from .utils import mailman


Expand Down Expand Up @@ -93,6 +93,14 @@ def set_password(self, raw_password):
if self.pk:
user_password_changed.send(User, user=self, password=raw_password)

def delete(self, using=None):

emails = " ".join(self.emails.values_list('address', flat=True))
super(User, self).delete(using)

user = User.objects.filter(id=self.id)
if not user:
delete_user.send(User, user=self, emails=emails)

# We need to have `email` field set as unique but Django does not
# support field overriding (at least not until 1.6).
Expand Down
3 changes: 1 addition & 2 deletions colab/accounts/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

from django.dispatch import Signal


user_created = Signal(providing_args=['user', 'password'])
user_password_changed = Signal(providing_args=['user', 'password'])
user_basic_info_updated = Signal(providing_args=['user', 'update_email'])
delete_user = Signal(providing_args=['user', 'emails'])
4 changes: 1 addition & 3 deletions colab/super_archives/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from django.apps import AppConfig


Expand All @@ -7,5 +6,4 @@ class SuperArchivesConfig(AppConfig):
verbose_name = 'Super Archives'

def ready(self):
pass
# from . import signals
import colab.super_archives.signals # NOQA
29 changes: 11 additions & 18 deletions colab/super_archives/signals.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@

from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings

from .models import EmailAddress
from django.dispatch import receiver
from colab.accounts.signals import (delete_user)


@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_email_address(sender, instance, created, **kwargs):
if not created:
return
@receiver(delete_user)
def delete_user_from_superarchive(sender, **kwargs):
user = kwargs.get('user')
emails = []

email, email_created = EmailAddress.objects.get_or_create(
address=instance.email,
defaults={
'real_name': instance.get_full_name(),
'user': instance,
}
)
if kwargs.get('emails'):
emails = kwargs.get('emails').split(' ')

email.user = instance
email.save()
for email in emails:
EmailAddress.objects.filter(address=email).first().delete()
user.update_subscription(email, [])
43 changes: 43 additions & 0 deletions colab/super_archives/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding:utf-8 -*-

from mock import patch

from colab.accounts.models import User
from django.test import TestCase
from colab.super_archives.models import EmailAddress


class SignalsTest(TestCase):
def setUp(self):
self.user = self.create_user()

def create_user(self):
user = User()
user.username = "USERtestCoLaB"
user.set_password("123colab4")
user.email = "usertest@colab.com.br"
user.id = 1
user.first_name = "USERtestCoLaB"
user.last_name = "COLAB"
user.save()

return user

@patch.object(User, 'update_subscription')
def test_delete_user_without_email(self, update_subscription_mock):
update_subscription_mock.return_value = True
self.user.delete()
self.assertEqual(0, update_subscription_mock.call_count)

@patch.object(User, 'update_subscription')
def test_delete_user_with_email(self, update_subscription_mock):
update_subscription_mock.return_value = True

EmailAddress.objects.get_or_create(user=self.user,
address="usertest@colab.com.br")
EmailAddress.objects.get_or_create(user=self.user,
address="teste@gmail.com")

self.user.delete()
self.assertEqual(2, update_subscription_mock.call_count)
self.assertEqual(0, EmailAddress.objects.count())

0 comments on commit a8c52fd

Please sign in to comment.