def check_streak(self, date=None): if date is None: date = timezone.localtime(timezone.now()).date() scope = get_scope_by_name(self.scope)(date) else: scope = get_scope_by_name(self.scope)() return self.track_events.filter(created__date__range=(scope.start, scope.end)).exists()
def untracked(self): """ Filters the queryset to only habits that have not been tracked successfully within the their current scope period. :return: Untracked habits """ # Getting all scopes. day_scope = get_scope_by_name(Scope.DAY.value)() week_scope = get_scope_by_name(Scope.WEEK.value)() month_scope = get_scope_by_name(Scope.MONTH.value)() year_scope = get_scope_by_name(Scope.YEAR.value)() return self.exclude( Q(scope=day_scope.name, track_events__created__date__range=(day_scope.start, day_scope.end)) | Q(scope=week_scope.name, track_events__created__date__range=(week_scope.start, week_scope.end)) | Q(scope=month_scope.name, track_events__created__date__range=(month_scope.start, month_scope.end)) | Q(scope=year_scope.name, track_events__created__date__range=(year_scope.start, year_scope.end)))
def mutate_and_get_payload(cls, root, info, scope=Scope.DAY.value, start=None, content='', keywords=''): user = info.context.user if not start: start = get_scope_by_name(scope)().start entry = user.journal_entries.create(scope=scope, start=start, content=content, keywords=keywords) # Updating streak today = timezone.localtime(timezone.now()).date() if entry.scope == Scope.DAY.value and entry.start == today: # Only update if this is today's day-entry. habit = get_controlled_habit(user, ControlledHabit.JOURNAL_HABIT) tracked = habit.track() if tracked: habit.save() add_experience(user, 'studio') return AddJournalEntryMutation(journal_entry=entry)
def mutate_and_get_payload(cls, root, info, scope=Scope.DAY.value, start=None, content='', keywords=''): user = info.context.user if not start: start = get_scope_by_name(scope)().start entry = user.journal_entries.create(scope=scope, start=start, content=content, keywords=keywords) # Updating streak today = timezone.localtime(timezone.now()).date() if entry.scope == Scope.DAY.value and entry.start == today: # Only update if this is today's day-entry. user.journal.streak += 1 user.journal.save(update_fields=['streak']) add_experience(user, 'studio') return AddJournalEntryMutation(journal_entry=entry)
def handle(self, scope, *args, **options): """ Check if the user's focus status and create a report for his partner(s). Is supposed to run after midday. General solution idea: 1. Filtering: Only groups with at least one premium user. 2. Generating a pre-day status report: Per outcome: Green: User completed min 1 step. Yellow: Next step is defined. Red: No next step defined. 3. Generate a current day status report: Per outcome: Outcome Name Next step Next version: Html report. """ date = timezone.localtime(timezone.now()).date() scope = get_scope_by_name(scope)(date=date) context = {'date': date, 'scope': scope} self.stdout.write( f'# Generating {scope.name} focus reports for {date}') # Get relevant groups. if scope.name == Scope.DAY.value: groups = Duo.objects.filter(members__is_premium=True).distinct() elif scope.name == Scope.WEEK.value: groups = Clan.objects.filter(members__is_premium=True).distinct() elif scope.name == Scope.MONTH.value: groups = Tribe.objects.filter(members__is_premium=True).distinct() elif scope.name == Scope.YEAR.value: groups = [Community] else: groups = [] for group in groups: context['group'] = group subject = f"[{group.name}] {scope.name.title()} focus report: ({scope})" message = render_to_string('office/emails/focus_report.txt', context) from_email = 'colegend groups <*****@*****.**>' emails = group.members.values_list('email', flat=True) email = EmailMultiAlternatives(subject=subject, body=message, from_email=from_email, to=emails, reply_to=emails) email.send() self.stdout.write(f'Sent for {group.name}')
def untracked(self): """ Filters the queryset to only habits that have not been tracked successfully within the their current scope period. :return: Untracked habits """ # Getting all scopes. day_scope = get_scope_by_name(Scope.DAY.value)() week_scope = get_scope_by_name(Scope.WEEK.value)() month_scope = get_scope_by_name(Scope.MONTH.value)() year_scope = get_scope_by_name(Scope.YEAR.value)() return self.exclude( Q(scope=day_scope.name, track_events__created__date__range=(day_scope.start, day_scope.end)) | Q(scope=week_scope.name, track_events__created__date__range=(week_scope.start, week_scope.end)) | Q(scope=month_scope.name, track_events__created__date__range=(month_scope.start, month_scope.end)) | Q(scope=year_scope.name, track_events__created__date__range=(year_scope.start, year_scope.end)) )
def has_track(self, date=None): """ Check if a track record already exists (for the given date or today). :param date: The date to check for: If a track exists? :return: True if a track event was found. False if not found. """ if date is None: date = timezone.localtime(timezone.now()).date() scope = get_scope_by_name(self.scope)(date) return self.track_events.filter(created__date__range=(scope.start, scope.end)).exists()
def has_track(self, date=None): """ Check if a track record already exists (for the given date or today). :param date: The date to check for: If a track exists? :return: True if a track event was found. False if not found. """ if date is None: date = timezone.localtime(timezone.now()).date() scope = get_scope_by_name(self.scope)(date) return self.track_events.filter( created__date__range=(scope.start, scope.end)).exists()
def filter_queryset(self, queryset): scope = self.request.query_params.get('scope') start = self.request.query_params.get('start') if scope and start and scope != Scope.DAY.value: # Update start to match correct scope start date. start = get_scope_by_name(scope)(start).start params = self.request.query_params params._mutable = True params['start'] = str(start) params._mutable = False return super().filter_queryset(queryset)
def resolve_gender_quote(self, info, scope=None): kwargs = {} if scope is not None: kwargs['date_joined__gte'] = get_scope_by_name(scope)().start males = User.objects.filter(**kwargs).filter(gender=User.MALE).count() females = User.objects.filter(**kwargs).filter(gender=User.FEMALE).count() neutrals = User.objects.filter(**kwargs).filter(gender=User.NEUTRAL).count() total = males + females + neutrals if not total: return '0% male, 0% female, 0% neutral' return '{males:.0f}% male, {females:.0f}% female, {neutrals:.0f}% neutral'.format( males=males / total * 100, females=females / total * 100, neutrals=neutrals / total * 100)
def mutate_and_get_payload(cls, root, info, scope=Scope.DAY.value, content=''): user = info.context.user start = get_scope_by_name(scope)().start entry, created = user.journal_entries.get_or_create(scope=scope, start=start) if content: entry.content += '{prefix}{content}'.format( prefix='\n' if entry.content else '', content=content) entry.save() return AddJournalEntryNoteMutation(journal_entry=entry)
def handle(self, scope, *args, **options): """ Check if the user's focus status and create a report for his partner(s). Is supposed to run after midday. General solution idea: 1. Filtering: Only groups with at least one premium user. 2. Generating a pre-day status report: Per outcome: Green: User completed min 1 step. Yellow: Next step is defined. Red: No next step defined. 3. Generate a current day status report: Per outcome: Outcome Name Next step Next version: Html report. """ date = timezone.localtime(timezone.now()).date() scope = get_scope_by_name(scope)(date=date) context = {'date': date, 'scope': scope} self.stdout.write(f'# Generating {scope.name} focus reports for {date}') # Get relevant groups. if scope.name == Scope.DAY.value: groups = Duo.objects.filter(members__is_premium=True).distinct() elif scope.name == Scope.WEEK.value: groups = Clan.objects.filter(members__is_premium=True).distinct() elif scope.name == Scope.MONTH.value: groups = Tribe.objects.filter(members__is_premium=True).distinct() elif scope.name == Scope.YEAR.value: groups = [Community] else: groups = [] for group in groups: context['group'] = group subject = f"[{group.name}] {scope.name.title()} focus report: ({scope})" message = render_to_string('office/emails/focus_report.txt', context) from_email = 'coLegend groups <*****@*****.**>' emails = group.members.values_list('email', flat=True) email = EmailMultiAlternatives(subject=subject, body=message, from_email=from_email, to=emails, reply_to=emails) email.send() self.stdout.write(f'Sent for {group.name}')
def resolve_gender_quote(self, info, scope=None): kwargs = {} if scope is not None: kwargs['date_joined__gte'] = get_scope_by_name(scope)().start males = User.objects.filter(**kwargs).filter(gender=User.MALE).count() females = User.objects.filter(**kwargs).filter( gender=User.FEMALE).count() neutrals = User.objects.filter(**kwargs).filter( gender=User.NEUTRAL).count() total = males + females + neutrals if not total: return '0% male, 0% female, 0% neutral' return '{males:.0f}% male, {females:.0f}% female, {neutrals:.0f}% neutral'.format( males=males / total * 100, females=females / total * 100, neutrals=neutrals / total * 100)
def get_stats(self, phases=4): """ Check the last X phases for completion. Example: Performance of last 4 weeks? Result: [0, 1, 1, 0] => Not yet a success this week, success the 2 weeks before, no success 3 weeks ago. Result type: [{this_phase}, {last_phase}, {pre_last_phase}, {pre_pre_last_phase}] """ # TODO: Refactor to static variables plus updates. (signals?). scope = get_scope_by_name(self.scope)() stats = [0 for x in range(phases)] # [0, 0, 0, ...] empty result array for phase in range(phases): # for each phase start = scope.start end = scope.end stats[phase] = self.track_events.filter(created__date__range=(start, end)).count() scope = scope.previous return stats
def resolve_users(self, info, scope=None): kwargs = {} if scope is not None: kwargs['date_joined__gte'] = get_scope_by_name(scope)().start return User.objects.filter(**kwargs).count()
def save(self, *args, **kwargs): if self.pk is None: # Creation. # Adapting start date to scope. scope = get_scope_by_name(self.scope)(self.start) self.start = scope.start super().save(*args, **kwargs)
def resolve_journal_entries(self, info, scope=None): kwargs = {} if scope is not None: kwargs['created__gte'] = get_scope_by_name(scope)().start return JournalEntry.objects.filter(**kwargs).count()
def resolve_donations(self, info, scope=None): kwargs = {} if scope is not None: kwargs['date__gte'] = get_scope_by_name(scope)().start return Donation.objects.filter(**kwargs).total()
def resolve_active_users(self, info, scope=Scope.MONTH.value): start = get_scope_by_name(scope)().start return User.objects.filter(experience__created__gte=start).distinct().count()
def resolve_active_users(self, info, scope=Scope.MONTH.value): start = get_scope_by_name(scope)().start return User.objects.filter( experience__created__gte=start).distinct().count()
def resolve_focuses(self, info, scope=None): kwargs = {} if scope is not None: kwargs['created__gte'] = get_scope_by_name(scope)().start return Focus.objects.filter(**kwargs).count()
def get_scope(self): return get_scope_by_name(self.scope)(self.start)