Merge pull request #89 from miurahr/sponsorship-i18n-benefit

i18n sponsor fields
This commit is contained in:
Patrick Altman 2015-08-03 07:59:09 -05:00
commit c161f55c43
2 changed files with 57 additions and 3 deletions

View file

@ -1,5 +1,6 @@
import datetime
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.db import models
@ -144,9 +145,17 @@ post_save.connect(_check_level_change, sender=Sponsor)
BENEFIT_TYPE_CHOICES = [
("text", "Text"),
("richtext", "Rich Text"),
("file", "File"),
("weblogo", "Web Logo"),
("simple", "Simple")
("simple", "Simple"),
("option", "Option")
]
CONTENT_TYPE_CHOICES = [
("simple", "Simple"),
] + [
("listing_text_%s" % lang, "Listing Text (%s)" % label) for lang, label in settings.LANGUAGES
]
@ -154,8 +163,10 @@ class Benefit(models.Model):
name = models.CharField(_("name"), max_length=100)
description = models.TextField(_("description"), blank=True)
type = models.CharField(_("type"), choices=BENEFIT_TYPE_CHOICES, max_length=10,
default="simple")
type = models.CharField(_("type"), choices=BENEFIT_TYPE_CHOICES,
max_length=10, default="simple")
content_type = models.CharField(_("content type"), choices=CONTENT_TYPE_CHOICES,
max_length=20, default="simple")
def __unicode__(self):
return self.name

View file

@ -1,4 +1,5 @@
from django import template
from django.template.defaultfilters import linebreaks, urlize
from symposion.conference.models import current_conference
from symposion.sponsorship.models import Sponsor, SponsorLevel
@ -75,3 +76,45 @@ def sponsor_levels(parser, token):
{% sponsor_levels as levels %}
"""
return SponsorLevelNode.handle_token(parser, token)
class LocalizedTextNode(template.Node):
@classmethod
def handle_token(cls, parser, token):
bits = token.split_contents()
if len(bits) == 3:
return cls(bits[2], bits[1][1:-1])
elif len(bits) == 5 and bits[-2] == "as":
return cls(bits[2], bits[1][1:-1], bits[4])
else:
raise template.TemplateSyntaxError("%r takes 'as var'" % bits[0])
def __init__(self, sponsor, content_type, context_var=None):
self.sponsor_var = template.Variable(sponsor)
self.content_type = content_type
self.content_var = context_var
def render(self, context):
s = ''
try:
sponsor = self.sponsor_var.resolve(context)
content_type = '%s_%s' % (self.content_type, context['request'].LANGUAGE_CODE)
texts = sponsor.sponsor_benefits.filter(benefit__content_type=content_type)
if texts.count() > 0:
s = linebreaks(urlize(texts[0].text, autoescape=True))
if self.content_var:
context[self.content_var] = s
s = ''
except:
pass
return s
@register.tag
def localized_text(parser, token):
"""
{% localized_text "content_type" sponsor %}
{% localized_text "content_type" sponsor as localized_text %}
"""
return LocalizedTextNode.handle_token(parser, token)