Merge pull request #42 from TurTaskProject/dashboard-api

create apiview
This commit is contained in:
Sirin Puenggun 2023-11-20 01:40:51 +07:00 committed by GitHub
commit 6a85af4482
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 110 additions and 1 deletions

View File

@ -52,6 +52,7 @@ INSTALLED_APPS = [
'tasks',
'users',
'authentications',
'dashboard',
'corsheaders',
'drf_spectacular',

View File

@ -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('api/', include('dashboard.urls')),
]

View File

View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class DashboardConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'dashboard'

View File

View 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']

View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

View File

@ -0,0 +1,6 @@
from django.urls import path
from .views import DashboardStatsAPIView
urlpatterns = [
path('dashboard/stats/', DashboardStatsAPIView.as_view(), name='dashboard-stats'),
]

View 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)

View File

@ -0,0 +1,23 @@
# Generated by Django 4.2.6 on 2023-11-17 16:40
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('tasks', '0013_alter_recurrencetask_recurrence_rule'),
]
operations = [
migrations.AddField(
model_name='recurrencetask',
name='completed',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='todo',
name='completed',
field=models.BooleanField(default=False),
),
]

View File

@ -18,7 +18,6 @@ class Task(models.Model):
:param title: Title of the task.
:param notes: Optional additional notes for the task.
:param tags: Associated tags for the task.
:param completed: A boolean field indicating whether the task is completed.
:param importance: The importance of the task (range: 1 to 5)
:param difficulty: The difficulty of the task (range: 1 to 5).
:param challenge: Associated challenge (optional).
@ -62,12 +61,14 @@ class Todo(Task):
NOT_IMPORTANT_URGENT = 3, 'Not Important & Urgent'
NOT_IMPORTANT_NOT_URGENT = 4, 'Not Important & Not Urgent'
completed = models.BooleanField(default=False)
priority = models.PositiveSmallIntegerField(choices=EisenhowerMatrix.choices, default=EisenhowerMatrix.NOT_IMPORTANT_NOT_URGENT)
def __str__(self):
return self.title
class RecurrenceTask(Task):
completed = models.BooleanField(default=False)
recurrence_rule = models.CharField()
def __str__(self) -> str: