mpeltriaux
e029f8c61e
* adds button and functionality for leaving a team * if the admin leaves the team, another user will be chosen as new admin automatically * improves Team (django) admin backend * better control over user adding-removing * only added team members are selectable as admin
94 lines
2.0 KiB
Python
94 lines
2.0 KiB
Python
from django.contrib import admin
|
|
|
|
from user.models import UserNotification, UserActionLogEntry, User, Team
|
|
|
|
|
|
class UserNotificationAdmin(admin.ModelAdmin):
|
|
list_display = [
|
|
"id",
|
|
"name",
|
|
"is_active",
|
|
]
|
|
|
|
|
|
class UserAdmin(admin.ModelAdmin):
|
|
list_display = [
|
|
"id",
|
|
"username",
|
|
"first_name",
|
|
"last_name",
|
|
"email",
|
|
]
|
|
fields = [
|
|
"username",
|
|
"first_name",
|
|
"last_name",
|
|
"email",
|
|
"is_active",
|
|
"is_staff",
|
|
"is_superuser",
|
|
"api_token",
|
|
"groups",
|
|
"notifications",
|
|
"date_joined",
|
|
"last_login",
|
|
]
|
|
search_fields = [
|
|
"username",
|
|
"first_name",
|
|
"last_name",
|
|
"email",
|
|
]
|
|
filter_horizontal = [
|
|
"groups",
|
|
"notifications",
|
|
]
|
|
readonly_fields = [
|
|
"date_joined",
|
|
"last_login",
|
|
]
|
|
autocomplete_fields = [
|
|
"api_token",
|
|
]
|
|
exclude = [
|
|
"user_permissions",
|
|
]
|
|
|
|
|
|
class UserActionLogEntryAdmin(admin.ModelAdmin):
|
|
list_display = [
|
|
"id",
|
|
"user",
|
|
"timestamp",
|
|
"action",
|
|
]
|
|
|
|
|
|
class TeamAdmin(admin.ModelAdmin):
|
|
list_display = [
|
|
"name",
|
|
"description",
|
|
"admin",
|
|
]
|
|
search_fields = [
|
|
"name",
|
|
"description",
|
|
]
|
|
filter_horizontal = [
|
|
"users"
|
|
]
|
|
|
|
def formfield_for_foreignkey(self, db_field, request, **kwargs):
|
|
if db_field.name == "admin":
|
|
team_id = request.resolver_match.kwargs.get("object_id", None)
|
|
kwargs["queryset"] = User.objects.filter(teams__id__in=[team_id])
|
|
return super().formfield_for_foreignkey(db_field, request, **kwargs)
|
|
|
|
|
|
admin.site.register(User, UserAdmin)
|
|
admin.site.register(Team, TeamAdmin)
|
|
|
|
# Outcommented for a cleaner admin backend on production
|
|
#admin.site.register(UserNotification, UserNotificationAdmin)
|
|
#admin.site.register(UserActionLogEntry, UserActionLogEntryAdmin)
|