Beispiel #1
0
def subscription_signed_up(sender, **kwargs):
    subscription = kwargs.get('subscription')
    user_subscription = kwargs.get('usersubscription')
    user = kwargs.get('user')

    if 'premium' in subscription.category:
        if user_subscription.expires is None:
            user_subscription.expires = DateTimeService.today()
        user_subscription.extend(datetime.timedelta(days=365.2425))
        user_subscription.save()

        # Invalidate other premium subscriptions
        UserSubscription.active_objects \
            .filter(user=user_subscription.user,
                    subscription__category__startswith='premium') \
            .exclude(pk=user_subscription.pk) \
            .update(active=False)

        if Transaction.objects.filter(user=user,
                                      event='new usersubscription',
                                      timestamp__gte=DateTimeService.now() -
                                      datetime.timedelta(minutes=5)):
            push_notification([user], 'new_subscription',
                              {'BASE_URL': settings.BASE_URL})
        else:
            push_notification([user], 'new_payment',
                              {'BASE_URL': settings.BASE_URL})
Beispiel #2
0
    def get_next_available_selection_time_for_judge(self, judge):
        # type: (User) -> datetime

        today = DateTimeService.today()  # date
        now = DateTimeService.now()  # datetime

        next_time_due_to_max_per_day = \
            DateTimeService.next_midnight() if \
                Iotd.objects.filter(
                    judge=judge,
                    date=today).count() >= settings.IOTD_JUDGEMENT_MAX_PER_DAY \
                else now  # datetime

        latest_scheduled = Iotd.objects.filter(
            judge=judge).order_by('-date').first()  # Iotd
        next_time_due_to_max_scheduled_per_judge = \
            DateTimeService.next_midnight(latest_scheduled.date) if \
                Iotd.objects.filter(
                    judge=judge,
                    date__gt=today).count() >= settings.IOTD_JUDGEMENT_MAX_FUTURE_PER_JUDGE \
                else now

        next_time_due_to_max_scheduled = \
            DateTimeService.next_midnight() if \
                Iotd.objects.filter(date__gte=today).count() >= settings.IOTD_JUDGEMENT_MAX_FUTURE_DAYS \
            else now

        return max(next_time_due_to_max_per_day,
                   next_time_due_to_max_scheduled_per_judge,
                   next_time_due_to_max_scheduled)
Beispiel #3
0
        def _compute_queue(reviewer: User):
            days = settings.IOTD_REVIEW_WINDOW_DAYS
            cutoff = datetime.now() - timedelta(days)

            return Image.objects.annotate(
                num_submissions=Count('iotdsubmission', distinct=True),
                num_dismissals=Count('iotddismissedimage', distinct=True),
                last_submission_timestamp=Subquery(IotdSubmission.last_for_image(OuterRef('pk')).values('date'))
            ).filter(
                Q(deleted__isnull=True) &
                Q(last_submission_timestamp__gte=cutoff) &
                Q(designated_iotd_reviewers=reviewer) &
                Q(num_submissions__gte=settings.IOTD_SUBMISSION_MIN_PROMOTIONS) &
                Q(num_dismissals__lt=settings.IOTD_MAX_DISMISSALS) &
                Q(
                    Q(iotd__isnull=True) |
                    Q(iotd__date__gt=datetime.now().date())
                )
            ).exclude(
                Q(iotdsubmission__submitter=reviewer) |
                Q(user=reviewer) |
                Q(iotddismissedimage__user=reviewer) |
                Q(
                    Q(iotdvote__reviewer=reviewer) &
                    Q(iotdvote__date__lt=DateTimeService.today())
                )
            )
Beispiel #4
0
def send_notifications_when_promoted_image_becomes_iotd():
    try:
        iotd = Iotd.objects.get(date=DateTimeService.today())
    except Iotd.DoesNotExist:
        logger.error(
            "send_notifications_when_promoted_image_becomes_iotd: Iotd not found"
        )
        return

    image = iotd.image
    thumb = image.thumbnail_raw('gallery', None, sync=True)

    submitters = [
        x.submitter for x in IotdSubmission.objects.filter(image=image)
    ]
    push_notification(submitters, None, 'image_you_promoted_is_iotd', {
        'image': image,
        'image_thumbnail': thumb.url if thumb else None
    })

    reviewers = [x.reviewer for x in IotdVote.objects.filter(image=image)]
    push_notification(reviewers, None, 'image_you_promoted_is_iotd', {
        'image': image,
        'image_thumbnail': thumb.url if thumb else None
    })

    push_notification([image.user], None, 'your_image_is_iotd', {
        'image': image,
        'image_thumbnail': thumb.url if thumb else None
    })
Beispiel #5
0
    def get_inactive_submitter_and_reviewers(self, days):
        inactive_members = []
        members = User.objects.filter(
            groups__name__in=['iotd_submitters', 'iotd_reviewers'])

        for member in members.iterator():
            if member.is_superuser:
                continue

            if 'iotd_reviewers' in member.groups.all().values_list('name',
                                                                   flat=True):
                actions = IotdVote.objects.filter(
                    reviewer=member).order_by('-date')
                action_count = actions.count()
                last_action = actions.first(
                ).date if action_count > 0 else None
            elif 'iotd_submitters' in member.groups.all().values_list(
                    'name', flat=True):
                actions = IotdSubmission.objects.filter(
                    submitter=member).order_by('-date')
                action_count = actions.count()
                last_action = actions.first(
                ).date if action_count > 0 else None
            else:
                continue

            if last_action is None or last_action.date(
            ) == DateTimeService.today() - timedelta(days=days):
                inactive_members.append(member)

        return inactive_members
Beispiel #6
0
    def show_10_year_anniversary_logo() -> bool:
        today = DateTimeService.today()

        first_code_anniversary = datetime(2020, 11, 27).date()
        publication_anniversary = datetime(2021, 11, 27).date()

        return today in (first_code_anniversary, publication_anniversary)
Beispiel #7
0
    def judge_cannot_select_now_reason(self, judge):
        # type: (User) -> Union[str, None]

        if Iotd.objects.filter(
                judge=judge,
                date=DateTimeService.today()).count() >= settings.IOTD_JUDGEMENT_MAX_PER_DAY:
            return gettext("you already selected %s IOTD today (UTC)" % settings.IOTD_JUDGEMENT_MAX_PER_DAY)

        if Iotd.objects.filter(
                judge=judge,
                date__gt=DateTimeService.today()).count() >= settings.IOTD_JUDGEMENT_MAX_FUTURE_PER_JUDGE:
            return gettext("you already selected %s scheduled IOTDs" % settings.IOTD_JUDGEMENT_MAX_FUTURE_PER_JUDGE)

        if Iotd.objects.filter(date__gte=DateTimeService.today()).count() >= settings.IOTD_JUDGEMENT_MAX_FUTURE_DAYS:
            return gettext("there are already %s scheduled IOTDs" % settings.IOTD_JUDGEMENT_MAX_FUTURE_DAYS)

        return None
def has_an_expired_premium_subscription(user):
    if is_any_premium_subscription(user):
        return False

    return UserSubscription.objects.filter(
        user=user,
        expires__lt=DateTimeService.today(),
        subscription__category__contains="premium"
    ).exists()
Beispiel #9
0
def has_premium_subscription_near_expiration(user, days):
    cache_key = "has_premium_subscription_near_expiration_%d" % user.pk

    cached = cache.get(cache_key)
    if cached is not None:
        return cached

    if has_an_expired_premium_subscription(user):
        result = False
    else:
        result = UserSubscription.objects.filter(
            Q(user=user) & Q(active=True)
            & Q(expires__lt=DateTimeService.today() + datetime.timedelta(days))
            & Q(expires__gt=DateTimeService.today())
            & Q(subscription__category__contains="premium")).exists()

    cache.set(cache_key, result, 300)
    return result
Beispiel #10
0
    def is_remote_source_affiliate(code):
        # type: (unicode) -> bool

        remote_source = get_object_or_None(RemoteSourceAffiliate, code=code)  # type: RemoteSourceAffiliate

        if remote_source is None:
            return False

        return remote_source.affiliation_start is not None and \
               remote_source.affiliation_expiration >= DateTimeService.today()
Beispiel #11
0
    def create(self, validated_data):
        if 'judge' not in validated_data:
            validated_data['judge'] = self.context['request'].user

        latest_iotd = self.Meta.model.objects.first()

        validated_data['date'] = latest_iotd.date + timedelta(1) \
            if latest_iotd \
            else DateTimeService.today()

        return super().create(validated_data)
Beispiel #12
0
    def show_10_year_anniversary_logo():
        # type: () -> bool

        today = DateTimeService.today()

        first_code_anniversary = datetime(2020, 11, 27).date()
        publication_anniversary = datetime(2021, 11, 27).date()
        one_week = timedelta(days=7)

        return \
            first_code_anniversary <= today < first_code_anniversary + one_week or \
            publication_anniversary <= today < publication_anniversary + one_week
Beispiel #13
0
def has_an_expired_premium_subscription(user):
    cache_key = "has_an_expired_premium_subscription_%d" % user.pk

    cached = cache.get(cache_key)
    if cached is not None:
        return cached

    if is_any_premium_subscription(user):
        result = False
    else:
        result = UserSubscription.objects.filter(
            user=user,
            expires__lt=DateTimeService.today(),
            subscription__category__contains="premium").exists()

    cache.set(cache_key, result, 300)
    return result
 def get_queryset(self):
     return self.serializer_class.Meta.model.objects.filter(
         date__gt=DateTimeService.today()).order_by('date')