chat dla jdnej strony

This commit is contained in:
Jędrzej Klepacki 2021-01-07 23:55:14 +01:00
parent 5b66859a59
commit 95db90b813
7 changed files with 263 additions and 6 deletions

View File

@ -51,18 +51,18 @@
<div class="col-lg-8 col-md-6"> <div class="col-lg-8 col-md-6">
<div class="c_product_text"> <div class="c_product_text">
<h1>{{ product.title }}</h1> <a href="offer_details/{{ product.id }}"><h1>{{ product.title }}</h1></a>
<h6><a href="user_profile/{{ product.user_iden }}">{{ product.user_iden }}</a></h6> <h6> <a href="user_profile/{{ product.user_iden }}">{{ product.user_iden }}</a></h6>
<h5> <h5>
{% if types.0.0 == product.type %} {% if types.0.0 == product.type %}
Need Potrzebuje
{% else %} {% else %}
Give Oddam
{% endif %} {% endif %}
{% if types_o.0.0 == product.offer %} {% if types_o.0.0 == product.offer %}
Service Usługe
{% else %} {% else %}
Items Przedmiot
{% endif %}</h5> {% endif %}</h5>
<h6>Location: <span>{{ product.place }}</span></h6> <h6>Location: <span>{{ product.place }}</span></h6>

View File

@ -0,0 +1,195 @@
{% extends 'base.html' %}
{% load static %}
{% block content %}
<!DOCTYPE html>
<html lang="en">
<body>
<!--================Categories Banner Area =================-->
<section class="solid_banner_area">
<div class="container">
<div class="solid_banner_inner">
{% if offer.Wolontatriat %}
<h5>Ogłoszenie Wolontariackie</h5>
{% endif %}
<h2>{{ offer.title }}</h2>
<h5>{{ offer.user_iden }}</h5><p><br></p>
<img class="img-fluid" src="{{ offer.picture.url }}" width="200">
{{ offer.create_date }}
{% if types.0.0 == offer.type %}
Potrzebuje
{% else %}
Oddam
{% endif %}
{% if types_o.0.0 == offer.offer %}
Usługe
{% else %}
Przedmiot
{% endif %}
{{ offer.description }}
{{ offer.place }}
</div>
<div class="solid_banner_inner">
<h3>Wiadomości</h3>
{% if mess == 0 %}
Brak Wiadomości
{% else %}
<div class="tab">
{% for user in email_list %}
<button class="tablinks" onclick="openCity(event, '{{ user.id }}')">{{ user.email }}</button>
{% endfor %}
</div>
{% for user in user_list %}
<div id="{{ user }}" class="tabcontent">
{% for chat in mess %}
{% if user == chat.author_id.id or user == chat.sec_user_id %}
<p>{{ chat.pub_date }}</p>
<p>{{ chat.author_id }}: {{ chat.text }}</p>
-----
{% endif %}
{% endfor %}
<form action="{% url 'send_message' %}" method="post">
{% csrf_token %}
<input type="hidden" name="author_id" value="{{ offer.user_iden.id }}">
<input type="hidden" name="sec_user_id" value="{{ user }}">
<input type="hidden" name="product_id" value="{{ offer.id }}">
<textarea class="form-control" id="order" rows="3" name="text", placeholder="Napisz Wiadomość"></textarea>
<input type="submit" class="btn btn-primary update_btn" value="Wyślij Wiadomość">
</form>
</div>
{% endfor %}
{% endif %}
</div>
</div>
</section>
<script>
function openCity(evt, cityName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(cityName).style.display = "block";
evt.currentTarget.className += " active";
}
</script>
<!--================End Categories Banner Area =================-->
<!--================Categories Product Area =================-->
<section class="categories_product_main p_80">
<div class="container">
</div>
</section>
<!--================End Categories Product Area =================-->
<!--================Footer Area =================-->
<footer class="footer_area">
<div class="container">
<div class="footer_widgets">
<div class="row">
<div class="col-lg-4 col-md-4 col-6">
<aside class="f_widget f_about_widget">
<img src="img/logo.png" alt="">
<p>Persuit is a Premium PSD Template. Best choice for your online store. Let purchase it to enjoy now</p>
<h6>Social:</h6>
<ul>
<li><a href="#"><i class="social_facebook"></i></a></li>
<li><a href="#"><i class="social_twitter"></i></a></li>
<li><a href="#"><i class="social_pinterest"></i></a></li>
<li><a href="#"><i class="social_instagram"></i></a></li>
<li><a href="#"><i class="social_youtube"></i></a></li>
</ul>
</aside>
</div>
<div class="col-lg-2 col-md-4 col-6">
<aside class="f_widget link_widget f_info_widget">
<div class="f_w_title">
<h3>Information</h3>
</div>
<ul>
<li><a href="#">About us</a></li>
<li><a href="#">Delivery information</a></li>
<li><a href="#">Terms & Conditions</a></li>
<li><a href="#">Help Center</a></li>
<li><a href="#">Returns & Refunds</a></li>
</ul>
</aside>
</div>
<div class="col-lg-2 col-md-4 col-6">
<aside class="f_widget link_widget f_service_widget">
<div class="f_w_title">
<h3>Customer Service</h3>
</div>
<ul>
<li><a href="#">My account</a></li>
<li><a href="#">Ordr History</a></li>
<li><a href="#">Wish List</a></li>
<li><a href="#">Newsletter</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</aside>
</div>
<div class="col-lg-2 col-md-4 col-6">
<aside class="f_widget link_widget f_extra_widget">
<div class="f_w_title">
<h3>Extras</h3>
</div>
<ul>
<li><a href="#">Brands</a></li>
<li><a href="#">Gift Vouchers</a></li>
<li><a href="#">Affiliates</a></li>
<li><a href="#">Specials</a></li>
</ul>
</aside>
</div>
<div class="col-lg-2 col-md-4 col-6">
<aside class="f_widget link_widget f_account_widget">
<div class="f_w_title">
<h3>My Account</h3>
</div>
<ul>
<li><a href="#">My account</a></li>
<li><a href="#">Ordr History</a></li>
<li><a href="#">Wish List</a></li>
<li><a href="#">Newsletter</a></li>
</ul>
</aside>
</div>
</div>
</div>
<div class="footer_copyright">
<h5>© <script>document.write(new Date().getFullYear());</script> <!-- Link back to Colorlib can't be removed. Template is licensed under CC BY 3.0. -->
Copyright &copy;<script>document.write(new Date().getFullYear());</script> All rights reserved | This template is made with <i class="fa fa-heart-o" aria-hidden="true"></i> by <a href="https://colorlib.com" target="_blank">Colorlib</a>. Downloaded from <a href="https://themeslab.org/" target="_blank">Themeslab</a>
<!-- Link back to Colorlib can't be removed. Template is licensed under CC BY 3.0. -->
</h5>
</div>
</div>
</footer>
<!--================End Footer Area =================-->
</body>
</html>
{% endblock %}

View File

@ -9,6 +9,8 @@ urlpatterns = [
path('product', views.index_product, name='index_product'), path('product', views.index_product, name='index_product'),
path('offer', views.index_offer, name='index_offer'), path('offer', views.index_offer, name='index_offer'),
path('all', views.index, name='index'), path('all', views.index, name='index'),
path('offer_details/<int:offer_id>', views.offer, name='offer'),
path('send_message', views.send_message, name='send_message'),
path('', views.index, name='index'), path('', views.index, name='index'),
path('contact', views.contact, name='contact'), path('contact', views.contact, name='contact'),

View File

@ -6,6 +6,7 @@ from django.urls import reverse
from django.template import loader from django.template import loader
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from django.contrib import messages, auth from django.contrib import messages, auth
from django.utils import timezone
from .models import Product, TYPE_T, TYPE_O, Places, Ocena, chat from .models import Product, TYPE_T, TYPE_O, Places, Ocena, chat
@ -488,3 +489,62 @@ def add_comment(request, user_name):
return HttpResponse(template.render(context, request)) return HttpResponse(template.render(context, request))
else: else:
return redirect(login) return redirect(login)
def offer(request, offer_id):
offer = Product.objects.filter(id = offer_id, active = True).first()
mess = 0
if chat.objects.filter(product_id = offer_id, author_id=request.user.id).exists():
chat_fst = chat.objects.filter(product_id = offer_id, author_id=request.user.id)
else:
chat_fst = chat.objects.none()
mess = mess + 1
if chat.objects.filter(product_id = offer_id, sec_user_id=request.user.id).exists():
chat_scd = chat.objects.filter(product_id = offer_id, sec_user_id=request.user.id)
else:
chat_scd = chat.objects.none()
mess = mess + 1
if mess == 2:
mess = 0
user_list = 0
else:
mess = chat_fst.union(chat_scd).order_by('pub_date')
if offer.user_iden == request.user:
user_list = chat.objects.values_list('author_id', flat=True).distinct('author_id').filter(product_id = offer_id, sec_user_id = request.user.id).exclude(author_id = request.user.id)
User = get_user_model()
email_list = User.objects.filter(id__in = user_list)
template = loader.get_template('homepage/offer_page.html')
types = TYPE_T
types_o = TYPE_O
context = {
'offer': offer,
'types': types,
'types_o': types_o,
'mess': mess,
'user_list': user_list,
'email_list': email_list,
}
return HttpResponse(template.render(context, request))
def send_message(request):
if request.user.is_authenticated:
if request.method == 'POST':
author_id = request.POST['author_id']
sec_user_id = request.POST['sec_user_id']
product_id = request.POST['product_id']
text = request.POST['text']
User = get_user_model()
massage = chat(
product_id = Product.objects.filter(id=product_id).first(),
author_id = User.objects.filter(id=author_id).first(),
sec_user_id = sec_user_id,
text = text,
pub_date = timezone.now(),
)
massage.save()
return redirect(offer, product_id)
else:
return redirect(login)