Move profile following code into the Profile model #78

Merged
gergely merged 1 commits from follow-refactor into master 2018-07-12 10:39:28 +02:00
2 changed files with 34 additions and 13 deletions

View File

@ -300,11 +300,12 @@ class CalendarSocialApp(Flask):
@staticmethod @staticmethod
@route('/profile/@<string:username>/follow') @route('/profile/@<string:username>/follow')
@login_required
def follow_user(username): def follow_user(username):
"""View for following a user """View for following a user
""" """
from .models import db, Profile, User, UserFollow, Notification, NotificationAction from .models import db, Profile, User
try: try:
profile = Profile.query.join(User).filter(User.username == username).one() profile = Profile.query.join(User).filter(User.username == username).one()
@ -312,16 +313,7 @@ class CalendarSocialApp(Flask):
abort(404) abort(404)
if profile.user != current_user: if profile.user != current_user:
follow = UserFollow(follower=current_user.profile, profile.follow(follower=current_user.profile)
followed=profile,
accepted_at=datetime.utcnow())
db.session.add(follow)
notification = Notification(profile=profile,
actor=current_user.profile,
item=profile,
action=NotificationAction.follow)
db.session.add(notification)
db.session.commit() db.session.commit()

View File

@ -18,9 +18,9 @@
""" """
import calsocial import calsocial
from calsocial.models import db, Notification, NotificationAction, Profile, User from calsocial.models import db, Notification, NotificationAction, Profile, User, UserFollow
from helpers import database from helpers import client, database, login
def test_profile_follow(database): def test_profile_follow(database):
@ -53,3 +53,32 @@ def test_profile_follow(database):
assert follower in followed.follower_list assert follower in followed.follower_list
assert followed in follower.followed_list assert followed in follower.followed_list
def test_follow_ui(client):
"""Test following on the web interface
"""
with calsocial.app.app_context():
follower_user = User(username='follower',
email='follower@example.com',
password='passworder',
active=True)
followed_user = User(username='followed', email='followed@example.com')
follower = Profile(display_name='Follower', user=follower_user)
followed = Profile(display_name='Followed', user=followed_user)
db.session.add_all([follower, followed])
db.session.commit()
login(client, 'follower', 'passworder')
page = client.get('/profile/@followed/follow')
assert page.status_code == 302
assert page.location == 'http://localhost/profile/%40followed'
with calsocial.app.app_context():
db.session.add_all([follower, followed])
follow = UserFollow.query.one()
assert follow.follower == follower
assert follow.followed == followed
assert follow.accepted_at is not None