Permalink
Browse files

Add a public timeline.

  • Loading branch information...
1 parent 54e283d commit e3c873323e16d80b50310e7727659c4d313115ca @ericflo ericflo committed Feb 26, 2010
Showing with 24 additions and 4 deletions.
  1. +7 −4 cass.py
  2. +1 −0 templates/base.html
  3. +1 −0 tweets/urls.py
  4. +15 −0 tweets/views.py
View
11 cass.py
@@ -17,7 +17,7 @@
'get_follower_ids', 'get_users_for_user_ids', 'get_friends',
'get_followers', 'get_timeline', 'get_userline', 'get_tweet', 'save_user',
'save_tweet', 'add_friends', 'remove_friends', 'DatabaseError',
- 'NotFound', 'InvalidDictionary']
+ 'NotFound', 'InvalidDictionary', 'PUBLIC_USERLINE_KEY']
CLIENT = pycassa.connect_thread_local(framed_transport=True)
@@ -36,6 +36,8 @@
USERLINE = pycassa.ColumnFamily(CLIENT, 'Twissandra', 'Userline',
dict_class=OrderedDict)
+PUBLIC_USERLINE_KEY = '!PUBLIC!'
+
class DatabaseError(Exception):
pass
@@ -62,7 +64,7 @@ def _get_friend_or_follower_ids(cf, user_id, count):
return []
return friends.keys()
-def _get_userline_or_timeline(cf, user_id, start, limit):
+def _get_line(cf, user_id, start, limit):
start = _long(start) if start else ''
try:
timeline = cf.get(str(user_id), column_start=start, column_count=limit,
@@ -131,10 +133,10 @@ def get_followers(user_id, count=5000):
return get_users_for_user_ids(follower_ids)
def get_timeline(user_id, start=None, limit=40):
- return _get_userline_or_timeline(TIMELINE, user_id, start, limit)
+ return _get_line(TIMELINE, user_id, start, limit)
def get_userline(user_id, start=None, limit=40):
- return _get_userline_or_timeline(USERLINE, user_id, start, limit)
+ return _get_line(USERLINE, user_id, start, limit)
def get_tweet(tweet_id):
try:
@@ -163,6 +165,7 @@ def save_tweet(tweet_id, user_id, tweet):
encoded = dict(((k, json.dumps(v)) for k, v in tweet.iteritems()))
TWEET.insert(str(tweet_id), encoded)
USERLINE.insert(str(user_id), {ts: str(tweet_id)})
+ USERLINE.insert(PUBLIC_USERLINE_KEY, {ts: str(tweet_id)})
follower_ids = [user_id] + get_follower_ids(user_id)
for follower_id in follower_ids:
TIMELINE.insert(str(follower_id), {ts: str(tweet_id)})
View
@@ -8,6 +8,7 @@
<body>
<ul id="nav">
<li><a href="{% url timeline %}">Home</a></li>
+ <li><a href="{% url publicline %}">Public</a></li>
<li><a href="{% url find_friends %}">Find Friends</a></li>
{% if request.user.is_authenticated %}
<li><a href="{% url logout %}">Sign out of {{ request.user.username }}</a></li>
View
@@ -2,5 +2,6 @@
urlpatterns = patterns('tweets.views',
url(r'^/?$', 'timeline', name='timeline'),
+ url(r'^public/$', 'publicline', name='publicline'),
url(r'^(?P<username>\w+)/$', 'userline', name='userline'),
)
View
@@ -36,6 +36,21 @@ def timeline(request):
return render_to_response('tweets/timeline.html', context,
context_instance=RequestContext(request))
+def publicline(request):
+ start = request.GET.get('start')
+ tweets = cass.get_userline(cass.PUBLIC_USERLINE_KEY, start=start,
+ limit=NUM_PER_PAGE + 1)
+ next = None
+ if tweets and len(tweets) == NUM_PER_PAGE + 1:
+ next = tweets[-1]['_ts']
+ tweets = tweets[:NUM_PER_PAGE]
+ context = {
+ 'tweets': tweets,
+ 'next': next,
+ }
+ return render_to_response('tweets/publicline.html', context,
+ context_instance=RequestContext(request))
+
def userline(request, username=None):
try:
user = cass.get_user_by_username(username)

0 comments on commit e3c8733

Please sign in to comment.