# Copyright (c) 2016 Luke San Antonio Bialecki
# All rights reserved.

from . import models
from .models import db


def user_is_participant(user_id, org_id):
    q = db.session.query(models.org_participants).filter_by(
        participant=user_id, org=org_id
    )
    (ret,) = db.session.query(q.exists()).first()
    return ret


def user_is_mod(user_id, org_id):
    q = db.session.query(models.org_mods).filter_by(mod=user_id, org=org_id)
    (ret,) = db.session.query(q.exists()).first()
    return ret

@@ -64,16 +64,47 @@
 erved.%0A%0A
+from . import exceptions as ex%0A
 from . i
@@ -138,16 +138,27 @@
 mport db
+, User, Org
 %0A%0A%0Adef u
@@ -521,31 +521,31 @@
 ists()).first()%0A
-
     return ret%0A
@@ -524,28 +524,493 @@
 s()).first()%0A    return ret%0A
+%0A%0Adef get_field(request, field):%0A    val = request.get_json().get(field, None)%0A    if val is None:%0A        raise ex.MissingFieldError(field)%0A    return val%0A%0A%0Adef get_org_by_id(org_id):%0A    org = Org.query.filter_by(id=org_id).first()%0A    if org is None:%0A        raise ex.OrgNotFound(org_id)%0A    return org%0A%0A%0Adef get_user_by_id(user_id):%0A    user = User.query.filter_by(id=user_id).first()%0A    if user is None:%0A        raise ex.UserNotFound(user_id)%0A    return user%0A

Add helper functions to view_util for better error handling