mirror of
https://github.com/TurTaskProject/TurTaskWeb.git
synced 2025-12-19 22:14:07 +01:00
create apiview
This commit is contained in:
parent
c8aabdbaff
commit
8c91b3030a
@ -52,6 +52,7 @@ INSTALLED_APPS = [
|
||||
'tasks',
|
||||
'users',
|
||||
'authentications',
|
||||
'dashboard',
|
||||
|
||||
'corsheaders',
|
||||
'drf_spectacular',
|
||||
|
||||
@ -27,4 +27,5 @@ urlpatterns = [
|
||||
path('api/schema/', SpectacularAPIView.as_view(), name='schema'),
|
||||
path('api/schema/swagger-ui/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'),
|
||||
path('api/schema/redoc/', SpectacularRedocView.as_view(url_name='schema'), name='redoc'),
|
||||
path('dashboard/', include('dashboard.urls')),
|
||||
]
|
||||
0
backend/dashboard/__init__.py
Normal file
0
backend/dashboard/__init__.py
Normal file
3
backend/dashboard/admin.py
Normal file
3
backend/dashboard/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
backend/dashboard/apps.py
Normal file
6
backend/dashboard/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DashboardConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'dashboard'
|
||||
0
backend/dashboard/migrations/__init__.py
Normal file
0
backend/dashboard/migrations/__init__.py
Normal file
3
backend/dashboard/models.py
Normal file
3
backend/dashboard/models.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
7
backend/dashboard/serializers.py
Normal file
7
backend/dashboard/serializers.py
Normal file
@ -0,0 +1,7 @@
|
||||
from rest_framework import serializers
|
||||
from .models import UserStats
|
||||
|
||||
class UserStatsSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = UserStats
|
||||
fields = ['health', 'gold', 'experience', 'strength', 'intelligence', 'endurance', 'perception', 'luck', 'level']
|
||||
3
backend/dashboard/tests.py
Normal file
3
backend/dashboard/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
6
backend/dashboard/urls.py
Normal file
6
backend/dashboard/urls.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from .views import DashboardStatsAPIView
|
||||
|
||||
urlpatterns = [
|
||||
path('dashboard/stats/', DashboardStatsAPIView.as_view(), name='dashboard-stats'),
|
||||
]
|
||||
58
backend/dashboard/views.py
Normal file
58
backend/dashboard/views.py
Normal file
@ -0,0 +1,58 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from django.db.models import Count
|
||||
from django.utils import timezone
|
||||
|
||||
from tasks.models import Todo, RecurrenceTask
|
||||
|
||||
class DashboardStatsAPIView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def get(self, request):
|
||||
user = request.user
|
||||
|
||||
# Calculate task usage statistics
|
||||
todo_count = Todo.objects.filter(user=user).count()
|
||||
recurrence_task_count = RecurrenceTask.objects.filter(user=user).count()
|
||||
|
||||
# Calculate how many tasks were completed in the last 7 days
|
||||
completed_todo_count_last_week = Todo.objects.filter(user=user, completed=True, last_update__gte=timezone.now() - timezone.timedelta(days=7)).count()
|
||||
completed_recurrence_task_count_last_week = RecurrenceTask.objects.filter(user=user, completed=True, last_update__gte=timezone.now() - timezone.timedelta(days=7)).count()
|
||||
|
||||
# Calculate subtask completion rate
|
||||
total_subtasks = Todo.objects.filter(user=user).aggregate(total=Count('subtask__id'))['total']
|
||||
completed_subtasks = Todo.objects.filter(user=user, subtask__completed=True).aggregate(total=Count('subtask__id'))['total']
|
||||
|
||||
# Calculate overall completion rate
|
||||
total_tasks = todo_count + recurrence_task_count
|
||||
completed_tasks = completed_todo_count_last_week + completed_recurrence_task_count_last_week
|
||||
overall_completion_rate = (completed_tasks / total_tasks) * 100 if total_tasks > 0 else 0
|
||||
|
||||
data = {
|
||||
'todo_count': todo_count,
|
||||
'recurrence_task_count': recurrence_task_count,
|
||||
'completed_todo_count_last_week': completed_todo_count_last_week,
|
||||
'completed_recurrence_task_count_last_week': completed_recurrence_task_count_last_week,
|
||||
'total_subtasks': total_subtasks,
|
||||
'completed_subtasks': completed_subtasks,
|
||||
'overall_completion_rate': overall_completion_rate,
|
||||
}
|
||||
|
||||
return Response(data, status=status.HTTP_200_OK)
|
||||
|
||||
def post(self, request):
|
||||
# Handle incoming data from the POST request
|
||||
# Update the necessary information based on the data
|
||||
|
||||
task_id = request.data.get('task_id')
|
||||
is_completed = request.data.get('is_completed')
|
||||
|
||||
try:
|
||||
task = Todo.objects.get(id=task_id, user=request.user)
|
||||
task.completed = is_completed
|
||||
task.save()
|
||||
return Response({'message': 'Task completion status updated successfully'}, status=status.HTTP_200_OK)
|
||||
except Todo.DoesNotExist:
|
||||
return Response({'error': 'Task not found'}, status=status.HTTP_404_NOT_FOUND)
|
||||
Loading…
Reference in New Issue
Block a user