From 95db90b81369fe2b0050c455da08298de77d49e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Klepacki?= Date: Thu, 7 Jan 2021 23:55:14 +0100 Subject: [PATCH] chat dla jdnej strony --- .../homepage/__pycache__/admin.cpython-37.pyc | Bin 344 -> 346 bytes .../homepage/__pycache__/urls.cpython-37.pyc | Bin 1190 -> 1278 bytes .../homepage/__pycache__/views.cpython-37.pyc | Bin 10901 -> 12217 bytes .../homepage/templates/homepage/index.html | 12 +- .../templates/homepage/offer_page.html | 195 ++++++++++++++++++ SocialHelper/homepage/urls.py | 2 + SocialHelper/homepage/views.py | 60 ++++++ 7 files changed, 263 insertions(+), 6 deletions(-) create mode 100644 SocialHelper/homepage/templates/homepage/offer_page.html diff --git a/SocialHelper/homepage/__pycache__/admin.cpython-37.pyc b/SocialHelper/homepage/__pycache__/admin.cpython-37.pyc index 9b3b46f396b1b7aea50c8a258391b51d29bd1b64..632d36ad3ceb43b75beba187ab73cfd22c3034c6 100644 GIT binary patch delta 29 jcmcb?bc>10iIS^Dy}` zGRjYmVoqe_oxGjdlc`8<@?T~x0X1M)6oH6e#;#T|0Y#HdS!@MNfP4W)9wr_}9%ep9 KKE}y)EE)hp^iw1N delta 252 zcmeyzxr|fYiIBnCw4bVl**?hrSSf(?lQT#RtzBx?KZ2IFozs@1Aq+ znRD*>-TCO{?~BP}i9}4m=Y{15#lK%#NshwY%B1(Sz@zo7IZ_w_fya2fzR?^ljGDPZ z&KxU@nVSlm%*}<(=6GQo?E~MHA3w|PkqVsk|BAc;uLSez zVM^}v==550_o5aQf)lar41-s+Q)vBGn^?2n*DBBqPA2|fko9L$+acwjOKn4+#nc_R z?N6rfBK{{`hF^oZOoffDS8`!Je?nRRx8a}R_u!lCstDrao5`ZM_}aM%ALzqegxxQ(6!jtGw9IGm#(2(D0r8uNeyXG^Rjc7+z>VoP+Thk8$_ z$u)&bXp3|O`}xQ9Ey>D|wxi-+$rHJ}QFvs3Tj5GS!1YH0puQ#lCxE;b0O!bMX5Xe{ za46PAPwGTHxeJ{bSG&*>JywfTdY9QU*jgA@<54UmhTlr?Xg4g3Jla?ReyEo0R})`? zmf{Xw6Iu~364t434RxxU>ZExBgGb#=E9P!!#XaW5ytpTNil_4Ar%Fq6hrMVg>uH|Q zW!C_&#L(%pINWK z5gK-rMqOxLEb@|T*zc1c^rZQE$CUZ8%OZ8h)oNswBZ+57gmqbA91CxI zPi|V(hGjoXwi!~#UoX|0h7(5XsT^Zgt+LyT7>nh4lN&ZtF7pJ+-q*72*Ck;-LtWoxh9bC zW4rq8e-bZJWIaRTED4E3*gyr595UZYW`%xBQu@UD+dOA4Y1VKUx-_!@Bjb+ delta 542 zcmdlPKQ)xkiII!c<6A%!7@J4Ys0HcEEljrpv+!3>%PlY1EVOb%hP;N&RD%uTJz&r99h$t1$a zC^&f@vj&?G(264A$!D0Ox%FJEVv6&VGZS+%CR?y*GYW1_Vc`I&%Vm{-sq1D| zfpra|z~s4X(dd$4lXKa57;S+HbSE!o&tMFmEWlCD7`C~C!v4`7IA0Oa&uv24l(QEZ$d4j3Sdm1(X=YCszn40@ckDSjV_>vZvq` zAX`o-neoBqCZP-_DM64=ltDxch=6zu#1fgzB6^Hph^NG(21K=J>9fyq7kngCYe Bi>m+t diff --git a/SocialHelper/homepage/templates/homepage/index.html b/SocialHelper/homepage/templates/homepage/index.html index f50936d..27c9c2b 100644 --- a/SocialHelper/homepage/templates/homepage/index.html +++ b/SocialHelper/homepage/templates/homepage/index.html @@ -51,18 +51,18 @@
-

{{ product.title }}

-
{{ product.user_iden }}
+

{{ product.title }}

+
{{ product.user_iden }}
{% if types.0.0 == product.type %} - Need + Potrzebuje {% else %} - Give + Oddam {% endif %} {% if types_o.0.0 == product.offer %} - Service + Usługe {% else %} - Items + Przedmiot {% endif %}
Location: {{ product.place }}
diff --git a/SocialHelper/homepage/templates/homepage/offer_page.html b/SocialHelper/homepage/templates/homepage/offer_page.html new file mode 100644 index 0000000..10ad80e --- /dev/null +++ b/SocialHelper/homepage/templates/homepage/offer_page.html @@ -0,0 +1,195 @@ +{% extends 'base.html' %} +{% load static %} +{% block content %} + + + + + + +
+
+
+ {% if offer.Wolontatriat %} +
Ogłoszenie Wolontariackie
+ {% endif %} +

{{ offer.title }}

+
{{ offer.user_iden }}


+ + {{ 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 }} +
+ +
+

Wiadomości

+ {% if mess == 0 %} + Brak Wiadomości + {% else %} + +
+ {% for user in email_list %} + + {% endfor %} +
+ + {% for user in user_list %} +
+ {% for chat in mess %} + {% if user == chat.author_id.id or user == chat.sec_user_id %} + +

{{ chat.pub_date }}

+

{{ chat.author_id }}: {{ chat.text }}

+ ----- + {% endif %} + {% endfor %} +
+ {% csrf_token %} + + + + + + +
+ +
+ {% endfor %} + {% endif %} + +
+ +
+
+ + + + + + +
+
+ +
+
+ + + + + + + +{% endblock %} diff --git a/SocialHelper/homepage/urls.py b/SocialHelper/homepage/urls.py index a127bc7..3ccaf77 100644 --- a/SocialHelper/homepage/urls.py +++ b/SocialHelper/homepage/urls.py @@ -9,6 +9,8 @@ urlpatterns = [ path('product', views.index_product, name='index_product'), path('offer', views.index_offer, name='index_offer'), path('all', views.index, name='index'), + path('offer_details/', views.offer, name='offer'), + path('send_message', views.send_message, name='send_message'), path('', views.index, name='index'), path('contact', views.contact, name='contact'), diff --git a/SocialHelper/homepage/views.py b/SocialHelper/homepage/views.py index 277fa3a..b68c7ce 100644 --- a/SocialHelper/homepage/views.py +++ b/SocialHelper/homepage/views.py @@ -6,6 +6,7 @@ from django.urls import reverse from django.template import loader from django.contrib.auth import get_user_model from django.contrib import messages, auth +from django.utils import timezone 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)) else: 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)