8000 Clean up signal handling by Elarnon · Pull Request #231 · mangaki/mangaki · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Clean up signal handling #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mangaki/mangaki/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default_app_config = 'mangaki.apps.MangakiConfig'
11 changes: 11 additions & 0 deletions mangaki/mangaki/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.apps import AppConfig


class MangakiConfig(AppConfig):
name = 'mangaki'
verbose_name = 'Mangaki'

def ready(self):
# Ensure signal receivers decorated with `@receiver` are connected by
# importing the `receivers` module.
from . import receivers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain what does this line do in a comment? It is not that evident, I think.

Copy link
Contributor Author
@Elarnon Elarnon Nov 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It ensures that the mangaki.receivers module is loaded, which ensures that the signal receivers decorated with @receiver are connected when the app is ready. I will add a comment before merging.

4 changes: 3 additions & 1 deletion mangaki/mangaki/factories.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import factory
from factory.django import DjangoModelFactory
from factory.django import DjangoModelFactory, mute_signals

from .models import Profile, Work, Category
from django.contrib.auth.models import User
from django.db.models.signals import post_save

class ProfileFactory(DjangoModelFactory):
class Meta:
Expand All @@ -15,6 +16,7 @@ class Meta:
newsletter_ok = factory.Faker('boolean')
avatar_url = factory.LazyAttribute(lambda o: '{}{}.png'.format(factory.Faker('url').generate({}), o.mal_username))

@mute_signals(post_save)
class UserFactory(DjangoModelFactory):
class Meta:
model = User
Expand Down
7 changes: 3 additions & 4 deletions mangaki/mangaki/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,9 @@ def update_scores(self):
score = suggestions_score + recommendations_score
Profile.objects.filter(user=self.user).update(score=score)


def suggestion_saved(sender, instance, *args, **kwargs):
instance.update_scores()
models.signals.post_save.connect(suggestion_saved, sender=Suggestion)
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
self.update_scores()


class Neighborship(models.Model):
Expand Down
11 changes: 11 additions & 0 deletions mangaki/mangaki/receivers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.conf import settings

from mangaki.models import Profile


@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
13 changes: 13 additions & 0 deletions mangaki/mangaki/tests/test_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.test import TestCase
from django.contrib.auth import get_user_model

from mangaki.models import Profile


class UserTest(TestCase):
def setUp(self):
self.User = get_user_model()

def test_user_creation_creates_profile(self):
u = self.User.objects.create_user('testuser')
self.assertIsInstance(u.profile, Profile)
9 changes: 0 additions & 9 deletions mangaki/mangaki/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from django.dispatch import receiver
from django.db.models import Count, Case, When, F, Value, Sum, IntegerField
from django.db import connection
from allauth.account.signals import user_signed_up
from allauth.socialaccount.signals import social_account_added
from mangaki.models import Work, Rating, Page, Profile, Artist, Suggestion, SearchIssue, Announcement, Recommendation, Pairing, Top, Ranking, Staff, Category, FAQTheme
from mangaki.mixins import AjaxableResponseMixin, JSONResponseMixin
from mangaki.mixins import AjaxableResponseMixin
Expand Down Expand Up @@ -696,13 +694,6 @@ def add_pairing(request, artist_id, work_id):
return HttpResponse()


@receiver(user_signed_up)
@receiver(social_account_added)
def register_profile(sender, **kwargs):
user = kwargs['user']
Profile(user=user).save()


def faq_index(request):
latest_theme_list = FAQTheme.objects.order_by('order')
all_information = [[faqtheme.theme, [(entry.question, entry.answer) for entry in faqtheme.entries.filter(is_active=True).order_by('-pub_date')]] for faqtheme in latest_theme_list]
Expand Down
0