Пример #1
0
    def set_owner(self,
                  instance: Any,
                  owner: Any = None,
                  owner_name: str = None,
                  owner_obj: Any = None,
                  commit: bool = False) -> None:
        if owner:
            owner = owner
        elif owner_name:
            try:
                owner = self.owner_manager.get(name=owner_name)
            except ObjectDoesNotExist:
                raise OwnershipError(
                    'Could not set an owner, owner name not found.')
        elif owner_obj:
            try:
                owner = self.owner_manager.get(
                    object_id=owner_obj.id,
                    content_type_id=self.content_type_manager.get_for_model(
                        owner_obj).id)
            except ObjectDoesNotExist:
                raise OwnershipError(
                    'Could not set an owner, owner name not found.')

        self.check_owner_type(owner=owner)
        instance.owner = owner
        if commit:
            instance.save(update_fields=['object_id', 'content_type_id'])
Пример #2
0
 def set_default_owner(self, instance):
     if settings.ALLOW_USER_PROJECTS:
         try:
             self.set_owner(instance=instance, owner_obj=instance.user)
         except OwnershipError:
             raise OwnershipError(
                 'You are not allowed to create a project, '
                 'please contact your admin.')
     else:
         raise OwnershipError('You are not allowed to create a project, '
                              'please contact your admin.')
Пример #3
0
    def set_default_owner(self, instance: Any) -> None:
        import conf

        if conf.get(ALLOW_USER_PROJECTS):
            try:
                self.set_owner(instance=instance, owner_obj=instance.user)
            except OwnershipError:
                raise OwnershipError('You are not allowed to create a project, '
                                     'please contact your admin.')
        else:
            raise OwnershipError('You are not allowed to create a project, '
                                 'please contact your admin.')
Пример #4
0
    def _set_user_owner(self, instance: Any):
        import conf

        if conf.get(ALLOW_USER_OWNERSHIP):
            try:
                self.set_owner(instance=instance, owner_obj=instance.user)
            except OwnershipError:
                raise OwnershipError(
                    'You are not allowed to create {}, '
                    'please contact your admin.'.format(instance))
        else:
            raise OwnershipError('You are not allowed to create {}, '
                                 'please contact your admin.'.format(instance))
Пример #5
0
 def _set_cluster_owner(self, instance: Any):
     try:
         self.set_owner(instance=instance, owner=self.cluster_owner)
     except OwnershipError:
         raise OwnershipError('You are not allowed to create {}, '
                              'please contact your admin.'.format(instance))
Пример #6
0
 def check_owner_type(owner: Any = None, owner_type: str = None) -> None:
     owner_type = owner.owner_type if owner else owner_type
     if not owner_type or owner_type not in settings.OWNER_TYPES:
         raise OwnershipError('Received an invalid owner type `{}`.'.format(
             owner.owner_type))
Пример #7
0
    def wrapper(*args, **kwargs):
        sync = kwargs.get('sync', False)
        ownsership = kwargs.get('ownership', False)
        name = f.__name__
        testnet = args[0].testnet
        t = args[0]._t
        from_address = args[1][1]
        to_address = args[2]
        password = args[4]
        # a piece has no edition number
        if name not in ['register_piece', 'consigned_registration']:
            edition_number = args[5]
        hash = ''
        if name not in ['refill', 'refill_main_wallet']:
            hash = args[3][0]

        # check ownership
        if ownsership:
            if name == 'register' and edition_number == 0:
                ow = Ownership(to_address, hash, edition_number, testnet=testnet)
                if not ow.can_register_master:
                    raise OwnershipError(ow.reason)
            elif name == 'register' and edition_number != 0:
                ow = Ownership(to_address, hash, edition_number, testnet=testnet)
                if not ow.can_register:
                    raise OwnershipError(ow.reason)
            elif name == 'editions':
                ow = Ownership(to_address, hash, edition_number, testnet=testnet)
                if not ow.can_editions:
                    raise OwnershipError(ow.reason)
            elif name == 'transfer' or name == 'consign' or name == 'loan':
                ow = Ownership(from_address, hash, edition_number, testnet=testnet)
                if not ow.can_transfer:
                    raise OwnershipError(ow.reason)
            elif name == 'unconsign':
                ow = Ownership(from_address, hash, edition_number, testnet=testnet)
                if not ow.can_unconsign:
                    raise OwnershipError(ow.reason)

                # check the to address
                chain = BlockchainSpider.chain(ow._tree, edition_number)
                chain_from_address = chain[-1]['from_address']
                if chain_from_address != to_address:
                    raise OwnershipError('You can only unconsign to {}'.format(chain_from_address))

        # do a synchronous transaction
        if sync:
            txid = f(*args, **kwargs)
            # lets give it some time for the transaction to reach the network
            confirmations = 0
            timeout = TIMEOUT
            while not confirmations:
                # lets do a simple exponential backoff. Transactions may take some time to be picked up by some
                # services
                try:
                    confirmations = t.get(txid).get('confirmations', 0)
                    timeout = TIMEOUT
                except Exception as e:
                    if e.message.find('code: 404') != -1:
                        timeout *= 2
                        if timeout > MAX_TIMEOUT:
                            raise
                    else:
                        raise
                time.sleep(timeout)
            return txid
        else:
            return f(*args, **kwargs)