[WIP] Generate models

This commit is contained in:
REDNBLACK 2016-11-01 00:11:25 +03:00
parent f4396fa2eb
commit 5ecf3cd4c7
13 changed files with 145 additions and 37 deletions

View File

@ -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')

View File

@ -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')

View File

@ -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')

View File

@ -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
migrations/__init__.py Normal file
View File

7
orator.py Normal file
View File

@ -0,0 +1,7 @@
import configparser
config = configparser.ConfigParser()
config.read('./main.cfg')
DATABASES = {'db': config['db']}

9
run.py
View File

@ -5,15 +5,10 @@ from src.bot import Bot
def main():
config = get_config('./main.cfg')
config = configparser.ConfigParser()
config.read('./main.cfg')
logging.basicConfig(level=config['logging']['level'])
Bot(config).run()
def get_config(path):
config = configparser.ConfigParser()
config.read(path)
return config
if __name__ == '__main__':
main()

View File

@ -1,13 +1,11 @@
import logging
import random
import urllib.request
import sqlite3
from telegram.ext import Updater
from telegram.ext import MessageHandler, Filters
from orator import DatabaseManager, Model
from src.domain.message import Message
from orator.orm import Model
from orator import DatabaseManager
class Bot:
messages = [
@ -25,28 +23,8 @@ class Bot:
self.dispatcher = self.updater.dispatcher
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):
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)
if value == 1:
message = random.choice(self.messages)

9
src/domain/chat.py Normal file
View File

@ -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

View File

@ -1,6 +0,0 @@
from orator import Model
class Message(Model):
__fillable__ = ['chat_id', 'user_name', 'user_id', 'payload']
__guarded__ = ['id']

15
src/domain/pair.py Normal file
View File

@ -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

15
src/domain/reply.py Normal file
View File

@ -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

9
src/domain/word.py Normal file
View File

@ -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