Commit 0b516c412bbfdd8918efdc5edc2c137800ae5df7

Authored by Rohan Rangray
1 parent 4ff8077bc2
Exists in master

Added potential fix to get SQL expdecay sorting working on postgres

Showing 2 changed files with 23 additions and 4 deletions Side-by-side Diff

flashcards/models.py View file @ 0b516c4
... ... @@ -16,6 +16,7 @@
16 16 from simple_email_confirmation import SimpleEmailConfirmationUserMixin
17 17 from fields import MaskField
18 18 from cached_property import cached_property
  19 +from flashy.settings import IN_PRODUCTION
19 20  
20 21 # Hack to fix AbstractUser before subclassing it
21 22  
22 23  
23 24  
24 25  
... ... @@ -472,10 +473,28 @@
472 473 def __unicode__(self):
473 474 return '%s whitelisted for %s' % (str(self.email), str(self.section))
474 475  
  476 +
475 477 class JulianDay(Func):
476 478 function = 'julianday'
  479 + template = '%(function)s(%(expressions)s)'
477 480  
478 481 def __init__(self, expression, **extra):
479   - output = extra.pop('output_field', DateTimeField())
  482 + output = extra.pop('output_field', FloatField())
480 483 super(JulianDay, self).__init__(expression, output_field=output, **extra)
  484 +
  485 +
  486 +class EncapsulateIntervalDay(Func):
  487 + function = 'EXTRACT'
  488 + template_postgres = "(((%(function)s(EPOCH FROM INTERVAL %(expressions)s) / 60) / 60) / 24)::integer"
  489 + arg_joiner = ' - '
  490 +
  491 + def __init__(self, expression, **extra):
  492 + output = extra.pop('output_field', FloatField())
  493 + super(EncapsulateIntervalDay, self).__init__(expression, output_field=output, **extra)
  494 +
  495 +
  496 +def interval_days(expression1, expression2):
  497 + if IN_PRODUCTION:
  498 + return EncapsulateIntervalDay(expression1, expression2)
  499 + return JulianDay(expression1) - JulianDay(expression2)
flashcards/views.py View file @ 0b516c4
... ... @@ -5,7 +5,7 @@
5 5 from flashcards.api import StandardResultsSetPagination, IsEnrolledInAssociatedSection, IsFlashcardReviewer, \
6 6 IsAuthenticatedAndConfirmed
7 7 from flashcards.models import Section, User, Flashcard, FlashcardHide, UserFlashcard, UserFlashcardQuiz, \
8   - FlashcardAlreadyPulledException, FlashcardNotInDeckException
  8 + FlashcardAlreadyPulledException, FlashcardNotInDeckException, interval_days
9 9 from flashcards.notifications import notify_new_card
10 10 from flashcards.serializers import SectionSerializer, UserUpdateSerializer, RegistrationSerializer, UserSerializer, \
11 11 PasswordResetSerializer, PasswordResetRequestSerializer, EmailPasswordSerializer, FlashcardSerializer, \
... ... @@ -406,8 +406,8 @@
406 406 quiz_filter = user_flashcard_filter.prefetch_related('userflashcardquiz_set').annotate(
407 407 revise_count=Count('pk'),
408 408 days_since=Case(
409   - When(userflashcardquiz__when=None, then=Value(now(), output_field=DateTimeField())-F('pulled')),
410   - default=Value(now(), output_field=DateTimeField())-Max('userflashcardquiz__when'),
  409 + When(userflashcardquiz__when=None, then=interval_days(Value(now(), output_field=DateTimeField()), F('pulled'))),
  410 + default=interval_days(Value(now(), output_field=DateTimeField()), Max('userflashcardquiz__when')),
411 411 output_field=FloatField()
412 412 ),
413 413 retention_score=Case(