Commit e2636846 authored by Sven Kuhlmann's avatar Sven Kuhlmann

Fix: Sql rollback

parent 3411cbbf
......@@ -138,7 +138,6 @@ class QuizzBot(Bot):
logging.debug(user)
message = self.get_or_add(update.message, chat.id, user.id)
logging.debug(message)
self.error_callback(update, context)
def private_all(self, update, context):
logging.debug("Got private all message")
......
......@@ -6,7 +6,7 @@ config_file = "settings.yml"
def get_config():
dir = Path().absolute()
return yaml.safe_load(open(os.path.join(str(dir), config_file)))
return yaml.safe_load(open(os.path.join(str(dir), config_file))) # TODO SettingsException
def get_token():
return get_config()["token"]
......
import re
import datetime
from sys import stdout
from sqlalchemy.ext.declarative import declarative_base
......@@ -25,8 +26,13 @@ emoji_pattern = re.compile("["
u"\ufe0f"
u"\u3030"
"]+", flags=re.UNICODE)
def emoji_decode(text):
""" Decodes a string for db by removing emojis """
text = emoji_pattern.sub(r':)', str(text))
text = text.encode(stdout.encoding, errors="replace")
return text
""" Decodes a string for db by removing emojis """
text = emoji_pattern.sub(r':)', str(text))
text = text.encode(stdout.encoding, errors="replace")
return text
# Date
def get_date_now():
return datetime.datetime.now()
\ No newline at end of file
......@@ -4,6 +4,27 @@ from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session, Session
from sqlalchemy_utils.functions import database_exists, create_database
# TODO -> Change work with session to from docs:
"""
from contextlib import contextmanager
@contextmanager
def session_scope():
#Provide a transactional scope around a series of operations.
session = Session()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
def run_my_program():
with session_scope() as session:
ThingOne().go(session)
ThingTwo().go(session)
"""
class Database:
_engine = None
......@@ -20,7 +41,14 @@ class Database:
Base.metadata.create_all(self._engine)
def get_session(self):
return self._session()
session = self._session()
try:
pass
#session.commit() #Bad solution
except Exception:
session.rollback()
session = self._session()
return session
def query(self, _class, _id, all=True):
if all:
......
""" Just add goup chat to db, remember that. """
from sys import stdout
from DB.base import Base, emoji_decode
from DB.base import Base, emoji_decode, get_date_now
from Model.user import User
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, ForeignKey
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, ForeignKey, Date
from sqlalchemy.orm import relationship
class Chat(Base):
......@@ -17,6 +16,8 @@ class Chat(Base):
description = Column(String(512))
invite_link = Column(String(100))
sticker_set = Column(String(100))
created_at = Column(Date, default=get_date_now)
updated_at = Column(Date, onupdate=get_date_now)
# Relations
message = relationship("Message")
users = relationship("User_Chat", back_populates="chat")
......
import datetime
from sys import stdout
from DB.base import Base, emoji_decode
from DB.base import Base, emoji_decode, get_date_now
from Model.chat import Chat
from Model.user import User
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, ForeignKey
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, ForeignKey, Date
from sqlalchemy.orm import relationship
class Message(Base):
......@@ -15,20 +14,16 @@ class Message(Base):
user_id = Column(BigInteger, ForeignKey("user.id"))
chat_id = Column(BigInteger, ForeignKey("chat.id"))
text = Column(String(1024))
date = Column(String(50))
created_at = Column(Date, default=get_date_now)
updated_at = Column(Date, onupdate=get_date_now)
@staticmethod
def create_from_tg(tg_message, chat_id=None, user_id=None):
# message_id dont have to be set
# Normalisize
text = emoji_decode(tg_message.text)
try:
date = tg_message.date.strftime("%m/%d/%Y,%H:%M:%S")
except Exception:
date = tg_message.date
return Message(
id=tg_message.message_id,
text=text,
date=str(date),
chat_id=chat_id,
user_id=user_id)
\ No newline at end of file
from sys import stdout
from DB.base import Base, emoji_decode
from DB.base import Base, emoji_decode, get_date_now
from Model.user import User
from Model.poll_option import PollOption
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, ForeignKey
from sqlalchemy import Column, Integer, BigInteger, String, Boolean, ForeignKey, Date
from sqlalchemy.orm import relationship
class Poll(Base):
......@@ -16,6 +16,8 @@ class Poll(Base):
question = Column(String(512))
is_closed = Column(Boolean)
date = Column(String(50))
created_at = Column(Date, default=get_date_now)
updated_at = Column(Date, onupdate=get_date_now)
# Relations
correct_option = relationship("PollOption", uselist=False, back_populates="correct_poll")
options = relationship("PollOption")
......
from sys import stdout
from DB.base import Base, emoji_decode
from sqlalchemy import Column, Integer, String, BigInteger, Boolean, ForeignKey
from DB.base import Base, emoji_decode, get_date_now
from sqlalchemy import Column, Integer, String, BigInteger, Boolean, ForeignKey, Date
from sqlalchemy.orm import relationship
class PollOption(Base):
......@@ -12,6 +12,8 @@ class PollOption(Base):
poll_id = Column(BigInteger, ForeignKey("poll.id"))
text = Column(String(512))
voter_count = Column(Integer)
created_at = Column(Date, default=get_date_now)
updated_at = Column(Date, onupdate=get_date_now)
# Relations
correct_poll = relationship("Poll", back_populates="correct_option")
vote = relationship("Vote")
......
from sys import stdout
from DB.base import Base, emoji_decode
from sqlalchemy import Column, Integer, String, BigInteger, Boolean, ForeignKey
from DB.base import Base, emoji_decode, get_date_now
from sqlalchemy import Column, Integer, String, BigInteger, Boolean, ForeignKey, Date
from sqlalchemy.orm import relationship
class User(Base):
......@@ -15,6 +15,8 @@ class User(Base):
last_name = Column(String(100))
link = Column(String(100))
is_bot = Column(Boolean)
created_at = Column(Date, default=get_date_now)
updated_at = Column(Date, onupdate=get_date_now)
# Relations
poll = relationship("Poll")
vote = relationship("Vote")
......
from DB.base import Base
from DB.base import Base, get_date_now
from Model.user import User
from Model.poll import Poll
from Model.poll_option import PollOption
from sqlalchemy import Column, Integer, String, BigInteger, Boolean, ForeignKey
from sqlalchemy import Column, Integer, String, BigInteger, Boolean, ForeignKey, Date
from sqlalchemy.orm import relationship
class Vote(Base):
......@@ -15,6 +15,8 @@ class Vote(Base):
user_id = Column(BigInteger, ForeignKey("user.id"))
poll_id = Column(BigInteger, ForeignKey("poll.id"))
is_right_option = Column(Boolean)
created_at = Column(Date, default=get_date_now)
updated_at = Column(Date, onupdate=get_date_now)
@staticmethod
def create_from_tg(tg_vote, user_id=None, poll_id=None):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment