dragon-fire-fly / developer_matcher

2 stars 1 forks source link

[BUG] 'edited' status being added to new messages #72

Closed dragon-fire-fly closed 1 year ago

dragon-fire-fly commented 1 year ago

Describe the bug The edited property is being added to new messages as well as edited ones. The "edited" checkbox in the message model is being checked whenever a new message is created, but it should only get checked when the message is edited for the first time.

To Reproduce Steps to reproduce the behavior:

  1. Go to 'new message'
  2. Send a new message
  3. Click that message to view it
  4. See error

Expected behavior New messages should not have the "edited" checkbox ticked. This should only be ticked once the message has passed through the "edit" view.

Screenshots image

Desktop (please complete the following information):

dragon-fire-fly commented 1 year ago

There is only one form for messages regardless of whether they are new or edited (the edited form is creating by passing the instance=message argument when the MessageForm class is instantiated). The original form save method did not distinguish between new and edited messages when adding the edited property.

def save(self, commit=True):
        message = super(MessageForm, self).save(commit=False)
        message.edited = True
        if commit:
            message.save()
        return message

To resolve this, a msgtype argument was added to the save method, then either "new" or "edit" were passed as arguments for new and edited messages respectively.

The new code has this argument added and an if statement check as so:

    def save(self, msgtype, commit=True):
        message = super(MessageForm, self).save(commit=False)
        if msgtype == "edit":
            message.edited = True
        if commit:
            message.save()
        return message

And the add message view and edit message view pass this argument as so: add message:

if new_msg.is_valid():
            new_msg = new_msg.save("new", commit=False)
            new_msg.user_sender_id = request.POST["sender"]
            new_msg.user_receiver_id = request.POST["receiver"]
            new_msg.save("new")
            messages.success(request, "Message sent!")

edit message:

if form.is_valid():
            form.save("edit")
            messages.success(request, "Message successfully updated.")