[WIP] Generate models
This commit is contained in:
parent
f4396fa2eb
commit
5ecf3cd4c7
|
@ -0,0 +1,21 @@
|
||||||
|
from orator.migrations import Migration
|
||||||
|
|
||||||
|
|
||||||
|
class CreateChatsTable(Migration):
|
||||||
|
|
||||||
|
def up(self):
|
||||||
|
"""
|
||||||
|
Run the migrations.
|
||||||
|
"""
|
||||||
|
with self.schema.create('chats') as table:
|
||||||
|
table.increments('id')
|
||||||
|
table.integer('telegram_id')
|
||||||
|
table.integer('chat_type')
|
||||||
|
table.integer('random_chance').default(5)
|
||||||
|
table.timestamps()
|
||||||
|
|
||||||
|
def down(self):
|
||||||
|
"""
|
||||||
|
Revert the migrations.
|
||||||
|
"""
|
||||||
|
self.schema.drop('chats')
|
|
@ -0,0 +1,18 @@
|
||||||
|
from orator.migrations import Migration
|
||||||
|
|
||||||
|
|
||||||
|
class CreateWordsTable(Migration):
|
||||||
|
|
||||||
|
def up(self):
|
||||||
|
"""
|
||||||
|
Run the migrations.
|
||||||
|
"""
|
||||||
|
with self.schema.create('words') as table:
|
||||||
|
table.increments('id')
|
||||||
|
table.string('word').unique()
|
||||||
|
|
||||||
|
def down(self):
|
||||||
|
"""
|
||||||
|
Revert the migrations.
|
||||||
|
"""
|
||||||
|
self.schema.drop('words')
|
|
@ -0,0 +1,24 @@
|
||||||
|
from orator.migrations import Migration
|
||||||
|
|
||||||
|
|
||||||
|
class CreatePairsTable(Migration):
|
||||||
|
|
||||||
|
def up(self):
|
||||||
|
"""
|
||||||
|
Run the migrations.
|
||||||
|
"""
|
||||||
|
with self.schema.create('pairs') as table:
|
||||||
|
table.increments('id')
|
||||||
|
table.integer('chat_id').unsigned()
|
||||||
|
table.foreign('chat_id').references('id').on('chats')
|
||||||
|
table.integer('first_id').unsigned()
|
||||||
|
table.foreign('first_id').references('id').on('replies').nullable()
|
||||||
|
table.integer('second_id').unsigned()
|
||||||
|
table.foreign('second_id').references('id').on('replies').nullable()
|
||||||
|
table.timestamp('created_at')
|
||||||
|
|
||||||
|
def down(self):
|
||||||
|
"""
|
||||||
|
Revert the migrations.
|
||||||
|
"""
|
||||||
|
self.schema.drop('pairs')
|
|
@ -0,0 +1,23 @@
|
||||||
|
from orator.migrations import Migration
|
||||||
|
|
||||||
|
|
||||||
|
class CreateRepliesTable(Migration):
|
||||||
|
|
||||||
|
def up(self):
|
||||||
|
"""
|
||||||
|
Run the migrations.
|
||||||
|
"""
|
||||||
|
with self.schema.create('replies') as table:
|
||||||
|
table.increments('id')
|
||||||
|
table.integer('pair_id').unsigned()
|
||||||
|
table.foreign('pair_id').references('id').on('pairs')
|
||||||
|
table.integer('word_id').unsigned()
|
||||||
|
table.foreign('word_id').references('id').on('words').nullable()
|
||||||
|
table.integer('count').default(1)
|
||||||
|
table.timestamps()
|
||||||
|
|
||||||
|
def down(self):
|
||||||
|
"""
|
||||||
|
Revert the migrations.
|
||||||
|
"""
|
||||||
|
self.schema.drop('replies')
|
|
@ -0,0 +1,7 @@
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read('./main.cfg')
|
||||||
|
|
||||||
|
DATABASES = {'db': config['db']}
|
9
run.py
9
run.py
|
@ -5,15 +5,10 @@ from src.bot import Bot
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = get_config('./main.cfg')
|
config = configparser.ConfigParser()
|
||||||
|
config.read('./main.cfg')
|
||||||
logging.basicConfig(level=config['logging']['level'])
|
logging.basicConfig(level=config['logging']['level'])
|
||||||
Bot(config).run()
|
Bot(config).run()
|
||||||
|
|
||||||
|
|
||||||
def get_config(path):
|
|
||||||
config = configparser.ConfigParser()
|
|
||||||
config.read(path)
|
|
||||||
return config
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
26
src/bot.py
26
src/bot.py
|
@ -1,13 +1,11 @@
|
||||||
import logging
|
import logging
|
||||||
import random
|
import random
|
||||||
import urllib.request
|
import urllib.request
|
||||||
import sqlite3
|
|
||||||
|
|
||||||
from telegram.ext import Updater
|
from telegram.ext import Updater
|
||||||
from telegram.ext import MessageHandler, Filters
|
from telegram.ext import MessageHandler, Filters
|
||||||
from orator import DatabaseManager, Model
|
from orator.orm import Model
|
||||||
from src.domain.message import Message
|
from orator import DatabaseManager
|
||||||
|
|
||||||
|
|
||||||
class Bot:
|
class Bot:
|
||||||
messages = [
|
messages = [
|
||||||
|
@ -25,28 +23,8 @@ class Bot:
|
||||||
self.dispatcher = self.updater.dispatcher
|
self.dispatcher = self.updater.dispatcher
|
||||||
|
|
||||||
Model.set_connection_resolver(DatabaseManager({'db': config['db']}))
|
Model.set_connection_resolver(DatabaseManager({'db': config['db']}))
|
||||||
self.create_table()
|
|
||||||
|
|
||||||
def create_table(self):
|
|
||||||
connection = sqlite3.connect(self.config['db']['database'])
|
|
||||||
cursor = connection.cursor()
|
|
||||||
cursor.execute('''CREATE TABLE IF NOT EXISTS messages
|
|
||||||
(id INTEGER PRIMARY KEY,
|
|
||||||
chat_id text,
|
|
||||||
user_name text,
|
|
||||||
user_id text,
|
|
||||||
payload text,
|
|
||||||
created_at TIMESTAMP,
|
|
||||||
updated_at TIMESTAMP)''')
|
|
||||||
connection.commit()
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
def handler(self, bot, update):
|
def handler(self, bot, update):
|
||||||
Message.create(chat_id=update.message.chat_id,
|
|
||||||
user_name=update.message.from_user.username,
|
|
||||||
user_id=update.message.from_user.id,
|
|
||||||
payload=update.message.text)
|
|
||||||
|
|
||||||
value = random.randint(0, 2)
|
value = random.randint(0, 2)
|
||||||
if value == 1:
|
if value == 1:
|
||||||
message = random.choice(self.messages)
|
message = random.choice(self.messages)
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
from orator.orm import Model
|
||||||
|
from orator.orm import has_many
|
||||||
|
from src.domain.pair import Pair
|
||||||
|
|
||||||
|
|
||||||
|
class Chat(Model):
|
||||||
|
@has_many
|
||||||
|
def pairs(self):
|
||||||
|
return Pair
|
|
@ -1,6 +0,0 @@
|
||||||
from orator import Model
|
|
||||||
|
|
||||||
|
|
||||||
class Message(Model):
|
|
||||||
__fillable__ = ['chat_id', 'user_name', 'user_id', 'payload']
|
|
||||||
__guarded__ = ['id']
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
from orator.orm import Model
|
||||||
|
from orator.orm import has_many
|
||||||
|
from orator.orm import belongs_to
|
||||||
|
from src.domain.reply import Reply
|
||||||
|
from src.domain.chat import Chat
|
||||||
|
|
||||||
|
|
||||||
|
class Pair(Model):
|
||||||
|
@has_many
|
||||||
|
def replies(self):
|
||||||
|
return Reply
|
||||||
|
|
||||||
|
@belongs_to
|
||||||
|
def chat(self):
|
||||||
|
return Chat
|
|
@ -0,0 +1,15 @@
|
||||||
|
from orator.orm import Model
|
||||||
|
from orator.orm import belongs_to_many
|
||||||
|
from orator.orm import belongs_to
|
||||||
|
from src.domain.pair import Pair
|
||||||
|
from src.domain.word import Word
|
||||||
|
|
||||||
|
|
||||||
|
class Reply(Model):
|
||||||
|
@belongs_to_many
|
||||||
|
def pairs(self):
|
||||||
|
return Pair
|
||||||
|
|
||||||
|
@belongs_to
|
||||||
|
def word(self):
|
||||||
|
return Word
|
|
@ -0,0 +1,9 @@
|
||||||
|
from orator.orm import Model
|
||||||
|
from orator.orm import has_many
|
||||||
|
from src.domain.chat import Chat
|
||||||
|
|
||||||
|
|
||||||
|
class Word(Model):
|
||||||
|
@has_many
|
||||||
|
def chats(self):
|
||||||
|
return Chat
|
Loading…
Reference in New Issue