diff --git a/api/dashboard/organisation/serializers.py b/api/dashboard/organisation/serializers.py index d4d760d2..534d9640 100644 --- a/api/dashboard/organisation/serializers.py +++ b/api/dashboard/organisation/serializers.py @@ -122,6 +122,7 @@ def update(self, instance, validated_data): instance.title = validated_data.get("title", instance.title) instance.code = validated_data.get("code", instance.code) instance.affiliation = validated_data.get("affiliation", instance.affiliation) + instance.district = validated_data.get("district", instance.district) instance.updated_by_id = user_id instance.save() return instance diff --git a/api/url_shortener/serializers.py b/api/url_shortener/serializers.py index 3a5a3e07..5fa3cb29 100644 --- a/api/url_shortener/serializers.py +++ b/api/url_shortener/serializers.py @@ -4,7 +4,7 @@ from rest_framework import serializers from rest_framework.serializers import ModelSerializer -from db.url_shortener import UrlShortener +from db.url_shortener import UrlShortener,UrlShortenerTracker from utils.permission import JWTUtils from utils.utils import DateTimeUtils @@ -52,3 +52,10 @@ def validate_short_url(self, value): raise serializers.ValidationError("Your shortened URL should be less than 300 characters in length," "only include letters, numbers and following special characters (/_)") return value + + +class ShowShortenUrlsTrackerSerializer(ModelSerializer): + + class Meta: + model = UrlShortenerTracker + fields = ["id","ip_address","device_type","operating_system","browser"] diff --git a/api/url_shortener/url_shortener_view.py b/api/url_shortener/url_shortener_view.py index d720c636..78cf8f07 100644 --- a/api/url_shortener/url_shortener_view.py +++ b/api/url_shortener/url_shortener_view.py @@ -3,6 +3,7 @@ from api.url_shortener.serializers import ( ShowShortenUrlsSerializer, ShortenUrlsCreateUpdateSerializer, + ShowShortenUrlsTrackerSerializer ) from db.url_shortener import UrlShortener, UrlShortenerTracker from utils.permission import CustomizePermission @@ -10,6 +11,7 @@ from utils.response import CustomResponse from utils.types import RoleType from utils.utils import CommonUtils +from django.db.models import Count class UrlShortenerAPI(APIView): @@ -126,32 +128,51 @@ def delete(self, request, url_id): class UrlAnalyticsAPI(APIView): + authentication_classes = [CustomizePermission] + + @role_required( + [RoleType.ADMIN.value, RoleType.FELLOW.value, RoleType.ASSOCIATE.value] + ) def get(self, request, url_id): - url_shortener_object = UrlShortener.objects.get( - id=url_id - ) + url_tracker_obj = UrlShortener.objects.filter(id=url_id).first() - url_data = UrlShortenerTracker.objects.filter( - url_shortener_id=url_shortener_object.id - ) + if url_tracker_obj is None: + return CustomResponse( + general_message="No URL related data available" + ).get_failure_response() + + queryset = UrlShortenerTracker.objects.filter(url_shortener_id = url_tracker_obj.id) + device_counts = UrlShortenerTracker.objects.values('device_type').annotate(count=Count('device_type')).order_by('device_type') + platform_counts = UrlShortenerTracker.objects.values('operating_system').annotate(count=Count('operating_system')).order_by('operating_system') + browser_counts = UrlShortenerTracker.objects.values('browser').annotate(count=Count('browser')).order_by('browser') + + countset={} + countset[1] = device_counts + countset[2] = platform_counts + countset[3] = browser_counts + + print(countset) + paginated_queryset = CommonUtils.get_paginated_queryset( + queryset, + request, + ["id","ip_address","device_type","operating_system","browser",] - print(url_data) + ) - location_data = UrlShortenerTracker.objects.filter( - url_shortener=url_shortener_object - ).values( - 'city', - 'region', - 'country' + serializer = ShowShortenUrlsTrackerSerializer( + paginated_queryset.get("queryset"), + many=True ) - print(location_data) - # Browsers through which users accessed - browsers_data = UrlShortenerTracker.objects.filter( - url_shortener=url_shortener_object - ).values( - 'browser' - ).count() + return CustomResponse( + general_message="URL Stats", + response=countset + ).paginated_response( + data=serializer.data, + pagination=paginated_queryset.get( + "pagination" + ) + ) + - print(browsers_data) diff --git a/db/url_shortener.py b/db/url_shortener.py index 7bc3facc..73cd2d57 100644 --- a/db/url_shortener.py +++ b/db/url_shortener.py @@ -27,7 +27,6 @@ class UrlShortenerTracker(models.Model): browser = models.CharField(max_length=255, blank=True, null=True) operating_system = models.CharField(max_length=255, blank=True, null=True) version = models.CharField(max_length=255, blank=True, null=True) - created_at = models.DateTimeField(blank=True, null=True) device_type = models.CharField(max_length=255, blank=True, null=True) url_shortener = models.ForeignKey(UrlShortener, on_delete=models.CASCADE, blank=True, null=True) city = models.CharField(max_length=36, blank=True, null=True)