-
Notifications
You must be signed in to change notification settings - Fork 7
Adding message and chat HTML #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| {% extends "main/base_template.html" %} | ||
| {% load static %} | ||
|
|
||
| {% block stylesheets %} | ||
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | ||
| <link rel="stylesheet" type="text/css" href="{% static 'css/chat.css' %}"> | ||
| {% endblock %} | ||
|
|
||
| {% block content %} | ||
| <div class="chatcontainer"> | ||
| <div class="messaging"> | ||
| <div class="inbox_msg"> | ||
| <div class="inbox_people"> | ||
| <div class="headind_srch"> | ||
| <div class="recent_heading"> | ||
| <h4>Recent Chats</h4> | ||
| </div> | ||
| </div> | ||
| <div class="inbox_chat"> | ||
| {% for contact in contacts %} | ||
| <a href="/chat/{{contact.id}}"><div class="chat_list"> | ||
| <div class="chat_people"> | ||
| {% if user == 'D' %} | ||
| <div class="chat_img"> <img src="{{contact.dog_picture_url}}" alt="nopic"> </div> | ||
| <div class="chat_ib"> | ||
| <h5>{{ contact }}</h5> | ||
| </div> | ||
| {% else %} | ||
| <div class="chat_img"> <img src="{{contact.get_daycare_primary_image_url}}" alt="nopic"> </div> | ||
| <div class="chat_ib"> | ||
| <h5>{{ contact.name }}</h5> | ||
| </div> | ||
| {% endif %} | ||
| </div> | ||
| </div> | ||
| </a> | ||
| {% endfor %} | ||
| </div> | ||
| </div> | ||
| <h3 class=" text-center">Chat with {{ contact_name }}</h3> | ||
| <div class="mesgs"> | ||
| <div class="msg_history"> | ||
| {% for msg in chat %} | ||
| {% if msg.author == user %} | ||
| <div class="outgoing_msg"> | ||
| <div class="sent_msg"> | ||
| <p>{{msg.text}}</p> | ||
| <span class="time_date">{{msg.date}}</span> </div> | ||
| </div> | ||
| {% else %} | ||
| <div class="incoming_msg"> | ||
| {% if user == 'D' %} | ||
| <div class="incoming_msg_img"> <img src="{{dog_owner_picture}}" alt="nopic"> </div> | ||
| {% else %} | ||
| <div class="incoming_msg_img"> <img src="{{day_care_img}}" alt="nopic"> </div> | ||
| {% endif %} | ||
| <div class="received_msg"> | ||
| <div class="received_withd_msg"> | ||
| <p>{{ msg.text }}</p> | ||
| <span class="time_date">{{msg.date}}</span></div> | ||
| </div> | ||
| </div> | ||
| {% endif %} | ||
| {% endfor %} | ||
| </div> | ||
| <form method="POST"> | ||
| {% csrf_token %} | ||
| <div class="type_msg"> | ||
| <div class="input_msg_write"> | ||
| <input type="text" name="msg_sent" class="write_msg" placeholder="Type a message" /> | ||
| <button class="msg_send_btn" type="submit"><i class="fa fa-paper-plane-o" aria-hidden="true"></i></button> | ||
| </div> | ||
| </div> | ||
| </form> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| {% extends "main/base_template.html" %} | ||
| {% load static %} | ||
|
|
||
| {% block stylesheets %} | ||
| <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | ||
| <link rel="stylesheet" type="text/css" href="{% static 'css/chat.css' %}"> | ||
| {% endblock %} | ||
|
|
||
| {% block content %} | ||
| <div class="chatcontainer"> | ||
| <div class="messaging"> | ||
| <div class="inbox_msg"> | ||
| <div class="inbox_people"> | ||
| <div class="headind_srch"> | ||
| <div class="recent_heading"> | ||
| <h4>Recent Chats</h4> | ||
| </div> | ||
| </div> | ||
| <div class="inbox_chat"> | ||
| {% for contact in contacts %} | ||
| <a href="/chat/{{contact.id}}"><div class="chat_list"> | ||
| <div class="chat_people"> | ||
| {% if user == 'D' %} | ||
| <div class="chat_img"> <img src="{{contact.dog_picture_url}}" alt="nopic"> </div> | ||
| <div class="chat_ib"> | ||
| <h5>{{ contact }}</h5> | ||
| </div> | ||
| {% else %} | ||
| <div class="chat_img"> <img src="{{contact.get_daycare_primary_image_url}}" alt="nopic"> </div> | ||
| <div class="chat_ib"> | ||
| <h5>{{ contact.name }}</h5> | ||
| </div> | ||
| {% endif %} | ||
| </div> | ||
| </div> | ||
| </a> | ||
| {% endfor %} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| {% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.