Skip to content

Commit

Permalink
api: use core FILTERS definition
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaume committed Aug 7, 2021
1 parent 2cbb6ba commit a45bed2
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
15 changes: 15 additions & 0 deletions mpcontribs-api/mpcontribs/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import requests
import rq_dashboard
import flask_monitoringdashboard as dashboard
import flask_mongorest.operators as ops

from importlib import import_module
from websocket import create_connection
Expand Down Expand Up @@ -46,6 +47,20 @@
logger = logging.getLogger("app")
sns_client = boto3.client("sns")

# NOTE not including Size below (special for arrays)
FILTERS = {
"LONG_STRINGS": [
ops.Contains, ops.IContains,
ops.Startswith, ops.IStartswith,
ops.Endswith, ops.IEndswith
],
"NUMBERS": [ops.Lt, ops.Lte, ops.Gt, ops.Gte, ops.Range],
"DATES": [ops.Before, ops.After],
"OTHERS": [ops.Boolean, ops.Exists],
}
FILTERS["STRINGS"] = [ops.In, ops.Exact, ops.IExact, ops.Ne] + FILTERS["LONG_STRINGS"]
FILTERS["ALL"] = FILTERS["STRINGS"] + FILTERS["NUMBERS"] + FILTERS["DATES"] + FILTERS["OTHERS"]


def enter(path, key, value):
if isinstance(value, BaseDict):
Expand Down
5 changes: 3 additions & 2 deletions mpcontribs-api/mpcontribs/api/attachments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask_mongorest.exceptions import UnknownFieldError
from flask import Blueprint

from mpcontribs.api import FILTERS
from mpcontribs.api.core import SwaggerView
from mpcontribs.api.attachments.document import Attachments

Expand All @@ -19,8 +20,8 @@ class AttachmentsResource(Resource):
filters = {
"id": [ops.In, ops.Exact],
"md5": [ops.In, ops.Exact],
"name": ops.STRINGS,
"mime": ops.STRINGS,
"name": FILTERS["STRINGS"],
"mime": FILTERS["STRINGS"],
}
fields = ["id", "name", "mime", "md5"]
allowed_ordering = ["name", "mime"]
Expand Down
21 changes: 7 additions & 14 deletions mpcontribs-api/mpcontribs/api/contributions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,11 @@
from flask_mongorest.resources import Resource
from flask_mongorest import operators as ops
from flask_mongorest.methods import (
Fetch,
Delete,
Update,
BulkFetch,
BulkCreate,
BulkUpdate,
BulkDelete,
Download,
Fetch, Delete, Update, BulkFetch, BulkCreate, BulkUpdate, BulkDelete, Download,
)
from flask_mongorest.exceptions import UnknownFieldError

from mpcontribs.api import enter
from mpcontribs.api import enter, FILTERS
from mpcontribs.api.core import SwaggerView
from mpcontribs.api.contributions.document import Contributions
from mpcontribs.api.structures.views import StructuresResource
Expand Down Expand Up @@ -53,13 +46,13 @@ class ContributionsResource(Resource):
save_related_fields = ["structures", "tables", "attachments", "notebook"]
filters = {
"id": [ops.In, ops.Exact],
"project": ops.STRINGS,
"identifier": ops.STRINGS,
"formula": ops.STRINGS,
"project": FILTERS["STRINGS"],
"identifier": FILTERS["STRINGS"],
"formula": FILTERS["STRINGS"],
"is_public": [ops.Boolean],
"last_modified": ops.DATES,
"last_modified": FILTERS["DATES"],
"needs_build": [ops.Boolean],
re.compile(r"^data__((?!__).)*$"): ops.ALL,
re.compile(r"^data__((?!__).)*$"): FILTERS["ALL"],
"structures": [ops.Size],
"tables": [ops.Size],
"attachments": [ops.Size]
Expand Down
23 changes: 16 additions & 7 deletions mpcontribs-api/mpcontribs/api/projects/views.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# -*- coding: utf-8 -*-
import os
import flask_mongorest

from mongoengine.queryset import DoesNotExist
from flask import Blueprint, current_app, url_for
from flask_mongorest.resources import Resource
from flask_mongorest import operators as ops
from flask_mongorest.methods import Fetch, Create, Delete, Update, BulkFetch
from werkzeug.exceptions import Unauthorized
from itsdangerous import SignatureExpired

from mpcontribs.api import FILTERS
from mpcontribs.api.core import SwaggerView
from mpcontribs.api.projects.document import Projects, Column, Reference, Stats

Expand All @@ -25,7 +28,13 @@ class ReferenceResource(Resource):

class StatsResource(Resource):
document = Stats
# TODO filters, fields?
filters = {
"columns": FILTERS["NUMBERS"],
"contributions": FILTERS["NUMBERS"],
"tables": FILTERS["NUMBERS"],
"structures": FILTERS["NUMBERS"],
"attachments": FILTERS["NUMBERS"],
}


class ProjectsResource(Resource):
Expand All @@ -36,13 +45,13 @@ class ProjectsResource(Resource):
"stats": StatsResource,
}
filters = {
"name": ops.STRINGS,
"name": FILTERS["STRINGS"],
"is_public": [ops.Boolean],
"title": ops.LONG_STRINGS,
"long_title": ops.LONG_STRINGS,
"authors": ops.LONG_STRINGS,
"description": ops.LONG_STRINGS,
"owner": ops.STRINGS,
"title": FILTERS["LONG_STRINGS"],
"long_title": FILTERS["LONG_STRINGS"],
"authors": FILTERS["LONG_STRINGS"],
"description": FILTERS["LONG_STRINGS"],
"owner": FILTERS["LONG_STRINGS"],
"is_approved": [ops.Boolean],
"unique_identifiers": [ops.Boolean],
"columns": [ops.Size],
Expand Down
3 changes: 2 additions & 1 deletion mpcontribs-api/mpcontribs/api/structures/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask_mongorest.methods import Fetch, BulkFetch, Download
from flask import Blueprint

from mpcontribs.api import FILTERS
from mpcontribs.api.core import SwaggerView
from mpcontribs.api.structures.document import Structures

Expand All @@ -18,7 +19,7 @@ class StructuresResource(Resource):
filters = {
"id": [ops.In, ops.Exact],
"md5": [ops.In, ops.Exact],
"name": ops.STRINGS,
"name": FILTERS["STRINGS"],
"sites": [ops.Size]
}
fields = ["id", "name", "md5"]
Expand Down
3 changes: 2 additions & 1 deletion mpcontribs-api/mpcontribs/api/tables/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask_mongorest.exceptions import UnknownFieldError
from flask import Blueprint

from mpcontribs.api import FILTERS
from mpcontribs.api.core import SwaggerView
from mpcontribs.api.tables.document import Tables, Attributes, Labels

Expand All @@ -29,7 +30,7 @@ class TablesResource(Resource):
filters = {
"id": [ops.In, ops.Exact],
"md5": [ops.In, ops.Exact],
"name": ops.STRINGS,
"name": FILTERS["STRINGS"],
"columns": [ops.Size]
}
fields = ["id", "name", "md5"]
Expand Down

0 comments on commit a45bed2

Please sign in to comment.