Property

class fsglue.property.BaseProperty(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)[source]

Bases: object

BasePropety

You can define your CustomProperty by extending this BaseProperty.

Examples

import fsglue

class YesNoProperty(fsglue.BaseProperty):
    # Return 'Yes' or 'No' for application and store True or False in firestore

    def to_app_value(self, value, obj):
        return 'Yes' if bool(value) else 'No'

    def from_app_value(self, value):
        return True if value == 'Yes' else False

    def to_db_value(self, value, obj):
        return bool(value)

    def from_db_value(self, value):
        return bool(value)

    def get_schema(self):
        return {"type": "string", "enum": ["Yes", "No"]}
__init__(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)[source]

Constructor

Parameters
  • required (bool, optional) – If True, property value cannot be None

  • default (optional) – If property value is None, property return this default value

  • choices (list, optional) – List of values that property value can take

  • schema (dict, optional) – JsonSchema definition for property.

  • validator (Callable[[value, obj], None], optional) – value validator for property

  • is_virtual (bool, optional) – If True, do not save property value in firestore.

to_app_value(value, obj)[source]

Convert property-internal value to exposed value for application

Parameters
  • value – property-internal value

  • obj – model instance

Returns

exposed value

from_app_value(value, obj)[source]

Convert exposed value to property-internal value from application

Parameters
  • value – exposed value

  • obj – model instance

Returns

property-internal value

to_db_value(value, obj)[source]

Convert property-internal value to firestore value

Parameters
  • value – property-internal value

  • obj – model instance

Returns

firestore value

from_db_value(value, obj)[source]

Convert firestore value to internal value

Parameters
  • value – firestore value

  • obj – model instance

Returns

property-internal value

to_db_search_value(value)[source]

Convert exposed value to firestore value for where/all method

get_schema()[source]

Get JsonSchema definition for property

Parameters

value – property firestore value

Returns

property inside value

class fsglue.property.StringProperty(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)[source]

Bases: fsglue.property.BaseProperty

__init__(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)

Constructor

Parameters
  • required (bool, optional) – If True, property value cannot be None

  • default (optional) – If property value is None, property return this default value

  • choices (list, optional) – List of values that property value can take

  • schema (dict, optional) – JsonSchema definition for property.

  • validator (Callable[[value, obj], None], optional) – value validator for property

  • is_virtual (bool, optional) – If True, do not save property value in firestore.

class fsglue.property.IntegerProperty(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)[source]

Bases: fsglue.property.BaseProperty

__init__(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)

Constructor

Parameters
  • required (bool, optional) – If True, property value cannot be None

  • default (optional) – If property value is None, property return this default value

  • choices (list, optional) – List of values that property value can take

  • schema (dict, optional) – JsonSchema definition for property.

  • validator (Callable[[value, obj], None], optional) – value validator for property

  • is_virtual (bool, optional) – If True, do not save property value in firestore.

class fsglue.property.FloatProperty(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)[source]

Bases: fsglue.property.BaseProperty

__init__(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)

Constructor

Parameters
  • required (bool, optional) – If True, property value cannot be None

  • default (optional) – If property value is None, property return this default value

  • choices (list, optional) – List of values that property value can take

  • schema (dict, optional) – JsonSchema definition for property.

  • validator (Callable[[value, obj], None], optional) – value validator for property

  • is_virtual (bool, optional) – If True, do not save property value in firestore.

class fsglue.property.BooleanProperty(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)[source]

Bases: fsglue.property.BaseProperty

__init__(required=False, default=None, choices=None, schema=None, validator=None, is_virtual=False)

Constructor

Parameters
  • required (bool, optional) – If True, property value cannot be None

  • default (optional) – If property value is None, property return this default value

  • choices (list, optional) – List of values that property value can take

  • schema (dict, optional) – JsonSchema definition for property.

  • validator (Callable[[value, obj], None], optional) – value validator for property

  • is_virtual (bool, optional) – If True, do not save property value in firestore.

class fsglue.property.TimestampProperty(auto_now=False, auto_now_add=False, **kwargs)[source]

Bases: fsglue.property.BaseProperty

Provide UTC Timestamp(int) value for application and Date value for firestore

__init__(auto_now=False, auto_now_add=False, **kwargs)[source]

Constructor

Parameters
  • auto_now (bool, optional) – If True, store last updated time

  • auto_now_add (bool, optional) – If True, store created time

  • **kwargs (optional) – Same as BaseProperty.__init__()

class fsglue.property.JsonProperty(store_as_string=False, **kwargs)[source]

Bases: fsglue.property.BaseProperty

Can store dict or list value for application and firestore.

Examples

import fsglue

ITEMS_SCHEMA = {
    "type": "array",
    "minItems": 1,
    "items": {
        "type": "object",
        "required": ["name", "price", "cnt"],
        "additionalProperties": False,
        "properties": {
            "name": {
                "type": "string",
            },
            "price": {
                "type": "number",
            },
            "cnt": {
                "type": "number",
            },
        },
    },
}

COUPON_SCHEMA = {
    "type": "object",
    "additionalProperties": False,
    "required": ["coupon_id", "coupon_name", "condition"],
    "properties": {
        "coupon_id": {
            "type": "string",
        },
        "coupon_name": {
            "type": "string",
        },
        "condition": {"type": "object", "additionalProperties": True},
    },
}

class Purchase(fsglue.BaseModel):
    COLLECTION_PATH = "purchase"
    COLLECTION_PATH_PARAMS = []

    items = fsglue.JsonProperty(schema=ITEMS_SCHEMA, default=[], required=True)
    coupon = fsglue.JsonProperty(schema=COUPON_SCHEMA, default=None)

# create
purchase = Purchase.create()
purchase.items = [{"name": "apple", "price": 100, "cnt": 1}]
purchase.coupon = {
    "coupon_id": "test",
    "coupon_name": "time sale 10% off",
    "condition": {"discount_rate": 0.9},
}
purchase.put()
__init__(store_as_string=False, **kwargs)[source]

Constructor

Parameters
  • _store_as_string (bool, optional) – If True, store value as string in firestore.

  • **kwargs (optional) – Same as BaseProperty.__init__()

class fsglue.property.ComputedProperty(computer=None, **kwargs)[source]

Bases: fsglue.property.BaseProperty

Can store computed value from other property values.

Examples

import fsglue

def calc_sum(obj):
    return obj.num1 + obj.num2

class TestModel(fsglue.BaseModel):
    COLLECTION_PATH = "test"
    COLLECTION_PATH_PARAMS = []

    num1 = fsglue.IntegerProperty(required=True)
    num2 = fsglue.IntegerProperty(required=True)
    sum = fsglue.ComputedProperty(computer=calc_sum)
__init__(computer=None, **kwargs)[source]

Constructor

Parameters
  • computer (Callable[[obj], Any]) – Calculate property value from other propery values

  • **kwargs (optional) – Same as BaseProperty.__init__()

class fsglue.property.ConstantProperty(value=None, **kwargs)[source]

Bases: fsglue.property.BaseProperty

Provide constant value for application and firestore

__init__(value=None, **kwargs)[source]

Constructor

Parameters