How to use the pypistats.models.download.RecentDownloadCount.query function in pypistats

To help you get started, we’ve selected a few pypistats examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github crflynn / pypistats.org / pypistats / views / general.py View on Github external
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
    )
github crflynn / pypistats.org / pypistats / views / general.py View on Github external
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
    )
github crflynn / pypistats.org / pypistats / views / user.py View on Github external
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
github crflynn / pypistats.org / pypistats / views / api.py View on Github external
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)
github crflynn / pypistats.org / pypistats / views / api.py View on Github external
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: