# 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)


from couch_named_python import ForbiddenError, version
from .utils import must_be_admin
# 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 core habitat design document.

Contains a validation function that applies to every document.
"""


allowed_types = set(
    ("flight", "listener_information", "listener_telemetry",
        "payload_telemetry", "payload_configuration"))

@version(1)
def validate(new, old, userctx, secobj):
    """
    Core habitat validation function.

    * Prevent deletion by anyone except administrators.
    * Prevent documents without a type.
    * Prevent documents whose type is invalid.
    * Prevent changing document type.

    """
    if '_deleted' in new:

# 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)


# Path: habitat/views/habitat.py
from couch_named_python import ForbiddenError, version
from .utils import must_be_admin

# 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 core habitat design document.

Contains a validation function that applies to every document.
"""


allowed_types = set(
    ("flight", "listener_information", "listener_telemetry",
        "payload_telemetry", "payload_configuration"))

@version(1)
def validate(new, old, userctx, secobj):
    """
    Core habitat validation function.

    * Prevent deletion by anyone except administrators.
    * Prevent documents without a type.
    * Prevent documents whose type is invalid.
    * Prevent changing document type.

    """
    if '_deleted' in new:

        must_be_admin(userctx, "Only administrators may delete documents.")