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


import logging
from sqlalchemy import orm
from sqlalchemy.sql import func, desc, null, and_, distinct
from ckanext.etalab.model import CertifiedPublicService
from weckan import model
from weckan.model import Package, Member, Group, User, Related, Role
from weckan.model import RelatedDataset, PackageRelationship, PackageRole
FORK_COMMENT = 'Fork'


def datasets(private=False):
    '''Query dataset with their organization'''
    query = DB.query(Package, Group)
    query = query.outerjoin(Group, Group.id == Package.owner_org)
    query = query.outerjoin(CertifiedPublicService)
    query = query.filter(Package.state == 'active')
    if private:
        query = query.filter(Package.private == True)
    else:
        query = query.filter(~Package.private)
    query = query.options(orm.contains_eager(Group.certified_public_service))
    return query


def organizations_and_counters():
    '''Query organizations with their counters'''
    query = DB.query(Group,
        func.count(distinct(Package.id)).label('nb_datasets'),
        func.count(distinct(Member.id)).label('nb_members')
    )
    query = query.outerjoin(CertifiedPublicService)
    query = query.outerjoin(Package, and_(
        Group.id == Package.owner_org,
        ~Package.private,
        Package.state == 'active',
    ))
    query = query.outerjoin(Member, and_(

# 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/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/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/queries.py
import logging
from sqlalchemy import orm
from sqlalchemy.sql import func, desc, null, and_, distinct
from ckanext.etalab.model import CertifiedPublicService
from weckan import model
from weckan.model import Package, Member, Group, User, Related, Role
from weckan.model import RelatedDataset, PackageRelationship, PackageRole

FORK_COMMENT = 'Fork'


def datasets(private=False):
    '''Query dataset with their organization'''
    query = DB.query(Package, Group)
    query = query.outerjoin(Group, Group.id == Package.owner_org)
    query = query.outerjoin(CertifiedPublicService)
    query = query.filter(Package.state == 'active')
    if private:
        query = query.filter(Package.private == True)
    else:
        query = query.filter(~Package.private)
    query = query.options(orm.contains_eager(Group.certified_public_service))
    return query


def organizations_and_counters():
    '''Query organizations with their counters'''
    query = DB.query(Group,
        func.count(distinct(Package.id)).label('nb_datasets'),
        func.count(distinct(Member.id)).label('nb_members')
    )
    query = query.outerjoin(CertifiedPublicService)
    query = query.outerjoin(Package, and_(
        Group.id == Package.owner_org,
        ~Package.private,
        Package.state == 'active',
    ))
    query = query.outerjoin(Member, and_(

        Member.group_id == Group.id,