def messages_view(request): if request.user.is_authenticated: users = [contact.user for contact in request.user.contacts.all()] users = build_paginator(request, users) last_messages = [] for user in users: message = Message.get(user1=user, user2=request.user) last_messages.append({ 'user': user, 'message': message.last() if message else [], }) return render( request, 'messenger/messages.html', { 'title': _('Messages'), 'datetime': timezone.now(), 'users': users, 'last_messages': last_messages, }) else: return redirect('login')
def get_messages_view(request, pk): if request.user.is_authenticated: if request.user.pk != pk: user = get_object_or_404(User, pk=pk) messages = Message.get(user1=user, user2=request.user) messages = build_paginator(request, messages) for message in messages: if not message.received and message.receiver == request.user: message.received = True message.date_received = timezone.now() message.save() return render( request, 'messenger/get_messages.html', { 'title': _('Messages'), 'messages': messages, 'datetime': timezone.now(), 'user': user, }) else: return redirect('home') else: return redirect('login')