Browse Source

Merge branch 'send_msg' into metasynced_module

blezz-tech 2 months ago
parent
commit
7cb8886246
3 changed files with 156 additions and 2 deletions
  1. 10 0
      config.py
  2. 83 0
      main.py
  3. 63 2
      serializer/orders.py

+ 10 - 0
config.py

@@ -0,0 +1,10 @@
+#Подключение к базе данных
+host = "host"
+SSH_H = "46.138.247.90"
+user = "user"
+password = "password"
+port = 334
+db_name = "db_name"
+#Подключение к боту
+jid = "test@msg.sharix-app.org"
+jidpassword = "test1234@"

+ 83 - 0
main.py

@@ -0,0 +1,83 @@
+import psycopg2 as pg
+from sshtunnel import SSHTunnelForwarder
+from config import host, user, password, db_name, jid, jidpassword, port, SSH_H
+from multiprocessing import connection
+from dateutil import parser
+import datetime, time
+import xmpp
+#Проверка соединения и само соединение
+try:
+    print('Connecting to the PostgreSQL Database...')
+    ssh_tunnel = SSHTunnelForwarder(
+    (SSH_H, 334),
+    ssh_username = "artem_valiakhmetov",
+    ssh_private_key = 'D:/keys/home/.ssh/id_rsa ',
+    ssh_private_key_password = '',
+    remote_bind_address = ("localhost", 5432)
+    )
+    ssh_tunnel.start()
+    print("Tunnel start!")
+    conn = pg.connect(
+        host = "localhost",
+        port = ssh_tunnel.local_bind_port,
+        user = "postgres",
+        password = "",
+        database = "sharixdrive"
+    )
+    print("Success ssh connect!")
+    print("Successfully connection...")
+except Exception as ex:
+    print("Error connection to database...")
+    print(ex)
+
+    def add_client_to_metaservice(user_id, ticket_status, id_metaservice):
+        with conn.cursor() as cursor:
+            insert_into_client_to_metaservice = "INSERT INTO client (user_id, requirements, status, ticket_status, id_metaservice, is_global, is_visible) VALUES (%s,'sgGshHz','A',%s,%s,'t','f')"
+            val = [user_id, ticket_status, id_metaservice]
+            cursor.execute(insert_into_client_to_metaservice)
+            conn.commit()
+    def change_client_visibility(user_id, id_metaservice, is_visible):
+        with conn.cursor() as cursor:
+            update_client_visibility = "UPDATE client SET is_visible = %s WHERE user_id = %s AND id_metaservice = %s"
+            val = [is_visible, user_id, id_metaservice]
+            cursor.execute(update_client_visibility)
+            conn.commit()
+    def change_client_globality(user_id, id_metaservice, is_global):
+        with conn.cursor() as cursor:
+            update_client_globality = "UPDATE client SET is_global = %s WHERE user_id = %s AND id_metaservice = %s"
+            val = [is_global, user_id, id_metaservice]
+            cursor.execute(update_client_globality)
+            conn.commit()
+    def check_client_status():
+
+    def find_client_tickets():
+
+    def add_provider_to_metaservice(id, id_metaservice):
+        with conn.cursor() as cursor:
+            insert_into_provider_to_metaservice = "INSERT INTO provider (type, id_company, user_id, id_metaservice, requirements, status, ticket_status, is_global, is_visible) VALUES (%s, %s, %s, %s, %s, status, ticket_status, is_global, is_visible)"
+            val = [type, id_company, user_id, id_metaservice, requirements, status, ticket_status, is_global, is_visible]
+            cursor.execute(insert_into_provider_to_metaservice)
+            conn.commit()
+    def change_provider_visibility(id, id_metaservice, is_visible):
+        with conn.cursor() as cursor:
+            update_provider_visibility = "UPDATE provider SET is_visible = %s WHERE id = %s AND id_metaservice = %s"
+            val = [is_visible, id, id_metaservice]
+            cursor.execute(update_provider_visibility)
+            conn.commit()
+    def change_provider_globality(id, id_metaservice, is_global):
+        with conn.cursor() as cursor:
+            update_provider_visibility = "UPDATE provider SET is_global = %s WHERE id = %s AND id_metaservice = %s"
+            val = [is_global, id, id_metaservice]
+            cursor.execute(update_provider_visibility)
+            conn.commit()
+    
+
+
+
+
+
+
+
+
+
+

+ 63 - 2
serializer/orders.py

@@ -1,16 +1,22 @@
 from rest_framework import serializers
 from dbsynce.models import Orders
 from tickets.models import Ticket, TicketList
+from dbsynce.ejabber import *
+from core.config import DEBUG, WEBSERVICE_NAME, EJ_HOST, EJ_SERVICE
 import traceback
+import requests
+    
 
 class OrdersSerializer(serializers.ModelSerializer):
     class Meta:
         model = Orders
         fields = '__all__'
 
+    
     def create(self, validated_data):
         ModelClass = self.Meta.model
-
+        
+        
         if "ticket" not in validated_data:
             # Create ticket with minimal required data
             ticket_data = {
@@ -50,5 +56,60 @@ class OrdersSerializer(serializers.ModelSerializer):
                 )
             )
             raise TypeError(msg)
-        
+        else:
+            if DEBUG: print("[CREATING EJABBER ROOMS]")
+            # Get neccessary data
+            order_num = instance.pk
+            support = pick_support()
+            client = instance.client_id.user.phone_number
+            provider = instance.provider.user_id.phone_number
+
+            for room_name, room_members in get_rooms(WEBSERVICE_NAME, order_num, support, client, provider).items():
+                # Create ejabber rooms
+                data = {
+                    "name": room_name,
+                    "service": EJ_SERVICE,
+                    "host": EJ_HOST,
+                }
+                res = ej_execute("create_room", data)
+                if DEBUG: print(f"[RESPONSE CREATED]: {res.json()}")
+                if DEBUG: print(f"[ROOM]: {room_name}")
+
+                # Set rooms to members_only
+                data = {
+                    "name": room_name,
+                    "service": EJ_SERVICE,
+                    "option": "members_only",
+                    "value": "true"
+                }
+                res = ej_execute("change_room_option", data)
+                if DEBUG: print(f"[SET ROOM TO MEMBERS ONLY]: {res.json()}")
+
+                """
+                Add members to the room
+
+                member_account is a dict that contains members credentials
+                Key - the same thing that is in room_members
+                Value - tuple (jid, ejabber affiliation).
+                Affiliation can be one of: owner, admin, member, outcast, none.
+                owner > admin
+                """
+                members_accounts = {
+                    "owner": (WEBSERVICE_NAME + "_backend@" + EJ_HOST, "owner"),
+                    "client": (client, "member"),
+                    "support": (support, "member"),
+                    "provider": (provider, "member"),
+                }
+                data = {
+                    "name": room_name,
+                    "service": EJ_SERVICE,
+                    "jid": WEBSERVICE_NAME + "_backend@" + EJ_HOST,
+                    "affiliation": "owner"
+                }
+                # Add all neccessary member to the room
+                for member in room_members:
+                    data["jid"] = members_accounts[member][0]
+                    data["affiliation"] = members_accounts[member][1]
+                    res = ej_execute("set_room_affiliation", data)
+                    if DEBUG: print(f"[ADDED USER]: {res.json()}")
         return instance