2024-10-23 11:06:57 +02:00
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
|
2017-09-24 00:42:36 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2024-10-23 11:06:57 +02:00
|
|
|
from django.http import response
|
2017-09-24 18:20:10 +02:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2017-09-23 21:40:45 +02:00
|
|
|
|
2017-09-24 00:42:36 +02:00
|
|
|
import mainsite.models as mainModels
|
|
|
|
|
2024-10-23 11:06:57 +02:00
|
|
|
from . import models
|
|
|
|
|
2017-09-24 00:42:36 +02:00
|
|
|
|
2017-09-24 18:20:10 +02:00
|
|
|
def authentify(data, payload):
|
2024-10-23 11:06:57 +02:00
|
|
|
"""returns whether the request's authentification is correct"""
|
|
|
|
required = ["keyId", "timestamp", "hmac"]
|
2017-09-24 00:42:36 +02:00
|
|
|
for field in required:
|
|
|
|
if field not in data:
|
|
|
|
return response.HttpResponseForbidden(
|
2024-10-23 11:06:57 +02:00
|
|
|
'Missing required field "{}"'.format(field)
|
|
|
|
)
|
2017-09-24 00:42:36 +02:00
|
|
|
try:
|
2024-10-23 11:06:57 +02:00
|
|
|
key = models.ApiKey.objects.get(id=data["keyId"])
|
2017-09-24 00:42:36 +02:00
|
|
|
except models.ApiKey.DoesNotExist:
|
2024-10-23 11:06:57 +02:00
|
|
|
return response.HttpResponseForbidden("Bad authentication")
|
2017-09-24 00:42:36 +02:00
|
|
|
|
2024-10-23 11:06:57 +02:00
|
|
|
if not key.isCorrect(data["timestamp"], data["hmac"], payload):
|
|
|
|
return response.HttpResponseForbidden("Bad authentication")
|
2017-09-24 00:42:36 +02:00
|
|
|
|
|
|
|
|
2017-09-24 18:20:10 +02:00
|
|
|
def apiView(required=[]):
|
|
|
|
def decorator(fct):
|
|
|
|
@csrf_exempt
|
|
|
|
def wrap(request, *args, **kwargs):
|
|
|
|
try:
|
2024-10-23 11:06:57 +02:00
|
|
|
data = json.loads(request.body.decode("utf-8"))
|
2017-10-16 20:02:36 +02:00
|
|
|
except TypeError:
|
|
|
|
return response.HttpResponseBadRequest("Bad packet format")
|
|
|
|
except json.decoder.JSONDecodeError:
|
2017-09-24 18:20:10 +02:00
|
|
|
return response.HttpResponseBadRequest("Bad json")
|
2017-09-24 00:42:36 +02:00
|
|
|
|
2017-09-24 18:20:10 +02:00
|
|
|
try:
|
2024-10-23 11:06:57 +02:00
|
|
|
authData = data["auth"]
|
|
|
|
reqDataOrig = data["req"]
|
2017-09-24 18:20:10 +02:00
|
|
|
except KeyError:
|
|
|
|
return response.HttpResponseBadRequest("Bad request format")
|
2017-09-24 00:42:36 +02:00
|
|
|
|
2017-10-16 20:02:36 +02:00
|
|
|
try:
|
|
|
|
reqData = json.loads(reqDataOrig)
|
|
|
|
except TypeError:
|
|
|
|
return response.HttpResponseBadRequest("Bad packet format")
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
return response.HttpResponseBadRequest("Bad inner json")
|
|
|
|
|
2017-09-24 18:20:10 +02:00
|
|
|
for field in required:
|
|
|
|
if field not in reqData:
|
|
|
|
return response.HttpResponseBadRequest(
|
2024-10-23 11:06:57 +02:00
|
|
|
"Missing field {}".format(field)
|
|
|
|
)
|
2017-09-24 00:42:36 +02:00
|
|
|
|
2017-10-16 20:02:36 +02:00
|
|
|
authVal = authentify(authData, reqDataOrig)
|
2017-09-24 18:20:10 +02:00
|
|
|
if authVal is not None:
|
|
|
|
return authVal
|
2017-09-24 00:42:36 +02:00
|
|
|
|
2017-09-24 18:20:10 +02:00
|
|
|
return fct(request, reqData, *args, **kwargs)
|
2024-10-23 11:06:57 +02:00
|
|
|
|
2017-09-24 18:20:10 +02:00
|
|
|
return wrap
|
2024-10-23 11:06:57 +02:00
|
|
|
|
2017-09-24 18:20:10 +02:00
|
|
|
return decorator
|
2017-09-24 00:42:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
@apiView(required=["id", "url", "date"])
|
|
|
|
def publishApiView(request, data):
|
2024-10-23 11:06:57 +02:00
|
|
|
"""Publish a BOcal, and create the corresponding year if needed"""
|
|
|
|
if mainModels.Publication.objects.filter(num=data["id"]).count() > 0:
|
2017-09-24 00:42:36 +02:00
|
|
|
return response.HttpResponseBadRequest(
|
|
|
|
"Un BOcal du même numéro est déjà présent ! Ajoutez celui-ci à la "
|
2024-10-23 11:06:57 +02:00
|
|
|
"main si vous voulez vraiment faire ça."
|
|
|
|
)
|
2017-09-24 00:42:36 +02:00
|
|
|
|
|
|
|
try:
|
2024-10-23 11:06:57 +02:00
|
|
|
year, month, day = [int(x) for x in data["date"].split("-")]
|
2017-09-24 00:42:36 +02:00
|
|
|
date = datetime.date(year, month, day)
|
2024-10-23 11:06:57 +02:00
|
|
|
except Exception:
|
2017-09-24 00:42:36 +02:00
|
|
|
return response.HttpResponseBadRequest("Bad date")
|
|
|
|
|
2024-10-23 11:06:57 +02:00
|
|
|
pub = mainModels.Publication(num=data["id"], url=data["url"], date=date)
|
2017-09-24 00:42:36 +02:00
|
|
|
try:
|
|
|
|
pub.full_clean()
|
|
|
|
except ValidationError as e:
|
2024-10-23 11:06:57 +02:00
|
|
|
return response.HttpResponseBadRequest("Invalid data: {}".format(e))
|
2017-09-24 00:42:36 +02:00
|
|
|
pub.save()
|
|
|
|
|
2017-09-24 18:41:31 +02:00
|
|
|
pub.createPubYear()
|
|
|
|
|
2017-09-24 00:42:36 +02:00
|
|
|
return response.HttpResponse("OK")
|