django-cms-simple-events / simple_events / templatetags /

from django import template
from models import Event

register = template.Library()

def get_upcoming_events(parser, token):
		{% get_upcoming_events as events %}
		{% get_upcoming_events as events with num=5 days=180 categories=1,13 %}
		5 = number of events
		60 = in the next 60 days
		categories = 
			- Not supplied (None): any category
			- 'none' (string): uncategorized
			- 1,2,3 (string in list): in any of the supplied categories
	bits = token.split_contents()
	if len(bits) == 3:
		# Get all the upcoming events.
		if bits[1] != 'as':
			raise template.TemplateSyntaxError("Second argument to 'get_upcoming_events' must be 'as': {% get_upcoming_events as events %}")
		return UpcomingEventsNode(bits[2])
	if len(bits) > 3:
		# Some options were specified.
		if bits[3] != 'with':
			raise template.TemplateSyntaxError("get_upcoming_events expects paramters if using 'with'")
		if len(bits) < 5:
			raise template.TemplateSyntaxError("get_upcoming_events expects paramters if using 'with'")
		kwargs = {}
		for arg in bits[4:]:
			key, value = arg.split('=')
		return UpcomingEventsNode(bits[2], **kwargs)
class UpcomingEventsNode(template.Node):
	def __init__(self, varname, num=None, days=None, categories=None):
		self.varname = varname
		self.num = num
		self.days = days
		if categories is not None and categories.lower() != 'none':
			self.categories = [ int(c) for c in categories.split(',') ]
			self.categories = categories
	def render(self, context):
		events = Event.on_site.upcoming(self.days)
		if self.categories == 'none':
			# Get only events that have no (null) category.
			# ick...
			event_ids = [ for e in events if e.categories.all().count() ==0 ]
			events = events.filter(id__in=event_ids)
		elif self.categories is not None and isinstance(self.categories, list):
			events = events.filter(categories__id__in=self.categories)
		if self.num is None:
			context[self.varname] = events
			context[self.varname] = events[:int(self.num)]
		return u''
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.