2012-02-05 20:03:30 +00:00
|
|
|
import datetime
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.urlresolvers import reverse
|
2012-02-05 20:03:30 +00:00
|
|
|
from django.db import models
|
2012-07-18 23:24:46 +00:00
|
|
|
from django.db.models.signals import post_init, post_save
|
2012-02-22 22:35:21 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-02-05 20:03:30 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
2012-05-29 19:08:05 +00:00
|
|
|
from symposion.conference.models import Conference
|
2012-02-06 03:50:47 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
from symposion.sponsorship.managers import SponsorManager
|
|
|
|
|
2012-02-05 20:03:30 +00:00
|
|
|
|
|
|
|
class SponsorLevel(models.Model):
|
|
|
|
|
2012-02-22 22:35:21 +00:00
|
|
|
conference = models.ForeignKey(Conference, verbose_name=_("conference"))
|
|
|
|
name = models.CharField(_("name"), max_length=100)
|
|
|
|
order = models.IntegerField(_("order"), default=0)
|
2012-07-12 19:17:42 +00:00
|
|
|
cost = models.PositiveIntegerField(_("cost"))
|
|
|
|
description = models.TextField(_("description"), blank=True, help_text=_("This is private."))
|
2012-02-05 20:03:30 +00:00
|
|
|
|
|
|
|
class Meta:
|
2012-02-06 03:50:47 +00:00
|
|
|
ordering = ["conference", "order"]
|
2012-02-22 22:35:21 +00:00
|
|
|
verbose_name = _("sponsor level")
|
|
|
|
verbose_name_plural = _("sponsor levels")
|
2012-02-05 20:03:30 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
2012-07-13 12:59:25 +00:00
|
|
|
return self.name
|
2012-02-05 22:13:06 +00:00
|
|
|
|
|
|
|
def sponsors(self):
|
|
|
|
return self.sponsor_set.filter(active=True).order_by("added")
|
2012-02-05 20:03:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Sponsor(models.Model):
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
applicant = models.ForeignKey(User, related_name="sponsorships", verbose_name=_("applicant"), null=True)
|
|
|
|
|
|
|
|
name = models.CharField(_("Sponsor Name"), max_length=100)
|
2012-02-22 22:35:21 +00:00
|
|
|
external_url = models.URLField(_("external URL"))
|
|
|
|
annotation = models.TextField(_("annotation"), blank=True)
|
2012-07-12 19:17:42 +00:00
|
|
|
contact_name = models.CharField(_("Contact Name"), max_length=100)
|
|
|
|
contact_email = models.EmailField(_(u"Contact Email"))
|
2012-02-22 22:35:21 +00:00
|
|
|
level = models.ForeignKey(SponsorLevel, verbose_name=_("level"))
|
|
|
|
added = models.DateTimeField(_("added"), default=datetime.datetime.now)
|
|
|
|
active = models.BooleanField(_("active"), default=False)
|
2012-02-05 20:03:30 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
# Denormalization (this assumes only one logo)
|
|
|
|
sponsor_logo = models.ForeignKey("SponsorBenefit", related_name="+", null=True, blank=True, editable=False)
|
2013-06-11 17:10:29 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
objects = SponsorManager()
|
|
|
|
|
2012-02-05 20:03:30 +00:00
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
2012-02-22 22:35:21 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
verbose_name = _("sponsor")
|
|
|
|
verbose_name_plural = _("sponsors")
|
2012-07-12 19:17:42 +00:00
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
if self.active:
|
|
|
|
return reverse("sponsor_detail", kwargs={"pk": self.pk})
|
|
|
|
return reverse("sponsor_list")
|
2012-07-18 23:23:59 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def website_logo(self):
|
|
|
|
if self.sponsor_logo is None:
|
|
|
|
benefits = self.sponsor_benefits.filter(benefit__type="weblogo", upload__isnull=False)[:1]
|
|
|
|
if benefits.count():
|
|
|
|
if benefits[0].upload:
|
|
|
|
self.sponsor_logo = benefits[0]
|
|
|
|
self.save()
|
|
|
|
return self.sponsor_logo.upload
|
|
|
|
|
|
|
|
@property
|
|
|
|
def listing_text(self):
|
|
|
|
if not hasattr(self, "_listing_text"):
|
|
|
|
self._listing_text = None
|
|
|
|
# @@@ better than hard-coding a pk but still not good
|
|
|
|
benefits = self.sponsor_benefits.filter(benefit__name="Sponsor Description")
|
|
|
|
if benefits.count():
|
|
|
|
self._listing_text = benefits[0].text
|
|
|
|
return self._listing_text
|
|
|
|
|
2012-07-13 12:59:25 +00:00
|
|
|
def reset_benefits(self):
|
|
|
|
"""
|
|
|
|
Reset all benefits for this sponsor to the defaults for their
|
|
|
|
sponsorship level.
|
|
|
|
"""
|
|
|
|
level = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
level = self.level
|
|
|
|
except SponsorLevel.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
allowed_benefits = []
|
|
|
|
if level:
|
|
|
|
for benefit_level in level.benefit_levels.all():
|
|
|
|
# Create all needed benefits if they don't exist already
|
|
|
|
sponsor_benefit, created = SponsorBenefit.objects.get_or_create(
|
|
|
|
sponsor=self, benefit=benefit_level.benefit)
|
|
|
|
|
|
|
|
# and set to default limits for this level.
|
|
|
|
sponsor_benefit.max_words = benefit_level.max_words
|
|
|
|
sponsor_benefit.other_limits = benefit_level.other_limits
|
|
|
|
|
|
|
|
# and set to active
|
|
|
|
sponsor_benefit.active = True
|
|
|
|
|
|
|
|
# @@@ We don't call sponsor_benefit.clean here. This means
|
|
|
|
# that if the sponsorship level for a sponsor is adjusted
|
|
|
|
# downwards, an existing too-long text entry can remain,
|
|
|
|
# and won't raise a validation error until it's next
|
|
|
|
# edited.
|
|
|
|
sponsor_benefit.save()
|
|
|
|
|
|
|
|
allowed_benefits.append(sponsor_benefit.pk)
|
|
|
|
|
|
|
|
# Any remaining sponsor benefits that don't normally belong to
|
|
|
|
# this level are set to inactive
|
|
|
|
self.sponsor_benefits.exclude(pk__in=allowed_benefits).update(active=False, max_words=None, other_limits="")
|
2013-06-11 17:10:29 +00:00
|
|
|
|
2012-07-13 12:59:25 +00:00
|
|
|
def send_coordinator_emails(self):
|
|
|
|
pass # @@@ should this just be done centrally?
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
|
2012-07-18 23:24:46 +00:00
|
|
|
def _store_initial_level(sender, instance, **kwargs):
|
|
|
|
if instance:
|
|
|
|
instance._initial_level_id = instance.level_id
|
|
|
|
post_init.connect(_store_initial_level, sender=Sponsor)
|
|
|
|
|
|
|
|
|
|
|
|
def _check_level_change(sender, instance, created, **kwargs):
|
|
|
|
if instance and (created or instance.level_id != instance._initial_level_id):
|
|
|
|
instance.reset_benefits()
|
|
|
|
post_save.connect(_check_level_change, sender=Sponsor)
|
|
|
|
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
BENEFIT_TYPE_CHOICES = [
|
|
|
|
("text", "Text"),
|
|
|
|
("file", "File"),
|
|
|
|
("weblogo", "Web Logo"),
|
|
|
|
("simple", "Simple")
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
|
|
|
|
class BenefitLevel(models.Model):
|
|
|
|
|
|
|
|
benefit = models.ForeignKey(Benefit, related_name="benefit_levels", verbose_name=_("benefit"))
|
|
|
|
level = models.ForeignKey(SponsorLevel, related_name="benefit_levels", verbose_name=_("level"))
|
2013-06-11 17:10:29 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
# default limits for this benefit at given level
|
|
|
|
max_words = models.PositiveIntegerField(_("max words"), blank=True, null=True)
|
|
|
|
other_limits = models.CharField(_("other limits"), max_length=200, blank=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ["level"]
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s - %s" % (self.level, self.benefit)
|
|
|
|
|
|
|
|
|
|
|
|
class SponsorBenefit(models.Model):
|
|
|
|
|
|
|
|
sponsor = models.ForeignKey(Sponsor, related_name="sponsor_benefits", verbose_name=_("sponsor"))
|
|
|
|
benefit = models.ForeignKey(Benefit, related_name="sponsor_benefits", verbose_name=_("benefit"))
|
|
|
|
active = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
# Limits: will initially be set to defaults from corresponding BenefitLevel
|
|
|
|
max_words = models.PositiveIntegerField(_("max words"), blank=True, null=True)
|
|
|
|
other_limits = models.CharField(_("other limits"), max_length=200, blank=True)
|
|
|
|
|
|
|
|
# Data: zero or one of these fields will be used, depending on the
|
|
|
|
# type of the Benefit (text, file, or simple)
|
|
|
|
text = models.TextField(_("text"), blank=True)
|
|
|
|
upload = models.FileField(_("file"), blank=True, upload_to="sponsor_files")
|
|
|
|
|
|
|
|
class Meta:
|
2013-06-11 17:10:29 +00:00
|
|
|
ordering = ["-active"]
|
2012-07-12 19:17:42 +00:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return u"%s - %s" % (self.sponsor, self.benefit)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
if self.max_words and len(self.text.split()) > self.max_words:
|
|
|
|
raise ValidationError("Sponsorship level only allows for %s words." % self.max_words)
|
|
|
|
|
|
|
|
def data_fields(self):
|
|
|
|
"""
|
|
|
|
Return list of data field names which should be editable for
|
|
|
|
this ``SponsorBenefit``, depending on its ``Benefit`` type.
|
|
|
|
"""
|
|
|
|
if self.benefit.type == "file" or self.benefit.type == "weblogo":
|
|
|
|
return ["upload"]
|
|
|
|
elif self.benefit.type == "text":
|
|
|
|
return ["text"]
|
|
|
|
return []
|