2017-06-25 18:57:23 +02:00
|
|
|
import nested_admin
|
|
|
|
|
2017-02-09 21:04:32 +01:00
|
|
|
from django.contrib import admin
|
2017-02-18 19:06:43 +01:00
|
|
|
from django.contrib.auth.admin import UserAdmin
|
|
|
|
from django.contrib.auth.models import User
|
2017-06-25 18:57:23 +02:00
|
|
|
from django.forms.models import modelform_factory
|
2017-02-09 21:04:32 +01:00
|
|
|
|
2017-03-18 21:48:45 +01:00
|
|
|
from .models import (
|
2017-06-25 18:57:23 +02:00
|
|
|
Association, Profile, Club, Event, Location,
|
2017-03-18 21:48:45 +01:00
|
|
|
EventOption, EventCommentField, EventOptionChoice
|
|
|
|
)
|
2017-02-18 19:06:43 +01:00
|
|
|
|
|
|
|
|
2017-02-20 01:16:50 +01:00
|
|
|
# ---
|
|
|
|
# The user related stuff
|
|
|
|
# ---
|
|
|
|
|
2017-02-18 19:06:43 +01:00
|
|
|
class ProfileInline(admin.StackedInline):
|
|
|
|
model = Profile
|
|
|
|
inline_classes = ["collapse open"]
|
|
|
|
|
|
|
|
|
|
|
|
class UserProfileAdmin(UserAdmin):
|
|
|
|
inlines = [
|
|
|
|
ProfileInline,
|
|
|
|
]
|
|
|
|
|
2017-08-07 18:56:56 +02:00
|
|
|
admin.site.unregister(User)
|
|
|
|
admin.site.register(User, UserProfileAdmin)
|
|
|
|
|
|
|
|
|
2017-02-20 01:16:50 +01:00
|
|
|
# ---
|
|
|
|
# Clubs
|
|
|
|
# ---
|
|
|
|
|
2017-08-07 18:56:56 +02:00
|
|
|
class ClubUserInline(admin.TabularInline):
|
|
|
|
model = Club.members.through
|
2017-02-20 01:16:50 +01:00
|
|
|
|
|
|
|
|
2017-08-07 18:56:56 +02:00
|
|
|
@admin.register(Club)
|
|
|
|
class ClubAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
|
|
|
ClubUserInline,
|
|
|
|
]
|
|
|
|
exclude = ('members',)
|
2017-02-20 01:16:50 +01:00
|
|
|
|
|
|
|
|
2017-03-18 21:48:45 +01:00
|
|
|
# ---
|
|
|
|
# Events
|
|
|
|
# ---
|
|
|
|
|
2017-06-25 18:57:23 +02:00
|
|
|
class EventOptionChoiceInline(nested_admin.NestedTabularInline):
|
2017-03-18 21:48:45 +01:00
|
|
|
model = EventOptionChoice
|
2017-06-25 18:57:23 +02:00
|
|
|
extra = 1
|
2017-03-18 21:48:45 +01:00
|
|
|
|
|
|
|
|
2017-06-25 18:57:23 +02:00
|
|
|
class EventOptionInline(nested_admin.NestedTabularInline):
|
2017-03-18 21:48:45 +01:00
|
|
|
model = EventOption
|
2017-06-25 18:57:23 +02:00
|
|
|
extra = 1
|
|
|
|
inlines = [EventOptionChoiceInline]
|
2017-03-18 21:48:45 +01:00
|
|
|
|
|
|
|
|
2017-06-25 18:57:23 +02:00
|
|
|
class EventCommentFieldInline(nested_admin.NestedTabularInline):
|
2017-03-18 21:48:45 +01:00
|
|
|
model = EventCommentField
|
2017-06-25 18:57:23 +02:00
|
|
|
extra = 1
|
2017-03-18 21:48:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Event)
|
2017-06-25 18:57:23 +02:00
|
|
|
class EventAdmin(nested_admin.NestedModelAdmin):
|
2017-03-18 21:48:45 +01:00
|
|
|
search_fields = ['title', 'location', 'description']
|
|
|
|
inlines = [
|
|
|
|
EventOptionInline,
|
|
|
|
EventCommentFieldInline,
|
|
|
|
]
|
2017-06-25 18:57:23 +02:00
|
|
|
|
|
|
|
def get_queryset(self, request):
|
|
|
|
"""Restrict the event you can edit"""
|
|
|
|
qs = super().get_queryset(request)
|
|
|
|
if request.user.is_superuser:
|
|
|
|
return qs
|
|
|
|
else:
|
|
|
|
assocs = Association.objects.filter(staff_group__user=request.user)
|
|
|
|
return qs.filter(associations__in=assocs)
|
|
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
"""Restrict the applications you can attach to an event"""
|
|
|
|
if request.user.is_superuser:
|
|
|
|
return super().get_form(request, obj, **kwargs)
|
|
|
|
else:
|
|
|
|
assocs = Association.objects.filter(staff_group__user=request.user)
|
|
|
|
|
|
|
|
def formfield_callback(f, **kwargs):
|
|
|
|
if f.name == "associations":
|
|
|
|
kwargs["queryset"] = assocs
|
|
|
|
return f.formfield(**kwargs)
|
|
|
|
|
|
|
|
return modelform_factory(
|
|
|
|
Event,
|
|
|
|
exclude=[],
|
|
|
|
formfield_callback=formfield_callback
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Location)
|