2025-01-26 00:53:17 +01:00
|
|
|
from django.core.files.uploadedfile import UploadedFile
|
2025-01-26 02:01:38 +01:00
|
|
|
from django.db.models import Sum, Q
|
|
|
|
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-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-01-26 02:01:38 +01:00
|
|
|
from dispatcher.models import Project, Parent, Task
|
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-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
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
data = [
|
|
|
|
{
|
|
|
|
"id": parent.id,
|
|
|
|
"name": parent.name,
|
|
|
|
"project_num": parent.project_num,
|
|
|
|
"duration": parent.total_duration
|
|
|
|
}
|
|
|
|
for parent in parents
|
|
|
|
]
|
|
|
|
return JsonResponse({"status": "success", "data": data})
|
|
|
|
|
|
|
|
|
2025-01-26 00:53:17 +01:00
|
|
|
class ParentsView(generic.ListView):
|
|
|
|
model = Parent
|
|
|
|
template_name = "parents.html"
|
|
|
|
context_object_name = "elements"
|