<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def assert_allowed(request, level, pid):
 """Assert that one or more subjects are allowed to perform action on object. Raise NotAuthorized if object exists and subject is not allowed. Raise NotFound if object does not exist. """

    if not d1_gmn.app.models.ScienceObject.objects.filter(pid__did=pid).exists():
        raise d1_common.types.exceptions.NotFound(
            0,
            'Attempted to perform operation on non-existing object. pid="{}"'.format(
                pid
            ),
        )
    if not is_allowed(request, level, pid):
        raise d1_common.types.exceptions.NotAuthorized(
            0,
            'Operation is denied. level="{}", pid="{}", active_subjects="{}"'.format(
                level_to_action(level), pid, format_active_subjects(request)
            ),
        )