From 90df9a55c8b4b5dcd989a8f4520aa7526354091e Mon Sep 17 00:00:00 2001 From: Andrew Buss Date: Tue, 2 Jun 2015 08:43:11 -0700 Subject: [PATCH] django-debug-toolbar; performance improvements --- flashcards/models.py | 13 +++++++++++-- flashy/settings.py | 4 +++- requirements.txt | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/flashcards/models.py b/flashcards/models.py index 379b554..d4f4fae 100644 --- a/flashcards/models.py +++ b/flashcards/models.py @@ -25,6 +25,10 @@ from cached_property import cached_property + + + + # Hack to fix AbstractUser before subclassing it AbstractUser._meta.get_field('email')._unique = True @@ -316,6 +320,7 @@ class Flashcard(Model): :param user: :return: Whether the card is hidden from the user. """ + if self.is_not_hidden: return False return self.is_hidden or self.flashcardhide_set.filter(user=user).exists() def is_in_deck(self, user): @@ -395,8 +400,12 @@ class Flashcard(Model): :param user: :return: A queryset with all cards that should be visible to a user. """ - return cls.objects.filter(Q(author__confirmed_email=True) | Q(author=user) - ).exclude(Q(is_hidden=True) | Q(flashcardhide__user=user)) + # All flashcards where the author is either confirmed, or the user + rqs = cls.objects.filter(Q(author__confirmed_email=True) | Q(author=user)) + # Exclude hidden cards + rqs = rqs.exclude(Q(is_hidden=True) | Q(flashcardhide__user=user)) + # Annotate the cards so we don't have to check if they're hidden in the future + return rqs.annotate(is_not_hidden=Value(True, output_field=BooleanField())) @classmethod def cards_hidden_by(cls, user): diff --git a/flashy/settings.py b/flashy/settings.py index 5e35731..94315ee 100644 --- a/flashy/settings.py +++ b/flashy/settings.py @@ -36,7 +36,6 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'ws4redis', - 'rest_framework_swagger', 'rest_framework', 'django_extensions', @@ -120,6 +119,9 @@ SERVER_EMAIL = 'noreply@flashy.cards' EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' +if DEBUG: + INSTALLED_APPS.append('debug_toolbar') + if IN_PRODUCTION: INSTALLED_APPS.append('django_ses') AWS_SES_REGION_NAME = 'us-west-2' diff --git a/requirements.txt b/requirements.txt index b4eee1b..bf9ce82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ pytz django-extensions django-redis-sessions python-gcm +django-debug-toolbar -- 1.9.1