Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bunch of tweaks

  • Loading branch information...
commit 23295a52242be9e761c9d7bfb1bef87bc720de8b 1 parent fa0af7b
@ericflo ericflo authored
View
13 templates/tweets/userline.html
@@ -15,4 +15,17 @@
{% if next %}
<a href="?start={{ next }}">More</a>
{% endif %}
+
+ {% if request.user.is_authenticated %}
+ {% if not user.friend %}
+ <form method="POST" action="{% url modify_friend %}?next={{ request.path }}">
+ <input type="hidden" name="{% if user.friend %}remove{% else %}add{% endif %}-friend" value="{{ user.id }}" />
+ <input type="submit" value="{% if user.friend %}Remove{% else %}Add{% endif %} Friend" />
+ </form>
+ {% endif %}
+ {% else %}
+ <a href="{% url login %}?next={{ request.path }}&q={{ q }}">
+ Login to add {{ user.username }} as a friend
+ </a>
+ {% endif %}
{% endblock %}
View
6 templates/users/add_friends.html
@@ -3,7 +3,7 @@
{% block content %}
<h2>Add Friends</h2>
<form method="GET">
- <input type="text" name="q" />
+ <input type="text" name="q" value="{% if q %}{{ q }}{% endif %}" />
<input type="submit" value="Search" />
</form>
<div>
@@ -12,7 +12,7 @@
<p>Hooray, your friend {{ result.username }} is on the site!</p>
{% if request.user.is_authenticated %}
{% if not result.friend %}
- <form method="POST" action="{{ request.path }}">
+ <form method="POST" action="{% url modify_friend %}?next={{ request.path }}">
<input type="hidden" name="{% if result.friend %}remove{% else %}add{% endif %}-friend" value="{{ result.id }}" />
<input type="submit" value="{% if result.friend %}Remove{% else %}Add{% endif %} Friend" />
</form>
@@ -36,7 +36,7 @@
<p>{{ user.username }}</p>
{% if request.user.is_authenticated %}
{% if not user.friend %}
- <form method="POST" action="{{ request.path }}">
+ <form method="POST" action="{% url modify_friend %}?next={{ request.path }}">
<input type="hidden" name="{% if user.friend %}remove{% else %}add{% endif %}-friend" value="{{ user.id }}" />
<input type="submit" value="{% if user.friend %}Remove{% else %}Add{% endif %} Friend" />
</form>
View
6 templates/users/modify_friend.html
@@ -0,0 +1,6 @@
+{% extends "base.html" %}
+
+{% block content %}
+ <h2>Friend successfully {% if added %}Added{% else %}Removed{% endif %}</h2>
+ <p>The user has been {% if added %}added to{% else %}removed from{% endif %} your friends list.</p>
+{% endblock %}
View
14 tweets/views.py
@@ -7,7 +7,7 @@
from tweets.forms import TweetForm
-from lib.database import save_tweet, get_timeline, get_userline
+from lib.database import save_tweet, get_timeline, get_userline, get_friend_ids
from lib.database import get_user_by_username, DatabaseError
NUM_PER_PAGE = 40
@@ -42,16 +42,28 @@ def userline(request, username=None):
user = get_user_by_username(username)
except DatabaseError:
raise Http404
+
+ # Query for the friend ids
+ friend_ids = []
+ if request.user['is_authenticated']:
+ friend_ids = get_friend_ids(request.user['id']) + [request.user['id']]
+
+ # Add a property on the user to indicate whether the currently logged-in
+ # user is friends with the user
+ user['friend'] = user['id'] in friend_ids
+
start = request.GET.get('start')
tweets = get_userline(user['id'], 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 = {
'user': user,
'tweets': tweets,
'next': next,
+ 'friend_ids': friend_ids,
}
return render_to_response('tweets/userline.html', context,
context_instance=RequestContext(request))
View
1  users/urls.py
@@ -4,4 +4,5 @@
url('^login/$', 'login', name='login'),
url('^logout/$', 'logout', name='logout'),
url(r'^find-friends/$', 'find_friends', name='find_friends'),
+ url(r'^modify-friend/$', 'modify_friend', name='modify_friend'),
)
View
29 users/views.py
@@ -44,7 +44,7 @@ def logout(request):
def find_friends(request):
friend_ids = []
if request.user['is_authenticated']:
- friend_ids = get_friend_ids(request.user['id'])
+ friend_ids = get_friend_ids(request.user['id']) + [request.user['id']]
q = request.GET.get('q')
result = None
searched = False
@@ -55,13 +55,6 @@ def find_friends(request):
result['friend'] = result['id'] in friend_ids
except DatabaseError:
pass
- if request.user['is_authenticated']:
- if 'add-friend' in request.POST:
- add_friends(request.user['id'], [request.POST['add-friend']])
- return HttpResponseRedirect(request.path)
- elif 'remove-friend' in request.POST:
- remove_friends(request.user['id'], [request.POST['remove-friend']])
- return HttpResponseRedirect(request.path)
context = {
'q': q,
'result': result,
@@ -69,4 +62,24 @@ def find_friends(request):
'friend_ids': friend_ids,
}
return render_to_response('users/add_friends.html', context,
+ context_instance=RequestContext(request))
+
+def modify_friend(request):
+ next = request.REQUEST.get('next')
+ added = False
+ removed = False
+ if request.user['is_authenticated']:
+ if 'add-friend' in request.POST:
+ add_friends(request.user['id'], [request.POST['add-friend']])
+ added = True
+ if 'remove-friend' in request.POST:
+ remove_friends(request.user['id'], [request.POST['remove-friend']])
+ removed = True
+ if next:
+ return HttpResponseRedirect(next)
+ context = {
+ 'added': added,
+ 'removed': removed,
+ }
+ return render_to_response('users/modify_friend.html', context,
context_instance=RequestContext(request))
Please sign in to comment.
Something went wrong with that request. Please try again.