Skip to content

Commit

Permalink
admin: add add_support_remark_to_obj utility
Browse files Browse the repository at this point in the history
This is used for User transfer and is likely to be used for company
transfer.
  • Loading branch information
xavfernandez committed Jul 8, 2024
1 parent 440b303 commit 0b710a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
18 changes: 3 additions & 15 deletions itou/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from allauth.account.models import EmailAddress
from django.contrib import admin, messages
from django.contrib.auth.admin import UserAdmin
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.db.models import Exists, OuterRef
from django.shortcuts import get_object_or_404, redirect
Expand Down Expand Up @@ -35,9 +34,9 @@
ItouModelAdmin,
ItouTabularInline,
PkSupportRemarkInline,
add_support_remark_to_obj,
get_admin_view_link,
)
from itou.utils.models import PkSupportRemark


class EmailAddressInline(ItouTabularInline):
Expand Down Expand Up @@ -295,17 +294,6 @@ def get_fields_to_transfer_for_job_seekers():
return {models.User._meta.get_field(name) for name in JOB_SEEKER_FIELDS_TO_TRANSFER}


def add_support_remark_to_user(user, text):
user_content_type = ContentType.objects.get_for_model(models.User)
try:
remark = PkSupportRemark.objects.filter(content_type=user_content_type, object_id=user.pk).get()
except PkSupportRemark.DoesNotExist:
PkSupportRemark.objects.create(content_type=user_content_type, object_id=user.pk, remark=text)
else:
remark.remark += "\n" + text
remark.save(update_fields=("remark",))


@admin.register(models.User)
class ItouUserAdmin(InconsistencyCheckMixin, UserAdmin):
class Media:
Expand Down Expand Up @@ -670,8 +658,8 @@ def _get_transfer_data_from_user(user, field):
+ [f"- {item_title} {item} transféré " for item_title, item in items_transfered]
+ ["-" * 20]
)
add_support_remark_to_user(from_user, summary_text)
add_support_remark_to_user(to_user, summary_text)
add_support_remark_to_obj(from_user, summary_text)
add_support_remark_to_obj(to_user, summary_text)
message = format_html(
"Transfert effectué avec succès de l'utilisateur {from_user} vers {to_user}.",
from_user=from_user,
Expand Down
12 changes: 12 additions & 0 deletions itou/utils/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.contrib.admin import ModelAdmin, StackedInline, TabularInline
from django.contrib.contenttypes.admin import GenericStackedInline
from django.contrib.contenttypes.fields import GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.forms import fields as gis_fields
from django.contrib.messages import WARNING
from django.urls import reverse
Expand Down Expand Up @@ -137,3 +138,14 @@ def get_object(self, request, object_id, from_field=None):
# Eager-loading all relations, but only when editing one object because `list_select_related` exists
with mock.patch.object(self, "get_queryset", self._get_queryset_with_relations):
return super().get_object(request, object_id, from_field)


def add_support_remark_to_obj(obj, text):
obj_content_type = ContentType.objects.get_for_model(obj)
try:
remark = PkSupportRemark.objects.filter(content_type=obj_content_type, object_id=obj.pk).get()
except PkSupportRemark.DoesNotExist:
PkSupportRemark.objects.create(content_type=obj_content_type, object_id=obj.pk, remark=text)
else:
remark.remark += "\n" + text
remark.save(update_fields=("remark",))

0 comments on commit 0b710a1

Please sign in to comment.