# Path: habitat/views/utils.py
# def must_be_admin(user,
#                   msg="Only server administrators may edit this document."):
#     """Raise UnauthorizedError if the user is not an admin"""
#     try:
#         if '_admin' not in user['roles']:
#             raise UnauthorizedError(msg)
#     except (KeyError, TypeError):
#         raise UnauthorizedError(msg)
# 
# 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) 
# 
# Path: habitat/views/utils.py
# 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
# 
# 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 version
from strict_rfc3339 import rfc3339_to_timestamp
from .utils import must_be_admin, validate_doc
from .utils import read_json_schema, only_validates
# 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
# along with habitat.  If not, see <http://www.gnu.org/licenses/>.

"""
Functions for the listener_telemetry design document.

Contains schema validation and a view by creation time and callsign.
"""


schema = None

@version(1)
@only_validates("listener_telemetry")
def validate(new, old, userctx, secobj):
    """
    Only allow admins to edit/delete and validate the document against the
    schema for listener_telemetry documents.
    """
    if old:

# Path: habitat/views/utils.py
# def must_be_admin(user,
#                   msg="Only server administrators may edit this document."):
#     """Raise UnauthorizedError if the user is not an admin"""
#     try:
#         if '_admin' not in user['roles']:
#             raise UnauthorizedError(msg)
#     except (KeyError, TypeError):
#         raise UnauthorizedError(msg)
# 
# 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) 
# 
# Path: habitat/views/utils.py
# 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
# 
# 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/listener_telemetry.py
from couch_named_python import version
from strict_rfc3339 import rfc3339_to_timestamp
from .utils import must_be_admin, validate_doc
from .utils import read_json_schema, only_validates

# 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
# along with habitat.  If not, see <http://www.gnu.org/licenses/>.

"""
Functions for the listener_telemetry design document.

Contains schema validation and a view by creation time and callsign.
"""


schema = None

@version(1)
@only_validates("listener_telemetry")
def validate(new, old, userctx, secobj):
    """
    Only allow admins to edit/delete and validate the document against the
    schema for listener_telemetry documents.
    """
    if old:

        must_be_admin(userctx)