Skip to content

Commit

Permalink
feat: bring back custom user rendering
Browse files Browse the repository at this point in the history
Username is not good enough, having name and email is better for us.

Fixes WEBSITE-3C
  • Loading branch information
nijel committed Dec 5, 2024
1 parent a52c2e1 commit bf27b73
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
21 changes: 0 additions & 21 deletions weblate_web/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from django.contrib import admin

from weblate_web.models import (
Expand All @@ -34,14 +32,6 @@
Subscription,
)

if TYPE_CHECKING:
from django.forms import ModelForm
from django.http import HttpRequest


def format_user(obj):
return f"{obj.username}: {obj.first_name} {obj.last_name} <{obj.email}>"


@admin.register(Donation)
class DonationAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -104,17 +94,6 @@ class ServiceAdmin(admin.ModelAdmin):
"site_version",
)

def get_form(
self,
request: HttpRequest,
obj: Any | None = None,
change: bool = False,
**kwargs: Any,
) -> type[ModelForm[Any]]:
form = super().get_form(request=request, obj=obj, change=change, **kwargs)
form.base_fields["users"].label_from_instance = format_user # type: ignore[attr-defined]
return form


@admin.register(Subscription)
class SubscriptionAdmin(admin.ModelAdmin):
Expand Down
5 changes: 5 additions & 0 deletions weblate_web/admin_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.contrib.admin.apps import AdminConfig


class CustomAdminConfig(AdminConfig):
default_site = "weblate_web.admin_site.CustomAdminSite"
26 changes: 26 additions & 0 deletions weblate_web/admin_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import annotations

from typing import TYPE_CHECKING

from django.contrib.admin.sites import AdminSite
from django.contrib.admin.views.autocomplete import AutocompleteJsonView
from django.contrib.auth.models import User

if TYPE_CHECKING:
from django.db.models import Model


class UserAutocompleteJsonView(AutocompleteJsonView):
def serialize_result(self, obj: Model, to_field_name: str) -> dict[str, str]:
result = super().serialize_result(obj, to_field_name) # type: ignore[misc]
if isinstance(obj, User):
result["text"] = f"{obj.first_name} {obj.last_name} <{obj.email}>"
return result


class CustomAdminSite(AdminSite):
def autocomplete_view(self, request):
return UserAutocompleteJsonView.as_view(admin_site=self)(request)


custom_admin_site = CustomAdminSite()
Empty file added weblate_web/apps.py
Empty file.
9 changes: 4 additions & 5 deletions weblate_web/payments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
from __future__ import annotations

from django.contrib import admin

from .models import Customer, Payment


@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
list_display = ("email", "name", "country", "vat", "origin")
list_display = ("name", "email", "country", "vat", "origin")
list_filter = ("country", "origin")
search_fields = ("name", "email", "users__email")
ordering = ("name",)
autocomplete_fields = ("users",)


@admin.register(Payment)
class PaymentAdmin(admin.ModelAdmin):
list_display = (
"description",
Expand All @@ -54,7 +57,3 @@ class PaymentAdmin(admin.ModelAdmin):
readonly_fields = ("created",)
date_hierarchy = "created"
ordering = ("-created",)


admin.site.register(Customer, CustomerAdmin)
admin.site.register(Payment, PaymentAdmin)
2 changes: 1 addition & 1 deletion weblate_web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
"django.contrib.staticfiles",
"django.contrib.sitemaps",
"django.contrib.messages",
"django.contrib.admin",
"weblate_web.admin_app.CustomAdminConfig",
"django.contrib.humanize",
"weblate_web.payments",
"weblate_web.invoices",
Expand Down

0 comments on commit bf27b73

Please sign in to comment.