-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from CodeForAfrica/ft-tweets-search
Ft tweets search
- Loading branch information
Showing
4 changed files
with
52 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |