#coding=utf-8
from django import template
from django.core.urlresolvers import reverse
from django.utils.translation import ugettext as _
from django.template import TemplateSyntaxError
from elephantblog.models import Entry
from django.core.exceptions import FieldError
from django.views.generic import list_detail

register = template.Library()
""" These tags require Django >=1.3 and request context """

@register.simple_tag(takes_context=True)
def get_entries(context, limit):
    try:
        language_code = context['request']._feincms_page.language
        queryset = Entry.objects.active().filter(language=language_code)
    except (AttributeError, FieldError):
        queryset = Entry.objects.active()
    if limit:
        queryset = queryset[:limit]
    context['entries'] = queryset
    return ''


@register.simple_tag(takes_context=True)
def get_frontpage(context, category=None):
    try:
        language_code = context['request']._feincms_page.language
        queryset = Entry.objects.active().filter(language=language_code, status__gt=50)
    except (AttributeError, FieldError):
        queryset = Entry.objects.active()
    if category:
        queryset = queryset.filter(categories__translations__title=category)
    context['entries'] = queryset
    return ''

""" Legacy Tags for Django 1.2 """

class GetEntries(template.Node):
    def __init__(self, limit):
        self.limit = limit
    def render(self, context):
        try:
            language_code = context['request']._feincms_page.language
            queryset = Entry.objects.active().filter(language=language_code)
        except (AttributeError, FieldError):
            queryset = Entry.objects.active()
        if self.limit:
            queryset = queryset[:self.limit]
        context['entries'] = queryset
        return ''


def do_get_entries(parser, token):
    try:
        tag_name, limit = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires exactly one argument" % token.contents.split()[0]
    return GetEntries(limit)
register.tag('get_entries_legacy', do_get_entries)
from django import template
from django.core.exceptions import FieldError

from feincms.translations import short_language_code

from elephantblog.models import Entry


register = template.Library()


@register.simple_tag(takes_context=True)
def get_entries(context, limit):
    try:
        queryset = Entry.objects.active().filter(language=short_language_code())
    except (AttributeError, FieldError):
        queryset = Entry.objects.active()

    if limit:
        queryset = queryset[:limit]

    context['entries'] = queryset
    return u''


@register.simple_tag(takes_context=True)
def get_frontpage(context, category=None):
    try:
        queryset = Entry.objects.active().filter(language=short_language_code(),
            status=Entry.FRONT_PAGE)
    except (AttributeError, FieldError):
        queryset = Entry.objects.active()

    if category:
        queryset = queryset.filter(categories__translations__title=category).distinct()

    context['entries'] = queryset
    return ''
Refactor blog_widgets a bit