<commit_before>import re

from django import template
import django.core.urlresolvers
from django.db import models
from django.utils import dates

from tagging.models import TaggedItem

from blog.models import Post, Category

register = template.Library()

def lit_or_val(var, context):
    """
    Returns a literal string or numeric value, or the value of the Variable resolved from the context
    """
    if var == None:
        return None
    if var[0] == var[-1] and var[0] in ('"', "'"):
        if len(var) < 3:
            return '' # empty string
        else:
            return var[1:-1] # string
    else:
        try:
            return float(var)
        except ValueError:
            return template.Variable(var).resolve(context) # resolve value

class BlogCategoriesNode(template.Node):
    def __init__(self, var_name):
        self.args = dict(var_name=var_name)
    def render(self, context):
        var_name = self.args['var_name']
        context[var_name] = Category.objects.all()
        return ''

class BlogTagPostsNode(template.Node):
    def __init__(self, tag, var_name):
        self.args = dict(tag=tag, var_name=var_name)
    def render(self, context):
        tag = lit_or_val(self.args['tag'], context)
        var_name = self.args['var_name']
        context[var_name] = TaggedItem.objects.get_by_model(Post.objects.get_published_posts(), tag)
        return ''
    
class PostArchiveNode(template.Node):
    def __init__(self, var_name, year=None, month=None):
        self.args = dict(var_name=var_name, year=year, month=month)        
    def render(self, context):
        var_name = self.args['var_name']
        year = lit_or_val(self.args['year'], context)
        month = lit_or_val(self.args['month'], context)
        context[var_name] = Post.objects.get_post_archive(year=year, month=month)
        return ''

def do_get_blog_tag_posts(parser, token):
    """
    Gets the blog Posts for a specified tag and stores it in a context variable.
    
    Usage::

      {% get_blog_tag_posts [tag_name] as [varname] %}
    
    tag should be a variable or a quoted string
    TODO: Use the "for" word...

    Example::
    
      {% get_blog_tag_posts "django" as blog_posts %}
    
    """<commit_msg>Added staff checking and post draft display to template tags<commit_after>import re

from django import template
import django.core.urlresolvers
from django.db import models
from django.utils import dates
from django.template import resolve_variable

from tagging.models import TaggedItem

from blog.models import Post, Category

register = template.Library()

def lit_or_val(var, context):
    """
    Returns a literal string or numeric value, or the value of the Variable resolved from the context
    """
    if var == None:
        return None
    if var[0] == var[-1] and var[0] in ('"', "'"):
        if len(var) < 3:
            return '' # empty string
        else:
            return var[1:-1] # string
    else:
        try:
            return float(var)
        except ValueError:
            return template.Variable(var).resolve(context) # resolve value

class BlogCategoriesNode(template.Node):
    def __init__(self, var_name):
        self.args = dict(var_name=var_name)
    def render(self, context):
        var_name = self.args['var_name']
        context[var_name] = Category.objects.all()
        return ''

class BlogTagPostsNode(template.Node):
    def __init__(self, tag, var_name):
        self.args = dict(tag=tag, var_name=var_name)
    def render(self, context):
        # is this user staff?  Determines published post display
        is_staff = resolve_variable('user', context).is_staff
        tag = lit_or_val(self.args['tag'], context)
        var_name = self.args['var_name']
        if not is_staff:
            model_query = Post.objects.get_published_posts()
        else:
            model_query = Post.objects.all()
        context[var_name] = TaggedItem.objects.get_by_model(model_query, tag)
        return ''
    
class PostArchiveNode(template.Node):
    def __init__(self, var_name, year=None, month=None):
        self.args = dict(var_name=var_name, year=year, month=month)        
    def render(self, context):
        # is this user staff?  Determines published post display
        is_staff = resolve_variable('user', context).is_staff
        var_name = self.args['var_name']
        year = lit_or_val(self.args['year'], context)
        month = lit_or_val(self.args['month'], context)
        context[var_name] = Post.objects.get_post_archive(require_published=(not is_staff), year=year, month=month)
        return ''

def do_get_blog_tag_posts(parser, token):
    """
    Gets the blog Posts for a specified tag and stores it in a context variable.
    
    Usage::

      {% get_blog_tag_posts [tag_name] as [varname] %}
    
    tag should be a variable or a quoted string
    TODO: Use the "for" word...

    Example::
    
      {% get_blog_tag_posts "django" as blog_posts %}
    
    """
{'fraction_non_alphanumeric': 0.10588470776621298, 'fraction_numerical': 0.0024019215372297837, 'mean_word_length': 2.8232593726090283, 'pattern_counts': {'":': 0, '<': 5, '<?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': '3720026', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1489, 'n_words': 409}