Skip to content

Commit

Permalink
Search tweets backend
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisConstant committed Jul 8, 2018
1 parent 8daa662 commit 8974788
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ede/services.py
@@ -0,0 +1,18 @@
from ede.models import Tweet


def get_tweets_search_data():
return dict(
tweets=[
{
'author_url': tweet.author.url,
'author_name': tweet.author.name,
'content': tweet.content,
'searchString': "{username} {name} {content}".format(
username=tweet.author.username,
name=tweet.author.name,
content=tweet.content
).lower(),
} for tweet in Tweet.objects.prefetch_related('author').all()
]
)
2 changes: 2 additions & 0 deletions ede/urls.py
Expand Up @@ -4,4 +4,6 @@

urlpatterns = [
path('', views.index, name="index"),
path('search-tweets', view=views.search_tweets, name='search_tweets'),
path('search-tweets.json', view=views.search_tweets_json, name='search_tweets_json'),
]
18 changes: 18 additions & 0 deletions ede/views.py
@@ -1,5 +1,23 @@
from django.http import JsonResponse
from django.shortcuts import render

from ede.services import get_tweets_search_data


def index(request):
return render(request, "ede/index.html", {})


def search_tweets(request):
"""
Search tweets
search is handled via elm, we provide a json with all the necessary data via search_tweets_json below
"""
return render(request, 'ede/search.html')


def search_tweets_json(request):
""" All the tweets """
return JsonResponse(
data=get_tweets_search_data()
)
14 changes: 14 additions & 0 deletions templates/ede/search.html
@@ -0,0 +1,14 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Elm Django Example</title>
</head>
<body>
<h1>Elm Django Example - Search</h1>
<div id="elm-search"></div>
<script src="{% static 'ede/elm.js' %}"></script>
<script type="text/javascript">Elm.Ede.embed(document.querySelector("#elm-search"));</script>
</body>
</html>

0 comments on commit 8974788

Please sign in to comment.