# 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 = 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)
    return query


def forked_from(dataset):
    '''Get the original dataset from which this dataset is forked'''
    query = PackageRelationship.by_subject(dataset)
    query = query.filter(PackageRelationship.type == 'derives_from')
    query = query.filter(PackageRelationship.comment == FORK_COMMENT)
    return query


def is_fork(dataset):
    '''Is this dataset a fork ?'''
    return forked_from(dataset).count() > 0


def forks(dataset):
    '''Get all the forks froma given dataset'''
    query = PackageRelationship.by_object(dataset)

# 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 = 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)
    return query


def forked_from(dataset):
    '''Get the original dataset from which this dataset is forked'''
    query = PackageRelationship.by_subject(dataset)
    query = query.filter(PackageRelationship.type == 'derives_from')
    query = query.filter(PackageRelationship.comment == FORK_COMMENT)
    return query


def is_fork(dataset):
    '''Is this dataset a fork ?'''
    return forked_from(dataset).count() > 0


def forks(dataset):
    '''Get all the forks froma given dataset'''
    query = PackageRelationship.by_object(dataset)

    query = query.filter(PackageRelationship.type == 'derives_from')