Skip to content

Commit

Permalink
Merge pull request #6 from CodeForAfrica/ft-tweets-search
Browse files Browse the repository at this point in the history
Ft tweets search
esirK authored Nov 1, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents cb9b49d + 07dc1aa commit e3ff703
Showing 4 changed files with 52 additions and 2 deletions.
1 change: 1 addition & 0 deletions contrib/docker/cmd.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions twoopstracker/settings.py
Original file line number Diff line number Diff line change
@@ -44,6 +44,7 @@
"django.contrib.messages",
"django.contrib.staticfiles",
# installed apps
"django.contrib.postgres",
"rest_framework",
"storages",
# Local apps
2 changes: 1 addition & 1 deletion twoopstracker/twoops/migrations/0002_twitter_account.py
Original file line number Diff line number Diff line change
@@ -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(
50 changes: 49 additions & 1 deletion twoopstracker/twoops/views.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e3ff703

Please sign in to comment.