added sponsorship template tags and template fragments; improved admin

This commit is contained in:
James Tauber 2012-02-05 17:13:06 -05:00
parent 64569bccc7
commit e4e7b890a2
9 changed files with 99 additions and 21 deletions

View file

@ -3,5 +3,5 @@ from django.contrib import admin
from sponsorship.models import SponsorLevel, Sponsor
admin.site.register(SponsorLevel)
admin.site.register(Sponsor)
admin.site.register(SponsorLevel, list_display=("order", "name"))
admin.site.register(Sponsor, list_display=("name", "level", "added", "active"), list_filter = ("level", ))

View file

@ -15,6 +15,9 @@ class SponsorLevel(models.Model):
def __unicode__(self):
return self.name
def sponsors(self):
return self.sponsor_set.filter(active=True).order_by("added")
class Sponsor(models.Model):

View file

@ -1,6 +1,6 @@
from django import template
from sponsorship.models import Sponsor
from sponsorship.models import Sponsor, SponsorLevel
register = template.Library()
@ -11,33 +11,62 @@ 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])
if len(bits) == 3 and bits[1] == "as":
return cls(bits[2])
elif len(bits) == 4 and bits[2] == "as":
return cls(bits[3], bits[1])
else:
raise template.TemplateSyntaxError("%r takes 'as var' or 'level as var'" % bits[0])
def __init__(self, level, context_var):
self.level = template.Variable(level)
def __init__(self, context_var, level=None):
if level:
self.level = template.Variable(level)
else:
self.level = None
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"
)
if self.level:
level = self.level.resolve(context)
queryset = Sponsor.objects.filter(level__name__iexact = level, active = True).order_by("added")
else:
queryset = Sponsor.objects.filter(active = True).order_by("level__order", "added")
context[self.context_var] = queryset
return u""
class SponsorLevelNode(template.Node):
@classmethod
def handle_token(cls, parser, token):
bits = token.split_contents()
if len(bits) == 3 and bits[1] == "as":
return cls(bits[2])
else:
raise template.TemplateSyntaxError("%r takes 'as var'" % bits[0])
def __init__(self, context_var):
self.context_var = context_var
def render(self, context):
context[self.context_var] = SponsorLevel.objects.all()
return u""
@register.tag
def sponsors(parser, token):
"""
{% sponsors "gold" as sponsors %}
{% sponsors as all_sponsors %}
or
{% sponsors "gold" as gold_sponsors %}
"""
return SponsorsNode.handle_token(parser, token)
@register.tag
def sponsor_levels(parser, token):
"""
{% sponsor_levels as levels %}
"""
return SponsorLevelNode.handle_token(parser, token)

View file

@ -5,3 +5,6 @@
# Put project-specific requirements here.
# See http://pip-installer.org/requirement-format.html for more information.
PIL==1.1.7
easy-thumbnails==1.0-alpha-21

View file

@ -157,6 +157,7 @@ INSTALLED_APPS = [
"timezones",
"emailconfirmation",
"metron",
"easy_thumbnails",
# Pinax
"pinax.apps.account",
@ -164,6 +165,7 @@ INSTALLED_APPS = [
# project
"about",
"sponsorship",
]
FIXTURE_DIRS = [

View file

@ -0,0 +1,11 @@
{% load sponsorship_tags %}
{% sponsor_levels as levels %}
<div class="sponsor-list">
{% for level in levels %}
<h3>{{ level.name }}</h3>
{% for sponsor in level.sponsors %}
{% include "sponsorship/_sponsor_link.html" with sponsor=sponsor %}
{% endfor %}
{% endfor %}
</div>

View file

@ -0,0 +1,10 @@
{% load thumbnail %}
{% spaceless %}
<a href="{{ sponsor.external_url }}">
{% if dimensions %}
<img src="{% thumbnail sponsor.logo dimensions %}" alt="{{ sponsor.name }}" />
{% else %}
<img src="{% thumbnail sponsor.logo '150x150' %}" alt="{{ sponsor.name }}" />
{% endif %}
</a>
{% endspaceless %}

View file

@ -0,0 +1,13 @@
{% load sponsorship_tags %}
{% sponsor_levels as levels %}
<div class="sponsor-list">
{% for level in levels %}
<h3>{{ level.name }}</h3>
{% for sponsor in level.sponsors %}
<div>
{% include "sponsorship/_sponsor_link.html" with sponsor=sponsor %}
</div>
{% endfor %}
{% endfor %}
</div>

View file

@ -0,0 +1,7 @@
{% load sponsorship_tags %}
{% sponsors as sponsors %}
<div class="sponsor-wall">
{% for sponsor in sponsors %}
{% include "sponsorship/_sponsor_link.html" with sponsor=sponsor %}
{% endfor %}
</div>