initial sponsorship app adapted from DjangoCon

This commit is contained in:
James Tauber 2012-02-05 15:03:30 -05:00
parent 0f6843b08d
commit 46e38bc758
5 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,7 @@
from django.contrib import admin
from symposion.sponsors.models import SponsorLevel, Sponsor
admin.site.register(SponsorLevel)
admin.site.register(Sponsor)

View file

@ -0,0 +1,32 @@
import datetime
from django.db import models
class SponsorLevel(models.Model):
name = models.CharField(max_length=100)
order = models.IntegerField(default=0)
description = models.TextField(blank=True)
class Meta:
ordering = ["order"]
def __unicode__(self):
return self.name
class Sponsor(models.Model):
name = models.CharField(max_length=100)
external_url = models.URLField("external URL")
annotation = models.TextField(blank=True)
contact_name = models.CharField(max_length=100)
contact_email = models.EmailField(u"Contact e\u2011mail")
logo = models.ImageField(upload_to="sponsor_logos/")
level = models.ForeignKey(SponsorLevel)
added = models.DateTimeField(default=datetime.datetime.now)
active = models.BooleanField(default=False)
def __unicode__(self):
return self.name

View file

@ -0,0 +1,43 @@
from django import template
from sponsorship.models import Sponsor
register = template.Library()
class SponsorsNode(template.Node):
@classmethod
def handle_token(cls, parser, token):
bits = token.split_contents()
if len(bits) != 4:
raise template.TemplateSyntaxError("%r takes exactly three arguments "
"(second argument must be 'as')" % bits[0])
if bits[2] != "as":
raise template.TemplateSyntaxError("Second argument to %r must be "
"'as'" % bits[0])
return cls(bits[1], bits[3])
def __init__(self, level, context_var):
self.level = template.Variable(level)
self.context_var = context_var
def render(self, context):
level = self.level.resolve(context)
queryset = Sponsor.objects.filter(
level__name__iexact = level,
active = True
).order_by(
"added"
)
context[self.context_var] = queryset
return u""
@register.tag
def sponsors(parser, token):
"""
{% sponsors "gold" as sponsors %}
"""
return SponsorsNode.handle_token(parser, token)