def convert_field_to_union(field, registry=None):

    _types = []
    for choice in field.choices:
        if isinstance(field, motorengine.GenericReferenceField):
            _field = motorengine.ReferenceField(get_document(choice))
        elif isinstance(field, motorengine.GenericEmbeddedDocumentField):
            _field = motorengine.EmbeddedDocumentField(choice)

        _field = convert_motorengine_field(_field, registry)
        _type = _field.get_type()
        if _type:
            _types.append(_type.type)
        else:
            # TODO: Register type auto-matically here.
            pass

    if len(_types) == 0:
        return None

    # XXX: Use uuid to avoid duplicate name
    name = "{}_{}_union_{}".format(
        field._owner_document.__name__,
        field.db_field,
        str(uuid.uuid1()).replace("-", ""),
    )
    Meta = type("Meta", (object,), {"types": tuple(_types)})
    _union = type(name, (graphene.Union,), {"Meta": Meta})
    return graphene.Field(_union)
Beispiel #2
0
class Portfolio(motorengine.Document):
    user = motorengine.StringField(max_length=128, required=True)
    cash = motorengine.IntField(default=1000000)
    positions = motorengine.ListField(
        motorengine.EmbeddedDocumentField(Position))

    async def make_deal(self, sym, units, price):
        if units == 0:
            raise Exception('Amount must be positive!')

        amount = units * price
        if amount > self.cash:
            raise Exception('Cash limit exceeded!')

        pnl = 0
        for position in self.positions:
            if position.symbol == sym:
                pnl = position.update(units, price)
                if position.units == 0:
                    self.positions.remove(position)
                break
        else:
            if units < 0:
                raise Exception('Position limit exceeded!')
            else:
                self.positions.append(
                    Position(symbol=sym, units=units, price=price))

        self.cash -= amount
        # self.cash += pnl
        await self.save()

    async def reset(self):
        self.cash = 1000000
        self.positions = []
        await self.save()

    def serialize(self):
        return {
            # 'user': self.user,
            'cash': self.cash,
            'positions': [p.serialize() for p in self.positions],
        }
Beispiel #3
0
class Reporter(motorengine.Document):

    meta = {"collection": "test_reporter"}
    id = motorengine.StringField(primary_key=True)
    first_name = motorengine.StringField(required=True)
    last_name = motorengine.StringField(required=True)
    email = motorengine.EmailField()
    awards = motorengine.ListField(motorengine.StringField())
    articles = motorengine.ListField(motorengine.ReferenceField(Article))
    embedded_articles = motorengine.ListField(
        motorengine.EmbeddedDocumentField(EmbeddedArticle))
    embedded_list_articles = motorengine.EmbeddedDocumentListField(
        EmbeddedArticle)
    generic_reference = motorengine.GenericReferenceField(
        choices=[Article, Editor])
    generic_embedded_document = motorengine.GenericEmbeddedDocumentField(
        choices=[EmbeddedArticle, EmbeddedFoo])
    generic_references = motorengine.ListField(
        motorengine.GenericReferenceField(choices=[Article, Editor]))
Beispiel #4
0
 class MotorDoc(motorengine.Document):
     __collection__ = 'IntegrationTestEmbeddedDoc'
     embed = motorengine.EmbeddedDocumentField(MotorBase)
class Other(motorengine.Document):
    __collection__ = "Other_%s" % COLLECTION
    items = motorengine.ListField(
        motorengine.EmbeddedDocumentField(MotorDocument))
Beispiel #6
0
class ProfessorVector(motorengine.Document):

    meta = {"collection": "test_professor_vector"}
    vec = motorengine.ListField(motorengine.FloatField())
    metadata = motorengine.EmbeddedDocumentField(ProfessorMetadata)