def __init__(self, validators=None, filters=None, **kwargs):

        self.validators = \
            self._ensure_callable_or_list(validators, 'validators')
        self.filters = self._ensure_callable_or_list(filters, 'filters')

        BaseField.__init__(self, **kwargs)
Beispiel #2
0
    def __init__(self, validators=None, filters=None, **kwargs):

        self.validators = \
            self._ensure_callable_or_list(validators, 'validators')
        self.filters = self._ensure_callable_or_list(filters, 'filters')

        BaseField.__init__(self, **kwargs)
Beispiel #3
0
class Campaign(Document):
    user_id = ObjectIdField(required=True)
    name = StringField(required=True)
    created_at = DateTimeField(default=datetime.datetime.now())
    status = BaseField(default='draft', choices=['draft', 'published'])

    state = DictField()
Beispiel #4
0
class Response(EmbeddedDocument):
    """
    响应包
    """
    status_code = IntField(required=True)
    header = DictField()
    body_content = BaseField()
    body_type = StringField()  # 与request相同
Beispiel #5
0
class Request(EmbeddedDocument):
    """
    请求包
    """
    url = StringField(required=True)
    method = StringField(required=True)
    header = DictField()
    body_content = BaseField()  # 请求体
    body_type = StringField(
    )  # 请求头类型:json/form/xml/bytes (若为bytes类型,则在base64编码后存储)
class Setting(Document):
    """
    The setting document structure.
    """

    _id: ObjectId = ObjectIdField()
    name = StringField(required=True)
    platform = EnumField(Platform)
    starting_build = IntField(required=True)
    value = BaseField(required=True)

    meta = {"indexes": [{"fields": ["platform", "-starting_build", "name"], "unique": True}]}

    @classmethod
    def with_build_and_platform(cls, *, build: int, platform: Platform) -> Dict:
        """
        Return the settings matching the given build and platform.
        If there are more settings with the same name, the one with the higher starting build
        version is returned.

        :param build: the app build number.
        :param platform: the app platform.
        :return: a dictionary containing the settings name and value.
        """

        settings = cls.objects.aggregate(
            *cls.get_build_and_platform_pipeline(platform=platform, build=build)
        )
        return {s["name"]: s["value"] for s in settings}

    @staticmethod
    def get_build_and_platform_pipeline(*, build: int, platform: Platform,) -> List[Dict]:
        """
        Return the aggregation pipeline for retrieving the settings with platform and build.

        :param build: the app build number.
        :param platform: the app platform.
        :return: the list of aggregation stages to perform the query.
        """
        return [
            {"$match": {"platform": platform.name, "starting_build": {"$lte": build}}},
            {"$sort": {"starting_build": -1}},
            {
                "$group": {
                    "_id": "$name",
                    "name": {"$first": "$name"},
                    "value": {"$first": "$value"},
                }
            },
        ]