Skip to content
Permalink
Browse files

Starting more on front pag

  • Loading branch information
ericflo committed Feb 24, 2010
1 parent b81954d commit a67b1263edb486b1474c004a1165683f7f67d0d4
Showing with 75 additions and 3 deletions.
  1. +14 −3 lib/database.py
  2. +22 −0 templates/tweets/timeline.html
  3. +4 −0 tweets/forms.py
  4. +5 −0 tweets/urls.py
  5. +28 −0 tweets/views.py
  6. +1 −0 urls.py
  7. +1 −0 users/middleware.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


@@ -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 %}
@@ -0,0 +1,4 @@
from django import forms

class TweetForm(forms.Form):
body = forms.CharField(max_length=140)
@@ -0,0 +1,5 @@
from django.conf.urls.defaults import patterns, url

urlpatterns = patterns('tweets.views',
url('^/?$', 'timeline', name='timeline'),
)
@@ -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))
@@ -3,6 +3,7 @@

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

if settings.DEBUG:
@@ -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 {

0 comments on commit a67b126

Please sign in to comment.
You can’t perform that action at this time.