2024-12-04 11:41:57 +01:00
|
|
|
import datetime
|
2024-12-02 16:49:45 +01:00
|
|
|
import json
|
|
|
|
import base64
|
|
|
|
|
2024-12-03 11:38:13 +01:00
|
|
|
from core.models import Berichtsheft
|
2024-12-06 12:10:30 +01:00
|
|
|
from core.reports import DailyReport, WeeklyReport, choose_report_kind
|
2024-12-04 11:41:57 +01:00
|
|
|
import core.util
|
2024-12-06 14:04:17 +01:00
|
|
|
from enum import Enum
|
|
|
|
|
|
|
|
|
|
|
|
class Roles(Enum):
|
|
|
|
TRAINEE = "trainee"
|
|
|
|
MANAGER = "manager"
|
|
|
|
ADMIN = "admin"
|
2024-12-03 11:38:13 +01:00
|
|
|
|
|
|
|
|
2024-12-02 16:49:45 +01:00
|
|
|
class AzureUser:
|
|
|
|
display_name: str
|
|
|
|
id: str
|
|
|
|
claims: list
|
|
|
|
|
|
|
|
def __init__(self, request):
|
|
|
|
try:
|
|
|
|
# X-MS-CLIENT-PRINCIPAL-ID
|
|
|
|
self.id = request.headers["X-MS-CLIENT-PRINCIPAL-ID"]
|
|
|
|
|
|
|
|
# X-MS-CLIENT-PRINCIPAL-NAME
|
|
|
|
self.id = request.headers["X-MS-CLIENT-PRINCIPAL-NAME"]
|
|
|
|
|
|
|
|
# X-MS-CLIENT-PRINCIPAL
|
2024-12-03 11:38:13 +01:00
|
|
|
claims_json = json.loads(
|
|
|
|
base64.decode(request.headers["X-MS-CLIENT-PRINCIPAL"])
|
|
|
|
)
|
2024-12-02 16:49:45 +01:00
|
|
|
claims = {}
|
|
|
|
|
|
|
|
for claim in claims_json["claims"]:
|
|
|
|
claims[claim["typ"]] = claim["val"]
|
2024-12-03 11:38:13 +01:00
|
|
|
|
2024-12-02 16:49:45 +01:00
|
|
|
self.claims = claims
|
|
|
|
except:
|
|
|
|
self.display_name = "Anon"
|
|
|
|
self.id = "anon"
|
|
|
|
|
2024-12-03 11:38:13 +01:00
|
|
|
def reports(self):
|
2024-12-04 11:41:57 +01:00
|
|
|
return Berichtsheft.objects.filter(user=self.id).order_by("-year", "-week")
|
2024-12-03 13:13:50 +01:00
|
|
|
|
2024-12-04 09:37:01 +01:00
|
|
|
def get_report_kind(self):
|
2024-12-03 13:13:50 +01:00
|
|
|
# TODO : Implement
|
2024-12-04 09:37:01 +01:00
|
|
|
return "weekly"
|
|
|
|
|
2024-12-06 12:10:30 +01:00
|
|
|
def get_report_kind_form(self, request=None, files=None):
|
|
|
|
return choose_report_kind(self.get_report_kind(), request, files)
|
2024-12-04 11:41:57 +01:00
|
|
|
|
|
|
|
def latest_report(self):
|
|
|
|
return self.reports().order_by("-year", "-week").first()
|
|
|
|
|
|
|
|
def late_reports(self) -> int:
|
|
|
|
year_now, week_now, _ = datetime.datetime.today().isocalendar()
|
|
|
|
count = 0
|
|
|
|
latest = self.latest_report()
|
|
|
|
|
|
|
|
new_year, new_week = (latest.year, latest.week)
|
|
|
|
|
|
|
|
while week_now != new_week or (week_now == new_week and year_now != new_year):
|
|
|
|
count += 1
|
|
|
|
new_year, new_week = core.util.next_date(new_year, new_week)
|
|
|
|
|
|
|
|
return count
|
2024-12-06 14:04:17 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def role(self) -> Roles:
|
|
|
|
# TODO : Implement
|
|
|
|
return Roles.TRAINEE
|