From 07c4653dbf7c8fedd0c08938ab0771566c6fadbd Mon Sep 17 00:00:00 2001 From: Yuval-Vino <99268065+Yuval-Vino@users.noreply.github.com> Date: Sun, 1 May 2022 17:38:40 +0300 Subject: [PATCH] Adding message and chat HTML author Yuval-Vino <99268065+Yuval-Vino@users.noreply.github.com> --- Hotails/urls.py | 2 + main/templates/main/base_template.html | 41 +++++--- main/views.py | 12 +++ message/templates/chat.html | 80 ++++++++++++++ message/templates/messages.html | 43 ++++++++ message/test_html.py | 65 ++++++++++++ message/views.py | 73 ++++++++++++- static/CSS/chat.css | 138 +++++++++++++++++++++++++ 8 files changed, 436 insertions(+), 18 deletions(-) create mode 100644 message/templates/chat.html create mode 100644 message/templates/messages.html create mode 100644 message/test_html.py create mode 100644 static/CSS/chat.css diff --git a/Hotails/urls.py b/Hotails/urls.py index 21257e0..c72bc51 100644 --- a/Hotails/urls.py +++ b/Hotails/urls.py @@ -26,4 +26,6 @@ path('homepage/', views.homepage, name='homepage'), path('logout/', views.logout_view, name='logout'), path('about/', views.about, name='about'), + path('messages/', views.messages_view, name='messages'), + path('chat/', views.chat_view, name='chat'), ] diff --git a/main/templates/main/base_template.html b/main/templates/main/base_template.html index b045033..0ea6dc4 100644 --- a/main/templates/main/base_template.html +++ b/main/templates/main/base_template.html @@ -12,36 +12,43 @@ + {% block stylesheets %} {% endblock %}
-
- - {% block content %} - {% endblock %} +
+
+ {% block content %} + {% endblock %} +
+
+ \ No newline at end of file diff --git a/main/views.py b/main/views.py index 842c52d..f7352d2 100644 --- a/main/views.py +++ b/main/views.py @@ -2,10 +2,12 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import redirect from django.contrib.auth import logout + from dogowner.models import DogOwner from daycare.models import DayCare from dogowner.views import dog_owner_home from daycare.views import daycare_home +import message.views def index(request): @@ -29,3 +31,13 @@ def about(request): def logout_view(request): logout(request) return index(request) + + +@login_required() +def messages_view(request): + return message.views.messages(request) + + +@login_required() +def chat_view(request, contact): + return message.views.chat(request, contact) diff --git a/message/templates/chat.html b/message/templates/chat.html new file mode 100644 index 0000000..141dce1 --- /dev/null +++ b/message/templates/chat.html @@ -0,0 +1,80 @@ +{% extends "main/base_template.html" %} +{% load static %} + +{% block stylesheets %} + + +{% endblock %} + +{% block content %} +
+
+
+
+
+
+

Recent Chats

+
+
+ +
+

Chat with {{ contact_name }}

+
+
+ {% for msg in chat %} + {% if msg.author == user %} +
+
+

{{msg.text}}

+ {{msg.date}}
+
+ {% else %} +
+ {% if user == 'D' %} +
nopic
+ {% else %} +
nopic
+ {% endif %} +
+
+

{{ msg.text }}

+ {{msg.date}}
+
+
+ {% endif %} + {% endfor %} +
+
+ {% csrf_token %} +
+
+ + +
+
+
+
+
+
+
+ +{% endblock %} \ No newline at end of file diff --git a/message/templates/messages.html b/message/templates/messages.html new file mode 100644 index 0000000..fbfeb59 --- /dev/null +++ b/message/templates/messages.html @@ -0,0 +1,43 @@ +{% extends "main/base_template.html" %} +{% load static %} + +{% block stylesheets %} + + +{% endblock %} + +{% block content %} +
+
+
+
+
+
+

Recent Chats

+
+
+ +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/message/test_html.py b/message/test_html.py new file mode 100644 index 0000000..96bf07b --- /dev/null +++ b/message/test_html.py @@ -0,0 +1,65 @@ +from .models import Message +from daycare.models import DayCare +import pytest + + +@pytest.mark.django_db() +class TestMessagesPresentation: + def test_shown_only_relevant_contacts(self, client): + static_daycares = [daycare for daycare in DayCare.objects.all() if 'static' in daycare.user.username] + for daycare in static_daycares: + client.force_login(user=daycare.user) + response = client.get('/messages/') + + assert response.status_code == 200 + + shown_contacts = response.context['contacts'] + daycare_contacts = Message.get_all_daycare_contacts(daycare.id) + assert set(daycare_contacts) == set(shown_contacts) + + client.logout() + + def test_shown_only_relevant_message_in_chat(self, client): + static_daycares = [daycare for daycare in DayCare.objects.all() if 'static' in daycare.user.username] + for daycare in static_daycares: + client.force_login(user=daycare.user) + for contact in Message.get_all_daycare_contacts(daycare.id): + contact_id = str(contact.id) + response = client.get(f'/chat/{contact_id}' + ) + assert response.status_code == 200 + + shown_chat = set(response.context['chat']) + relevant_chat = set(Message.get_chat_between_dogowner_daycare(contact_id, daycare.id)) + assert shown_chat == relevant_chat + client.logout() + + def test_send_new_message(self, client): + daycare = [daycare for daycare in DayCare.objects.all() if 'static' in daycare.user.username][1] + contact = Message.get_all_daycare_contacts(daycare.id)[0] + contact_id = str(contact.id) + client.force_login(user=daycare.user) + client.get(f'/chat/{contact_id}') + + msg = {'msg_sent': "Test message!"} + client.post(f'/chat/{contact_id}', msg) + + last_message_in_chat = Message.get_chat_between_dogowner_daycare(dogowner_id=contact_id, + daycare_id=daycare.id).last() + assert last_message_in_chat.text == "Test message!" + + def test_cant_send_blank_message(self, client): + daycare = [daycare for daycare in DayCare.objects.all() if 'static' in daycare.user.username][1] + contact = Message.get_all_daycare_contacts(daycare.id)[0] + contact_id = str(contact.id) + client.force_login(user=daycare.user) + client.get(f'/chat/{contact_id}') + + last_message_before_test = Message.get_chat_between_dogowner_daycare(dogowner_id=contact_id, + daycare_id=daycare.id).last() + msg = {'msg_sent': ""} + client.post(f'/chat/{contact_id}', msg) + + last_message_after_test = Message.get_chat_between_dogowner_daycare(dogowner_id=contact_id, + daycare_id=daycare.id).last() + assert last_message_after_test == last_message_before_test diff --git a/message/views.py b/message/views.py index fd0e044..bdfc9ae 100644 --- a/message/views.py +++ b/message/views.py @@ -1,3 +1,74 @@ -# from django.shortcuts import render +from django.contrib.auth.decorators import login_required +from django.shortcuts import render # Create your views here. +from dogowner.models import DogOwner +from daycare.models import DayCare +from message.models import Message +from message.models import AuthorOptions + + +@login_required() +def messages(request): + if DogOwner.objects.filter(user=request.user).exists(): + user = DogOwner.objects.get(user=request.user) + contacts = get_dogowner_contacts_as_daycares(user.id) + user = AuthorOptions.DogOwner + + else: + user = DayCare.objects.get(user=request.user) + contacts = get_daycare_contacts_as_dogowners(user.id) + user = AuthorOptions.DayCare + + context = { + 'contacts': contacts, + 'user': user, + } + return render(request, 'messages.html', context) + + +@login_required() +def chat(request, contact_id): + if DogOwner.objects.filter(user=request.user).exists(): + daycare = DayCare.objects.get(pk=contact_id) + dogowner = DogOwner.objects.get(user=request.user) + contact_name = daycare.name + contacts = get_dogowner_contacts_as_daycares(dogowner.id) + user = AuthorOptions.DogOwner + + else: + daycare = DayCare.objects.get(user=request.user) + dogowner = DogOwner.objects.get(pk=contact_id) + contact_name = dogowner.first_name + " " + dogowner.last_name + contacts = get_daycare_contacts_as_dogowners(daycare.id) + user = AuthorOptions.DayCare + + chat = Message.get_chat_between_dogowner_daycare(dogowner_id=dogowner.id, daycare_id=daycare.id) + + if request.method == "POST": + msg = request.POST.get("msg_sent", "") + if msg != '': + Message.create(author=user, daycare_id=daycare, dogowner_id=dogowner, text=msg).save() + + context = { + 'chat': chat, + 'user': user, + 'contact_id': contact_id, + 'contact_name': contact_name, + 'contacts': contacts, + 'dog_owner_picture': dogowner.dog_picture_url, + 'day_care_img': daycare.get_daycare_primary_image_url + } + return render(request, 'chat.html', context) + + +def get_dogowner_contacts_as_daycares(dogowner_id): + user_contacts = Message.get_all_dogowner_contacts(dogowner_id) + contacts_id = [contact.id for contact in user_contacts] + return DayCare.objects.filter(pk__in=contacts_id) + + +def get_daycare_contacts_as_dogowners(daycare_id): + user_contacts = Message.get_all_daycare_contacts(daycare_id) + contacts_id = [contact.id for contact in user_contacts] + return DogOwner.objects.filter(pk__in=contacts_id) diff --git a/static/CSS/chat.css b/static/CSS/chat.css new file mode 100644 index 0000000..944a810 --- /dev/null +++ b/static/CSS/chat.css @@ -0,0 +1,138 @@ +.chatcontainer{ + width: 100%; + height: 100%; +} +img{ max-width:100%;} +.inbox_people { + background: #f8f8f8 none repeat scroll 0 0; + float: left; + overflow: hidden; + width: 40%; border-right:1px solid #c4c4c4; +} +.inbox_msg { + border: 1px solid #c4c4c4; + clear: both; + overflow: hidden; +} +.top_spac{ margin: 20px 0 0;} + + +.recent_heading {float: left; width:40%;} +.srch_bar { + display: inline-block; + text-align: right; + width: 60%; +} +.headind_srch{ padding:10px 29px 10px 20px; overflow:hidden; border-bottom:1px solid #c4c4c4;} + +.recent_heading h4 { + color: #05728f; + font-size: 21px; + margin: auto; +} +.srch_bar input{ border:1px solid #cdcdcd; border-width:0 0 1px 0; width:80%; padding:2px 0 4px 6px; background:none;} +.srch_bar .input-group-addon button { + background: rgba(0, 0, 0, 0) none repeat scroll 0 0; + border: medium none; + padding: 0; + color: #707070; + font-size: 18px; +} +.srch_bar .input-group-addon { margin: 0 0 0 -27px;} + +.chat_ib h5{ font-size:15px; color:#464646; margin:0 0 8px 0;} +.chat_ib h5 span{ font-size:13px; float:right;} +.chat_ib p{ font-size:14px; color:#989898; margin:auto} +.chat_img { + float: left; + width: 11%; +} +.chat_ib { + float: left; + padding: 0 0 0 15px; + width: 88%; +} + +.chat_people{ overflow:hidden; clear:both;} +.chat_list { + border-bottom: 1px solid #c4c4c4; + margin: 0; + padding: 18px 16px 10px; +} +.inbox_chat { height: 550px; overflow-y: scroll;} + +.active_chat{ background:#ebebeb;} + +.incoming_msg_img { + display: inline-block; + width: 6%; +} +.received_msg { + display: inline-block; + padding: 0 0 0 10px; + vertical-align: top; + width: 92%; + } + .received_withd_msg p { + background: #ebebeb none repeat scroll 0 0; + border-radius: 3px; + color: #646464; + font-size: 14px; + margin: 0; + padding: 5px 10px 5px 12px; + width: 100%; +} +.time_date { + color: #747474; + display: block; + font-size: 12px; + margin: 8px 0 0; +} +.received_withd_msg { width: 57%;} +.mesgs { + float: left; + padding: 30px 15px 0 25px; + width: 60%; +} + + .sent_msg p { + background: #05728f none repeat scroll 0 0; + border-radius: 3px; + font-size: 14px; + margin: 0; color:#fff; + padding: 5px 10px 5px 12px; + width:100%; +} +.outgoing_msg{ overflow:hidden; margin:26px 0 26px;} +.sent_msg { + float: right; + width: 46%; +} +.input_msg_write input { + background: rgba(0, 0, 0, 0) none repeat scroll 0 0; + border: medium none; + color: #4c4c4c; + font-size: 15px; + min-height: 48px; + width: 100%; +} + +.type_msg {border-top: 1px solid #c4c4c4;position: relative;} +.msg_send_btn { + background: #05728f none repeat scroll 0 0; + border: medium none; + border-radius: 50%; + color: #fff; + cursor: pointer; + font-size: 17px; + height: 33px; + position: absolute; + right: 0; + top: 11px; + width: 33px; +} +.messaging { padding: 0 0 50px 0;} +.msg_history { + height: 516px; + overflow-y: auto; +} \ No newline at end of file