Added support for validation only of put requests.

This commit is contained in:
Nicholas Staples
2016-01-29 11:11:00 +00:00
parent 2bd244032b
commit 2451f7e53d
5 changed files with 37 additions and 39 deletions

View File

@@ -16,13 +16,12 @@ def save_model_service(service, update_dict=None):
update_dict.pop('id', None) update_dict.pop('id', None)
update_dict.pop('users', None) update_dict.pop('users', None)
# TODO optimize this algorithm # TODO optimize this algorithm
new_users = User.query.filter(User.id.in_(users_list)).all() for i, x in enumerate(service.users):
for x in service.users: if x not in users_list:
if x in new_users:
new_users.remove(x)
else:
service.users.remove(x) service.users.remove(x)
for x in new_users: else:
users_list.remove(x)
for x in users_list:
service.users.append(x) service.users.append(x)
Service.query.filter_by(id=service.id).update(update_dict) Service.query.filter_by(id=service.id).update(update_dict)
else: else:

View File

@@ -9,9 +9,9 @@ from app.models import (Template, Service)
def save_model_template(template, update_dict=None): def save_model_template(template, update_dict=None):
if update_dict: if update_dict:
update_dict.pop('id', None) update_dict.pop('id', None)
service_id = update_dict.pop('service') service = update_dict.pop('service')
Template.query.filter_by(id=template.id).update(update_dict) Template.query.filter_by(id=template.id).update(update_dict)
template.service = Service.query.get(service_id) template.service = service
else: else:
db.session.add(template) db.session.add(template)
db.session.commit() db.session.commit()

View File

@@ -8,17 +8,11 @@ from marshmallow import post_load
# http://marshmallow.readthedocs.org/en/latest/api_reference.html # http://marshmallow.readthedocs.org/en/latest/api_reference.html
class UserSchema(ma.ModelSchema): class BaseSchema(ma.ModelSchema):
def __init__(self, *args, load_json=False, **kwargs): def __init__(self, *args, load_json=False, **kwargs):
self.load_json = load_json self.load_json = load_json
super(UserSchema, self).__init__(*args, **kwargs) super(BaseSchema, self).__init__(*args, **kwargs)
class Meta:
model = models.User
exclude = (
"updated_at", "created_at", "user_to_service",
"_password", "verify_codes")
@post_load @post_load
def make_instance(self, data): def make_instance(self, data):
@@ -29,35 +23,46 @@ class UserSchema(ma.ModelSchema):
""" """
if self.load_json: if self.load_json:
return data return data
return super(UserSchema, self).make_instance(data) return super(BaseSchema, self).make_instance(data)
# TODO process users list, to return a list of user.id class UserSchema(BaseSchema):
# Should that list be restricted by the auth parsed??
class ServiceSchema(ma.ModelSchema): class Meta:
model = models.User
exclude = (
"updated_at", "created_at", "user_to_service",
"_password", "verify_codes")
class ServiceSchema(BaseSchema):
class Meta: class Meta:
model = models.Service model = models.Service
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", "queue_name") exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", "queue_name")
class TemplateSchema(ma.ModelSchema): class TemplateSchema(BaseSchema):
class Meta: class Meta:
model = models.Template model = models.Template
exclude = ("updated_at", "created_at", "service_id", "jobs") exclude = ("updated_at", "created_at", "service_id", "jobs")
class ApiKeySchema(ma.ModelSchema): class ApiKeySchema(BaseSchema):
class Meta: class Meta:
model = models.ApiKey model = models.ApiKey
exclude = ("service", "secret") exclude = ("service", "secret")
class JobSchema(ma.ModelSchema): class JobSchema(BaseSchema):
class Meta: class Meta:
model = models.Job model = models.Job
class VerifyCodeSchema(ma.ModelSchema): class VerifyCodeSchema(BaseSchema):
class Meta: class Meta:
model = models.VerifyCode model = models.VerifyCode
exclude = ('user', "_code", "expiry_datetime", "code_used", "created_at") exclude = ('user', "_code", "expiry_datetime", "code_used", "created_at")
@@ -67,11 +72,15 @@ user_schema = UserSchema()
user_schema_load_json = UserSchema(load_json=True) user_schema_load_json = UserSchema(load_json=True)
users_schema = UserSchema(many=True) users_schema = UserSchema(many=True)
service_schema = ServiceSchema() service_schema = ServiceSchema()
service_schema_load_json = ServiceSchema(load_json=True)
services_schema = ServiceSchema(many=True) services_schema = ServiceSchema(many=True)
template_schema = TemplateSchema() template_schema = TemplateSchema()
template_schema_load_json = TemplateSchema(load_json=True)
templates_schema = TemplateSchema(many=True) templates_schema = TemplateSchema(many=True)
api_key_schema = ApiKeySchema() api_key_schema = ApiKeySchema()
api_key_schema_load_json = ApiKeySchema(load_json=True)
api_keys_schema = ApiKeySchema(many=True) api_keys_schema = ApiKeySchema(many=True)
job_schema = JobSchema() job_schema = JobSchema()
job_schema_load_json = JobSchema(load_json=True)
jobs_schema = JobSchema(many=True) jobs_schema = JobSchema(many=True)
verify_code_schema = VerifyCodeSchema() verify_code_schema = VerifyCodeSchema()

View File

@@ -13,7 +13,8 @@ from app.dao.templates_dao import (
from app.dao.api_key_dao import (save_model_api_key, get_model_api_keys, get_unsigned_secret) from app.dao.api_key_dao import (save_model_api_key, get_model_api_keys, get_unsigned_secret)
from app.models import ApiKey from app.models import ApiKey
from app.schemas import ( from app.schemas import (
services_schema, service_schema, template_schema, templates_schema, api_keys_schema) services_schema, service_schema, template_schema, templates_schema,
api_keys_schema, service_schema_load_json, template_schema_load_json)
from flask import Blueprint from flask import Blueprint
service = Blueprint('service', __name__) service = Blueprint('service', __name__)
@@ -47,14 +48,9 @@ def update_service(service_id):
delete_model_service(service) delete_model_service(service)
else: else:
status_code = 200 status_code = 200
# TODO there has got to be a better way to do the next three lines update_dict, errors = service_schema_load_json.load(request.get_json())
upd_serv, errors = service_schema.load(request.get_json())
if errors: if errors:
return jsonify(result="error", message=errors), 400 return jsonify(result="error", message=errors), 400
update_dict, errors = service_schema.dump(upd_serv)
# TODO FIX ME
# Remove update_service model which is added to db.session
db.session.rollback()
try: try:
save_model_service(service, update_dict=update_dict) save_model_service(service, update_dict=update_dict)
except DAOException as e: except DAOException as e:
@@ -170,15 +166,9 @@ def update_template(service_id, template_id):
delete_model_template(template) delete_model_template(template)
else: else:
status_code = 200 status_code = 200
# TODO there has got to be a better way to do the next three lines update_dict, errors = template_schema_load_json.load(request.get_json())
upd_temp, errors = template_schema.load(request.get_json())
if errors: if errors:
return jsonify(result="error", message=errors), 400 return jsonify(result="error", message=errors), 400
upd_temp.service = service
update_dict, errors = template_schema.dump(upd_temp)
# TODO FIX ME
# Remove update_temp model which is added to db.session
db.session.rollback()
try: try:
save_model_template(template, update_dict=update_dict) save_model_template(template, update_dict=update_dict)
except DAOException as e: except DAOException as e:

View File

@@ -270,7 +270,7 @@ def test_put_service_remove_user(notify_api, notify_db, notify_db_session, sampl
"new@digital.cabinet-office.gov.uk") "new@digital.cabinet-office.gov.uk")
data = { data = {
'name': sample_service.name, 'name': sample_service.name,
'users': [sample_user.id, another_user.id], 'users': [sample_user, another_user],
'limit': sample_service.limit, 'limit': sample_service.limit,
'restricted': sample_service.restricted, 'restricted': sample_service.restricted,
'active': sample_service.active} 'active': sample_service.active}