<commit_before>from channelguide.guide.models import (Category, Channel, Language, Tag, Rating,
                                       User)
from channelguide.guide import recommendations
from channelguide.guide import search as search_mod
from channelguide.cache import client
import operator

def login(connection, id):
    try:
        user = User.query(id=id).get(connection)
    except LookupError:
        return None
    return user

def get_channel(connection, id):
    return Channel.get(connection, id, join=['categories', 'tags', 'items',
        'owner', 'language', 'secondary_languages'])

def get_channel_by_url(connection, url):
    return Channel.query(url=url).join('categories', 'tags',
                                       'items', 'owner',
                                       'language',
                                       'secondary_languages').get(
        connection)

def get_channels(connection, filter, value, sort=None, limit=None, offset=None):
    query = Channel.query_approved()
    join = None
    if filter == 'category':
        try:
            category_id = Category.query(name=value).get(connection).id
        except LookupError:
            return []
        join = 'categories'
        query.join(join)
        query.joins[join].where(id=category_id)
    elif filter == 'tag':
        try:
            tag_id = Tag.query(name=value).get(connection).id
        except LookupError:
            return []
        join = 'tags'
        query.join(join)
        query.joins[join].where(id=tag_id)
    elif filter == 'language':
        try:
            language_id = Language.query(name=value).get(connection).id
        except LookupError:
            return []
        query.where((Channel.c.primary_language_id == language_id) |
                Language.secondary_language_exists_where(language_id))
    elif filter == 'name':
        if value:
            query.where(Channel.c.name.like(value + '%'))
    else:
        return []
    if sort is not None and sort[0] == '-':
        desc = True
        sort = sort[1:]
    else:
        desc = False
    if sort in ('name', 'id'):
        query.order_by(sort, desc=desc)
    elif sort == 'age':
        query.order_by('approved_at', desc=desc)
    elif sort == 'popular':
        query.load('subscription_count_month')
        query.order_by('subscription_count_month', desc=desc)
    elif sort == 'rating':
        query.join('rating')
        query.order_by(query.joins['rating'].c.average, desc=desc)
    else: # default to name
        query.order_by('name')
    if limit is None:
        limit = 20
    if limit > 100:
        limit = 100
    if offset is None:
        offset = 0
    query.limit(limit).offset(offset)
    results = query.execute(connection)
    if join:
        for result in results:
            delattr(result, join)
    return results

def search(connection, terms):
    query = search_mod.search_channels(terms)
    return query.execute(connection)

def get_rating(connection, user, channel):<commit_msg>don't be ambiguous with the sort name<commit_after>from channelguide.guide.models import (Category, Channel, Language, Tag, Rating,
                                       User)
from channelguide.guide import recommendations
from channelguide.guide import search as search_mod
from channelguide.cache import client
import operator

def login(connection, id):
    try:
        user = User.query(id=id).get(connection)
    except LookupError:
        return None
    return user

def get_channel(connection, id):
    return Channel.get(connection, id, join=['categories', 'tags', 'items',
        'owner', 'language', 'secondary_languages'])

def get_channel_by_url(connection, url):
    return Channel.query(url=url).join('categories', 'tags',
                                       'items', 'owner',
                                       'language',
                                       'secondary_languages').get(
        connection)

def get_channels(connection, filter, value, sort=None, limit=None, offset=None):
    query = Channel.query_approved()
    join = None
    if filter == 'category':
        try:
            category_id = Category.query(name=value).get(connection).id
        except LookupError:
            return []
        join = 'categories'
        query.join(join)
        query.joins[join].where(id=category_id)
    elif filter == 'tag':
        try:
            tag_id = Tag.query(name=value).get(connection).id
        except LookupError:
            return []
        join = 'tags'
        query.join(join)
        query.joins[join].where(id=tag_id)
    elif filter == 'language':
        try:
            language_id = Language.query(name=value).get(connection).id
        except LookupError:
            return []
        query.where((Channel.c.primary_language_id == language_id) |
                Language.secondary_language_exists_where(language_id))
    elif filter == 'name':
        if value:
            query.where(Channel.c.name.like(value + '%'))
    else:
        return []
    if sort is not None and sort[0] == '-':
        desc = True
        sort = sort[1:]
    else:
        desc = False
    if sort in ('name', 'id'):
        query.order_by(getattr(Channel.c, sort), desc=desc)
    elif sort == 'age':
        query.order_by(Channel.c.approved_at, desc=desc)
    elif sort == 'popular':
        query.load('subscription_count_month')
        query.order_by('subscription_count_month', desc=desc)
    elif sort == 'rating':
        query.join('rating')
        query.order_by(query.joins['rating'].c.average, desc=desc)
    else: # default to name
        query.order_by(Channel.c.name)
    if limit is None:
        limit = 20
    if limit > 100:
        limit = 100
    if offset is None:
        offset = 0
    query.limit(limit).offset(offset)
    results = query.execute(connection)
    if join:
        for result in results:
            delattr(result, join)
    return results

def search(connection, terms):
    query = search_mod.search_channels(terms)
    return query.execute(connection)

def get_rating(connection, user, channel):
{'fraction_non_alphanumeric': 0.11294540699575024, 'fraction_numerical': 0.0035959463877084014, 'mean_word_length': 2.246153846153846, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '781256', 'n_tokens_mistral': 1743, 'n_tokens_neox': 1694, 'n_words': 408}