2015-06-21 05:50:26 +00:00
|
|
|
from django.utils.html import escape
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-02-05 20:03:30 +00:00
|
|
|
|
2015-09-14 02:34:40 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
|
2015-09-08 20:03:06 +00:00
|
|
|
from symposion.sponsorship.models import (
|
|
|
|
Benefit,
|
|
|
|
BENEFITS,
|
|
|
|
BenefitLevel,
|
|
|
|
Sponsor,
|
|
|
|
SponsorBenefit,
|
|
|
|
SponsorLevel,
|
|
|
|
)
|
2012-02-05 20:03:30 +00:00
|
|
|
|
2015-09-14 02:34:40 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
class BenefitLevelInline(admin.TabularInline):
|
|
|
|
model = BenefitLevel
|
|
|
|
extra = 0
|
|
|
|
|
|
|
|
|
|
|
|
class SponsorBenefitInline(admin.StackedInline):
|
|
|
|
model = SponsorBenefit
|
|
|
|
extra = 0
|
|
|
|
fieldsets = [
|
|
|
|
(None, {
|
|
|
|
"fields": [
|
|
|
|
("benefit", "active"),
|
|
|
|
("max_words", "other_limits"),
|
|
|
|
"text",
|
|
|
|
"upload",
|
|
|
|
]
|
|
|
|
})
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class SponsorAdmin(admin.ModelAdmin):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
save_on_top = True
|
|
|
|
fieldsets = [
|
|
|
|
(None, {
|
|
|
|
"fields": [
|
|
|
|
("name", "applicant"),
|
|
|
|
("level", "active"),
|
|
|
|
"external_url",
|
|
|
|
"annotation",
|
|
|
|
("contact_name", "contact_email")
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
("Metadata", {
|
|
|
|
"fields": ["added"],
|
|
|
|
"classes": ["collapse"]
|
|
|
|
})
|
|
|
|
]
|
|
|
|
inlines = [SponsorBenefitInline]
|
2015-07-11 02:27:12 +00:00
|
|
|
list_filter = ["level", "active"]
|
2015-06-21 05:50:26 +00:00
|
|
|
list_display = ["name", "external_url", "level", "active", "contact", "applicant_field"]
|
|
|
|
|
|
|
|
def contact(self, sponsor):
|
|
|
|
return mark_safe('<a href="mailto:%s">%s</a>' % (escape(sponsor.contact_email), escape(sponsor.contact_name)))
|
|
|
|
|
|
|
|
def applicant_field(self, sponsor):
|
|
|
|
name = sponsor.applicant.get_full_name()
|
|
|
|
email = sponsor.applicant.email
|
|
|
|
return mark_safe('<a href="mailto:%s">%s</a>' % (escape(email), escape(name)))
|
|
|
|
applicant_field.short_description = _(u"Applicant")
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
def get_form(self, *args, **kwargs):
|
|
|
|
# @@@ kinda ugly but using choices= on NullBooleanField is broken
|
|
|
|
form = super(SponsorAdmin, self).get_form(*args, **kwargs)
|
|
|
|
form.base_fields["active"].widget.choices = [
|
2015-06-26 03:46:09 +00:00
|
|
|
("1", _("unreviewed")),
|
|
|
|
("2", _("approved")),
|
|
|
|
("3", _("rejected"))
|
2012-07-12 19:17:42 +00:00
|
|
|
]
|
|
|
|
return form
|
|
|
|
|
2015-07-11 02:27:12 +00:00
|
|
|
# Define accessor functions for our benefit fields and add them to
|
|
|
|
# list_display, so we can sort on them and give them sensible names.
|
|
|
|
# Add the fields to list_filters while we're at it.
|
|
|
|
for benefit in BENEFITS:
|
|
|
|
benefit_name = benefit['name']
|
|
|
|
field_name = benefit['field_name']
|
|
|
|
|
|
|
|
def func_generator(ben):
|
|
|
|
def column_func(obj):
|
|
|
|
return getattr(obj, ben['field_name'])
|
|
|
|
column_func.short_description = ben['column_title']
|
|
|
|
column_func.boolean = True
|
|
|
|
column_func.admin_order_field = ben['field_name']
|
|
|
|
return column_func
|
|
|
|
list_display.append(func_generator(benefit))
|
|
|
|
list_filter.append(field_name)
|
|
|
|
|
|
|
|
def save_related(self, request, form, formsets, change):
|
|
|
|
super(SponsorAdmin, self).save_related(request, form, formsets, change)
|
|
|
|
obj = form.instance
|
|
|
|
obj.save()
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
|
|
|
|
class BenefitAdmin(admin.ModelAdmin):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2015-07-11 02:27:12 +00:00
|
|
|
list_display = ["name", "type", "description", "levels"]
|
2012-07-12 19:17:42 +00:00
|
|
|
inlines = [BenefitLevelInline]
|
|
|
|
|
2015-07-11 02:27:12 +00:00
|
|
|
def levels(self, benefit):
|
|
|
|
return u", ".join(l.level.name for l in benefit.benefit_levels.all())
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
|
|
|
|
class SponsorLevelAdmin(admin.ModelAdmin):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
inlines = [BenefitLevelInline]
|
|
|
|
|
|
|
|
|
2015-07-11 02:27:12 +00:00
|
|
|
class SponsorBenefitAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ('benefit', 'sponsor', 'active', '_is_complete', 'show_text')
|
|
|
|
|
|
|
|
def show_text(self, sponsor_benefit):
|
|
|
|
if sponsor_benefit.text:
|
|
|
|
return sponsor_benefit.text[:100]
|
|
|
|
else:
|
|
|
|
return _("None")
|
|
|
|
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
admin.site.register(SponsorLevel, SponsorLevelAdmin)
|
|
|
|
admin.site.register(Sponsor, SponsorAdmin)
|
|
|
|
admin.site.register(Benefit, BenefitAdmin)
|
2015-07-11 02:27:12 +00:00
|
|
|
admin.site.register(SponsorBenefit, SponsorBenefitAdmin)
|