diff --git a/app/forms.py b/app/forms.py index eb5ab64..adb4feb 100644 --- a/app/forms.py +++ b/app/forms.py @@ -10,6 +10,10 @@ class LoginForm(FlaskForm): remember_me = BooleanField('Remember Me') submit = SubmitField('Sign In') +class SearchForm(FlaskForm): + username = StringField('Username') + submit = SubmitField('Search') + class RegistrationForm(FlaskForm): username = StringField('Username', validators=[DataRequired()]) diff --git a/app/routes.py b/app/routes.py index b1aaaa2..6dca8cb 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,6 +1,6 @@ from flask_login import login_user, logout_user, current_user, login_required from flask import render_template, flash, redirect, url_for, request -from app.forms import LoginForm, RegistrationForm, EmptyForm +from app.forms import LoginForm, RegistrationForm, EmptyForm, SearchForm from app.models import User, twitterPost from werkzeug.urls import url_parse from flask import Markup @@ -132,16 +132,66 @@ def unfollow(username): return redirect(url_for('user', username=username)) current_user.unfollow(user) db.session.commit() - flash('You are not following {}.'.format(username)) + flash('You are no longer following {}.'.format(username)) return redirect(url_for('user', username=username)) else: return redirect(url_for('index')) +@app.route('/unfollowList/', methods=['POST']) +@login_required +def unfollowList(username): + form = EmptyForm() + if form.validate_on_submit(): + user = User.query.filter_by(username=username).first() + if user is None: + flash('User {} not found.'.format(username)) + return redirect(url_for('index')) + if user == current_user: + flash('You cannot unfollow yourself!') + return redirect(url_for('user', username=username)) + current_user.unfollow(user) + db.session.commit() + flash('You are no longer following {}!'.format(username)) + return redirect(url_for('following')) + else: + return redirect(url_for('index')) + +@app.route('/following') +@login_required +def following(): + form = EmptyForm() + following = current_user.following_list() + followed = current_user.followed.count() + return render_template('following.html', accounts = following, count = followed, form = form) + +@app.route('/search', methods=['GET', 'POST']) +@login_required +def search(): + form = SearchForm() + if form.validate_on_submit(): + user = form.username.data + if isTwitterUser(user): + return redirect(url_for('user', username=user)) + else: + flash("User {} does not exist!".format(user)) + return render_template('search.html', form = form) + else: + return render_template('search.html', form = form) + + + +@app.route('/notfound') +def notfound(): + return render_template('404.html') + @app.route('/user/') @login_required def user(username): user = User.query.filter_by(username=username).first() isTwitter = True + rssFeed = feedparser.parse('https://nitter.net/{}/rss'.format(username)) + if rssFeed.entries == []: + isTwitter = False if user is None and isTwitter: x = ''.join(random.choice(string.ascii_uppercase + string.ascii_lowercase + string.digits) for _ in range(16)) newUser = User(username=username, email="{}@person.is".format(x)) @@ -149,6 +199,9 @@ def user(username): db.session.commit() #flash('User {} not found.'.format(username)) + elif not isTwitter and user is None: + return redirect(url_for('notfound')) + #Gather profile info. rssFeed = feedparser.parse('https://nitter.net/{}/rss'.format(username)) @@ -177,6 +230,7 @@ def user(username): posts.append(newPost) form = EmptyForm() + user = User.query.filter_by(username=username).first() return render_template('user.html', user=user, posts=posts, form=form, profilePic=rssFeed.channel.image.url) def getTimeDiff(t): @@ -194,4 +248,10 @@ def getTimeDiff(t): timeString = "{}m".format(minutes) else: timeString = "{}d".format(diff.days) - return timeString \ No newline at end of file + return timeString + +def isTwitterUser(username): + rssFeed = feedparser.parse('https://nitter.net/{}/rss'.format(username)) + if rssFeed.entries == []: + return False + return True \ No newline at end of file diff --git a/app/static/img/404.png b/app/static/img/404.png new file mode 100644 index 0000000..0f5921a Binary files /dev/null and b/app/static/img/404.png differ diff --git a/app/templates/404.html b/app/templates/404.html new file mode 100644 index 0000000..7103500 --- /dev/null +++ b/app/templates/404.html @@ -0,0 +1,10 @@ +{% extends "base.html" %} + +{% block content %} +
+ +
+
+

This page is not on the map.

+
+{% endblock %} \ No newline at end of file diff --git a/app/templates/_post.html b/app/templates/_post.html index d771856..eea25ac 100644 --- a/app/templates/_post.html +++ b/app/templates/_post.html @@ -3,7 +3,7 @@
- +
diff --git a/app/templates/base.html b/app/templates/base.html index aff58fc..501f4d4 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -19,6 +19,8 @@ {% if current_user.is_anonymous %} Login {% else %} + Search + Following Logout {% endif %}
diff --git a/app/templates/following.html b/app/templates/following.html new file mode 100644 index 0000000..68513b7 --- /dev/null +++ b/app/templates/following.html @@ -0,0 +1,35 @@ +{% extends "base.html" %} + +{% block content %} +
+
+ +
+
+

Accounts you follow ({{ count }}):

+
+
+
+ +
+
+ {% for acc in accounts %} +
+
+

+

+ {{ form.hidden_tag() }} + {{ form.submit(value='Unfollow') }} +
+

+
+ +
+ {{acc.username}} +
+
+ {% endfor %} +
+
+ +{% endblock %} \ No newline at end of file diff --git a/app/templates/search.html b/app/templates/search.html new file mode 100644 index 0000000..f185fa1 --- /dev/null +++ b/app/templates/search.html @@ -0,0 +1,18 @@ +{% extends "base.html" %} + +{% block content %} +
+
+ {{ form.hidden_tag() }} +

+ {{ form.username.label }}
+ {{ form.username(size=32) }}
+ {% for error in form.username.errors %} + [{{ error }}] + {% endfor %} +

+

{{ form.submit() }}

+
+
+ +{% endblock %} \ No newline at end of file