Advertisement
AntonPaly4

client_storage_handler

Aug 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. from sqlalchemy.orm import sessionmaker
  2. from sqlalchemy import create_engine, or_
  3.  
  4. from client_src.client_models import *
  5. from JIM.jim_config import *
  6.  
  7.  
  8. class ClientStorageHandler:
  9.     def __init__(self, name, database=None):
  10.         self.name = name
  11.         self.pattern = 'sqlite:///{}.sqlite?check_same_thread=False'.format(self.name)
  12.         self.engine = create_engine(self.pattern)
  13.         Session = sessionmaker(bind=self.engine)
  14.         self.session = Session()
  15.         self._create_db(self.engine)
  16.  
  17.     def _create_db(self, engine_):
  18.         Model.metadata.create_all(bind=engine_)
  19.  
  20.     def check_in_list(self, contact_name):
  21.         result = self.session.query(UserContacts).filter(UserContacts.contact_name == contact_name).one_or_none()
  22.         if result:
  23.             return result
  24.         else:
  25.             return None
  26.  
  27.     def get_name_by_id(self, contact_id):
  28.         res = self.session.query(UserContacts).filter(UserContacts.contact_id == contact_id).one()
  29.         return res.contact_name
  30.  
  31.     def add_contact(self, contact_name):
  32.         contact = self.check_in_list(contact_name)
  33.         if not contact:
  34.             new_contact = UserContacts(contact_name)
  35.             self.session.add(new_contact)
  36.             self.session.commit()
  37.         #     return True
  38.         # else:
  39.         #     return False
  40.  
  41.     def del_contact(self, contact_name):
  42.         contact = self.check_in_list(contact_name)
  43.         if contact:
  44.             self.session.delete(contact)
  45.             self.session.commit()
  46.             return True
  47.         else:
  48.             return False
  49.  
  50.     def get_contacts(self):
  51.         contact_list = self.session.query(UserContacts).all()
  52.         return [contact.contact_name for contact in contact_list]
  53.  
  54.     def log_incoming_message(self, message):
  55.         from_contact = message[FROM]
  56.         message_to_log = MessagesHistory(message[MESSAGE], from_contact=from_contact, to_contact=None,
  57.                                          message_created=message[TIME])
  58.         self.session.add(message_to_log)
  59.         self.session.commit()
  60.  
  61.     def log_outgoing_message(self, message):
  62.         to_contact = message[TO]
  63.         message_to_log = MessagesHistory(message[MESSAGE], from_contact=None, to_contact=to_contact,
  64.                                          message_created=message[TIME])
  65.         self.session.add(message_to_log)
  66.         self.session.commit()
  67.  
  68.     # пока выгружает всю историю - добавить выгрузку по дням/ часам
  69.     def messages_history(self, contact, counted_period=None, period='day'):
  70.         messages_history = []
  71.         messages = self.session.query(MessagesHistory).\
  72.             filter(or_(MessagesHistory.from_contact == contact,
  73.                        MessagesHistory.to_contact == contact)).all()
  74.         for message_ in messages:
  75.             if message_.from_contact:
  76.                 messages_history.append(
  77.                     {TIME: message_.message_created, FROM: message_.from_contact,
  78.                      MESSAGE: message_.message})
  79.             else:
  80.                 messages_history.append(
  81.                     {TIME: message_.message_created, FROM: self.name, MESSAGE: message_.message})
  82.         return messages_history
  83.  
  84.     def add_to_ignore(self, name):
  85.         ignored_contact = IgnoreList(name)
  86.         self.session.a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement