# Path: habitat/views/utils.py
# def validate_doc(data, schema):
#     """Validate *data* against *schema*, raising descriptive errors"""
#     v = Validator()
#     errors = list(v.iter_errors(data, schema))
#     if errors:
#         errors = ', '.join((str(error) for error in errors))
#         raise ForbiddenError("Validation errors: {0}".format(errors))
#     _validate_formats(data, schema) 
# 
# def read_json_schema(schemaname):
#     mypath = os.path.dirname(inspect.getfile(inspect.currentframe()))
#     path = os.path.join(mypath, "..", "..", "couchdb", "schemas", schemaname)
#     with open(path) as f:
#         schema = json.load(f)
#     return schema
# 
# Path: habitat/views/utils.py
# def only_validates(doc_type):
#     def decorator(func):
#         def wrapped(new, old, userctx, secobj):
#             new_type = new.get("type", None)
#             new_deleted = new.get("_deleted", False)
#             if old:
#                 old_type = old.get("type", None)
#             else:
#                 old_type = None
# 
#             # sanity checks
#             if old_type is None:
#                 assert old == {} or old is None
#             if new_deleted:
#                 assert new_type is None
# 
#             if new_type == doc_type and old_type in [None, doc_type]:
#                 # new doc, or modified doc of correct type. validate:
#                 return func(new, old, userctx, secobj)
# 
#             elif new_deleted and old_type == doc_type:
#                 # deletion is managed by habitat.validate
#                 return
# 
#             elif new_type == doc_type or old_type == doc_type:
#                 # one or the other types match but not both, and not a new or deleted doc.
#                 raise ForbiddenError("You cannot change the type of a doc")
# 
#             else:
#                 # other type: not our business
#                 return
# 
#         # Be a well behaved decorator!
#         wrapped.__name__ = func.__name__
#         wrapped.__doc__ = func.__doc__
#         wrapped.__dict__.update(func.__dict__)
# 
#         return wrapped
#     return decorator


from couch_named_python import ForbiddenError, UnauthorizedError, version
from strict_rfc3339 import rfc3339_to_timestamp
from .utils import validate_doc, read_json_schema
from .utils import only_validates
# Copyright 2011, 2012 (C) Adam Greig
#
# This file is part of habitat.
#
# habitat is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# habitat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License

"""
Functions for the flight design document.

Contains schema validation and views by flight launch time, window end time and
payload name and window end time.
"""


schema = None

@version(1)

# Path: habitat/views/utils.py
# def validate_doc(data, schema):
#     """Validate *data* against *schema*, raising descriptive errors"""
#     v = Validator()
#     errors = list(v.iter_errors(data, schema))
#     if errors:
#         errors = ', '.join((str(error) for error in errors))
#         raise ForbiddenError("Validation errors: {0}".format(errors))
#     _validate_formats(data, schema) 
# 
# def read_json_schema(schemaname):
#     mypath = os.path.dirname(inspect.getfile(inspect.currentframe()))
#     path = os.path.join(mypath, "..", "..", "couchdb", "schemas", schemaname)
#     with open(path) as f:
#         schema = json.load(f)
#     return schema
# 
# Path: habitat/views/utils.py
# def only_validates(doc_type):
#     def decorator(func):
#         def wrapped(new, old, userctx, secobj):
#             new_type = new.get("type", None)
#             new_deleted = new.get("_deleted", False)
#             if old:
#                 old_type = old.get("type", None)
#             else:
#                 old_type = None
# 
#             # sanity checks
#             if old_type is None:
#                 assert old == {} or old is None
#             if new_deleted:
#                 assert new_type is None
# 
#             if new_type == doc_type and old_type in [None, doc_type]:
#                 # new doc, or modified doc of correct type. validate:
#                 return func(new, old, userctx, secobj)
# 
#             elif new_deleted and old_type == doc_type:
#                 # deletion is managed by habitat.validate
#                 return
# 
#             elif new_type == doc_type or old_type == doc_type:
#                 # one or the other types match but not both, and not a new or deleted doc.
#                 raise ForbiddenError("You cannot change the type of a doc")
# 
#             else:
#                 # other type: not our business
#                 return
# 
#         # Be a well behaved decorator!
#         wrapped.__name__ = func.__name__
#         wrapped.__doc__ = func.__doc__
#         wrapped.__dict__.update(func.__dict__)
# 
#         return wrapped
#     return decorator


# Path: habitat/views/flight.py
from couch_named_python import ForbiddenError, UnauthorizedError, version
from strict_rfc3339 import rfc3339_to_timestamp
from .utils import validate_doc, read_json_schema
from .utils import only_validates

# Copyright 2011, 2012 (C) Adam Greig
#
# This file is part of habitat.
#
# habitat is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# habitat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License

"""
Functions for the flight design document.

Contains schema validation and views by flight launch time, window end time and
payload name and window end time.
"""


schema = None

@version(1)

@only_validates("flight")