This repository has been archived by the owner on Sep 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Added AJAX support. #8
Open
conk3r353
wants to merge
1
commit into
SriNandan33:master
Choose a base branch
from
conk3r353:feature/ajax
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from flask import json, Response | ||
from datetime import datetime | ||
from flask import render_template, flash, redirect, \ | ||
url_for, request, current_app | ||
|
@@ -87,26 +88,70 @@ 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=502, | ||
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=502, | ||
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=502, | ||
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=502, | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
response=json.dumps({ | ||
'error': "" | ||
}), | ||
status=200, | ||
mimetype='application/json' | ||
) | ||
|
||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{% extends "base.html" %} | ||
|
||
{% block content %} | ||
<table> | ||
<table xmlns="http://www.w3.org/1999/html"> | ||
<tr valign="top"> | ||
<td><img src="{{ user.avatar(128) }}"></td> | ||
<td> | ||
|
@@ -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> | ||
|
@@ -29,4 +29,44 @@ <h1>User: {{ user.username }}</h1> | |
{% if next_url %} | ||
<a href="{{ next_url}}">Older Posts</a> | ||
{% endif %} | ||
|
||
<script> | ||
function startFollowing() { | ||
$.ajax({ | ||
url: '{{ url_for('core.follow', username=user.username)}}', | ||
statusCode: { | ||
200: function(data) { | ||
let btn = document.getElementById('follow'); | ||
btn.onclick = stopFollowing; | ||
btn.id = 'unfollow'; | ||
btn.innerText = 'Unfollow'; | ||
console.log('gotem'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please increase/decrease the follower count if the request is successful, modify the view functions accordingly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you also show flash messages when user follows/unfollows the other user. |
||
}, | ||
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 = startFollowing; | ||
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 %} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use 400 status code here instead of 502?