from datetime import datetime, timedelta from django.utils.timezone import make_aware from dataclasses import dataclass from typing import List from base_app.models import User from backend_app.models.exchangeFeedback import ExchangeFeedback from backend_app.models.courseFeedback import CourseFeedback from backend_app.models.exchange import Exchange def get_today_as_datetime(): now = datetime.now() today = now.replace(hour=0, minute=0, second=0, microsecond=1) return make_aware(today) def get_daily_connections() -> int: today = get_today_as_datetime() yesterday = today - timedelta(days=1) nb_connections = User.objects.filter( last_login__gte=yesterday, last_login__lt=today ).count() return nb_connections @dataclass(eq=True, frozen=True) class ContributionProfile: """ Class for keeping track a profile. """ major: str minor: str exchange_semester: str university_pk: int def _get_profile_from_exchange(exchange: Exchange) -> ContributionProfile: return ContributionProfile( major=exchange.student_major if exchange.student_major is not None else "", minor=exchange.student_minor if exchange.student_minor is not None else "", exchange_semester=f"{exchange.semester}{exchange.year}", university_pk=exchange.university.pk, ) def get_contributions_profiles() -> List[ContributionProfile]: """ return the yesterday contributions profiles If no university if associated with a contribution we don't return it """ today = get_today_as_datetime() yesterday = today - timedelta(days=1) contributions_profiles = [] exchange_feedbacks = ExchangeFeedback.objects.filter( updated_on__gte=yesterday, updated_on__lt=today, exchange__university__isnull=False, untouched=False, ).prefetch_related("exchange") for exchange_feedback in exchange_feedbacks: exchange = exchange_feedback.exchange contribution_profile = _get_profile_from_exchange(exchange) contributions_profiles.append(contribution_profile) course_feedbacks = CourseFeedback.objects.filter( updated_on__gte=yesterday, updated_on__lt=today, course__exchange__university__isnull=False, untouched=False, ).prefetch_related("course__exchange") for course_feedback in course_feedbacks: exchange = course_feedback.course.exchange contribution_profile = _get_profile_from_exchange(exchange) contributions_profiles.append(contribution_profile) return contributions_profiles