Skip to content
This repository has been archived by the owner on Aug 4, 2020. It is now read-only.

Commit

Permalink
Starting more on front pag
Browse files Browse the repository at this point in the history
  • Loading branch information
ericflo committed Feb 24, 2010
1 parent b81954d commit a67b126
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
22 changes: 22 additions & 0 deletions templates/tweets/timeline.html
Original file line number Diff line number Diff line change
@@ -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 %}
4 changes: 4 additions & 0 deletions tweets/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from django import forms

class TweetForm(forms.Form):
body = forms.CharField(max_length=140)
5 changes: 5 additions & 0 deletions tweets/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('tweets.views',
url('^/?$', 'timeline', name='timeline'),
)
28 changes: 28 additions & 0 deletions tweets/views.py
Original file line number Diff line number Diff line change
@@ -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))
1 change: 1 addition & 0 deletions urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

urlpatterns = patterns('',
url('^auth/', include('users.urls')),
url('', include('tweets.urls')),
)

if settings.DEBUG:
Expand Down
1 change: 1 addition & 0 deletions users/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a67b126

Please sign in to comment.