dragon-fire-fly / developer_matcher

2 stars 1 forks source link

[BUG] Wrong user being shown when editing message #71

Closed dragon-fire-fly closed 1 year ago

dragon-fire-fly commented 1 year ago

Describe the bug The wrong user is being shown when the "edit message" button is pressed on a selected message. In the example below, the message is to "Admin" but the "edit message" page shows "Casper" and their associated picture.

To Reproduce Steps to reproduce the behavior:

  1. Go to 'Message'
  2. Click on 'Edit message' (either on the main message screen on the individual page)
  3. See error

Expected behavior In this case, "Admin's" username and profile picture should have been shown with the message

Screenshots msg_to_admin wrong-msg-selected

Desktop (please complete the following information):

dragon-fire-fly commented 1 year ago

This was caused by the incorrect receiver variable being passed from the EditMessage view to the edit_message template. This was the original code:

class EditMessage(LoginRequiredMixin, TemplateView):
    model = Message
    template_name = "app_user/edit_message.html"

    def get(self, request, *args, **kwargs):
        message = get_object_or_404(Message, pk=kwargs["pk"])
        if message.user_sender == request.user:
            context = {
                "msg": message,
                "receiver": get_object_or_404(User, pk=message.pk),
                "form": MessageForm(instance=message),
            }
            return render(request, "app_user/edit_message.html", context)
        return redirect("app_user:messages")

The "receiver" variable was retrieving the User based on the message pk rather than the user pk

This bug was fixed by determining the correct parameter to access the correct user pk from the message object. debugging

The correct receiver was then accessed by calling the get_object_or_404() method below: receiver = get_object_or_404(User, pk=message.user_receiver_id) and returning as part of the context variable to the template.