Skip to content

add a sponsorship export function via admin #2231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ sorl-thumbnail==12.7.0
docxtpl==0.12.0
reportlab==3.6.6
django-extensions==3.1.4
django-import-export==2.7.1
1 change: 1 addition & 0 deletions pydotorg/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
'django_filters',
'polymorphic',
'django_extensions',
'import_export',
]

# Fixtures
Expand Down
78 changes: 77 additions & 1 deletion sponsors/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.contenttypes.admin import GenericTabularInline
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from ordered_model.admin import OrderedModelAdmin
from polymorphic.admin import PolymorphicInlineSupportMixin, StackedPolymorphicInline, PolymorphicParentModelAdmin, \
PolymorphicChildModelAdmin
Expand All @@ -13,6 +14,10 @@
from django.utils.functional import cached_property
from django.utils.html import mark_safe

from import_export import resources
from import_export.fields import Field
from import_export.admin import ImportExportActionModelAdmin

from mailing.admin import BaseEmailTemplateAdmin
from sponsors.models import *
from sponsors.models.benefits import RequiredAssetMixin
Expand Down Expand Up @@ -292,8 +297,78 @@ def choices(self, changelist):
return choices


class SponsorshipResource(resources.ModelResource):

sponsor_name = Field(attribute='sponsor__name', column_name='Company Name')
contact_name = Field(column_name='Contact Name(s)')
contact_email = Field(column_name='Contact Email(s)')
contact_phone = Field(column_name='Contact phone number')
contact_type = Field(column_name='Contact Type(s)')
start_date = Field(attribute='start_date', column_name='Start Date')
end_date = Field(attribute='end_date', column_name='End Date')
web_logo = Field(column_name='Logo')
landing_page_url = Field(attribute='sponsor__landing_page_url', column_name='Webpage link')
level = Field(attribute='package__name', column_name='Sponsorship Level')
cost = Field(attribute='sponsorship_fee', column_name='Sponsorship Cost')
admin_url = Field(attribute='admin_url', column_name='Admin Link')

class Meta:
model = Sponsorship
fields = (
'sponsor_name',
'contact_name',
'contact_email',
'contact_phone',
'contact_type',
'start_date',
'end_date',
'web_logo',
'landing_page_url',
'level',
'cost',
'admin_url',
)
export_order = (
"sponsor_name",
"contact_name",
"contact_email",
"contact_phone",
"contact_type",
"start_date",
"end_date",
"web_logo",
"landing_page_url",
"level",
"cost",
"admin_url",
)

def get_sponsorship_url(self, sponsorship):
domain = Site.objects.get_current().domain
url = reverse("admin:sponsors_sponsorship_change", args=[sponsorship.id])
return f'https://{domain}{url}'

def dehydrate_web_logo(self, sponsorship):
return sponsorship.sponsor.web_logo.url

def dehydrate_contact_type(self, sponsorship):
return "\n".join([contact.type for contact in sponsorship.sponsor.contacts.all()])

def dehydrate_contact_name(self, sponsorship):
return "\n".join([contact.name for contact in sponsorship.sponsor.contacts.all()])

def dehydrate_contact_email(self, sponsorship):
return "\n".join([contact.email for contact in sponsorship.sponsor.contacts.all()])

def dehydrate_contact_phone(self, sponsorship):
return "\n".join([contact.phone for contact in sponsorship.sponsor.contacts.all()])

def dehydrate_admin_url(self, sponsorship):
return self.get_sponsorship_url(sponsorship)


@admin.register(Sponsorship)
class SponsorshipAdmin(admin.ModelAdmin):
class SponsorshipAdmin(ImportExportActionModelAdmin, admin.ModelAdmin):
change_form_template = "sponsors/admin/sponsorship_change_form.html"
form = SponsorshipReviewAdminForm
inlines = [SponsorBenefitInline, AssetsInline]
Expand All @@ -310,6 +385,7 @@ class SponsorshipAdmin(admin.ModelAdmin):
]
list_filter = [SponsorshipStatusListFilter, "package", "year", TargetableEmailBenefitsFilter]
actions = ["send_notifications"]
resource_class = SponsorshipResource
fieldsets = [
(
"Sponsorship Data",
Expand Down
13 changes: 13 additions & 0 deletions sponsors/models/sponsors.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ def can_manage(self):
if self.user is not None and (self.primary or self.manager):
return True

@property
def type(self):
types=[]
if self.primary:
types.append('Primary')
if self.administrative:
types.append('Administrative')
if self.manager:
types.append('Manager')
if self.accounting:
types.append('Accounting')
return ", ".join(types)

def __str__(self):
return f"Contact {self.name} from {self.sponsor}"

Expand Down