Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding media type support #162

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Scripts

#pyenv
.python-version
.venv

#OSX
.Python
Expand All @@ -65,3 +66,6 @@ _build
.idea

.cache

# vscode
.vscode
7 changes: 6 additions & 1 deletion eve_sqlalchemy/config/fieldconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ def _render(self):
def _get_field_nullable(self):
return getattr(self._sqla_column, 'nullable', True)

def _has_server_default(self):
return bool(getattr(self._sqla_column, 'server_default'))

def _get_field_required(self):
autoincrement = (self._sqla_column.primary_key
and self._sqla_column.autoincrement
and isinstance(self._sqla_column.type, types.Integer))
return not (self._get_field_nullable() or autoincrement)
return not (self._get_field_nullable()
or autoincrement
or self._has_server_default())

def _get_field_unique(self):
return getattr(self._sqla_column, 'unique', None)
Expand Down
5 changes: 4 additions & 1 deletion eve_sqlalchemy/tests/config/resourceconfig/schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import sqlalchemy as sa
import sqlalchemy.dialects.postgresql as postgresql
from sqlalchemy import Column, types
from sqlalchemy.ext.declarative import declarative_base
Expand All @@ -22,6 +23,7 @@ class SomeModel(Base):
a_pickle = Column(types.PickleType)
a_string = Column(types.String(42), default='H2G2')
_internal = Column(types.Integer)
a_server_default_col = Column(types.Integer, server_default=sa.text('0'))


class StringPK(Base):
Expand All @@ -39,7 +41,7 @@ def test_all_columns_appear_in_schema(self):
self.assertEqual(set(schema.keys()),
set(('id', 'a_boolean', 'a_date', 'a_datetime',
'a_float', 'a_json', 'another_json',
'a_pickle', 'a_string')))
'a_pickle', 'a_string', 'a_server_default_col')))

def test_field_types(self):
schema = self._render(SomeModel)['schema']
Expand All @@ -66,6 +68,7 @@ def test_required(self):
# autoincrement='auto' per default (see SQLAlchemy docs for
# details). As such, it is not required.
self.assertFalse(schema['id']['required'])
self.assertFalse(schema['a_server_default_col']['required'])

def test_required_string_pk(self):
schema = self._render(StringPK)['schema']
Expand Down
13 changes: 13 additions & 0 deletions eve_sqlalchemy/tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ def setUp(self):
'a_objectid': {
'type': 'objectid',
},
'a_media': {
'type': 'media',
}
}
self.validator = eve_sqlalchemy.validation.ValidatorSQL(schemas)

Expand All @@ -25,3 +28,13 @@ def test_type_json(self):
def test_type_objectid(self):
self.validator.validate_update(
{'a_objectid': ''}, None)

def test_type_media(self):
from werkzeug.datastructures import FileStorage

self.assertFalse(self.validator.validate(
{'a_media': 100} # check if integer type data passes through
))
self.assertTrue(self.validator.validate(
{'a_media': FileStorage(filename=__file__)}
))
10 changes: 10 additions & 0 deletions eve_sqlalchemy/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get_data_version_relation_document, missing_version_field,
)
from flask import current_app as app
from werkzeug.datastructures import FileStorage

from eve_sqlalchemy.utils import dict_update, remove_none_values

Expand Down Expand Up @@ -118,6 +119,15 @@ def _validate_type_json(self, field, value):
"""
pass

def _validate_type_media(self, field, value):
"""Enable validation for `media` type schema.

:param field: field name.
:param value: field value.
"""
if not isinstance(value, FileStorage):
self._error(field, "File was expected, got '%s' instead." % value)

def _validate_readonly(self, read_only, field, value):
# Copied from eve/io/mongo/validation.py.
original_value = self._original_document.get(field) \
Expand Down