from django.template import Library, Node
from g43exercise.models import Article 
from django.utils import timezone

import random

register = Library()

@register.assignment_tag
def get_random_article():
	# Usage Example: get_random_article as article_preview
	# Determine how many articles in the database are not future dated
	# and use that count to select from a list of present/past dated articles
	# one article for output
	count_of_present_articles = Article.objects.exclude(pub_date__gt=timezone.now()).count()
	random_number = random.randint(0, count_of_present_articles-1)
	random_present_article = Article.objects.exclude(pub_date__gt=timezone.now())[random_number]
	return random_present_article

@register.assignment_tag
def get_random_article_list(list_size):
	# Usage Example: get_random_article_list 4 as article_set
	# Depending on the number given to the method, create a list of
	# articles generated by the get_random_article method
	article_list = []
	for iter in range(list_size):
		article_list.append(get_random_article())
	return article_list