Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Starting more on front pag

  • Loading branch information...
commit a67b1263edb486b1474c004a1165683f7f67d0d4 1 parent b81954d
@ericflo ericflo authored
View
17 lib/database.py
@@ -65,15 +65,26 @@ def _get_friend_or_follower_ids(cf, user_id, count):
def _get_userline_or_timeline(cf, user_id, start, limit):
start = _long(start) if start else ''
try:
- timeline = cf.get(str(user_id), column_start=start, column_count=limit)
+ timeline = cf.get(str(user_id), column_start=start, column_count=limit,
+ column_reversed=True)
except NotFoundException:
return []
- tweets = TWEET.multiget(map(str, timeline.values()))
+ tweets = TWEET.multiget(timeline.values())
+ tweets = dict(((json.loads(t['id']), t) for t in tweets.values()))
+ print tweets
decoded = []
- for tweet in tweets.values():
+ for tweet_id in timeline.values():
+ print tweet_id
+ tweet = tweets.get(tweet_id)
+ if not tweet:
+ continue
decoded.append(
dict(((k, json.loads(v)) for k, v in tweet.iteritems()))
)
+ users = get_users_for_user_ids([u['user_id'] for u in decoded])
+ users = dict(((u['id'], u) for u in users))
+ for tweet in decoded:
+ tweet['user'] = users.get(tweet['user_id'])
return decoded
View
22 templates/tweets/timeline.html
@@ -0,0 +1,22 @@
+{% extends "base.html" %}
+
+{% block content %}
+ {% if request.user.is_authenticated %}
+ <form method="POST">
+ {{ form.body }}
+ <input type="submit" value="Post Tweet" />
+ </form>
+ {% else %}
+ <a href="{% url login %}">Log in to post a tweet</a>
+ {% endif %}
+ <ul>
+ {% for tweet in tweets %}
+ <li>
+ <span class="username">{{ tweet.user.username }}</span>
+ <span class="body">{{ tweet.body }}</span>
+ </li>
+ {% empty %}
+ <li>There are no tweets yet. Make sure to post one!</li>
+ {% endfor %}
+ </ul>
+{% endblock %}
View
4 tweets/forms.py
@@ -0,0 +1,4 @@
+from django import forms
+
+class TweetForm(forms.Form):
+ body = forms.CharField(max_length=140)
View
5 tweets/urls.py
@@ -0,0 +1,5 @@
+from django.conf.urls.defaults import patterns, url
+
+urlpatterns = patterns('tweets.views',
+ url('^/?$', 'timeline', name='timeline'),
+)
View
28 tweets/views.py
@@ -0,0 +1,28 @@
+import uuid
+
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.http import HttpResponseRedirect
+from django.core.urlresolvers import reverse
+
+from tweets.forms import TweetForm
+
+from lib.database import save_tweet, get_timeline
+
+def timeline(request):
+ form = TweetForm(request.POST or None)
+ if request.user['is_authenticated'] and form.is_valid():
+ tweet_id = str(uuid.uuid1())
+ save_tweet(tweet_id, request.user['id'], {
+ 'id': tweet_id,
+ 'user_id': request.user['id'],
+ 'body': form.cleaned_data['body'],
+ })
+ return HttpResponseRedirect(reverse('timeline'))
+ tweets = get_timeline(request.user['id'])
+ context = {
+ 'form': form,
+ 'tweets': tweets,
+ }
+ return render_to_response('tweets/timeline.html', context,
+ context_instance=RequestContext(request))
View
1  urls.py
@@ -3,6 +3,7 @@
urlpatterns = patterns('',
url('^auth/', include('users.urls')),
+ url('', include('tweets.urls')),
)
if settings.DEBUG:
View
1  users/middleware.py
@@ -5,6 +5,7 @@ def get_user(request):
try:
user = get_user_by_id(request.session['user_id'])
user['is_authenticated'] = True
+ return user
except DatabaseError:
pass
return {
Please sign in to comment.
Something went wrong with that request. Please try again.