Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the Join lookup on transfer user #2645

Open
1 task
rachidatecs opened this issue Aug 23, 2024 · 3 comments · May be fixed by #3346
Open
1 task

Refactor the Join lookup on transfer user #2645

rachidatecs opened this issue Aug 23, 2024 · 3 comments · May be fixed by #3346
Assignees
Labels
dev issue is for the dev team refactor

Comments

@rachidatecs
Copy link
Contributor

rachidatecs commented Aug 23, 2024

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
Copy link

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

@abroddrick abroddrick self-assigned this Aug 26, 2024
@abroddrick
Copy link
Contributor

temporary slotting in 55

@abroddrick abroddrick moved this from 👶 New to 🎯 Ready in .gov Product Board Aug 29, 2024
@abroddrick
Copy link
Contributor

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.

@abroddrick abroddrick removed their assignment Aug 29, 2024
@Matt-Spence Matt-Spence self-assigned this Jan 8, 2025
@Matt-Spence Matt-Spence moved this from 🎯 Ready to 🏗 In progress in .gov Product Board Jan 8, 2025
@Matt-Spence Matt-Spence linked a pull request Jan 14, 2025 that will close this issue
32 tasks
@MzBowieFan77 MzBowieFan77 moved this from 🏗 In progress to 👀 In review in .gov Product Board Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev issue is for the dev team refactor
Projects
Status: 👀 In review
Development

Successfully merging a pull request may close this issue.

4 participants