2025-01-28 21:14:15 +01:00
|
|
|
import datetime
|
2025-01-27 18:50:24 +01:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from django.db.models import Sum, Q, QuerySet
|
2025-01-26 02:01:38 +01:00
|
|
|
from django.http import JsonResponse
|
2025-01-26 00:53:17 +01:00
|
|
|
from django.shortcuts import render, get_object_or_404, redirect
|
|
|
|
from django.views import generic
|
2025-02-02 13:33:56 +01:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2025-01-26 02:01:38 +01:00
|
|
|
from django.views.decorators.http import require_POST
|
2025-01-26 00:53:17 +01:00
|
|
|
|
|
|
|
from dispatcher.core import import_tasks
|
|
|
|
from dispatcher.forms import ProjectForm, ImportForm
|
2025-02-02 13:33:56 +01:00
|
|
|
from dispatcher.models import Project, Parent, Clocking
|
|
|
|
from dispatcher.serializers import ClockingSerializer
|
2025-01-25 00:47:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def dashboard_view(request):
|
2025-01-26 00:53:17 +01:00
|
|
|
context = {
|
|
|
|
"projects": Project.objects.all(),
|
|
|
|
"parents": Parent.objects.all()
|
|
|
|
}
|
|
|
|
return render(request, "dashboard.html", context)
|
|
|
|
|
|
|
|
def projects_view(request):
|
|
|
|
context = {
|
|
|
|
"projects": Project.objects.all(),
|
|
|
|
"parents": Parent.objects.all()
|
|
|
|
}
|
|
|
|
return render(request, "projects.html", context)
|
|
|
|
|
|
|
|
def project_view(request, id):
|
|
|
|
project = get_object_or_404(Project, id=id)
|
|
|
|
context = {}
|
|
|
|
form = ProjectForm(request.POST or None, request.FILES or None, instance=project)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
context["form"] = ProjectForm(instance=project)
|
|
|
|
return render(request, "project.html", context)
|
|
|
|
|
2025-01-28 21:14:15 +01:00
|
|
|
def table_view(request):
|
|
|
|
return render(request, "table.html")
|
|
|
|
|
2025-01-26 02:01:38 +01:00
|
|
|
@require_POST
|
|
|
|
def set_parent(request, id):
|
|
|
|
project = get_object_or_404(Project, id=id)
|
|
|
|
parent_id = request.POST.get("parent_id")
|
|
|
|
try:
|
|
|
|
parent = Parent.objects.get(id=parent_id)
|
|
|
|
except Parent.DoesNotExist:
|
|
|
|
parent = None
|
|
|
|
|
|
|
|
project.parent = parent
|
|
|
|
project.save()
|
|
|
|
return JsonResponse({"status": "success"})
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-26 00:53:17 +01:00
|
|
|
def import_view(request):
|
|
|
|
if request.method == "POST":
|
|
|
|
form = ImportForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
print(request.FILES)
|
|
|
|
import_tasks(request.FILES["file"].read().decode("utf-8"))
|
|
|
|
return redirect("dashboard")
|
|
|
|
|
|
|
|
return render(request, "import.html")
|
|
|
|
|
2025-01-26 02:01:38 +01:00
|
|
|
def get_stats_by_month(request, year: int, month: int):
|
|
|
|
if month < 1 or month > 12:
|
|
|
|
return JsonResponse({"status": "error", "error": f"Invalid month {month}"})
|
|
|
|
|
|
|
|
parents = Parent.objects.annotate(
|
|
|
|
total_duration=Sum(
|
|
|
|
"project__task__duration",
|
|
|
|
filter=Q(
|
|
|
|
project__task__date__year=year,
|
|
|
|
project__task__date__month=month
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2025-01-27 18:50:24 +01:00
|
|
|
projects = Project.objects.annotate(
|
|
|
|
total_duration=Sum(
|
|
|
|
"task__duration",
|
|
|
|
filter=Q(
|
|
|
|
task__date__year=year,
|
|
|
|
task__date__month=month
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return JsonResponse({"status": "success", "data": format_stats(parents, projects)})
|
|
|
|
|
2025-01-28 21:14:15 +01:00
|
|
|
def get_stats_between(request, start_date: datetime.date, end_date: datetime.date):
|
2025-01-27 18:50:24 +01:00
|
|
|
parents = Parent.objects.annotate(
|
|
|
|
total_duration=Sum(
|
|
|
|
"project__task__duration",
|
|
|
|
filter=Q(
|
|
|
|
project__task__date__gte=start_date,
|
|
|
|
project__task__date__lt=end_date + timedelta(days=1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
projects = Project.objects.annotate(
|
|
|
|
total_duration=Sum(
|
|
|
|
"task__duration",
|
|
|
|
filter=Q(
|
|
|
|
task__date__gte=start_date,
|
|
|
|
task__date__lt=end_date + timedelta(days=1)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return JsonResponse({"status": "success", "data": format_stats(parents, projects)})
|
2025-01-26 02:01:38 +01:00
|
|
|
|
2025-01-27 18:50:24 +01:00
|
|
|
def format_stats(parents: QuerySet[Parent], projects: QuerySet[Project]):
|
|
|
|
data = {
|
|
|
|
"parents": [
|
|
|
|
{
|
|
|
|
"id": parent.id,
|
|
|
|
"name": parent.name,
|
|
|
|
"project_num": parent.project_num,
|
|
|
|
"duration": parent.total_duration
|
|
|
|
}
|
|
|
|
for parent in parents
|
|
|
|
],
|
|
|
|
"projects": [
|
|
|
|
{
|
|
|
|
"id": project.id,
|
|
|
|
"name": project.name,
|
|
|
|
"duration": project.total_duration
|
|
|
|
}
|
|
|
|
for project in projects
|
|
|
|
]
|
|
|
|
}
|
|
|
|
return data
|
2025-01-26 02:01:38 +01:00
|
|
|
|
2025-01-26 00:53:17 +01:00
|
|
|
class ParentsView(generic.ListView):
|
|
|
|
model = Parent
|
|
|
|
template_name = "parents.html"
|
|
|
|
context_object_name = "elements"
|
2025-01-28 21:14:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_table_data(request, start_date: datetime.date, end_date: datetime.date):
|
|
|
|
end_date = end_date + timedelta(days=1)
|
2025-02-02 13:33:56 +01:00
|
|
|
clockings = Clocking.objects.filter(
|
|
|
|
date__gte=start_date,
|
|
|
|
date__lte=end_date
|
|
|
|
)
|
|
|
|
|
2025-01-28 21:14:15 +01:00
|
|
|
parents = Parent.objects.all().order_by("id")
|
|
|
|
data = {
|
|
|
|
"parents": [
|
|
|
|
{
|
|
|
|
"id": parent.id,
|
|
|
|
"name": parent.name,
|
|
|
|
"project_num": parent.project_num,
|
|
|
|
"projects": [
|
|
|
|
{
|
|
|
|
"id": project.id,
|
|
|
|
"name": project.name,
|
|
|
|
"tasks": [
|
|
|
|
{
|
|
|
|
"date": task["date"],
|
|
|
|
"duration": task["duration"]
|
|
|
|
}
|
|
|
|
for task in project.task_set.filter(
|
|
|
|
date__gte=start_date,
|
|
|
|
date__lt=end_date
|
|
|
|
).values("date").order_by("date").annotate(duration=Sum("duration"))
|
|
|
|
]
|
|
|
|
}
|
|
|
|
for project in parent.project_set.order_by("id")
|
|
|
|
]
|
|
|
|
}
|
|
|
|
for parent in parents
|
2025-02-02 13:33:56 +01:00
|
|
|
],
|
|
|
|
"clockings": ClockingSerializer(clockings, many=True).data
|
2025-01-28 21:14:15 +01:00
|
|
|
}
|
2025-02-02 13:33:56 +01:00
|
|
|
return JsonResponse({"status": "success", "data": data})
|
|
|
|
|
|
|
|
@require_POST
|
|
|
|
@csrf_exempt
|
|
|
|
def set_clocking(request, date: datetime.date):
|
|
|
|
clocking, created = Clocking.objects.get_or_create(date=date)
|
|
|
|
clocking.in_am = request.POST.get("in_am", clocking.in_am) or None
|
|
|
|
clocking.out_am = request.POST.get("out_am", clocking.out_am) or None
|
|
|
|
clocking.in_pm = request.POST.get("in_pm", clocking.in_pm) or None
|
|
|
|
clocking.out_pm = request.POST.get("out_pm", clocking.out_pm) or None
|
|
|
|
clocking.remote = request.POST.get("remote", clocking.remote) or None
|
|
|
|
clocking.save()
|
|
|
|
clocking.refresh_from_db()
|
|
|
|
|
|
|
|
return JsonResponse({
|
|
|
|
"status": "success",
|
|
|
|
"clocking": ClockingSerializer(clocking).data
|
|
|
|
})
|