I'm hoping to get feedback on my current code from someone more experienced in Django, and in the concepts I'm trying to implement. Let me preface by saying that I am a novice to Django. This is my first time using the framework.
Here's the gist of my goals in my project:
- Create a web application that displays reddit posts and tweets in a 'newsfeed' like format.
What I've done:
Create a services.py script that fetches the data from reddit and twitter from their API's using PRAW and Tweepy libraries respectively.
Grab each post from either reddit or twitter, and store in a collective list, ordered by earliest creation time first.
Pass this list through the view as context to the page template (joke.html, sorry for the unconventional naming).
In the template, loop through the context, check if the item is a reddit or twitter post. Then pass the list item into the respective template tag. (I have two template tags which essentially spit out proper HTML to embed either a reddit or twitter post onto the page).
Basically, is it possible to implement this newsfeed-esque design using Django, or does that reach the limit of the framework's functionality? Right now, the posts are being embedded onto the page, but there isn't a cache of previous posts, nor does the 'feed' update based on scrolling.
The template is in joke.html
, the function in views.py
, API fetching and post sorting (on creation date) in services.py
.
joke.html
{% block content %}{% load reddit_tag_test %} {% load tweet_tags %} {% autoescape off %}
{% for item in connects %}
<div class="mdl-card__media" id="timeline">
{% if item|first in 'm' %}
{% reddit_tag_test item %}
{% else %}
{% tweet_tags item %}
{% endif %}
{% endfor %} {% endautoescape %}
</div>
{% endblock %}
<script async src="//embed.redditmedia.com/widgets/platform.js" charset="UTF-8"></script>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
views.py
def joke(request):
connects = services.connections()
return render(request, 'joke.html', dict(connects=connects))
services.py
sources = {}
def tweet_connect(key, secret, access_key, access_secret):
auth = tweepy.OAuthHandler(key, secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
t_post_dict = {}
my_timeline = api.home_timeline(count=5)
current_status = []
default = 'https://twitter.com/twitter/statuses/'
for status in my_timeline:
if status.id not in db:
db.append(status.id)
url = default + str(status.id)
t_post_dict[url] = str(status.created_at)
current_status.append(url)
return t_post_dict
def reddit_connect(client_id, secret, username, password, user_agent):
sub = 'nba'
r_post_dict = {}
reddit = praw.Reddit(
client_id=client_id,
client_secret=secret,
username=username,
password=password,
user_agent=user_agent)
id_list = []
for r in reddit.subreddit(sub).new(limit=9):
time = r.created
dt_str = str(datetime.datetime.fromtimestamp(time))
r_post_dict[r.id] = dt_str
id_list.append(r.id)
return r_post_dict
sources['twitter'] = tweet_result
sources['reddit'] = red_result
def connections():
time_assoc_dict = {}
times = []
src = []
for keys, value in sources.items():
for k, v in value.items():
time_posted = v
source = k
time_assoc_dict[source] = time_posted
times.append(time_posted)
for post in times:
newest_post = min(times) # find the newest post
times.remove(newest_post) # remove it from the list
for k, v in time_assoc_dict.items():
if newest_post == v:
src.append(k)
src = src[::-1]
return src
P.S. I know Stack Exchange mentions that I shouldn't ask how to implement new features. I am looking for genuine feedback on my code, and improvements I can make. I am obviously open to suggestions on what to do next, because I would like to learn how to cache previous posts (lets say I kept scrolling down the page), but it is not expected. If I only receive feedback, I will be more than satisfied.