예제 #1
0
 def get_required(self, schema: FormencodeSchema):
     fields = schema.fields
     required = []
     for name, validator in fields.items():
         if self.is_required(validator):
             required.append(name)
     return required
예제 #2
0
    def fields_for_model(self, model, *args, **kwargs):
        # Look up aliases on all classes in the inheritance chain of
        # the model so mixins can define their own aliases if needed.
        def _get_from_mro(attr, key, default=None, _mro=getmro(model)):
            for cls in _mro:
                try:
                    return getattr(cls, attr, {})[key]
                except (TypeError, KeyError, AttributeError):
                    continue
            return default

        # XXX: To allow renaming/aliasing of fields we need to let mm-sqlalchemy
        # generate all fields from the models and leave it up to mm itself to
        # exclude fields we don't care about
        kwargs['fields'] = ()
        fields = super().fields_for_model(model, *args, **kwargs)

        # remove column_property leftovers so they don't break things when using
        # the schema without restricting the list of fields (it would still include
        # them even though they are explicitly excluded)
        for prop in model.__mapper__.iterate_properties:
            if _is_column_property(prop):
                del fields[prop.key]

        for key, field in list(fields.items()):
            new_key = _get_from_mro('marshmallow_aliases', key)
            if new_key:
                del fields[key]
                fields[new_key] = field
                if field.attribute is None:
                    field.attribute = key
        return fields
예제 #3
0
    def fields_for_model(self, model, *args, **kwargs):
        # Look up aliases on all classes in the inheritance chain of
        # the model so mixins can define their own aliases if needed.
        def _get_from_mro(attr, key, default=None, _mro=getmro(model)):
            for cls in _mro:
                try:
                    return getattr(cls, attr, {})[key]
                except (TypeError, KeyError, AttributeError):
                    continue
            return default

        # XXX: To allow renaming/aliasing of fields we need to let mm-sqlalchemy
        # generate all fields from the models and leave it up to mm itself to
        # exclude fields we don't care about
        kwargs['fields'] = ()
        fields = super(IndicoModelConverter,
                       self).fields_for_model(model, *args, **kwargs)
        for key, field in fields.items():
            new_key = _get_from_mro('marshmallow_aliases', key)
            if new_key:
                del fields[key]
                fields[new_key] = field
                if field.attribute is None:
                    field.attribute = key
        return fields
예제 #4
0
    def get_parameters(self, schema):
        """Get swagger query parameters from schema
        
        :param schema: marshmallow schema
        :param where: can be path or query
        """
        fields = schema.__dict__.get(u'_declared_fields', [])
        res = []
        for field, value in fields.items():
            if value.load_from is not None:
                field = value.load_from

            context = value.metadata.get(u'context', None)
            if context is not None:
                if context == u'body':
                    kvargs = {
                        u'in': u'body',
                        u'name': u'body',
                        u'schema': {
                            u'$ref':
                            u'#/definitions/%s' % value.nested.__name__
                        }
                    }
                else:
                    kvargs = {
                        u'in': context,
                        u'name': field,
                        u'required': value.required,
                        u'description':
                        value.metadata.get(u'description', u''),
                    }

                    field_type = value.__class__.__name__.lower()
                    if field_type == u'date':
                        kvargs[u'type'] = u'string'
                        kvargs[u'format'] = u'date'
                    if field_type == u'datetime':
                        kvargs[u'type'] = u'string'
                        kvargs[u'format'] = u'datetime'
                    else:
                        kvargs[u'type'] = field_type
                    if bool(value.default) is not False:
                        kvargs[u'default'] = value.default
                    if value.validate is not None and isinstance(
                            value.validate, OneOf):
                        kvargs[u'enum'] = value.validate.choices

            res.append(kvargs)
            #self.spec.add_parameter(field, u'query', **kvargs)

        #return self.spec._parameters.values()
        return res
예제 #5
0
    def make_cells(self, fields: Dict[str, Any]) -> List[Cell]:
        """Create a list of Cell objects from dictionary

        Args:
            fields: dictionary where key is a column title and value is a cell value

        Returns:
            list of Cell objects
        """
        result: List[Cell] = []
        for column_title, field_value in fields.items():
            result.append(self.make_cell(column_title, field_value))
        return result
예제 #6
0
    def fields_for_model(self, model, *args, **kwargs):
        # Look up aliases on all classes in the inheritance chain of
        # the model so mixins can define their own aliases if needed.
        def _get_from_mro(attr, key, default=None, _mro=getmro(model)):
            for cls in _mro:
                try:
                    return getattr(cls, attr, {})[key]
                except (TypeError, KeyError, AttributeError):
                    continue
            return default

        # XXX: To allow renaming/aliasing of fields we need to let mm-sqlalchemy
        # generate all fields from the models and leave it up to mm itself to
        # exclude fields we don't care about
        kwargs['fields'] = ()
        fields = super(IndicoModelConverter, self).fields_for_model(model, *args, **kwargs)
        for key, field in fields.items():
            new_key = _get_from_mro('marshmallow_aliases', key)
            if new_key:
                del fields[key]
                fields[new_key] = field
                if field.attribute is None:
                    field.attribute = key
        return fields
예제 #7
0
 def get_properties(self, schema: FormencodeSchema):
     fields = schema.fields
     properties = {}
     for name, validator in fields.items():
         properties[name] = self.convert_validator(validator)
     return properties