Backend Draft
This commit is contained in:
62
backend/dashboard/views.py
Normal file
62
backend/dashboard/views.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from rest_framework import viewsets, generics, permissions, status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.decorators import action
|
||||
from django.db.models import Count
|
||||
from django.contrib.auth import get_user_model
|
||||
from projects.models import Project, Task
|
||||
from analytics.models import ActivityLog
|
||||
from analytics.serializers import ActivityLogSerializer
|
||||
from .models import Notification
|
||||
from .serializers import NotificationSerializer
|
||||
from accounts.permissions import IsTenantUser
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.decorators.cache import cache_page
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
class DashboardSummaryView(APIView):
|
||||
permission_classes = [permissions.IsAuthenticated, IsTenantUser]
|
||||
|
||||
def get(self, request):
|
||||
tenant = request.tenant
|
||||
|
||||
# Basic Counts
|
||||
total_projects = Project.objects.filter(tenant=tenant).count()
|
||||
total_tasks = Task.objects.filter(tenant=tenant).count()
|
||||
total_users = User.objects.filter(tenant=tenant).count()
|
||||
|
||||
# Task Status Breakdown
|
||||
task_status_counts = Task.objects.filter(tenant=tenant).values('status').annotate(count=Count('status'))
|
||||
status_breakdown = {item['status']: item['count'] for item in task_status_counts}
|
||||
|
||||
# Recent Activity (Last 10)
|
||||
recent_activity = ActivityLog.objects.filter(tenant=tenant).order_by('-created_at')[:10]
|
||||
activity_serializer = ActivityLogSerializer(recent_activity, many=True)
|
||||
|
||||
return Response({
|
||||
'total_projects': total_projects,
|
||||
'total_tasks': total_tasks,
|
||||
'total_users': total_users,
|
||||
'task_status_breakdown': status_breakdown,
|
||||
'recent_activity': activity_serializer.data
|
||||
})
|
||||
|
||||
class NotificationViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = NotificationSerializer
|
||||
permission_classes = [permissions.IsAuthenticated, IsTenantUser]
|
||||
|
||||
def get_queryset(self):
|
||||
return Notification.objects.filter(tenant=self.request.tenant, user=self.request.user)
|
||||
|
||||
@action(detail=True, methods=['post'])
|
||||
def mark_read(self, request, pk=None):
|
||||
notification = self.get_object()
|
||||
notification.is_read = True
|
||||
notification.save()
|
||||
return Response({'status': 'notification marked as read'})
|
||||
|
||||
@action(detail=False, methods=['post'])
|
||||
def mark_all_read(self, request):
|
||||
self.get_queryset().filter(is_read=False).update(is_read=True)
|
||||
return Response({'status': 'all notifications marked as read'})
|
||||
Reference in New Issue
Block a user