from django.db.models import get_model
from django import template
from django.contrib.comments.models import Comment, FreeComment
from coltrane.models import Entry, Link


register = template.Library()


class LatestFeaturedNode(template.Node):
    def __init__(self, varname):
        self.varname = varname
    
    def render(self, context):
        context[self.varname] = Entry.live.latest_featured()
        return ''


def do_latest_featured(parser, token):
    """
    Retrieves the latest featured Entry and stores it in a specified
    context variable.
    
    Syntax::
    
        {% get_latest_featured_entry as [varname] %}
    
    Example::
    
        {% get_latest_featured_entry as latest_featured_entry %}
    
    """
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError("'%s' tag takes two arguments" % bits[0])
    if bits[1] != 'as':
        raise template.TemplateSyntaxError("first argument to '%s' tag must be 'as'" % bits[0])
    return LatestFeaturedNode(bits[2])

register.tag('get_latest_featured_entry', do_latest_featured)

from django.db.models import get_model
from django import template
from django.contrib.comments.models import Comment, FreeComment
from template_utils.templatetags.generic_content import GenericContentNode

from coltrane.models import Entry, Link


register = template.Library()


class LatestFeaturedNode(GenericContentNode):
    def _get_query_set(self):
        if self._queryset is not None:
            self._queryset = self._queryset.filter(featured__exact=True)


def do_featured_entries(parser, token):
    """
    Retrieves the latest ``num`` featured entries and stores them in a
    specified context variable.
    
    Syntax::
    
        {% get_featured_entries [num] as [varname] %}
    
    Example::
    
        {% get_featured_entries 5 as featured_entries %}
    
    """
    bits = token.contents.split()
    if len(bits) != 4:
        raise template.TemplateSyntaxError("'%s' tag takes three arguments" % bits[0])
    if bits[2] != 'as':
        raise template.TemplateSyntaxError("second argument to '%s' tag must be 'as'" % bits[0])
    return LatestFeaturedNode('coltrane.entry', bits[1], bits[3])

def do_featured_entry(parser, token):
    """
    Retrieves the latest featured Entry and stores it in a specified
    context variable.
    
    Syntax::
    
        {% get_featured_entry as [varname] %}
    
    Example::
    
        {% get_featured_entry as featured_entry %}
    
    """
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError("'%s' tag takes two arguments" % bits[0])
    if bits[1] != 'as':
        raise template.TemplateSyntaxError("first argument to '%s' tag must be 'as'" % bits[0])
    return LatestFeaturedNode('coltrane.entry', 1, bits[2])

register.tag('get_featured_entries', do_featured_entries)
register.tag('get_featured_entry', do_featured_entry)

Refactor LatestFeaturedNode to use GenericContentNode and accept a configurable number of entries to fetch
Refactor LatestFeaturedNode to use GenericContentNode and accept a configurable number of entries to fetch


git-svn-id: 9770886a22906f523ce26b0ad22db0fc46e41232@54 5f8205a5-902a-0410-8b63-8f478ce83d95

  from django.db.models import get_model
  from django import template
  from django.contrib.comments.models import Comment, FreeComment
+ from template_utils.templatetags.generic_content import GenericContentNode
+ 
  from coltrane.models import Entry, Link
  
  
  register = template.Library()
  
  
- class LatestFeaturedNode(template.Node):
+ class LatestFeaturedNode(GenericContentNode):
+     def _get_query_set(self):
+         if self._queryset is not None:
+             self._queryset = self._queryset.filter(featured__exact=True)
-     def __init__(self, varname):
-         self.varname = varname
-     
-     def render(self, context):
-         context[self.varname] = Entry.live.latest_featured()
-         return ''
  
  
+ def do_featured_entries(parser, token):
+     """
+     Retrieves the latest ``num`` featured entries and stores them in a
+     specified context variable.
+     
+     Syntax::
+     
+         {% get_featured_entries [num] as [varname] %}
+     
+     Example::
+     
+         {% get_featured_entries 5 as featured_entries %}
+     
+     """
+     bits = token.contents.split()
+     if len(bits) != 4:
+         raise template.TemplateSyntaxError("'%s' tag takes three arguments" % bits[0])
+     if bits[2] != 'as':
+         raise template.TemplateSyntaxError("second argument to '%s' tag must be 'as'" % bits[0])
+     return LatestFeaturedNode('coltrane.entry', bits[1], bits[3])
+ 
- def do_latest_featured(parser, token):
+ def do_featured_entry(parser, token):
      """
      Retrieves the latest featured Entry and stores it in a specified
      context variable.
      
      Syntax::
      
-         {% get_latest_featured_entry as [varname] %}
+         {% get_featured_entry as [varname] %}
      
      Example::
      
-         {% get_latest_featured_entry as latest_featured_entry %}
+         {% get_featured_entry as featured_entry %}
      
      """
      bits = token.contents.split()
      if len(bits) != 3:
          raise template.TemplateSyntaxError("'%s' tag takes two arguments" % bits[0])
      if bits[1] != 'as':
          raise template.TemplateSyntaxError("first argument to '%s' tag must be 'as'" % bits[0])
-     return LatestFeaturedNode(bits[2])
+     return LatestFeaturedNode('coltrane.entry', 1, bits[2])
  
+ register.tag('get_featured_entries', do_featured_entries)
- register.tag('get_latest_featured_entry', do_latest_featured)
+ register.tag('get_featured_entry', do_featured_entry)
  
Refactor LatestFeaturedNode to use GenericContentNode and accept a configurable number of entries to fetch
## Code Before:
from django.db.models import get_model
from django import template
from django.contrib.comments.models import Comment, FreeComment
from coltrane.models import Entry, Link


register = template.Library()


class LatestFeaturedNode(template.Node):
    def __init__(self, varname):
        self.varname = varname
    
    def render(self, context):
        context[self.varname] = Entry.live.latest_featured()
        return ''


def do_latest_featured(parser, token):
    """
    Retrieves the latest featured Entry and stores it in a specified
    context variable.
    
    Syntax::
    
        {% get_latest_featured_entry as [varname] %}
    
    Example::
    
        {% get_latest_featured_entry as latest_featured_entry %}
    
    """
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError("'%s' tag takes two arguments" % bits[0])
    if bits[1] != 'as':
        raise template.TemplateSyntaxError("first argument to '%s' tag must be 'as'" % bits[0])
    return LatestFeaturedNode(bits[2])

register.tag('get_latest_featured_entry', do_latest_featured)

## Instruction:
Refactor LatestFeaturedNode to use GenericContentNode and accept a configurable number of entries to fetch
## Code After:
from django.db.models import get_model
from django import template
from django.contrib.comments.models import Comment, FreeComment
from template_utils.templatetags.generic_content import GenericContentNode

from coltrane.models import Entry, Link


register = template.Library()


class LatestFeaturedNode(GenericContentNode):
    def _get_query_set(self):
        if self._queryset is not None:
            self._queryset = self._queryset.filter(featured__exact=True)


def do_featured_entries(parser, token):
    """
    Retrieves the latest ``num`` featured entries and stores them in a
    specified context variable.
    
    Syntax::
    
        {% get_featured_entries [num] as [varname] %}
    
    Example::
    
        {% get_featured_entries 5 as featured_entries %}
    
    """
    bits = token.contents.split()
    if len(bits) != 4:
        raise template.TemplateSyntaxError("'%s' tag takes three arguments" % bits[0])
    if bits[2] != 'as':
        raise template.TemplateSyntaxError("second argument to '%s' tag must be 'as'" % bits[0])
    return LatestFeaturedNode('coltrane.entry', bits[1], bits[3])

def do_featured_entry(parser, token):
    """
    Retrieves the latest featured Entry and stores it in a specified
    context variable.
    
    Syntax::
    
        {% get_featured_entry as [varname] %}
    
    Example::
    
        {% get_featured_entry as featured_entry %}
    
    """
    bits = token.contents.split()
    if len(bits) != 3:
        raise template.TemplateSyntaxError("'%s' tag takes two arguments" % bits[0])
    if bits[1] != 'as':
        raise template.TemplateSyntaxError("first argument to '%s' tag must be 'as'" % bits[0])
    return LatestFeaturedNode('coltrane.entry', 1, bits[2])

register.tag('get_featured_entries', do_featured_entries)
register.tag('get_featured_entry', do_featured_entry)
