Skip to content

Commit

Permalink
Added AJAX functions for following/unfollowing users, *this is
Browse files Browse the repository at this point in the history
conk3r353*'s code, I was not sure how to work from his fork but work on
this issue, so I copied his code.  I believe I fixed the broken user
popup that was not working in his fork.  There were no changes to update
the user follow/unfollow count, but the console output was changed from
'gotem' to 'user followed' and 'user unfollowed' respectively.

This is towards issue SriNandan33#5
  • Loading branch information
S3rbane committed Oct 11, 2019
1 parent ad6ceb6 commit 80786c0
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 17 deletions.
64 changes: 51 additions & 13 deletions app/core/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime
from flask import render_template, flash, redirect, \
url_for, request, current_app
url_for, request, current_app, json, Response
from flask_login import current_user, login_required
from app import db
from app.core import bp
Expand Down Expand Up @@ -87,26 +87,64 @@ def edit_profile():
def follow(username):
user = User.query.filter_by(username=username).first()
if not user:
flash("User not found")
return redirect(url_for("core.index"))
response = Response(
response=json.dumps({
'error': 'User not found'
}),
status=400,
mimetype='application/json'
)
return response
if user == current_user:
flash("You can't follow yourself")
return redirect(url_for('core.user', username=username))
response = Response(
response=json.dumps({
'error': "You can't follow yourself"
}),
status=400,
mimetype='application/json'
)
return response
current_user.follow(user)
db.session.commit()
flash(f"You are following {username}!")
return redirect(url_for("core.user", username=username))
response = Response(
response=json.dumps({
'error': ""
}),
status=200,
mimetype='application/json'
)

return response

@bp.route('/unfollow/<username>')
def unfollow(username):
user = User.query.filter_by(username=username).first()
if not user:
flash("User not found")
return redirect(url_for("core.index"))
response = Response(
response=json.dumps({
'error': 'User not found'
}),
status=400,
mimetype='application/json'
)
return response
if user == current_user:
flash("You can unfollow yourself")
return redirect(url_for("core.user", username=username))
response = Response(
response=json.dumps({
'error': "You can't unfollow yourself"
}),
status=400,
mimetype='application/json'
)
return response
current_user.unfollow(user)
db.session.commit()
flash(f"You unfollowed {username}")
return redirect(url_for('core.user', username=username))
response = Response(
response=json.dumps({
'error': ""
}),
status=200,
mimetype='application/json'
)

return response
2 changes: 1 addition & 1 deletion app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,4 @@
})
</script>

</html>
</html>
47 changes: 44 additions & 3 deletions app/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ <h1>User: {{ user.username }}</h1>
{% if user == current_user %}
<p><a href="{{ url_for('core.edit_profile')}}">Edit Profile</a></p>
{% elif current_user.is_following(user) %}
<p><a href="{{ url_for('core.unfollow', username=user.username)}}">Unfollow</a></p>
<p id="btn-holder"><button type="button" id="unfollow" class="btn btn-info" onclick="stopFollowing()">Unfollow</button></p>
{% else %}
<p><a href="{{ url_for('core.follow', username=user.username)}}">Follow</a></p>
<p id="btn-holder"><button type="button" id="follow" class="btn btn-info" onclick="startFollowing()">Follow</button></p>
{% endif %}
</td>
</tr>
Expand All @@ -29,4 +29,45 @@ <h1>User: {{ user.username }}</h1>
{% if next_url %}
<a href="{{ next_url}}">Older Posts</a>
{% endif %}
{% endblock %}

<script>
function startFollowing() {
$.ajax({
url: '{{ url_for('core.follow', username=user.username)}}',
statusCode: {
200: function(data) {
let btn = document.getElementById('follow');
btn.onclick = startFollowing;
btn.id = 'follow';
btn.innerText = 'Follow';
console.log('user has been followed');
},
502: function (data) {
let btnHolder = document.getElementById('btn-holder');
btnHolder.appendChild(document.createTextNode(data['error']));
}
}
})
}
function stopFollowing() {
$.ajax({
url: '{{ url_for('core.unfollow', username=user.username)}}',
statusCode: {
200: function(data) {
let btn = document.getElementById('unfollow');
btn.onclick = startFllowing;
btn.id = 'follow';
btn.innerText = 'Follow';
console.log('gotem');
},
502: function (data) {
let btnHolder = document.getElementById('btn-holder');
btnHolder.appendChild(document.createTextNode(data['error']));
}
}
})
}
</script>


{% endblock %}

0 comments on commit 80786c0

Please sign in to comment.