From f3564ee9437b2a16ffe65f4f6a2ec13db245f46e Mon Sep 17 00:00:00 2001 From: Mukhtar Date: Wed, 20 Jul 2016 15:12:59 +0300 Subject: [PATCH] #ARC-11 add changes for views --- api/views.py | 5 +- chat/templates/chat_contractor.html | 69 +-- chat/templates/chat_customer.html | 638 +++++++++++++++------------- chat/views.py | 42 +- templates/home.html | 2 +- 5 files changed, 418 insertions(+), 338 deletions(-) diff --git a/api/views.py b/api/views.py index ffe3cf5..408a7ed 100755 --- a/api/views.py +++ b/api/views.py @@ -39,11 +39,10 @@ class MessageViewSet(ModelViewSet): search_param = self.request.query_params.get('operand', None) recipent_id = self.request.query_params.get('recipent_id', None) sender_id = self.request.query_params.get('sender_id', None) - # queryset = queryset.filter(Q(sender=self.request.user.pk) | Q(recipent=self.request.user.pk)) - # import code; code.interact(local=dict(globals(), **locals())) if search_param: # import code; code.interact(local=dict(globals(), **locals())) - queryset = queryset.filter(Q(sender=self.request.user.pk) | Q(recipent=self.request.user.pk)) + if search_param == 'in': + queryset = queryset.filter(Q(sender__in=[sender_id,recipent_id]),Q(recipent__in=[sender_id,recipent_id])).order_by('created') return queryset diff --git a/chat/templates/chat_contractor.html b/chat/templates/chat_contractor.html index 1ac5f6f..89f5380 100644 --- a/chat/templates/chat_contractor.html +++ b/chat/templates/chat_contractor.html @@ -1,11 +1,12 @@ {% extends 'partials/base.html' %} {% load staticfiles %} + {% load thumbnail %} {% block content %} {% include 'partials/header.html' %}
-

Чат {{ request.user }}

+

Чат {{ request.user }} {{ request.user.pk }}

@@ -33,20 +34,19 @@

Контакты

- {% for contact in contractor_contacts %} -
+ {% for contact in contacts_users %} +
+
- - + +
@@ -137,7 +138,7 @@

Заказы

{% for order in orders %} -
+

{{ order }} @@ -161,15 +162,15 @@

- {% for msg in chat_messages %} -
-
-

{{ msg.sender.get_full_name }}

- {{ msg.created }} -
-

{{ msg }}

-
- {% endfor %} +{# {% for msg in chat_messages %}#} +{#
#} +{#
#} +{#

{{ msg.sender.get_full_name }}

#} +{# {{ msg.created }}#} +{#
#} +{#

{{ msg }}

#} +{#
#} +{# {% endfor %}#}
@@ -364,6 +365,7 @@ var SocketHandler = function () { }; sock.onmessage = function (event) { console.log(event.data); + alert(event.data); var message = JSON.parse(event.data); var inbox = document.getElementById('message-chat-space'); inbox.innerHTML += '
' + @@ -404,6 +406,7 @@ var SocketHandler = function () { } $(function () { + var currentChatUser = {{ request.user.pk }}; var socket = new SocketHandler(); var form = document.getElementById('message_form'); var csrftoken = getCookie('csrftoken'); @@ -411,7 +414,9 @@ $(function () { // Вытащить сообщения для чата заказа $('.order-block').on('click', function () { var orderId = $(this).attr('data-id'); + var recipentId = $(this).attr('data-recipent-id'); $("#chat-contractor-order #orderId").val(orderId); + $("#chat-contractor-order #recipentId").val(recipentId); var inbox = document.getElementById('message-chat-order-space'); inbox.innerHTML = ''; $.ajax({ @@ -439,14 +444,21 @@ $(function () { $.ajax({ url: '/api/message', type: 'GET', - data: {csrfmiddlewaretoken: csrftoken, 'recipent__id': userId}, + data: {csrfmiddlewaretoken: csrftoken, 'operand': 'in', 'sender_id':currentChatUser,'recipent_id': userId}, dataType: 'json', success: function (json) { $.each(json.results, function (i, v) { + console.log(v.sender.id); - console.log(v.recipent.id); - inbox.innerHTML += '
' + - '

Иванов

13.0.2016
' + + var senderName = 'Вы'; + var className = 'youChat'; + if(v.sender.id == userId){ + senderName = v.sender.username; + className = ''; + } + + inbox.innerHTML += '
' + + '

'+ senderName +'

'+ v.created +'
' + '

' + v.text + '

'; }); } @@ -469,14 +481,15 @@ $(function () { } }); + var inbox = $('#message-chat-space').html(); + $('#message-chat-space').html(inbox + '
' + + '

Вы

Сейчас
' + + '

' + chatMessage +'

'); + $("#chat").val(""); }); }); - - - - {% endblock %} diff --git a/chat/templates/chat_customer.html b/chat/templates/chat_customer.html index aab3ae6..57924d8 100644 --- a/chat/templates/chat_customer.html +++ b/chat/templates/chat_customer.html @@ -1,326 +1,378 @@ {% extends 'partials/base.html' %} {% load staticfiles %} +{% load thumbnail %} {% block content %} -{% include 'partials/header.html' %} -
-
-
-

Чат

-
-
-
-
- -
-
-
-
-
-
-

Контакты

- {% for contact in customer_contacts %} - - - {% endfor %} - -
-
-
-
-
- - - - - -
-
-

Прикрепить файл

+ {% include 'partials/header.html' %} +
+
+
+

Чат

+
+
+
+
+ +
+
+
+
+
+
+

Контакты

+ {% for contact in contacts_users %} +
+
+ {% thumbnail contact.avatar "60x60" crop="center" as im %} + mess-image + {% endthumbnail %} +
+

+ {{ contact.username }} {{ contact.username }} +

+ Контакты + 0 + + Удалить контакт + +
+ {% endfor %} + +
+
+
+
+
+ + + + + +
+
+

Прикрепить файл

Не более 10 файлов с общим объемом 500мб -
- - отправить - -
- - -
-
-
-

Контакты

- -
-
-
- Начало: - Срок сдачи: -
-
- 13.0.2016 - 13.0.2016 -
-
-
-

Входящие документы

-
    -
  • - Архитерурное 2.jpg - 7мб -
    -
  • -
- - Распечатать с помощью ресурса - -
-
-

Для заметок

- - сохранить -
- - -
-
-
-
- -
-

Заказы

- {% for order in orders %} -
- -

- {{ order }} -

-
-

- Испонитель: {{ order.order.contractor.get_full_name }} -

-{#

#} -{# Чаты: Иванов, Петров, Пенкин#} -{#

#} - - - Полное описание заказа - -
-
- {% endfor%} -
- -
-
-
- {% for msg in chat_messages %} -{#
#} -{#
#} -{#

{{ msg.sender.get_full_name }}

#} -{# {{ msg.created }}#} -{#
#} -{#

{{ msg }}

#} -{#
#} - {% endfor %} +
+ + отправить + +
+ + +
+
+
+

Контакты

+ +
+
+
+ Начало: + Срок сдачи: +
+
+ 13.0.2016 + 13.0.2016 +
+
+
+

Входящие документы

+
    +
  • + Архитерурное 2.jpg + 7мб +
    +
  • +
+ + Распечатать с помощью ресурса + +
+
+

Для заметок

+ + сохранить +
+ + +
-
- - - - -
-
-

Прикрепить файл

+
+
+ +
+

Заказы

+ {% for order in orders %} +
+ +

+ {{ order }} +

+
+

+ Испонитель: {{ order.order.contractor.get_full_name }} +

+ {#

#} + {# Чаты: Иванов, Петров, Пенкин#} + {#

#} + + + Полное описание заказа + +
+
+ {% endfor %} +
+ +
+
+
+ {% for msg in chat_messages %} + {#
#} + {#
#} + {#

{{ msg.sender.get_full_name }}

#} + {# {{ msg.created }}#} + {#
#} + {#

{{ msg }}

#} + {#
#} + {% endfor %} +
+ + + + + +
+
+

Прикрепить файл

Не более 10 файлов с общим объемом 500мб -
- отправить -
- -
-
-
-

Для заметок

- - сохранить -
- -
-
-
- {% include 'partials/footer.html' %} -
-
+
+ отправить +
+ +
+
+
+

Для заметок

+ + сохранить +
+ +
+
+
+ {% include 'partials/footer.html' %} +
+
{% endblock %} {% block js_block %} + {% endblock %} diff --git a/chat/views.py b/chat/views.py index 9bae005..e1d3b70 100644 --- a/chat/views.py +++ b/chat/views.py @@ -3,6 +3,7 @@ from django.views.generic import View from django.db.models import Q from .models import Message +from users.models import User class ChatUserView(View): @@ -10,25 +11,40 @@ class ChatUserView(View): def get(self, request, *args, **kwargs): if request.user.is_authenticated() and request.user.is_customer(): - customer_contacts = Message.objects.values('sender_id','recipent_id').\ - filter(Q(recipent_id=request.user.pk)| Q(sender_id=request.user.pk)).distinct() + customer_contacts = Message.objects.values_list('sender_id', 'recipent_id'). \ + filter(Q(recipent_id=request.user.pk) | Q(sender_id=request.user.pk)).distinct() + + users_ids = [] + for msg in customer_contacts: + a, b = msg + if a != request.user.pk: + users_ids.append(a) + if b != request.user.pk: + users_ids.append(b) + contacts_users = User.objects.filter(pk__in=users_ids) + chat_messages = Message.objects.filter(Q(sender=request.user.pk) | Q(recipent=request.user.pk)) orders = request.user.projects.select_related('order').all() self.template_name = 'chat_customer.html' - return render(request, self.template_name, {'customer_contacts': customer_contacts, + return render(request, self.template_name, {'contacts_users': contacts_users, 'chat_messages': chat_messages, 'orders': orders}) else: orders = request.user.orders.all() - # contractor_contacts = request.user.sender_messages.values('recipent_id').distinct('recipent_id').\ - # values('recipent_id__email', 'recipent_id__first_name', 'recipent_id__last_name', 'recipent_id__id') - contractor_contacts = Message.objects.values('sender_id','recipent_id').\ - filter(Q(recipent_id=request.user.pk)| Q(sender_id=request.user.pk)).distinct() - - chat_messages = Message.objects.filter(Q(sender=request.user.pk) | Q(recipent=request.user.pk)).order_by('created') + contractor_contacts = Message.objects.values_list('sender_id', 'recipent_id').filter( + Q(recipent_id=request.user.pk) | Q(sender_id=request.user.pk)).distinct() + users_ids = [] + for msg in contractor_contacts: + a, b = msg + if a != request.user.pk: + users_ids.append(a) + if b != request.user.pk: + users_ids.append(b) + contacts_users = User.objects.filter(pk__in=users_ids) + chat_messages = Message.objects.filter(Q(sender=request.user.pk) | Q(recipent=request.user.pk)).order_by( + 'created') self.template_name = 'chat_contractor.html' return render(request, self.template_name, {'orders': orders, - 'contractor_contacts': contractor_contacts, - 'chat_messages': chat_messages}) - - + 'contacts_users': contacts_users, + 'chat_messages': chat_messages + }) diff --git a/templates/home.html b/templates/home.html index ce3ac94..2052352 100644 --- a/templates/home.html +++ b/templates/home.html @@ -50,7 +50,7 @@