user12584812
user12584812

Reputation:

Marking message as read in django

I have a model message, that is related to the user:

class Message(models.Model):
    content = models.TextField(verbose_name='content')
    msg_to = models.ForeignKey(User, related_name='message_to', on_delete=models.CASCADE)
    msg_from = models.ForeignKey(User, related_name='message_from', on_delete=models.CASCADE)
    creation_date = models.DateTimeField(auto_now_add=True)
    read = models.BooleanField(default=False)

Also have an url when an user can see the message:

path('message/<int:msg_id>', ex_views.MessageView.as_view(), name="message"),

How to make changing "read" in message models to 'True' if user which the message is to, open the link with the specific message?

For example, i have an message id=2 and it's addressed to user1, user1 click/open the link: message/2 and automatically read changes to True.

Upvotes: 1

Views: 1477

Answers (4)

user12584812
user12584812

Reputation:

I've done it like that: (no post, but works)

class MessageView(LoginRequiredMixin, View):

    def get(self, request, msg_id):
        message = Message.objects.get(id=msg_id)
        current_user = request.user
        if message.msg_to_id == current_user.id:
            message.read = True
            message.save()
        return render(request, "twitter/message.html", locals())

Upvotes: 1

Abhisek Singh
Abhisek Singh

Reputation: 380

lets suppose your MessageView be like below example hope this will help your requirement

class MessageView(generics.ListCreateAPIView):
  permission_classes = [AllowAny]
  parser_classes = (MultiPartParser,FormParser,JSONParser)
  def post(self, request, format=None):
    msg_id = request.data.get('msg_id')
    message = Message.objects.get(id=msg_id)
    if message:  
        message.read = True  
        message.save() 
        message_response = MessageView().post(request)
        return message_response
    else:  
        return Response({'request_status': 0, 'msg': "fail to read msg."}, status=status.HTTP_400_BADREQUEST)

this is just an example you can also amend other case and update as well

Upvotes: 0

Edwin Cruz
Edwin Cruz

Reputation: 517

You should do this in your views.py. Whenever they make a request to that page then you simply change the read field to true. Below is a simple skeleton of what I mean.

views.py

def message_view(request, message_id):
    message = Message.objects.get(id=message_id)
    message.read = True
    message.save()

    return render(request, 'template.html')

Basically all I am doing here is getting the message they clicked on by querying the database with the id that was passed then changing the read field to true then saving the object and returning the html.

Upvotes: 1

Pankaj Sharma
Pankaj Sharma

Reputation: 2267

In views use request.user to get the current user and check if it is same as message owner if it is - do read=True, if you can post views I can help you to write the code.

Upvotes: 0

Related Questions