uv.py 3.94 KB
Newer Older
tleb's avatar
tleb committed
1
2
3
4
from rest_framework.response import Response
from rest_framework.decorators import api_view, renderer_classes
from rest_framework.renderers import JSONRenderer
from django.core.exceptions import PermissionDenied
5
from django.conf import settings
tleb's avatar
tleb committed
6
7
8
9
10
11
12
13
14
15
from rest_framework import serializers
import urllib.request
import json

from pedagogy.views import CanCreateUVFunctionMixin


@api_view(["GET"])
@renderer_classes((JSONRenderer,))
def uv_endpoint(request):
tleb's avatar
tleb committed
16
    if not CanCreateUVFunctionMixin.can_create_uv(request.user):
tleb's avatar
tleb committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
        raise PermissionDenied

    params = request.query_params
    if "year" not in params or "code" not in params:
        raise serializers.ValidationError("Missing query parameter")

    short_uv, full_uv = find_uv("fr", params["year"], params["code"])
    if short_uv is None or full_uv is None:
        return Response(status=204)

    return Response(make_clean_uv(short_uv, full_uv))


def find_uv(lang, year, code):
    """
tleb's avatar
tleb committed
32
        Uses the UTBM API to find an UV.
tleb's avatar
tleb committed
33
34
35
36
37
        short_uv is the UV entry in the UV list. It is returned as it contains
        information which are not in full_uv.
        full_uv is the detailed representation of an UV.
    """
    # query the UV list
tleb's avatar
tleb committed
38
39
    uvs_url = settings.SITH_PEDAGOGY_UTBM_API + "/uvs/{}/{}".format(lang, year)
    response = urllib.request.urlopen(uvs_url)
tleb's avatar
tleb committed
40
41
42
43
44
45
46
47
48
    uvs = json.loads(response.read().decode("utf-8"))

    try:
        # find the first UV which matches the code
        short_uv = next(uv for uv in uvs if uv["code"] == code)
    except StopIteration:
        return (None, None)

    # get detailed information about the UV
tleb's avatar
tleb committed
49
50
    uv_url = settings.SITH_PEDAGOGY_UTBM_API + "/uv/{}/{}/{}/{}".format(
        lang, year, code, short_uv["codeFormation"]
tleb's avatar
tleb committed
51
    )
tleb's avatar
tleb committed
52
    response = urllib.request.urlopen(uv_url)
tleb's avatar
tleb committed
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    full_uv = json.loads(response.read().decode("utf-8"))

    return (short_uv, full_uv)


def make_clean_uv(short_uv, full_uv):
    """
        Cleans the data up so that it corresponds to our data representation.
    """
    res = {}

    res["credit_type"] = short_uv["codeCategorie"]

    # probably wrong on a few UVs as we pick the first UV we find but
    # availability depends on the formation
    semesters = {
        (True, True): "AUTUMN_AND_SPRING",
        (True, False): "AUTUMN",
        (False, True): "SPRING",
    }
    res["semester"] = semesters.get(
        (short_uv["ouvertAutomne"], short_uv["ouvertPrintemps"]), "CLOSED"
    )

    langs = {"es": "SP", "en": "EN", "de": "DE"}
    res["language"] = langs.get(full_uv["codeLangue"], "FR")

    if full_uv["departement"] == "Pôle Humanités":
        res["department"] = "HUMA"
    else:
        departments = {
            "AL": "IMSI",
            "AE": "EE",
            "GI": "GI",
            "GC": "EE",
            "GM": "MC",
            "TC": "TC",
            "GP": "IMSI",
            "ED": "EDIM",
            "AI": "GI",
            "AM": "MC",
        }
        res["department"] = departments.get(full_uv["codeFormation"], "NA")

    res["credits"] = full_uv["creditsEcts"]

    activities = ("CM", "TD", "TP", "THE", "TE")
    for activity in activities:
        res["hours_{}".format(activity)] = 0
    for activity in full_uv["activites"]:
        if activity["code"] in activities:
            res["hours_{}".format(activity["code"])] += activity["nbh"] // 60

    # wrong if the manager changes depending on the semester
107
108
109
110
    semester = full_uv.get("automne", None)
    if not semester:
        semester = full_uv.get("printemps", {})
    res["manager"] = semester.get("responsable", "")
tleb's avatar
tleb committed
111
112
113

    res["title"] = full_uv["libelle"]

tleb's avatar
tleb committed
114
115
116
117
118
119
120
121
122
123
124
125
    descriptions = {
        "objectives": "objectifs",
        "program": "programme",
        "skills": "acquisitionCompetences",
        "key_concepts": "acquisitionNotions",
    }

    for res_key, full_uv_key in descriptions.items():
        res[res_key] = full_uv[full_uv_key]
        # if not found or the API did not return a string
        if type(res[res_key]) != str:
            res[res_key] = ""
tleb's avatar
tleb committed
126
127

    return res