Skip to content

Commit

Permalink
Merge pull request #97 from enesonus/OVTF-499-dev
Browse files Browse the repository at this point in the history
OVTF-499-dev: Recent 5 Days Data
  • Loading branch information
bilalbdertli authored Dec 5, 2023
2 parents 05d51ba + a76401b commit cf4e27a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 2 additions & 1 deletion apps/users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@
path('delete-request/', view=views.delete_request, name='delete-request'),
path('edit-user-preferences/', view = views.edit_user_preferences, name= 'edit_user_preferences'),
path('recommend-songs/', view = views.recommend_songs, name='recommend-songs'),
path('get-user-profile/', view = views.get_user_profile, name= 'get-user-profile')
path('get-user-profile/', view = views.get_user_profile, name= 'get-user-profile'),
path('get-recent-addition-counts/', view=views.get_recent_addition_by_count, name='get-recent-addition-count'),
]
29 changes: 28 additions & 1 deletion apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from datetime import datetime, timedelta
from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.db.models import Prefetch
from django.db.models import Prefetch, DateField, Count
from django.db.models.functions import TruncDay
from django.http import JsonResponse, HttpResponse
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -1222,3 +1223,29 @@ def get_user_profile(request, userid):

except User.DoesNotExist:
return JsonResponse({'error': 'User not found.'}, status=404)


@csrf_exempt
@token_required
def get_recent_addition_by_count(request, userid):
if request.method != 'GET':
return HttpResponse(status=405)
try:
user = User.objects.get(id=userid)
end_date = datetime.now().date()
start_date = end_date - timedelta(days=4)
song_count_by_day = {start_date + timedelta(days=i): 0 for i in range(5)}
user_songs_per_day = user.usersongrating_set.filter(created_at__gt=start_date).prefetch_related('song').order_by('-created_at')
for song_rating in user_songs_per_day:
# Extract just the date part of the 'created_at' datetime
created_date = song_rating.created_at.date()

# Check if the created date is within our range
if start_date <= created_date <= end_date:
song_count_by_day[created_date] += 1
formatted_song_count_by_day = {date.strftime("%d %B, %A"): count for date, count in song_count_by_day.items()}
return JsonResponse({'song_counts': formatted_song_count_by_day}, status=200)
except User.DoesNotExist:
return JsonResponse({'error': 'User does not exist'}, status=404)
except Exception as e:
return JsonResponse({'error': str(e)}, status=500)

0 comments on commit cf4e27a

Please sign in to comment.