示例#1
0
class IndexModel(Model):

    __table_name__ = 'index_model'
    first_key = columns.UUID(primary_key=True)
    second_key = columns.Text(index=True)
示例#2
0
class IndexCaseSensitiveModel(Model):

    __table_name__ = 'IndexModel'
    __table_name_case_sensitive__ = True
    first_key = columns.UUID(primary_key=True)
    second_key = columns.Text(index=True)
示例#3
0
class ExampleModel(DjangoCassandraModel):
    example_id = columns.UUID(primary_key=True, default=uuid.uuid4)
    example_type = columns.Integer(index=True)
    created_at = columns.DateTime()
    description = columns.Text(required=False)
示例#4
0
class TodoModel(Model):
    todo_id = columns.UUID(primary_key=True, default=uuid.uuid4)
    title_id = columns.Integer(primary_key=True, index=True)
    title = columns.Text(required=True)
    description = columns.Text(required=True)
    created_at = columns.DateTime(default=datetime.now, index=True)
示例#5
0
class AccountTransaction(AbstractBaseModel):
    """
    AccountTransaction
    """
    __table_name__ = 'account_transaction'

    account_id = columns.UUID(primary_key=True)
    id = columns.TimeUUID(primary_key=True,
                          clustering_order='DESC',
                          default=uuid.uuid1)
    description = columns.Text()
    value = columns.UserDefinedType(Amount)
    source_id = columns.UUID()
    signature = columns.Text()
    type = columns.Text(discriminator_column=True)
    status = columns.TinyInt(default=TRANSACTION_PENDING[0], index=True)
    # failure code
    failure_code = columns.SmallInt()

    def __init__(self, *args, **kwargs):
        super(AccountTransaction, self).__init__(*args, **kwargs)
        self._init_machine()

    # ---------------
    # Machine Methods
    # ---------------
    def _init_machine(self, transitions=TRANSACTION_STATE_TRANSITIONS):
        """
        Method to hook a state machine to the instance
        """
        states = list(TRANSACTION_STATUS_STRING_CHOICES)
        self.machine = Machine(model=self,
                               states=states,
                               transitions=transitions,
                               auto_transitions=False,
                               send_event=True,
                               initial=TRANSACTION_STATUS_MAP[self.status],
                               after_state_change='_state_changed')

    def _state_changed(self, event):
        """
        callback from state machine to change status on instance and persist
        :param event: EventData
        :return:
        """
        self.status = TRANSACTION_STATUS_STRING_MAP[event.state.name]
        persist = event.kwargs.get('persist', False)
        if persist:
            self.save()

    def execute_operation(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        transfer = event.kwargs.get('transfer')
        if transfer is None:
            raise TransferNotAvailable

        if transfer.is_sealed():
            account = CurrentAccount.objects(id=self.account_id).get()
            response = account.execute_pending(self.id)
            event.kwargs.update({'operation_response': response})
            account.save()
        else:
            event.kwargs.update({
                'operation_response':
                Response(error=FAILURE_TRANSFER_IS_NOT_SEALED)
            })

    def execute_cancel(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        transfer = event.kwargs.get('transfer')
        if transfer is None:
            raise TransferNotAvailable

        if transfer.is_created():
            account = CurrentAccount.objects(id=self.account_id).get()
            response = account.cancel_pending(self.id)
            event.kwargs.update({'operation_response': response})
            account.save()

    def has_execute_succeed(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        response = event.kwargs.get('operation_response')
        if not response.is_success:
            self.failure_code = response.error_code
            return False
        return True

    def has_failure_code(self, event, **kwargs):
        """

        :param event: EventData
        :return:
        """
        return self.failure_code is not None
class TestConditionalModel(Model):
    id = columns.UUID(primary_key=True, default=uuid4)
    count = columns.Integer()
    text = columns.Text(required=False)
        class WildDBNames(Model):

            id  = columns.UUID(primary_key=True, default=lambda:uuid4())
            content = columns.Text(db_field='words_and_whatnot')
            numbers = columns.Integer(db_field='integers_etc')