diff --git a/models.py b/models.py index aff87d8..00b114e 100644 --- a/models.py +++ b/models.py @@ -1,6 +1,7 @@ from flask_sqlalchemy import SQLAlchemy from flask_login import UserMixin from datetime import datetime +from hashlib import md5 db = SQLAlchemy() @@ -38,6 +39,10 @@ class User(UserMixin, db.Model): def reward_points(self): return sum(r.points for r in self.rewards) + + def avatar(self): + digest = md5(self.email.lower().encode('utf-8')).hexdigest() + return f'https://www.gravatar.com/avatar/{digest}?d=identicon&s=120' class Friendship(db.Model): id = db.Column(db.Integer, primary_key=True) diff --git a/requirments.txt b/requirments.txt index 00a7717..8c9bf51 100644 --- a/requirments.txt +++ b/requirments.txt @@ -7,3 +7,4 @@ waitress authlib sqlalchemy requests +hashlib diff --git a/routes/notifications.py b/routes/notifications.py index 8ffa720..28227d8 100644 --- a/routes/notifications.py +++ b/routes/notifications.py @@ -1,6 +1,6 @@ -from flask import Blueprint, redirect, url_for, flash, render_template +from flask import Blueprint, jsonify, redirect, request, url_for, flash, render_template from flask_login import login_required, current_user -from models import db, Notification +from models import User, db, Notification from flask_babel import gettext as _ from datetime import datetime, timedelta @@ -30,4 +30,21 @@ def notifications(): db.session.query(Notification).filter(Notification.created_at < expire_time).delete() db.session.commit() notifications = db.session.query(Notification).filter_by(user_id=current_user.id).order_by(Notification.created_at.desc()).all() - return render_template('notifications.html', notifications=notifications) \ No newline at end of file + return render_template('notifications.html', notifications=notifications) + +@noti_bp.route('/notifications_api') +@login_required +def notifications_api(): + expire_time = datetime.now() - timedelta(days=3) + db.session.query(Notification).filter(Notification.created_at < expire_time).delete() + db.session.commit() + notifications = db.session.query(Notification).filter_by(user_id=current_user.id).order_by(Notification.created_at.desc()).all() + return jsonify( + [ + { + 'name': User.query.get(n.user_id).username, + 'data': n.message, + 'timestamp': n.created_at + } for n in notifications + ] + ) diff --git a/routes/user.py b/routes/user.py index 5328501..e064260 100644 --- a/routes/user.py +++ b/routes/user.py @@ -101,4 +101,13 @@ def delete_account(): @login_required def users(): all_users = db.session.query(User).filter(User.id != current_user.id).all() - return render_template('users.html', users=all_users) \ No newline at end of file + return render_template('users.html', users=all_users) + +@user_bp.route('/use_gravatar', methods=['POST']) +@login_required +def gravatar(): + avatar_url = current_user.avatar() + current_user.profile_pic = avatar_url + db.session.commit() + flash(_('Added Gravatar profile picture.'), 'success') + return redirect(url_for('profil.profile')) \ No newline at end of file diff --git a/static/js/adstop.js b/static/js/adtab.js similarity index 100% rename from static/js/adstop.js rename to static/js/adtab.js diff --git a/static/js/events.js b/static/js/events.js index 159969c..9641421 100644 --- a/static/js/events.js +++ b/static/js/events.js @@ -12,5 +12,8 @@ function reloadEvents() { }); } -setInterval(reloadEvents, 10000); -window.onload = reloadEvents; \ No newline at end of file +$(function() { + setInterval(function() { + reloadEvents(); + }, 1000); +}); \ No newline at end of file diff --git a/static/js/feed.js b/static/js/feed.js deleted file mode 100644 index 9a2d8d3..0000000 --- a/static/js/feed.js +++ /dev/null @@ -1,5 +0,0 @@ -function reload_feed() { - setTimeout(function() { - window.location.reload(); - }, 120000); -} \ No newline at end of file diff --git a/templates/admin.html b/templates/admin.html index 4820f9f..d873c25 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -51,13 +51,26 @@
{% for user in users %}