# Path: weckan/model.py
# DB = meta.Session
# class Package(CkanPackage):
# class Tag(CkanTag):
# class User(CkanUser):
#     def get_url(self, ctx, *path, **query):
#     def display_name(self):
#     def active_resources(self):
#     def slug_to_name(cls, value, state = None):
#     def organizations(self):


from weckan.model import User, Member, Role, Group, Package, meta
        Member.capacity.in_(['admin', 'editor']),
        Member.group_id == dataset.owner_org,
        Member.state == 'active',
        Member.table_id == user.id,
        Member.table_name == 'user',
    )
    return query.count() > 0


def can_edit_org(user, organization):
    '''Returns True if a given user can edit a given organization'''
    if user is None:
        return False
    if user.sysadmin:
        return True

    query = DB.query(Member).filter(
        Member.capacity == 'admin',
        Member.group_id == organization.id,
        Member.state == 'active',
        Member.table_id == user.id,
        Member.table_name == 'user',
    )
    return query.count() > 0


def get_role_for(user, target):
    if user is None:
        return None
    if user.sysadmin:

# Path: weckan/model.py
# DB = meta.Session
# class Package(CkanPackage):
# class Tag(CkanTag):
# class User(CkanUser):
#     def get_url(self, ctx, *path, **query):
#     def display_name(self):
#     def active_resources(self):
#     def slug_to_name(cls, value, state = None):
#     def organizations(self):


# Path: weckan/auth.py
from weckan.model import User, Member, Role, Group, Package, meta

        Member.capacity.in_(['admin', 'editor']),
        Member.group_id == dataset.owner_org,
        Member.state == 'active',
        Member.table_id == user.id,
        Member.table_name == 'user',
    )
    return query.count() > 0


def can_edit_org(user, organization):
    '''Returns True if a given user can edit a given organization'''
    if user is None:
        return False
    if user.sysadmin:
        return True

    query = DB.query(Member).filter(
        Member.capacity == 'admin',
        Member.group_id == organization.id,
        Member.state == 'active',
        Member.table_id == user.id,
        Member.table_name == 'user',
    )
    return query.count() > 0


def get_role_for(user, target):
    if user is None:
        return None
    if user.sysadmin:

        return Role.ADMIN