diff options
author | rubenwardy <rw@rubenwardy.com> | 2018-07-28 18:12:22 +0100 |
---|---|---|
committer | rubenwardy <rw@rubenwardy.com> | 2018-07-28 18:12:22 +0100 |
commit | a3e82ad42f7707af43db477596fb6fe5fc5a6b53 (patch) | |
tree | b87e2041ea76d81ff567943bffeadac4f9dad192 | |
parent | 404200b8f0809a4aeb2b02b91db3d3682a59f692 (diff) | |
download | cheatdb-a3e82ad42f7707af43db477596fb6fe5fc5a6b53.tar.xz |
Add support for multiple types in packages list
-rw-r--r-- | app/models.py | 7 | ||||
-rw-r--r-- | app/views/packages/__init__.py | 20 |
2 files changed, 18 insertions, 9 deletions
diff --git a/app/models.py b/app/models.py index f002abc..8f71146 100644 --- a/app/models.py +++ b/app/models.py @@ -212,6 +212,13 @@ class PackageType(enum.Enum): return self.name @classmethod + def get(cls, name): + try: + return PackageType[name.upper()] + except KeyError: + return None + + @classmethod def choices(cls): return [(choice, choice.value) for choice in cls] diff --git a/app/views/packages/__init__.py b/app/views/packages/__init__.py index 8e0410f..f227b19 100644 --- a/app/views/packages/__init__.py +++ b/app/views/packages/__init__.py @@ -29,20 +29,22 @@ from flask_wtf import FlaskForm from wtforms import * from wtforms.validators import * from wtforms.ext.sqlalchemy.fields import QuerySelectField, QuerySelectMultipleField -from sqlalchemy import or_ +from sqlalchemy import or_, any_ def build_packages_query(): - type_name = request.args.get("type") - type = None - if type_name is not None: - type = PackageType[type_name.upper()] - title = "Packages" + query = Package.query.filter_by(soft_deleted=False, approved=True) - if type is not None: - title = type.value + "s" - query = query.filter_by(type=type) + # Filter by requested type(s) + types = request.args.getlist("type") + types = [PackageType.get(tname) for tname in types] + types = [type for type in types if type is not None] + if len(types) > 0: + title = ", ".join([type.value + "s" for type in types]) + + query = query.filter(Package.type.in_(types)) + search = request.args.get("q") if search is not None and search.strip() != "": |