Skip to content

Commit

Permalink
Merge pull request #1594 from gtech-mulearn/dev
Browse files Browse the repository at this point in the history
[fix] org edit
  • Loading branch information
adnankattekaden authored Nov 23, 2023
2 parents d333e26 + ade56da commit 9982121
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
1 change: 1 addition & 0 deletions api/dashboard/organisation/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion api/url_shortener/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"]
63 changes: 42 additions & 21 deletions api/url_shortener/url_shortener_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from api.url_shortener.serializers import (
ShowShortenUrlsSerializer,
ShortenUrlsCreateUpdateSerializer,
ShowShortenUrlsTrackerSerializer
)
from db.url_shortener import UrlShortener, UrlShortenerTracker
from utils.permission import CustomizePermission
from utils.permission import role_required
from utils.response import CustomResponse
from utils.types import RoleType
from utils.utils import CommonUtils
from django.db.models import Count


class UrlShortenerAPI(APIView):
Expand Down Expand Up @@ -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)

1 change: 0 additions & 1 deletion db/url_shortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9982121

Please sign in to comment.