def create_node_at_path(user, full_path, node): """ Convenience method to create a node at a given path. :param user: User creating the path :param full_path: Full path specification for node (e.g. '/path/to/node') :param node: Node to create at the path. :return: Path that was modified or created. """ if not user.can_any('create_path'): raise OneBaseException('E-201', user=user, permissions=[ 'create_path', ]) path = Path.find(full_path) if path and path.node is not None: raise OneBaseException('E-204', path=path.string2) if not path: logger.debug("Creating new path {}".format(full_path)) path = Path.create(user, full_path) path.node = node path.save(user) return path
def validate_value(self, value, size, content_type='application/json', clean=False): """ Validate the value based on the type. :param value: Value to validate. :param size: Size of the value (given by the Key) :parm clean: Passed in from parent validate() method. :return: True on success, raise OneBaseException if invalid. """ headers = {} data = { 'value': value, 'size': size, } logger.debug('validating {} with {}'.format(data, self.validator)) response = requests.post(self.validator, json=data, headers=headers) if response.status_code == 200: return True raise OneBaseException('E-101', message=response.content)
def basic_regex_validation(REGEX_FUNC): """ Validate a slot by a given regex `match` function. This method will also check the length of the value. If the length of the `Type` is greater than the length of the slot, return False. :param REGEX_FUNC: Regular expression `match` function (or similar) :return: True if request's value is valid, False otherwise. """ (value, length) = get_parts() if len(value) > length: raise OneBaseException('E-100', key='value', expected=length, given=len(value)) if not REGEX_FUNC(value): raise OneBaseException('E-101', message="Invalid format") return (length is not None) and (len(value) < length and REGEX_FUNC(value))
def validate(self, value): """ Validate a value :param value: Value to validate. """ if len(str(value)) > self.maximum_size: raise OneBaseException('E-100', expected=self.maximum_size, given=len(value)) return True
def create(cls, user, full_path): if cls.find(full_path) is not None: raise OneBaseException('E-206', path=full_path) (head, tail) = path_head_tail(full_path) p = cls.objects(name=head, parent=None).first() if p is None: p = Path(name=head) p.save(user) if tail is not None: return p.make(user, tail) return p
def render_default(self, slot, size=DEFAULT_SIZE, include_alpha=True, encode='PNG'): if size not in SIZES.keys: raise OneBaseException('E-503', value=size, key='size') size = (SIZES[size], SIZES[size] * 1.5) value = slot.value pil_mode = 'RGBA' if not include_alpha: value = value[:7] pil_mode = 'RGB' image = Image.new(pil_mode, size, alpha) return image.tobytes(encode)
def get_reference(self): """ Get the slot's reference. If a slot contains a reference to another slot, get it. A reference is in the format: onebase://<slot_id> """ parts = None if str(self.value).startswith('onebase://'): parts = urlparse(self.value) else: return None slot_id = parts.net_loc if Slot.objects.find(id=slot_id).first() is None: raise OneBaseException('E-103', url=self.value)
def represent_value(self, value, headers={}, environment={}): """ Represent a value. :param value: Value to represent :param headers: Headers to pass to the request :param repr_kwargs: Keyword arguments to the representer :return: Response body. """ headers = headers or {} data = {'value': value, 'environment': environment} logger.debug('representing {} with {}'.format(data, self.repr)) response = requests.post(self.repr, json=data, headers=headers) if response.status_code == 200: return response.text raise OneBaseException('E-101', message=response.content)
def get_parts(): body = request.get_json() for req in ('value', 'size'): if not body.get(req, None): raise OneBaseException('E-102', keys=[req, ]) return (str(body['value']), int(body['size']))