2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-05-17 16:18:20 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2021-01-30 17:59:42 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2018-05-17 16:18:20 +02:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2021-01-30 17:59:42 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2018-05-17 16:18:20 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2018-05-17 16:18:20 +02:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-07-25 23:44:13 +02:00
|
|
|
import math
|
2018-05-17 16:18:20 +02:00
|
|
|
|
2018-03-18 19:05:53 +01:00
|
|
|
from flask import *
|
2020-12-04 23:05:10 +01:00
|
|
|
from flask_login import current_user, login_required
|
2020-12-04 03:23:04 +01:00
|
|
|
from sqlalchemy import func
|
|
|
|
|
|
|
|
from app.models import *
|
|
|
|
from app.tasks.forumtasks import checkForumAccount
|
|
|
|
from . import bp
|
|
|
|
|
2018-03-24 20:37:33 +01:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/users/", methods=["GET"])
|
|
|
|
def list_all():
|
2020-07-12 21:59:06 +02:00
|
|
|
users = db.session.query(User, func.count(Package.id)) \
|
|
|
|
.select_from(User).outerjoin(Package) \
|
|
|
|
.order_by(db.desc(User.rank), db.asc(User.display_name)) \
|
|
|
|
.group_by(User.id).all()
|
|
|
|
|
2018-03-24 20:24:34 +01:00
|
|
|
return render_template("users/list.html", users=users)
|
2018-03-20 20:07:20 +01:00
|
|
|
|
2018-03-24 20:24:34 +01:00
|
|
|
|
2020-12-07 21:11:40 +01:00
|
|
|
@bp.route("/user/forum/<username>/")
|
|
|
|
def by_forums_username(username):
|
|
|
|
user = User.query.filter_by(forums_username=username).first()
|
|
|
|
if user:
|
|
|
|
return redirect(url_for("users.profile", username=user.username))
|
|
|
|
|
|
|
|
return render_template("users/forums_no_such_user.html", username=username)
|
|
|
|
|
|
|
|
|
2020-12-05 02:20:00 +01:00
|
|
|
@bp.route("/users/<username>/")
|
2019-11-16 00:51:42 +01:00
|
|
|
def profile(username):
|
2018-03-20 20:07:20 +01:00
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if not user:
|
|
|
|
abort(404)
|
2018-03-18 19:05:53 +01:00
|
|
|
|
2018-06-02 19:22:57 +02:00
|
|
|
if not current_user.is_authenticated or (user != current_user and not current_user.canAccessTodoList()):
|
2021-07-25 19:42:11 +02:00
|
|
|
packages = user.packages.filter_by(state=PackageState.APPROVED)
|
|
|
|
maintained_packages = user.maintained_packages.filter_by(state=PackageState.APPROVED)
|
|
|
|
else:
|
|
|
|
packages = user.packages.filter(Package.state != PackageState.DELETED)
|
|
|
|
maintained_packages = user.maintained_packages.filter(Package.state != PackageState.DELETED)
|
|
|
|
|
|
|
|
packages = packages.order_by(db.asc(Package.title)).all()
|
|
|
|
maintained_packages = maintained_packages \
|
|
|
|
.filter(Package.author != user) \
|
|
|
|
.order_by(db.asc(Package.title)).all()
|
2018-06-02 19:22:57 +02:00
|
|
|
|
2021-07-25 17:09:21 +02:00
|
|
|
users_by_reviews = db.session.query(User.username, func.count(PackageReview.id).label("count")) \
|
|
|
|
.select_from(User).join(PackageReview) \
|
|
|
|
.group_by(User.username).order_by(text("count DESC")).all()
|
2021-07-25 23:44:13 +02:00
|
|
|
try:
|
|
|
|
review_boundary = users_by_reviews[math.floor(len(users_by_reviews) * 0.25)][1] + 1
|
|
|
|
except IndexError:
|
|
|
|
review_boundary = None
|
2021-07-25 17:09:21 +02:00
|
|
|
users_by_reviews = [ username for username, _ in users_by_reviews ]
|
2021-07-25 17:35:08 +02:00
|
|
|
|
2021-07-25 17:09:21 +02:00
|
|
|
review_idx = None
|
|
|
|
review_percent = None
|
|
|
|
try:
|
|
|
|
review_idx = users_by_reviews.index(user.username)
|
|
|
|
review_percent = round(100 * review_idx / len(users_by_reviews), 1)
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
2021-07-25 17:35:08 +02:00
|
|
|
total_downloads = db.session.query(func.sum(Package.downloads)) \
|
|
|
|
.select_from(User) \
|
2021-07-25 19:42:11 +02:00
|
|
|
.join(User.packages) \
|
2021-07-25 18:01:21 +02:00
|
|
|
.filter(User.id == user.id, Package.state == PackageState.APPROVED).scalar() or 0
|
2021-07-25 17:35:08 +02:00
|
|
|
|
2021-07-25 19:17:59 +02:00
|
|
|
all_package_ranks = db.session.query(
|
2021-07-25 23:44:13 +02:00
|
|
|
Package.type,
|
2021-07-25 19:17:59 +02:00
|
|
|
Package.author_id,
|
2021-07-25 23:44:13 +02:00
|
|
|
func.rank().over(order_by=db.desc(Package.score), partition_by=Package.type) \
|
2021-07-25 19:30:10 +02:00
|
|
|
.label('rank')).order_by(db.asc(text("rank"))) \
|
|
|
|
.filter_by(state=PackageState.APPROVED).subquery()
|
2021-07-25 23:44:13 +02:00
|
|
|
|
2021-07-25 19:17:59 +02:00
|
|
|
user_package_ranks = db.session.query(all_package_ranks) \
|
|
|
|
.filter_by(author_id=user.id).first()
|
2021-07-25 23:44:13 +02:00
|
|
|
min_package_rank = None
|
|
|
|
min_package_type = None
|
|
|
|
if user_package_ranks:
|
|
|
|
min_package_rank = user_package_ranks[2]
|
|
|
|
min_package_type = PackageType.coerce(user_package_ranks[0]).value
|
2021-07-25 19:17:59 +02:00
|
|
|
|
2018-03-18 19:05:53 +01:00
|
|
|
# Process GET or invalid POST
|
2021-07-25 19:42:11 +02:00
|
|
|
return render_template("users/profile.html", user=user,
|
|
|
|
packages=packages, maintained_packages=maintained_packages,
|
2021-07-25 23:44:13 +02:00
|
|
|
total_downloads=total_downloads,
|
|
|
|
review_idx=review_idx, review_percent=review_percent, review_boundary=review_boundary,
|
|
|
|
min_package_rank=min_package_rank, min_package_type=min_package_type)
|
2020-12-05 02:20:00 +01:00
|
|
|
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
@bp.route("/users/<username>/check/", methods=["POST"])
|
2018-12-25 20:28:32 +01:00
|
|
|
@login_required
|
|
|
|
def user_check(username):
|
|
|
|
user = User.query.filter_by(username=username).first()
|
|
|
|
if user is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
if current_user != user and not current_user.rank.atLeast(UserRank.MODERATOR):
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
if user.forums_username is None:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
task = checkForumAccount.delay(user.forums_username)
|
2019-11-16 00:51:42 +01:00
|
|
|
next_url = url_for("users.profile", username=username)
|
2018-12-25 20:28:32 +01:00
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
return redirect(url_for("tasks.check", id=task.id, r=next_url))
|