2017-03-31 15:29:28 +00:00
|
|
|
from django.contrib import admin
|
2017-04-02 16:04:57 +00:00
|
|
|
from .models import Team, TeamArea, TeamMember
|
2017-04-18 18:46:57 +00:00
|
|
|
from .email import send_add_membership_email, send_remove_membership_email
|
2017-03-31 15:29:28 +00:00
|
|
|
|
2017-04-02 16:04:57 +00:00
|
|
|
admin.site.register(TeamArea)
|
2017-04-08 07:27:33 +00:00
|
|
|
|
2017-04-14 14:11:43 +00:00
|
|
|
|
2017-04-08 07:27:33 +00:00
|
|
|
@admin.register(Team)
|
|
|
|
class TeamAdmin(admin.ModelAdmin):
|
2017-04-08 07:39:43 +00:00
|
|
|
def get_responsible(self, obj):
|
|
|
|
return ", ".join([resp.get_full_name() for resp in obj.responsible])
|
|
|
|
get_responsible.short_description = 'Responsible'
|
|
|
|
|
|
|
|
list_display = [
|
|
|
|
'name',
|
|
|
|
'area',
|
|
|
|
'get_responsible',
|
|
|
|
'needs_members',
|
|
|
|
]
|
|
|
|
|
2017-04-08 07:27:33 +00:00
|
|
|
list_filter = [
|
|
|
|
'camp',
|
2017-04-08 07:39:43 +00:00
|
|
|
'needs_members',
|
2017-04-08 07:27:33 +00:00
|
|
|
]
|
|
|
|
|
2017-04-14 14:11:43 +00:00
|
|
|
|
2017-04-08 07:27:33 +00:00
|
|
|
@admin.register(TeamMember)
|
|
|
|
class TeamMemberAdmin(admin.ModelAdmin):
|
|
|
|
list_filter = [
|
|
|
|
'team',
|
|
|
|
'approved',
|
|
|
|
]
|
|
|
|
|
2017-04-19 22:46:40 +00:00
|
|
|
actions = ['approve_membership', 'remove_member']
|
2017-04-14 14:11:43 +00:00
|
|
|
|
2017-04-19 22:46:40 +00:00
|
|
|
def approve_membership(self, request, queryset):
|
2017-04-14 14:11:43 +00:00
|
|
|
teams_count = queryset.values('team').distinct().count()
|
2017-04-18 18:46:57 +00:00
|
|
|
updated = 0
|
|
|
|
|
|
|
|
for membership in queryset:
|
|
|
|
membership.approved = True
|
|
|
|
membership.save()
|
|
|
|
updated += 1
|
|
|
|
send_add_membership_email(membership)
|
|
|
|
|
2017-04-14 14:11:43 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
2017-04-19 22:46:40 +00:00
|
|
|
'Membership(s) approved: Added {} user(s) to {} team(s).'.format(
|
2017-04-18 18:46:57 +00:00
|
|
|
updated,
|
2017-04-14 14:11:43 +00:00
|
|
|
teams_count
|
|
|
|
)
|
|
|
|
)
|
2017-04-19 22:49:36 +00:00
|
|
|
approve_membership.description = 'Approve membership.'
|
2017-04-14 14:11:43 +00:00
|
|
|
|
|
|
|
def remove_member(self, request, queryset):
|
|
|
|
teams_count = queryset.values('team').distinct().count()
|
2017-04-18 18:46:57 +00:00
|
|
|
updated = 0
|
|
|
|
|
|
|
|
for membership in queryset:
|
|
|
|
send_remove_membership_email(membership)
|
|
|
|
membership.delete()
|
|
|
|
updated += 1
|
|
|
|
|
2017-04-14 14:11:43 +00:00
|
|
|
self.message_user(
|
|
|
|
request,
|
|
|
|
'Removed {} user(s) from {} team(s).'.format(
|
2017-04-18 18:46:57 +00:00
|
|
|
updated,
|
2017-04-14 14:11:43 +00:00
|
|
|
teams_count
|
|
|
|
)
|
|
|
|
)
|
|
|
|
remove_member.description = 'Remove a user from the team.'
|
2017-04-19 22:46:40 +00:00
|
|
|
|