Skip to content

Commit

Permalink
Following and followers list
Browse files Browse the repository at this point in the history
  • Loading branch information
ineentho committed May 20, 2015
1 parent 76fc633 commit d517e07
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
46 changes: 41 additions & 5 deletions server/api_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,50 @@ def follow():
access_token = params['access_token']
user_to_follow = params['user_to_follow']
user = get_or_create(access_token)
print('found user')
to_follow = User.query.filter(User.id == user_to_follow).one()
user.following.append(to_follow)
db.session.add(user)
db.session.commit()


return jsonify({
'success': True,
'name': user.name
})
'success': True
})


def simple_user_list(followers, page):
base_resp = {
'page': page,
'total-pages': 1,
'total-followers': len(followers)
}

if page != 1:
return jsonify(dict(base_resp, error='Page not found')), 404

follower_list = []
for follower in followers:
follower_list.append({
'id': follower.id,
'name': follower.name
})
return jsonify(dict(base_resp, followers=follower_list))


@app.route('/api/my-followers/')
@app.route('/api/my-followers/<int:page>')
def my_followers(page):
"""
Currently using fake pagination
"""
user = get_or_create(request.headers['access_token'])
return simple_user_list(user.followers, page)


@app.route('/api/following/')
@app.route('/api/following/<int:page>')
def following(page):
"""
Currently using fake pagination
"""
user = get_or_create(request.headers['access_token'])
return simple_user_list(user.following, page)
12 changes: 5 additions & 7 deletions server/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from sqlalchemy.orm import relationship

from server import db


Expand All @@ -14,11 +12,11 @@ class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
google_id = db.Column(db.String(30), unique=True, index=True)
name = db.Column(db.String(80))
following = relationship('User',
secondary=following_association,
primaryjoin=(id == following_association.c.following),
secondaryjoin=(id == following_association.c.followed),
backref='followers')
following = db.relationship('User',
secondary=following_association,
primaryjoin=(id == following_association.c.following),
secondaryjoin=(id == following_association.c.followed),
backref=db.backref('followers'))

def __init__(self, name, google_id):
self.name = name
Expand Down

0 comments on commit d517e07

Please sign in to comment.