diff --git a/contrib/docker/cmd.sh b/contrib/docker/cmd.sh index 41654b5d..890ed209 100644 --- a/contrib/docker/cmd.sh +++ b/contrib/docker/cmd.sh @@ -18,4 +18,5 @@ exec gunicorn \ --log-file=/app/logs/gunicorn.log \ --access-logfile=/app/logs/access.log \ --name twoopsTracker \ + ${TWOOPSTRACKER_GUNICORN_EXTRA_CONFIG:-} \ twoopstracker.wsgi:application diff --git a/twoopstracker/settings.py b/twoopstracker/settings.py index 5c82bce0..09a5156a 100644 --- a/twoopstracker/settings.py +++ b/twoopstracker/settings.py @@ -44,6 +44,7 @@ "django.contrib.messages", "django.contrib.staticfiles", # installed apps + "django.contrib.postgres", "rest_framework", "storages", # Local apps diff --git a/twoopstracker/twoops/migrations/0002_twitter_account.py b/twoopstracker/twoops/migrations/0002_twitter_account.py index 4dcd1f49..3e2f5fe1 100644 --- a/twoopstracker/twoops/migrations/0002_twitter_account.py +++ b/twoopstracker/twoops/migrations/0002_twitter_account.py @@ -40,7 +40,7 @@ class Migration(migrations.Migration): ("favourites_count", models.IntegerField()), ("statuses_count", models.IntegerField()), ("created_at", models.DateTimeField(auto_now_add=True)), - ("profile_image_url", models.URLField()), + ("profile_image_url", models.URLField(max_length=255)), ( "deleted", models.BooleanField( diff --git a/twoopstracker/twoops/views.py b/twoopstracker/twoops/views.py index 6724bdd0..73e8b8b8 100644 --- a/twoopstracker/twoops/views.py +++ b/twoopstracker/twoops/views.py @@ -1,9 +1,57 @@ +from datetime import datetime + +from django.contrib.postgres.search import SearchQuery, SearchVector from rest_framework import generics from twoopstracker.twoops.models import Tweet from twoopstracker.twoops.serializers import TweetSerializer +def get_search_type(search_string): + search_type = "" + + if search_string.startswith('"') and search_string.endswith('"'): + return "phrase" + elif search_string.startswith("(") and search_string.endswith(")"): + return "websearch" + elif "," in search_string: + return "raw" + else: + return search_type + + +def refromat_search_string(search_string): + return " | ".join(search_string.split(",")) + + class TweetsView(generics.ListAPIView): serializer_class = TweetSerializer - queryset = Tweet.objects.all() + + def get_queryset(self): + query = self.request.GET.get("query") + startDate = self.request.GET.get("startDate") + endDate = self.request.GET.get("endDate") + + if startDate: + startDate = datetime.fromisoformat(startDate) + if endDate: + endDate = datetime.fromisoformat(endDate) + + if query: + search_type = get_search_type(query) + if search_type == "raw": + query = refromat_search_string(query) + vector = SearchVector("content", "actual_tweet") + if search_type: + search_query = SearchQuery(query, search_type=search_type) + else: + search_query = SearchQuery(query) + tweets = Tweet.objects.annotate(search=vector).filter(search=search_query) + else: + tweets = Tweet.objects.all() + + if startDate: + tweets = tweets.filter(created_at__gte=startDate) + if endDate: + tweets = tweets.filter(created_at__lte=endDate) + return tweets