Commit bddb88d9 authored by tleb's avatar tleb

Comment UV API and fix little bugs

parent dbe44a9c
Pipeline #2109 passed with stage
in 31 minutes and 57 seconds
...@@ -77,3 +77,4 @@ from .user import * ...@@ -77,3 +77,4 @@ from .user import *
from .club import * from .club import *
from .group import * from .group import *
from .launderette import * from .launderette import *
from .uv import *
...@@ -24,15 +24,9 @@ ...@@ -24,15 +24,9 @@
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.decorators import api_view, renderer_classes from rest_framework.decorators import api_view, renderer_classes
from rest_framework.renderers import StaticHTMLRenderer, JSONRenderer from rest_framework.renderers import StaticHTMLRenderer
from rest_framework.views import APIView
from django.core.exceptions import PermissionDenied
from rest_framework import serializers
import urllib.request
import json
from core.templatetags.renderer import markdown from core.templatetags.renderer import markdown
from pedagogy.views import CanCreateUVFunctionMixin
@api_view(["POST"]) @api_view(["POST"])
...@@ -46,94 +40,3 @@ def RenderMarkdown(request): ...@@ -46,94 +40,3 @@ def RenderMarkdown(request):
except: except:
data = "Error" data = "Error"
return Response(data) return Response(data)
@api_view(["GET"])
@renderer_classes((JSONRenderer,))
def uv_endpoint(request):
if not request.user.is_authenticated or not CanCreateUVFunctionMixin.can_create_uv(
request.user
):
raise PermissionDenied
lang = "fr"
params = request.query_params
if "year" not in params or "code" not in params:
raise serializers.ValidationError("Missing query parameter")
uvs_url = "https://extranet1.utbm.fr/gpedago/api/guide/uvs/{lang}/{year}"
response = urllib.request.urlopen(uvs_url.format(lang=lang, year=params["year"]))
uvs = json.loads(response.read().decode("utf-8"))
try:
found = next(uv for uv in uvs if uv["code"] == params["code"])
except StopIteration:
# shouldn't be 404, rather something like 204
return Response(status=404)
uv_url = "https://extranet1.utbm.fr/gpedago/api/guide/uv/{lang}/{year}/{code}/{formation}"
response = urllib.request.urlopen(
uv_url.format(
lang=lang,
year=params["year"],
code=params["code"],
formation=found["codeFormation"],
)
)
uv = json.loads(response.read().decode("utf-8"))
res = {}
res["credit_type"] = found["codeCategorie"]
semesters = {
(True, True): "AUTUMN_AND_SPRING",
(True, False): "AUTOMN",
(False, True): "SPRING",
}
res["semester"] = semesters.get(
(found["ouvertAutomne"], found["ouvertPrintemps"]), "CLOSED"
)
langs = {"es": "SP", "en": "EN", "de": "DE"}
res["language"] = langs.get(uv["codeLangue"], "FR")
if 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(uv["codeFormation"], "NA")
res["credits"] = uv["creditsEcts"]
activities = ("CM", "TD", "TP", "THE", "TE")
for activity in activities:
res["hours_{}".format(activity)] = 0
for activity in uv["activites"]:
if activity["code"] in activities:
res["hours_{}".format(activity["code"])] += activity["nbh"] // 60
res["manager"] = uv["automne"]["responsable"]
res["title"] = uv["libelle"]
res["objectives"] = uv["objectifs"]
res["program"] = uv["programme"]
res["skills"] = uv["acquisitionCompetences"]
res["key_concepts"] = uv["acquisitionNotions"]
return Response(res)
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
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):
# is authenticated and has the right to create an UV
if not request.user.is_authenticated or not CanCreateUVFunctionMixin.can_create_uv(
request.user
):
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):
"""
Uses the UTBM API to find a UV.
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
uvs_url = "https://extranet1.utbm.fr/gpedago/api/guide/uvs/{lang}/{year}"
response = urllib.request.urlopen(uvs_url.format(lang=lang, year=year))
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
uv_url = "https://extranet1.utbm.fr/gpedago/api/guide/uv/{lang}/{year}/{code}/{formation}"
response = urllib.request.urlopen(
uv_url.format(
lang=lang, year=year, code=code, formation=short_uv["codeFormation"]
)
)
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
res["manager"] = full_uv["automne"]["responsable"]
res["title"] = full_uv["libelle"]
res["objectives"] = full_uv["objectifs"]
res["program"] = full_uv["programme"]
res["skills"] = full_uv["acquisitionCompetences"]
res["key_concepts"] = full_uv["acquisitionNotions"]
return res
This diff is collapsed.
...@@ -52,7 +52,11 @@ ...@@ -52,7 +52,11 @@
year-- year--
} }
const url = "{{ url('api:uv_endpoint') }}?year=" + year + "&code=" + codeInput.value const url = "{{ url('api:uv_endpoint') }}?year=" + year + "&code=" + codeInput.value
$.getJSON(url, function(data) { $.getJSON(url, function(data, _, xhr) {
if (xhr.status != 200) {
alert('{% trans %}Unknown UV code{% endtrans %}')
return
}
for (let key in data) { for (let key in data) {
if (data.hasOwnProperty(key)) { if (data.hasOwnProperty(key)) {
const el = document.querySelector('[name="' + key + '"]') const el = document.querySelector('[name="' + key + '"]')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment