Merge branch 'master' of https://bitbucket.org/PekopT/archilance into artur
commit
ed31e61440
20 changed files with 107 additions and 205 deletions
@ -1,64 +0,0 @@ |
|||||||
from tornado import web, ioloop, options, websocket |
|
||||||
import os.path |
|
||||||
|
|
||||||
clients = [] |
|
||||||
messages = [] |
|
||||||
|
|
||||||
class Application(web.Application): |
|
||||||
def __init__(self): |
|
||||||
handlers = [ |
|
||||||
(r"/", MainHandler), |
|
||||||
(r"/chat", ChatHandler), |
|
||||||
] |
|
||||||
settings = dict( |
|
||||||
cookie_secret="tes12323hhdfdfre9312313", |
|
||||||
template_path=os.path.join(os.path.dirname(__file__), "templates"), |
|
||||||
static_path=os.path.join(os.path.dirname(__file__), "static"), |
|
||||||
xsrf_cookies=True, |
|
||||||
) |
|
||||||
super().__init__(handlers, **settings) |
|
||||||
|
|
||||||
|
|
||||||
class MainHandler(web.RequestHandler): |
|
||||||
|
|
||||||
def get(self): |
|
||||||
self.render("index.html") |
|
||||||
|
|
||||||
|
|
||||||
class ChatHandler(websocket.WebSocketHandler): |
|
||||||
|
|
||||||
def open(self): |
|
||||||
print("Web socket opened") |
|
||||||
clients.append(self) |
|
||||||
|
|
||||||
def on_message(self, message): |
|
||||||
messages.append({'msg':message}) |
|
||||||
for msg in messages: |
|
||||||
self.write_message(msg) |
|
||||||
|
|
||||||
def check_origin(self, origin): |
|
||||||
return True |
|
||||||
|
|
||||||
|
|
||||||
def main(): |
|
||||||
options.parse_command_line() |
|
||||||
app = Application() |
|
||||||
app.listen(8888) |
|
||||||
ioloop.IOLoop.current().start() |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__': |
|
||||||
main() |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@gen.coroutine |
|
||||||
def get_messages_from_order_chat(self, order_id): |
|
||||||
sql = "SELECT text FROM chat_message WHERE order_id=" + order_id |
|
||||||
cursor = yield self.db.execute(sql) |
|
||||||
messages = cursor.fetchall() |
|
||||||
for msg in messages: |
|
||||||
for waiter in self.waiters: |
|
||||||
waiter.write_message({'msg': msg[0], 'answer_type': 'contact'}) |
|
||||||
@ -1,102 +0,0 @@ |
|||||||
import logging |
|
||||||
import datetime |
|
||||||
import os.path |
|
||||||
from redis import StrictRedis |
|
||||||
from tornado import web, websocket, escape, options, locale, ioloop |
|
||||||
from tornado.httpserver import HTTPServer |
|
||||||
|
|
||||||
r = StrictRedis(db=1) |
|
||||||
|
|
||||||
logger = logging.getLogger('handlers') |
|
||||||
|
|
||||||
settings = { |
|
||||||
'cookie_secret': '__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__', |
|
||||||
'template_path': os.path.join(os.path.dirname(__file__), 'templates'), |
|
||||||
'static_path': os.path.join(os.path.dirname(__file__), 'static'), |
|
||||||
'login_url': '/login', |
|
||||||
'xsrf_cookies': True, |
|
||||||
'debug': True, |
|
||||||
'autoreload': True, |
|
||||||
'server_traceback': True, |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
class Application(web.Application): |
|
||||||
def __init__(self): |
|
||||||
handlers = [ |
|
||||||
(r"/",ChannelHandler), |
|
||||||
(r"/chatsocket/(?P<channel>\w+)/", ChatSocketHandler) |
|
||||||
] |
|
||||||
super().__init__(handlers=handlers, **settings) |
|
||||||
|
|
||||||
|
|
||||||
class ChannelHandler(web.RequestHandler): |
|
||||||
def get(self, *args, **kwargs): |
|
||||||
title = kwargs.get('channel', 'main') |
|
||||||
self.chnl = kwargs.get('channel', 'main') |
|
||||||
cache = r.lrange('channels:{}'.format(title), 0, -1) |
|
||||||
messages =(escape.json_decode(x) for x in cache) if cache else [] |
|
||||||
print(messages) |
|
||||||
|
|
||||||
self.render('index2.html',messages=messages) |
|
||||||
|
|
||||||
|
|
||||||
class ChatSocketHandler(websocket.WebSocketHandler): |
|
||||||
|
|
||||||
waiters = set() |
|
||||||
|
|
||||||
def open(self, *args, **kwargs): |
|
||||||
self.chnl = kwargs.get('channel', 'main') |
|
||||||
self.waiters.add((self.chnl, self)) |
|
||||||
# self.chnl_key = 'channels:{}:users'.format(self.chnl) |
|
||||||
# count = int(r.zcard(self.chnl_key)) |
|
||||||
# r.zadd(self.chnl_key, count+1, "mukhtar") |
|
||||||
# users = r.zrange(self.chnl_key,0,-1) |
|
||||||
# self.send_updates() |
|
||||||
|
|
||||||
def on_close(self): |
|
||||||
self.waiters.remove(self.chnl, self) |
|
||||||
|
|
||||||
def on_message(self, message): |
|
||||||
parsed = escape.json_decode(message) |
|
||||||
if 'dummy' in parsed: |
|
||||||
return |
|
||||||
|
|
||||||
chat = { |
|
||||||
'parent': 'inbox', |
|
||||||
'body': parsed['message'], |
|
||||||
'user': 'Mukhtar', |
|
||||||
'time': datetime.datetime.now().strftime('%H:%M:%S %Y-%m-%d') |
|
||||||
} |
|
||||||
self.update_channel_history(chat) |
|
||||||
self.send_updates(parsed); |
|
||||||
|
|
||||||
def update_channel_history(self,chat): |
|
||||||
chnl = 'channels:{}'.format(self.chnl) |
|
||||||
r.rpush(chnl, escape.json_encode(chat)) |
|
||||||
r.ltrim(chnl, -25, -1) |
|
||||||
|
|
||||||
def send_updates(self, chat): |
|
||||||
chnl_waiters = tuple(w for c, w in self.waiters) |
|
||||||
for waiter in chnl_waiters: |
|
||||||
try: |
|
||||||
waiter.write_message(chat) |
|
||||||
except: |
|
||||||
pass |
|
||||||
|
|
||||||
def __del__(self): |
|
||||||
r.zrem(self.chnl_key, self.current_user) |
|
||||||
self.log('PUSHED OUT') |
|
||||||
|
|
||||||
|
|
||||||
def main(): |
|
||||||
options.parse_command_line() |
|
||||||
app = Application() |
|
||||||
server = HTTPServer(app) |
|
||||||
server.listen(8888) |
|
||||||
loop = ioloop.IOLoop.current() |
|
||||||
loop.start() |
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__': |
|
||||||
main() |
|
||||||
@ -1,5 +0,0 @@ |
|||||||
from django.apps import AppConfig |
|
||||||
|
|
||||||
|
|
||||||
class ChatConfig(AppConfig): |
|
||||||
name = 'chat' |
|
||||||
@ -0,0 +1,20 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
# Generated by Django 1.9.7 on 2016-08-22 16:24 |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('chat', '0005_message_is_new'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.AddField( |
||||||
|
model_name='message', |
||||||
|
name='is_delete', |
||||||
|
field=models.BooleanField(default=False), |
||||||
|
), |
||||||
|
] |
||||||
@ -0,0 +1,14 @@ |
|||||||
|
{% extends 'partials/base.html' %} |
||||||
|
|
||||||
|
{% block content %} |
||||||
|
<section class="mainContainer"> |
||||||
|
{% include 'partials/header.html' %} |
||||||
|
<div class="container-fluid"> |
||||||
|
<div class="row"> |
||||||
|
<p class="welcomeMain">Ошибка 403! Доступ ограничен.</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</section> |
||||||
|
{% endblock %} |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,14 @@ |
|||||||
|
{% extends 'partials/base.html' %} |
||||||
|
|
||||||
|
{% block content %} |
||||||
|
<section class="mainContainer"> |
||||||
|
{% include 'partials/header.html' %} |
||||||
|
<div class="container-fluid"> |
||||||
|
<div class="row"> |
||||||
|
<p class="welcomeMain">404 ошибка сайта</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</section> |
||||||
|
{% endblock %} |
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,15 @@ |
|||||||
|
|
||||||
|
{% extends 'partials/base.html' %} |
||||||
|
|
||||||
|
{% block content %} |
||||||
|
<section class="mainContainer"> |
||||||
|
{% include 'partials/header.html' %} |
||||||
|
<div class="container-fluid"> |
||||||
|
<div class="row"> |
||||||
|
<p class="welcomeMain">500 ошибка сервера</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</section> |
||||||
|
{% endblock %} |
||||||
|
|
||||||
|
|
||||||
@ -1,8 +1,11 @@ |
|||||||
#!/usr/bin/env bash |
#!/usr/bin/env bash |
||||||
|
|
||||||
source ../env/bin/activate && |
source ../env/bin/activate && |
||||||
|
pip install -r requirements/base.txt && |
||||||
git reset --hard && |
git reset --hard && |
||||||
git pull && |
git pull && |
||||||
|
chmod +x update.sh && |
||||||
./manage.py migrate --noinput && |
./manage.py migrate --noinput && |
||||||
./manage.py collectstatic --noinput && |
./manage.py collectstatic --noinput && |
||||||
supervisorctl restart arch |
supervisorctl restart arch && |
||||||
|
./manage.py recalculation_spec |
||||||
Loading…
Reference in new issue