From b09bd8546a7e606b6da22f5d4e3e4b4b74994c37 Mon Sep 17 00:00:00 2001 From: Thomas Pollet Date: Fri, 22 Jan 2021 19:41:40 +0100 Subject: [PATCH] flake --- examples/mini_examples/ex6_filtering.py | 32 ++++++++++--------------- 1 file changed, 13 insertions(+), 19 deletions(-) diff --git a/examples/mini_examples/ex6_filtering.py b/examples/mini_examples/ex6_filtering.py index 633b4ce..6f6e1e3 100755 --- a/examples/mini_examples/ex6_filtering.py +++ b/examples/mini_examples/ex6_filtering.py @@ -14,13 +14,10 @@ import json from flask import Flask from flask_sqlalchemy import SQLAlchemy -from safrs import SAFRSBase, SAFRSAPI, jsonapi_attr -import sqlalchemy -import safrs +from safrs import SAFRSBase, SAFRSAPI, ValidationError import re import operator from flask import request -from sqlalchemy.orm import joinedload db = SQLAlchemy() @@ -28,8 +25,8 @@ @classmethod def jsonapi_filter_achim(cls): - - filters = [] + + filters = [] expressions = [] for req_arg, val in request.args.items(): filter_attr = re.search(r"filter\[(\w+)\]\[(\w+)\]", req_arg) @@ -38,17 +35,17 @@ def jsonapi_filter_achim(cls): op = filter_attr.group(2) if op in ["in", "notin"]: val = json.loads(val) - filters.append({ "name" : name , "op" : op , "val" : val}) + filters.append({"name": name, "op": op, "val": val}) continue - + filter_attr = re.search(r"filter\[(\w+)\]", req_arg) if filter_attr: name = filter_attr.group(1) op = "eq" - filters.append({ "name" : name , "op" : op , "val" : val}) - + filters.append({"name": name, "op": op, "val": val}) + query = cls._s_query - + for filt in filters: attr_name = filt.get("name") attr_val = filt.get("val") @@ -72,15 +69,15 @@ def jsonapi_filter_achim(cls): return query.filter(*expressions) - + class BaseModel(SAFRSBase, db.Model): __abstract__ = True jsonapi_filter = jsonapi_filter_achim - + class Person(BaseModel): """ - description: My person description + description: My person description """ __tablename__ = "People" @@ -88,14 +85,12 @@ class Person(BaseModel): name = db.Column(db.String, default="John Doe") - def create_app(config_filename=None, host="localhost"): app = Flask("demo_app") app.secret_key = "not so secret" app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://") db.init_app(app) - with app.app_context(): db.create_all() api = SAFRSAPI(app, host=host, port=5000) @@ -104,11 +99,10 @@ def create_app(config_filename=None, host="localhost"): # Populate the db with users and a books and add the book to the user.books relationship for i in range(20): user = Person(name=f"user{i}", email=f"email{i}@email.com") - - - + return app + # address where the api will be hosted, change this if you're not running the app on localhost! host = sys.argv[1] if sys.argv[1:] else "127.0.0.1" app = create_app(host=host)