# Path: weckan/wsgihelpers.py
# N_ = lambda message: message
# def bad_request(ctx, **kw):
# def discard_empty_items(data):
# def error(ctx, code, **kw):
# def forbidden(ctx, **kw):
# def handle_cross_origin_resource_sharing(ctx):
# def internal_error(ctx, **kw):
# def method_not_allowed(ctx, **kw):
# def no_content(ctx, headers = None):
# def not_found(ctx, **kw):
# def redirect(ctx, code = 302, location = None, **kw):
# def respond_json(ctx, data, code = None, headers = None, jsonp = None):
# def unauthorized(ctx, **kw):
# 
# Path: weckan/contexts.py
# class Ctx(conv.State):
#     def __init__(self, req = None):
#     def __getattribute__(self, name):
#     def _(self):
#     def blank_req(self, path, environ = None, base_url = None, headers = None, POST = None, **kw):
#     def get_containing(self, name, depth = 0):
#     def get_inherited(self, name, default = UnboundLocalError, depth = 1):
#     def iter(self):
#     def iter_containing(self, name):
#     def iter_inherited(self, name):
#     def lang_del(self):
#     def lang_get(self):
#     def lang_set(self, lang):
#     def new(self, **kwargs):
#     def parent(self):
#     def translator(self):
#     def user_del(self):
#     def user_get(self):
#     def user_set(self, user):
# 
# 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.sql import func, distinct
from weckan import wsgihelpers, contexts
from weckan.model import meta, Tag, PackageTag
# -*- coding: utf-8 -*-
from __future__ import unicode_literals





DB = meta.Session
log = logging.getLogger(__name__)


@wsgihelpers.wsgify
def autocomplete(request):
    context = contexts.Ctx(request)
    pattern = '{0}%'.format(request.params.get('q', ''))
    num = int(request.params.get('num', 0))

    query = DB.query(distinct(func.lower(Tag.name)).label('name'), func.count(PackageTag.package_id).label('total'))
    query = query.join(PackageTag)

# Path: weckan/wsgihelpers.py
# N_ = lambda message: message
# def bad_request(ctx, **kw):
# def discard_empty_items(data):
# def error(ctx, code, **kw):
# def forbidden(ctx, **kw):
# def handle_cross_origin_resource_sharing(ctx):
# def internal_error(ctx, **kw):
# def method_not_allowed(ctx, **kw):
# def no_content(ctx, headers = None):
# def not_found(ctx, **kw):
# def redirect(ctx, code = 302, location = None, **kw):
# def respond_json(ctx, data, code = None, headers = None, jsonp = None):
# def unauthorized(ctx, **kw):
# 
# Path: weckan/contexts.py
# class Ctx(conv.State):
#     def __init__(self, req = None):
#     def __getattribute__(self, name):
#     def _(self):
#     def blank_req(self, path, environ = None, base_url = None, headers = None, POST = None, **kw):
#     def get_containing(self, name, depth = 0):
#     def get_inherited(self, name, default = UnboundLocalError, depth = 1):
#     def iter(self):
#     def iter_containing(self, name):
#     def iter_inherited(self, name):
#     def lang_del(self):
#     def lang_get(self):
#     def lang_set(self, lang):
#     def new(self, **kwargs):
#     def parent(self):
#     def translator(self):
#     def user_del(self):
#     def user_get(self):
#     def user_set(self, user):
# 
# 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/controllers/tags.py
import logging
from sqlalchemy.sql import func, distinct
from weckan import wsgihelpers, contexts
from weckan.model import meta, Tag, PackageTag

# -*- coding: utf-8 -*-
from __future__ import unicode_literals





DB = meta.Session
log = logging.getLogger(__name__)


@wsgihelpers.wsgify
def autocomplete(request):
    context = contexts.Ctx(request)
    pattern = '{0}%'.format(request.params.get('q', ''))
    num = int(request.params.get('num', 0))

    query = DB.query(distinct(func.lower(Tag.name)).label('name'), func.count(PackageTag.package_id).label('total'))
    query = query.join(PackageTag)

    query = query.filter(Tag.name.ilike(pattern))