diff --git a/files/serializers.py b/files/serializers.py index 75afc14..28da7be 100644 --- a/files/serializers.py +++ b/files/serializers.py @@ -23,7 +23,10 @@ class MediaSerializer(serializers.ModelSerializer): ) def get_thumbnail_url(self, obj): - return self.context["request"].build_absolute_uri(obj.thumbnail_url) + if obj.thumbnail_url: + return self.context["request"].build_absolute_uri(obj.thumbnail_url) + else: + return None def get_author_profile(self, obj): return self.context["request"].build_absolute_uri(obj.author_profile()) diff --git a/files/views.py b/files/views.py index 5160e65..b3cedd9 100644 --- a/files/views.py +++ b/files/views.py @@ -1,3 +1,4 @@ +from datetime import datetime, timedelta from django.shortcuts import render from django.http import HttpResponseRedirect from django.conf import settings @@ -713,6 +714,7 @@ class MediaSearch(APIView): media_type = params.get("media_type", "").strip() author = params.get("author", "").strip() + upload_date = params.get('upload_date', '').strip() sort_by_options = ["title", "add_date", "edit_date", "views", "likes"] if sort_by not in sort_by_options: @@ -760,6 +762,24 @@ class MediaSearch(APIView): if author: media = media.filter(user__username=author) + if upload_date: + gte = lte = None + if upload_date == 'today': + gte = datetime.now().date() + if upload_date == 'this_week': + gte = datetime.now() - timedelta(days=7) + if upload_date == 'this_month': + year = datetime.now().date().year + month = datetime.now().date().month + gte = datetime(year,month,1) + if upload_date == 'this_year': + year = datetime.now().date().year + gte = datetime(year,1,1) + if lte: + media = media.filter(add_date__lte=lte) + if gte: + media = media.filter(add_date__gte=gte) + media = media.order_by(f"{ordering}{sort_by}") if self.request.query_params.get("show", "").strip() == "titles":