# 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
    query = query.group_by(Package, Group, CertifiedPublicService)
    if not empties:
        query = query.filter(model.ResourceRevision.current==True)
        query = query.filter(model.ResourceRevision.state=='active')
        query = query.having(func.count(model.ResourceRevision.id) > 0)
    query = query.order_by(desc(func.min(model.PackageRevision.revision_timestamp)))
    return query


def popular_datasets():
    '''Get the ``num`` most popular (ie. with the most related) datasets'''
    query = datasets()
    query = query.outerjoin(RelatedDataset)
    query = query.group_by(Package, Group, CertifiedPublicService)
    query = query.order_by(desc(func.count(RelatedDataset.related_id)))
    return query


def featured_reuses():
    '''Get ``num``featured reuses'''
    query = DB.query(Related)
    query = query.filter(Related.featured > 0)
    query = query.order_by(desc(Related.created))
    return query


def owner(dataset):
    '''Get the user owning a dataset'''
    query = DB.query(User).join(PackageRole)
    query = query.filter(PackageRole.package_id == dataset.id)

# 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

    query = query.group_by(Package, Group, CertifiedPublicService)
    if not empties:
        query = query.filter(model.ResourceRevision.current==True)
        query = query.filter(model.ResourceRevision.state=='active')
        query = query.having(func.count(model.ResourceRevision.id) > 0)
    query = query.order_by(desc(func.min(model.PackageRevision.revision_timestamp)))
    return query


def popular_datasets():
    '''Get the ``num`` most popular (ie. with the most related) datasets'''
    query = datasets()
    query = query.outerjoin(RelatedDataset)
    query = query.group_by(Package, Group, CertifiedPublicService)
    query = query.order_by(desc(func.count(RelatedDataset.related_id)))
    return query


def featured_reuses():
    '''Get ``num``featured reuses'''
    query = DB.query(Related)
    query = query.filter(Related.featured > 0)
    query = query.order_by(desc(Related.created))
    return query


def owner(dataset):
    '''Get the user owning a dataset'''
    query = DB.query(User).join(PackageRole)
    query = query.filter(PackageRole.package_id == dataset.id)

    query = query.filter(PackageRole.role == Role.ADMIN)