def download_release(package, id): release = PackageRelease.query.get(id) if release is None or release.package != package: abort(404) ip = request.headers.get("X-Forwarded-For") or request.remote_addr if ip is not None and not is_user_bot(): key = make_download_key(ip, release.package) if not has_key(key): set_key(key, "true") bonus = 1 PackageRelease.query.filter_by(id=release.id).update( {"downloads": PackageRelease.downloads + 1}) Package.query.filter_by(id=package.id).update({ "downloads": Package.downloads + 1, "score_downloads": Package.score_downloads + bonus, "score": Package.score + bonus }) db.session.commit() return redirect(release.url, code=300)
def list_all(): qb = QueryBuilder(request.args) query = qb.buildPackageQuery() title = qb.title query = query.options( joinedload(Package.license), joinedload(Package.media_license), subqueryload(Package.tags)) ip = request.headers.get("X-Forwarded-For") or request.remote_addr if ip is not None and not is_user_bot(): edited = False for tag in qb.tags: edited = True key = "tag/{}/{}".format(ip, tag.name) if not has_key(key): set_key(key, "true") Tag.query.filter_by(id=tag.id).update({ "views": Tag.views + 1 }) if edited: db.session.commit() if qb.lucky: package = query.first() if package: return redirect(package.getDetailsURL()) topic = qb.buildTopicQuery().first() if qb.search and topic: return redirect("https://forum.minetest.net/viewtopic.php?t=" + str(topic.topic_id)) page = get_int_or_abort(request.args.get("page"), 1) num = min(40, get_int_or_abort(request.args.get("n"), 100)) query = query.paginate(page, num, True) search = request.args.get("q") type_name = request.args.get("type") authors = [] if search: authors = User.query \ .filter(or_(*[func.lower(User.username) == name.lower().strip() for name in search.split(" ")])) \ .all() authors = [(author.username, search.lower().replace(author.username.lower(), "")) for author in authors] topics = None if qb.search and not query.has_next: qb.show_discarded = True topics = qb.buildTopicQuery().all() tags_query = db.session.query(func.count(Tags.c.tag_id), Tag) \ .select_from(Tag).join(Tags).join(Package).group_by(Tag.id).order_by(db.asc(Tag.title)) tags = qb.filterPackageQuery(tags_query).all() selected_tags = set(qb.tags) return render_template("packages/list.html", title=title, packages=query.items, pagination=query, query=search, tags=tags, selected_tags=selected_tags, type=type_name, authors=authors, packages_count=query.total, topics=topics)