utils.py 2.56 KB
Newer Older
1
2
from datetime import datetime, timedelta
from django.utils.timezone import make_aware
3
4
from dataclasses import dataclass
from typing import List
5
6
7

from base_app.models import User

8
9
10
11
from backend_app.models.exchangeFeedback import ExchangeFeedback
from backend_app.models.courseFeedback import CourseFeedback
from backend_app.models.exchange import Exchange

12
13
14
15
16
17
18
19
20
21
22
23
24
25

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


@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