<commit_before>from django import template
from django.core.cache import cache
from django.core.urlresolvers import resolve, reverse, Resolver404
from django.db.models import Count
from articles.models import Article, Tag
from datetime import datetime
import math

register = template.Library()

class GetCategoriesNode(template.Node):
    """
    Retrieves a list of live article tags and places it into the context
    """
    def __init__(self, varname):
        self.varname = varname

    def render(self, context):
        tags = Tag.objects.all()
        context[self.varname] = tags
        return ''

def get_article_tags(parser, token):
    """
    Retrieves a list of live article tags and places it into the context
    """
    args = token.split_contents()
    argc = len(args)

    try:
        assert argc == 3 and args[1] == 'as'
    except AssertionError:
        raise template.TemplateSyntaxError('get_article_tags syntax: {% get_article_tags as varname %}')

    return GetCategoriesNode(args[2])

class GetArticlesNode(template.Node):
    """
    Retrieves a set of article objects.

    Usage::

        {% get_articles 5 as varname %}

        {% get_articles 5 as varname asc %}

        {% get_articles 1 to 5 as varname %}

        {% get_articles 1 to 5 as varname asc %}
    """
    def __init__(self, varname, count=None, start=None, end=None, order='desc'):
        self.count = count
        self.start = start
        self.end = end
        self.order = order
        self.varname = varname.strip()

    def render(self, context):
        # determine the order to sort the articles
        if self.order and self.order.lower() == 'desc':
            order = '-publish_date'
        else:
            order = 'publish_date'

        user = context.get('user', None)

        # get the live articles in the appropriate order
        articles = Article.objects.live(user=user).order_by(order).select_related()

        if self.count:
            # if we have a number of articles to retrieve, pull the first of them
            articles = articles[:int(self.count)]
        else:
            # get a range of articles
            articles = articles[(int(self.start) - 1):int(self.end)]

        # don't send back a list when we really don't need/want one
        if len(articles) == 1 and int(self.count) == 1 and not self.start:
            articles = articles[0]
<commit_msg>#17 - Taking advantage of a short circuit when there's only one article to show<commit_after>from django import template
from django.core.cache import cache
from django.core.urlresolvers import resolve, reverse, Resolver404
from django.db.models import Count
from articles.models import Article, Tag
from datetime import datetime
import math

register = template.Library()

class GetCategoriesNode(template.Node):
    """
    Retrieves a list of live article tags and places it into the context
    """
    def __init__(self, varname):
        self.varname = varname

    def render(self, context):
        tags = Tag.objects.all()
        context[self.varname] = tags
        return ''

def get_article_tags(parser, token):
    """
    Retrieves a list of live article tags and places it into the context
    """
    args = token.split_contents()
    argc = len(args)

    try:
        assert argc == 3 and args[1] == 'as'
    except AssertionError:
        raise template.TemplateSyntaxError('get_article_tags syntax: {% get_article_tags as varname %}')

    return GetCategoriesNode(args[2])

class GetArticlesNode(template.Node):
    """
    Retrieves a set of article objects.

    Usage::

        {% get_articles 5 as varname %}

        {% get_articles 5 as varname asc %}

        {% get_articles 1 to 5 as varname %}

        {% get_articles 1 to 5 as varname asc %}
    """
    def __init__(self, varname, count=None, start=None, end=None, order='desc'):
        self.count = count
        self.start = start
        self.end = end
        self.order = order
        self.varname = varname.strip()

    def render(self, context):
        # determine the order to sort the articles
        if self.order and self.order.lower() == 'desc':
            order = '-publish_date'
        else:
            order = 'publish_date'

        user = context.get('user', None)

        # get the live articles in the appropriate order
        articles = Article.objects.live(user=user).order_by(order).select_related()

        if self.count:
            # if we have a number of articles to retrieve, pull the first of them
            articles = articles[:int(self.count)]
        else:
            # get a range of articles
            articles = articles[(int(self.start) - 1):int(self.end)]

        # don't send back a list when we really don't need/want one
        if len(articles) == 1 and not self.start and int(self.count) == 1:
            articles = articles[0]

{'fraction_non_alphanumeric': 0.09569672131147541, 'fraction_numerical': 0.0069672131147540985, 'mean_word_length': 2.5014347202295553, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6422560', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1328, 'n_words': 465}