diff --git a/AuthApp/js_urls.py b/AuthApp/js_urls.py index 00a21a8..f071d50 100644 --- a/AuthApp/js_urls.py +++ b/AuthApp/js_urls.py @@ -15,7 +15,7 @@ urlpatterns = [ path('my_routes/', my_routes_ajax, name='my_routes_ajax'), path('subscribe/', subscribe_ajax, name='subscribe_ajax'), - path('new_msg_to_user/', new_msg_to_user_ajax, name='new_msg_to_user' ), + path('chats/', chats_ajax, name='chats_ajax'), path('support_tickets/', support_tickets_ajax, name='support_tickets_ajax'), diff --git a/AuthApp/js_views.py b/AuthApp/js_views.py index 4d51687..d3c1fde 100644 --- a/AuthApp/js_views.py +++ b/AuthApp/js_views.py @@ -25,14 +25,14 @@ def subscribe_ajax(request): return JsonResponse({'html': html}, status=200) @login_required(login_url='/profile/login/') -def new_msg_to_user_ajax(request): +def chats_ajax(request): if request.method != 'POST': raise Http404 Dict = { } - html = render_to_string('blocks/profile/b_new_msg_to_user.html', Dict, request=request) + html = render_to_string('blocks/profile/b_chats.html', Dict, request=request) return JsonResponse({'html': html}, status=200) @login_required(login_url='/profile/login/') diff --git a/AuthApp/urls.py b/AuthApp/urls.py index 4a8dac7..f2aec95 100644 --- a/AuthApp/urls.py +++ b/AuthApp/urls.py @@ -9,6 +9,8 @@ urlpatterns = [ path('registration/', registration_View, name='registration_page'), path('', user_profile_View, name='user_profile'), + path('chat//', chat_w_user_View, name='chat_w_user'), + path('chat/', chat_w_user_View, name='chat_w_user_wo_user_id'), path('login/', login_View, name='login_profile'), diff --git a/AuthApp/views.py b/AuthApp/views.py index 4cc18a5..c47eb2f 100644 --- a/AuthApp/views.py +++ b/AuthApp/views.py @@ -22,6 +22,34 @@ def registration_View(request): return HttpResponse(t.render(Dict, request)) + +@login_required(login_url='/profile/login/') +def chat_w_user_View(request, user_id=None): + from ChatServiceApp.funcs import get_chat_receivers_for_user, get_msgs_for_chat_w_users + + receivers = get_chat_receivers_for_user(request.user) + + cur_chat_msgs = None + + try: + cur_receiver = User.objects.get(id=user_id) + if not cur_receiver in receivers: + receivers.insert(0, cur_receiver) + cur_chat_msgs = get_msgs_for_chat_w_users(request.user, cur_receiver) + except User.DoesNotExist: + cur_receiver = None + + + Dict = { + 'page': 'chat', + 'cur_receiver': cur_receiver, + 'receivers': receivers, + 'cur_chat_msgs':cur_chat_msgs + } + + t = loader.get_template('pages/profile/p_user_profile.html') + return HttpResponse(t.render(Dict, request)) + @login_required(login_url='/profile/login/') def user_profile_View(request): diff --git a/ChatServiceApp/funcs.py b/ChatServiceApp/funcs.py index 7e74b52..0097fdb 100644 --- a/ChatServiceApp/funcs.py +++ b/ChatServiceApp/funcs.py @@ -1,4 +1,27 @@ from .models import * +from django.db.models import Q + + +def get_msgs_for_chat_w_users(user1, user2): + + msgs = Message.objects.filter( + Q(sender=user1) | Q(receiver=user1), + Q(sender=user2) | Q(receiver=user2), + group=None + ) + return msgs + +def get_chat_receivers_for_user(user): + receivers = Message.objects.filter( + Q(sender=user) | Q(receiver=user), + group=None + ).order_by('-modifiedDT').values('sender', 'receiver') + + receivers_list = [] + receivers_list.extend([item['sender'] for item in receivers if item['sender'] != user]) + receivers_list.extend([item['receiver'] for item in receivers if item['receiver'] != user]) + + return receivers_list def get_messages_for_ticket(ticket): return ticket.rel_messages_for_group.filter(enable=True).order_by('-modifiedDT') diff --git a/ChatServiceApp/templatetags/tt_chat.py b/ChatServiceApp/templatetags/tt_chat.py index 4c93e8e..e65385a 100644 --- a/ChatServiceApp/templatetags/tt_chat.py +++ b/ChatServiceApp/templatetags/tt_chat.py @@ -17,10 +17,12 @@ def get_msg_side(cur_user, ticket, msg): return 'right' else: return 'left' - else: + elif ticket: if ticket.owner == cur_user: return 'right' else: return 'left' + else: + return 'right' # return 'left' \ No newline at end of file diff --git a/templates/blocks/b_user_profile.html b/templates/blocks/b_user_profile.html index 8e39b96..732cab5 100644 --- a/templates/blocks/b_user_profile.html +++ b/templates/blocks/b_user_profile.html @@ -31,20 +31,10 @@
-

Добро пожаловать: {{ user.first_name }} {{ user.last_name }} ({{ user.username }})

-
-
-
Статус: {{ user.user_profile.get_account_type_display }}
-{#
#} -{#
#} -
-
-
Если хотите отправить посылку - зарегистрируйтесь, как отправитель
-
Если у Вас возникнут вопросы Вы можете связаться с нами: support@twb.com
-
У Вас три новых сообщения. Посмотреть
-
Хотите получать уведомление о появлении посылок? Заполните форму
+ {% if not page %} + {% include "blocks/profile/b_profile_first_page.html" %} + {% elif page == 'chat' %} + {% include "blocks/profile/b_chats.html" %} + {% endif %}
\ No newline at end of file diff --git a/templates/blocks/profile/b_new_msg_to_user.html b/templates/blocks/profile/b_chats.html similarity index 100% rename from templates/blocks/profile/b_new_msg_to_user.html rename to templates/blocks/profile/b_chats.html diff --git a/templates/blocks/profile/b_profile_first_page.html b/templates/blocks/profile/b_profile_first_page.html new file mode 100644 index 0000000..a751731 --- /dev/null +++ b/templates/blocks/profile/b_profile_first_page.html @@ -0,0 +1,15 @@ +

Добро пожаловать: {{ user.first_name }} {{ user.last_name }} ({{ user.username }})

+
+
+
Статус: {{ user.user_profile.get_account_type_display }}
+{#
#} +{#
#} +
+
+
Если хотите отправить посылку - зарегистрируйтесь, как отправитель
+
Если у Вас возникнут вопросы Вы можете связаться с нами: support@twb.com
+
У Вас три новых сообщения. Посмотреть
+
Хотите получать уведомление о появлении посылок? Заполните форму
\ No newline at end of file