Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def index():
"""Render the home page."""
form = MyForm()
if form.validate_on_submit():
package = form.name.data
return redirect(f"/search/{package.lower()}")
package_count = \
RecentDownloadCount.query.filter_by(category="month").count()
return render_template(
"index.html",
form=form,
user=g.user,
package_count=package_count
)
def search(package):
"""Render the home page."""
package = package.replace(".", "-")
form = MyForm()
if form.validate_on_submit():
package = form.name.data
return redirect(f"/search/{package}")
results = RecentDownloadCount.query.filter(
RecentDownloadCount.package.like(f"{package}%"),
RecentDownloadCount.category == "month").\
order_by(RecentDownloadCount.package).\
limit(20).all()
packages = [r.package for r in results]
if len(packages) == 1:
package = packages[0]
return redirect(f"/packages/{package}")
return render_template(
"search.html", search=True, form=form, packages=packages, user=g.user
)
def user_package(package):
"""Handle adding and deleting packages to user's list."""
if g.user:
# Ensure package is valid.
downloads = RecentDownloadCount.query.filter_by(package=package).all()
# Handle add/remove to favorites
if g.user.favorites is None:
# Ensure package is valid before adding
if len(downloads) == 0:
return abort(400)
g.user.favorites = [package]
g.user.update()
return redirect(url_for("user.user"))
elif package in g.user.favorites:
favorites = g.user.favorites
favorites.remove(package)
# Workaround for sqlalchemy mutable ARRAY types
g.user.favorites = None
g.user.save()
g.user.favorites = favorites
def api_downloads_recent(package):
"""Get the recent downloads of a package."""
if package != "__all__":
package = package.replace(".", "-").replace("_", "-")
category = request.args.get("period")
if category is None:
downloads = RecentDownloadCount.query.\
filter_by(package=package).all()
elif category in RECENT_CATEGORIES:
downloads = RecentDownloadCount.query.\
filter_by(package=package, category=category).all()
else:
abort(404)
response = {"package": package, "type": "recent_downloads"}
if len(downloads) > 0:
if category is None:
response["data"] = {"last_" + rc: 0 for rc in RECENT_CATEGORIES}
else:
response["data"] = {"last_" + category: 0}
for r in downloads:
response["data"]["last_" + r.category] = r.downloads
else:
abort(404)
return jsonify(response)
def api_downloads_recent(package):
"""Get the recent downloads of a package."""
if package != "__all__":
package = package.replace(".", "-").replace("_", "-")
category = request.args.get("period")
if category is None:
downloads = RecentDownloadCount.query.\
filter_by(package=package).all()
elif category in RECENT_CATEGORIES:
downloads = RecentDownloadCount.query.\
filter_by(package=package, category=category).all()
else:
abort(404)
response = {"package": package, "type": "recent_downloads"}
if len(downloads) > 0:
if category is None:
response["data"] = {"last_" + rc: 0 for rc in RECENT_CATEGORIES}
else:
response["data"] = {"last_" + category: 0}
for r in downloads:
response["data"]["last_" + r.category] = r.downloads
else: