cisagov / manage.get.gov

A Django-based domain name registrar used by the .gov domain to communicate with an EPP registry
https://get.gov
Other
57 stars 17 forks source link

Refactor the Join lookup on transfer user #2645

Open rachidatecs opened 3 weeks ago

rachidatecs commented 3 weeks ago

Issue description

Right now we use a brute force approach that defines which field on user and joined fields from other models need to be tackled when transferring a user. For a more solid approach, use Django's lookup to find and handle these fields.

The downside with the current approach is that the transfer setup up could break as we add in new models with joins to user and require more code maintenance.

Acceptance criteria

Additional context

I (Rachid) tried using a related field lookup when working on #2579 which was partly successful. The lookup (based on Dave's work on the 'join' alert on contacts) works, but the field update fails:

`def post(self, request, user_id): current_user = get_object_or_404(User, pk=user_id) selected_user_id = request.POST.get('selected_user') selected_user = get_object_or_404(User, pk=selected_user_id)

    try:
        related_objects = []

        for related_field in selected_user._meta.get_fields():
            if related_field.is_relation and related_field.auto_created:
                related_manager = getattr(selected_user, related_field.get_accessor_name(), None)
                if related_manager:
                    if hasattr(related_manager, "get_queryset"):
                        queryset = related_manager.get_queryset()
                    else:
                        queryset = [related_manager]

                    for obj in queryset:
                        field_name = related_field.field.name

                        if related_field.many_to_one or related_field.one_to_one:
                            setattr(obj, field_name, current_user)
                        elif related_field.many_to_many:
                            print(f'many to many handling')
                            if selected_user in related_manager.all():
                                related_manager.remove(selected_user)
                            related_manager.add(current_user)

                        obj.save()

Links to other issues

No response

vickyszuchin commented 3 weeks ago

Added "refinement" label, no ACs defined. @rachidatecs would you add ACs to this ticket?

abroddrick commented 2 weeks ago

temporary slotting in 55

abroddrick commented 2 weeks ago

Added ACs and some other details, marking this as ready, due to the nature of this being a code maintenance issue, would like it to be done anywhere within the next 3 sprints.