def filter(self, prop, operator, value, label=None): """Add a filter. :param prop: Name of the property :type prop: str :param operator: Operator to use :type operator: str :param value: Value to filter :type value: str :param model: Optional label that has prop. Defaults to self.label :type model: str """ if label is None: label = self.label valid_properties = registry.properties(label) if not valid_properties: raise InvalidLabelError(label) if prop not in registry.properties(label): raise InvalidPropertyError(prop, label) if prop in registry.state_properties(label): label = '{}_state'.format(label) condition = '{}.{} {} {}'.format( label.lower(), prop, operator, '$filterval{}'.format(self.filter_count)) self.filter_wheres.append(condition) self.params['filterval{}'.format(self.filter_count)] = value self.filter_count += 1 return self
def validate(self, data): model_set = set([t[0] for t in registry.path(data['model'])]) model_set.add(data['model']) for f in data.get('filters', []): # Make sure filter model is in path of search model if f['model'] not in model_set: raise ValidationError('Model {} not in path of {}'.format( f['model'], data['model'])) # Make sure filter property is property of filter model if f['prop'] not in registry.properties(f['model']): raise ValidationError( 'Model {} does not have property {}'.format( f['model'], f['prop'])) for o in data.get('orders', []): # Make sure order model is in path of search model if o['model'] not in model_set: raise ValidationError('Model {} not in path of {}'.format( o['model'], data['model'])) # Make sure order property is property of order model if o['prop'] not in registry.properties(o['model']): raise ValidationError( 'Model {} does not have property {}'.format( o['model'], o['prop'])) return data
def retrieve(self, request, pk=None): """List all properties for a specific model.""" props = registry.properties(model=pk) if not props: raise Http404 serializer = PropertySerializer(props) return Response(serializer.data)
def to_representation(self, model): """Get dict repr :param model: VersionedEntity to represent :type model: class :returns: Dict representation :rtype: dict """ # Assemble list of properties. properties = {} for prop in registry.properties(model.label): properties[prop] = { 'type': model.properties.get(prop).type.__name__ } # Assemble child relationships children = {} for name, childtuple in model.children.items(): children[name] = { 'rel_name': childtuple[0], 'label': childtuple[1].label } return { 'label': model.label, 'state_label': model.state_label, 'properties': properties, 'identity': model.identity_property, 'children': children }
def orderby(self, prop, direction, label=None): """Add to orderby clause :param prop: Property to order by :type prop: str :param direction: Order direction (ASC or DESC) :type direction: str :param label: Label with the property. Defaults to target label. Can be target label or label in parent path :type label: str """ # Default label to target label if label is None: label = self.label # Make sure label is valid model = registry.models.get(label) if model is None: raise InvalidLabelError(label) # Make sure property is valid if prop not in registry.properties(label): raise InvalidPropertyError(label, prop) # Check if property is part of the state of the model if prop in registry.state_properties(label): varname = '{}_state'.format(label.lower()) else: varname = label.lower() self._orderby.append((varname, prop, direction))
def add_column(self, model, prop, name=None): """Add a column to return. Verify model is in path and prop belongs to model. Add a column tuple. These will be used in the return clause. :param model: Name of the model :type model: str :param prop: Name of the property belonging to the model. :type prop: str :param name: Optional name of the column instead of model.prop, the column name can be custom. RETURN model.prop vs RETURN model.prop AS custom :type name: str """ datapath = [p[0] for p in (registry.path(self.label) or [])] datapath += [self.label] if model not in datapath and model != self.label: raise InvalidLabelError(model) if prop not in registry.properties(model): raise InvalidPropertyError(prop, model) key = '{}.{}'.format(model, prop) if prop in registry.state_properties(model): model = _model_state(model) if name is not None: key = name self._columns[key] = '{}.{}'.format(model.lower(), prop) return self
def validate(self, data): """Custom validation. Ensure prop is a property of model. :param data: Data to validate :type data: dict """ if data['prop'] not in registry.properties(data['model']): raise ValidationError( {'prop': INVALID_PROP_STR.format(data['prop'], data['model'])}) return data
def validate(self, data): """Custom validation. Model should be a model listed with the registry. Prop should be a property of the model. :param data: Data to validate :type data: dict """ if data['model'] not in _models: raise ValidationError('Model {} is not a valid model.'.format( data['model'])) if data['prop'] not in registry.properties(data['model']): msg = ('{} is not a valid property of model {}'.format( data['prop'], data['model'])) raise ValidationError({'prop': msg}) return data
def list(self, request, model=None): """List all properties models.""" props = registry.properties() serializer = PropertySerializer(props) return Response(serializer.data)