# Path: amivapi/utils.py
# @contextmanager
# def admin_permissions():
#     """Switch to a context with admin rights and restore state afterwards.
# 
#     Use as context:
#     >> with admin_rights():
#     >>     do_something()
#     """
#     old_admin = g.get('resource_admin')
#     g.resource_admin = True
# 
#     yield
# 
#     if old_admin is not None:  # None means it wasn't set before..
#         g.resource_admin = old_admin


from eve.methods.delete import deleteitem_internal
from eve.methods.patch import patch_internal
from flask import current_app
from werkzeug.exceptions import NotFound
from amivapi.utils import admin_permissions
"""Delete cascading system.

This adds an option 'cascade_delete' to data_relations in the schema. If it is
set to true, then deleting the referenced object will also delete the
referencing object. If false, the reference will be set to NULL, when the
referenced object is deleted.
"""




def cascade_delete(resource, item):
    """Cascade DELETE.

    Hook to delete all objects, which have the 'cascade_delete' option set
    in the data_relation and relate to the object, which was just deleted.
    """
    domain = current_app.config['DOMAIN']
    deleted_id = item[domain[resource]['id_field']]

    for res, res_domain in domain.items():
        # Filter schema of `res` to get all fields containing references
        # to the resource of the deleted item
        relations = ((field, field_def['data_relation'])
                     for field, field_def in res_domain['schema'].items()
                     if 'data_relation' in field_def and
                     field_def['data_relation'].get('resource') == resource)
        for field, data_relation in relations:
            # All items in `res` with reference to the deleted item
            lookup = {field: deleted_id}

# Path: amivapi/utils.py
# @contextmanager
# def admin_permissions():
#     """Switch to a context with admin rights and restore state afterwards.
# 
#     Use as context:
#     >> with admin_rights():
#     >>     do_something()
#     """
#     old_admin = g.get('resource_admin')
#     g.resource_admin = True
# 
#     yield
# 
#     if old_admin is not None:  # None means it wasn't set before..
#         g.resource_admin = old_admin


# Path: amivapi/cascade.py
from eve.methods.delete import deleteitem_internal
from eve.methods.patch import patch_internal
from flask import current_app
from werkzeug.exceptions import NotFound
from amivapi.utils import admin_permissions

"""Delete cascading system.

This adds an option 'cascade_delete' to data_relations in the schema. If it is
set to true, then deleting the referenced object will also delete the
referencing object. If false, the reference will be set to NULL, when the
referenced object is deleted.
"""




def cascade_delete(resource, item):
    """Cascade DELETE.

    Hook to delete all objects, which have the 'cascade_delete' option set
    in the data_relation and relate to the object, which was just deleted.
    """
    domain = current_app.config['DOMAIN']
    deleted_id = item[domain[resource]['id_field']]

    for res, res_domain in domain.items():
        # Filter schema of `res` to get all fields containing references
        # to the resource of the deleted item
        relations = ((field, field_def['data_relation'])
                     for field, field_def in res_domain['schema'].items()
                     if 'data_relation' in field_def and
                     field_def['data_relation'].get('resource') == resource)
        for field, data_relation in relations:
            # All items in `res` with reference to the deleted item
            lookup = {field: deleted_id}

            with admin_permissions():