Skip to content

Commit

Permalink
Improvements to Django admin configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
anorthall committed Jun 20, 2023
1 parent e756cf2 commit 3790d14
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 36 deletions.
67 changes: 53 additions & 14 deletions app/logger/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,45 @@ class TripAdminForm(DistanceUnitFormMixin, ModelForm):
pass


class TripPhotoInline(admin.TabularInline):
model = TripPhoto
fk_name = "trip"
extra = 0
max_num = 0
show_change_link = True
fields = ("photo", "caption", "is_valid", "taken")

def has_add_permission(self, request, obj=None):
return False


class TripReportInline(admin.TabularInline):
model = TripReport
fk_name = "trip"
extra = 0
max_num = 0
show_change_link = True
fields = ("title", "pub_date")

def has_add_permission(self, request, obj=None):
return False


@admin.register(Trip)
class TripAdmin(admin.ModelAdmin):
form = TripAdminForm
inlines = [TripPhotoInline, TripReportInline]
search_fields = (
"cave_name",
"cave_entrance",
"cave_exit",
"cave_region",
"cave_country",
"cavers",
"clubs",
"expedition",
"notes",
"user__username",
"user__name" "user__email",
)
search_help_text = (
"Search by cave name, region and country, or author name, email or username."
)
readonly_fields = (
"added",
Expand All @@ -31,14 +57,14 @@ class TripAdmin(admin.ModelAdmin):
list_display = (
"user",
"cave_name",
"cave_country",
"privacy",
"added",
"start",
"added",
)
list_display_links = ("cave_name",)
list_filter = (
"type",
"privacy",
"added",
"updated",
)
ordering = ("-added",)
fieldsets = (
Expand Down Expand Up @@ -105,9 +131,11 @@ class TripAdmin(admin.ModelAdmin):

@admin.register(TripPhoto)
class TripPhotoAdmin(admin.ModelAdmin):
list_display = ("user", "trip", "taken", "added", "taken", "is_valid")
list_filter = ("is_valid",)
search_fields = ("user__username",)
list_display = ("user", "trip", "taken", "added")
list_display_links = ("taken",)
list_filter = ("is_valid", "added")
search_fields = ("user__username", "user__name", "user__email", "trip__uuid")
search_help_text = "Search by author name, email or username, or trip UUID."
autocomplete_fields = ("trip", "user")
readonly_fields = ("added", "updated", "uuid", "taken")
ordering = ("-added",)
Expand Down Expand Up @@ -140,8 +168,19 @@ class TripPhotoAdmin(admin.ModelAdmin):

@admin.register(TripReport)
class TripReportAdmin(admin.ModelAdmin):
list_display = ("title", "user", "pub_date", "trip", "privacy")
list_filter = ("user", "pub_date", "privacy")
search_fields = ("title", "content")
list_display = ("user", "title", "trip", "added")
list_display_links = ("title",)
list_filter = ("added", "updated", "pub_date")
ordering = ("-added",)
search_fields = (
"title",
"user__username",
"user__name",
"user__email",
"trip__uuid",
)
search_help_text = (
"Search by title or trip UUID, or by author name, email or username."
)
prepopulated_fields = {"slug": ("title",)}
readonly_fields = ("added", "updated")
37 changes: 15 additions & 22 deletions app/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,32 @@ class FriendRequestRecdInline(admin.TabularInline):

@admin.register(User)
class CavingUserAdmin(BaseUserAdmin):
inlines = [
NotificationInline,
FriendRequestSentInline,
FriendRequestRecdInline,
]
readonly_fields = ("last_login", "last_seen", "date_joined", "uuid", "friends")
form = UserAdminChangeForm
add_form = UserCreationForm

search_fields = (
"email",
"username",
"name",
"bio",
)
search_help_text = "Search by email, username, name or bio."
ordering = ("-last_seen",)
autocomplete_fields = ["friends"]
list_display = (
"email",
"username",
"name",
"date_joined",
"last_login",
"last_seen",
"is_active",
)
list_filter = (
"is_active",
"last_login",
"is_superuser",
"last_seen",
)
fieldsets = (
Expand Down Expand Up @@ -122,9 +132,6 @@ class CavingUserAdmin(BaseUserAdmin):
},
),
)

readonly_fields = ("last_login", "last_seen", "date_joined", "uuid")

add_fieldsets = (
(
None,
Expand All @@ -141,17 +148,3 @@ class CavingUserAdmin(BaseUserAdmin):
},
),
)

search_fields = (
"email",
"username",
"name",
"bio",
)
ordering = ("-last_seen",)
filter_horizontal = ()
inlines = [
NotificationInline,
FriendRequestSentInline,
FriendRequestRecdInline,
]

0 comments on commit 3790d14

Please sign in to comment.