1

I am building an Open Source Project, Python MongoDB ORM (for Flask especially) using flask_pymongo and I am kind of stuck at building dynamic conditions.

Below code is what I have written in corresponding files

Model.py

from app.database import Database

class Model:

    conditions = {"and":[], "or":[], "in":[]}
    operators = {
        "!=": "$ne",
        "<": "$lt",
        ">": "$gt",
        "<=": "$lte",
        ">=": "$gte",
        "in": "$in",
        "not in":"$nin",
        "and": "$and",
        "or": "$or"
    }

    def __init__(self):
        # collection property from User class
        # Database class takes collection to fire MongoDB queries 
        self.db = Database(self.collection)

    def where(self, field, operator, value=None):
        if value is None:
            # to enable Model.where("first_name", "John")
            value = operator
            operator = "="

        self._handle_condition("and", field, operator, value)
        # to enable Model.where().where_or() and etc
        return self

    def where_or(self, field, operator, value=None):
        if value is None:
            # to enable Model.where("first_name", "John")
            value = operator
            operator = "="

        self._handle_condition("or", field, operator, value)
        # to enable Model.where().where_or() and etc
        return self

   def _handle_condition(self, type, field, operator, value):
        self.conditions[type].append({"field":field, "operator":operator, value:value})

   def get(self):
       filetrs = {}
       for type in self.conditions:
           filetrs[self.operators[type]] = []
           for condition in self.conditions[type]:
               if condition["operator"] == "=":
                   filter = {condition["field"]:condition["value"]}
               else:
                   filter = {condition["field"]:{self.operators[condition["operator"]]:condition["value"]}}
               filetrs[self.operators[type]].append(filter)
       return self.db.find(filters)

User.py

from app.Model import Model

class UserModel(Model):
    # MongoDB collection name
    collection = "users"

    def __init__(self):
        Model.__init__(self)

User = UserModel()

What I want to achieve is, from UserController.py where User.py is imported and used like the mentioned code.

Where multiple conditions are being added using where and where_or Model methods, get methods is parsing all the conditions and passing as filter to find method

UserController.py

from app.User import User

class UserController:
    def index(self):
        # Should return all the users where _id is not blank or their first_name is equal to John
        return User.where("_id", "!=", "").where_or("first_name", "John").get()

The problem is this is not working at it should be, it seems working fine for any one condition, where or where_or but when I try to add multiple where and where_or conditions it is not working.

Your help is really appreciated.

PS: This question seems to have lots of code but to make you understand the complete scenario I had to, please feel free to comment if you still need any clarifications.

Eagerly looking forward.

0 Answers0