def clear_old_bouncy_objects(): Delivery.objects.filter(created_at__lte=DateTimeService.now() - timedelta(7)).delete() Bounce.objects.filter(created_at__lte=DateTimeService.now() - timedelta(30)).delete() Complaint.objects.filter(created_at__lte=DateTimeService.now() - timedelta(90)).delete()
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)
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})
def purge_expired_incomplete_uploads(): deleted = Image.uploads_in_progress.filter( uploader_expires__lt=DateTimeService.now()).delete() if deleted is not None: logger.info("Purged %d expired incomplete image uploads." % deleted[0]) deleted = ImageRevision.uploads_in_progress.filter( uploader_expires__lt=DateTimeService.now()).delete() if deleted is not None: logger.info("Purged %d expired incomplete image revision uploads." % deleted[0])
def may_submit_to_iotd_tp_process(user: User, image: Image): if not user.is_authenticated: return False, 'UNAUTHENTICATED' if user != image.user: return False, 'NOT_OWNER' if is_free(user): return False, 'IS_FREE' if image.is_wip: return False, 'NOT_PUBLISHED' if image.designated_iotd_submitters.exists() or image.designated_iotd_reviewers.exists(): return False, 'ALREADY_SUBMITTED' if image.subject_type in (SubjectType.GEAR, SubjectType.OTHER, '', None): return False, 'BAD_SUBJECT_TYPE' if image.user.userprofile.exclude_from_competitions: return False, 'EXCLUDED_FROM_COMPETITIONS' if image.user.userprofile.banned_from_competitions: return False, 'BANNED_FROM_COMPETITIONS' if image.published < DateTimeService.now() - timedelta(days=settings.IOTD_SUBMISSION_WINDOW_DAYS): return False, 'TOO_LATE' return True, None
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()) ) )
def assign_upload_length(): """ Run this tasks every hour to assign `uploader_upload_length` to images that lack it. """ def process(items): for item in items.iterator(): try: url = item.image_file.url response = requests.head(url) # type: Response except ValueError as e: logger.error("assign_upload_length: error %s", str(e)) continue if response.status_code == 200: size = response.headers.get("Content-Length") item.uploader_upload_length = size try: item.save(keep_deleted=True) except IntegrityError: continue logger.info( "assign_upload_length: proccessed %d (%s) = %s" % (item.pk, item.image_file.url, filesizeformat(size))) time_cut = DateTimeService.now() - timedelta(hours=2) images = Image.all_objects.filter(uploader_upload_length__isnull=True, uploaded__gte=time_cut) revisions = ImageRevision.all_objects.filter( uploader_upload_length__isnull=True, uploaded__gte=time_cut) process(images) process(revisions)
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
def get_last_notification_time(request, pk=None): if pk: try: return Message.objects.get(pk=pk).modified except (Message.DoesNotExist, AttributeError): pass else: if request.user.is_authenticated: try: return Message.objects.filter( user=request.user).latest('modified').modified except (Message.DoesNotExist, AttributeError): pass if 'HTTP_AUTHORIZATION' in request.META: token_in_header = request.META['HTTP_AUTHORIZATION'].replace( 'Token ', '') try: token = Token.objects.get(key=token_in_header) return Message.objects.filter( user=token.user).latest('modified').modified except (Token.DoesNotExist, Message.DoesNotExist, AttributeError): pass return DateTimeService.now()
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)
def test_judge_cannot_select_now_reason_none_no_iotds(self, now): now.return_value = datetime.now() judge = Generators.user(groups=['iotd_judges']) self.assertIsNone(IotdService().judge_cannot_select_now_reason(judge)) self.assertEquals( IotdService().get_next_available_selection_time_for_judge(judge), DateTimeService.now())
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 })
def subscription_paid(sender, **kwargs): subscription = kwargs.get('subscription') user = kwargs.get('user') profile = user.userprofile UserProfile.all_objects.filter(user=user).update(updated=timezone.now()) PremiumService.clear_subscription_status_cache_keys(user.pk) if subscription.group.name == 'astrobin_lite': profile.premium_counter = 0 profile.save(keep_deleted=True) elif subscription.group.name == 'astrobin_lite_2020': profile.premium_counter = Image.objects_including_wip.filter( user=user).count() profile.save(keep_deleted=True) if 'premium' in subscription.category and Transaction.objects.filter( user=user, event='new usersubscription', timestamp__gte=DateTimeService.now() - datetime.timedelta(minutes=5)): push_notification([user], None, 'new_subscription', {'BASE_URL': settings.BASE_URL}) else: push_notification([user], None, 'new_payment', {'BASE_URL': settings.BASE_URL})
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 test_judge_cannot_select_now_reason_none_no_scheduled_iotds(self, now): now.return_value = datetime.now() iotd = self._create_iotd(date=date.today() - timedelta(1)) self.assertIsNone(IotdService().judge_cannot_select_now_reason( iotd.judge)) self.assertEquals( IotdService().get_next_available_selection_time_for_judge( iotd.judge), DateTimeService.now())
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()
def test_judge_cannot_select_now_reason_already_selected_today(self, now): now.return_value = datetime.now() iotd = self._create_iotd(date=date.today()) reason = IotdService().judge_cannot_select_now_reason(iotd.judge) self.assertIsNotNone(reason) self.assertTrue('you already selected 1 IOTD today' in reason) self.assertEquals( IotdService().get_next_available_selection_time_for_judge( iotd.judge), DateTimeService.next_midnight())
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
def test_human_time_duration(self): self.assertEquals('0', DateTimeService.human_time_duration(None)) self.assertEquals('0', DateTimeService.human_time_duration(0)) self.assertEquals('1\'', DateTimeService.human_time_duration(60)) self.assertEquals('1h', DateTimeService.human_time_duration(60 * 60)) self.assertEquals('1h 1\'', DateTimeService.human_time_duration(60 * 60 + 60)) self.assertEquals('1h 1\' 1"', DateTimeService.human_time_duration(60 * 60 + 60 + 1)) self.assertEquals('25h', DateTimeService.human_time_duration(25 * 60 * 60))
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()
def test_judge_cannot_select_now_reason_already_2_scheduled(self, now): now.return_value = datetime.now() iotd = self._create_iotd(date=date.today() + timedelta(1)) self._create_iotd(judge=iotd.judge, date=date.today() + timedelta(2)) reason = IotdService().judge_cannot_select_now_reason(iotd.judge) self.assertIsNotNone(reason) self.assertTrue('there are already 2 scheduled IOTDs' in reason) self.assertEquals( IotdService().get_next_available_selection_time_for_judge( iotd.judge), DateTimeService.next_midnight())
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)
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
def get_current_user_profile_last_modified(request): if request.user.is_authenticated: return request.user.userprofile.updated if 'HTTP_AUTHORIZATION' in request.META: token_in_header = request.META['HTTP_AUTHORIZATION'].replace( 'Token ', '') try: token = Token.objects.get(key=token_in_header) return token.user.userprofile.updated except (Token.DoesNotExist, AttributeError): pass return DateTimeService.now()
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 subscription_paid(sender, **kwargs): subscription = kwargs.get('subscription') user = kwargs.get('user') if subscription.group.name == 'astrobin_lite': profile = user.userprofile profile.premium_counter = 0 profile.save(keep_deleted=True) if 'premium' in subscription.category and 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})
def get_queryset(self): return self.serializer_class.Meta.model.objects.filter( date__gt=DateTimeService.today()).order_by('date')
def get_images_pending_moderation(self): return Image.objects_including_wip.filter( moderator_decision=0, uploaded__lt=DateTimeService.now() - timedelta(minutes=10))
def timestamp(dt): return mark_safe('<abbr class="timestamp" data-epoch="%s">...</abbr>' % DateTimeService.epoch(dt))
def is_future(dt): return dt > DateTimeService.now()