Ejemplo n.º 1
0
def get(kb_name=None, kb_id=None):
    keyboard_table = db.Table("keyboard")
    if (kb_id != None):
        table = keyboard_table.select(["name", "is_inline"], "id", kb_id)
        kb_name, is_inline = table[0]
    elif (kb_name != None):
        table = keyboard_table.select(["id", "is_inline"], "name", kb_name)
        kb_id, is_inline = table[0]
    else:
        keyboard_table.close()
        return
    keyboard_table.close()

    bundles_table = db.Table("bundles")
    bundles = bundles_table.select(["id_button", "id_next_button", "newline"],
                                   "id_keyboard", kb_id)
    bundles_table.close()

    button_next = {}
    button_newline = {}
    for bundle in bundles:
        id_button, id_next, newline = bundle
        button_next[id_button] = id_next
        button_newline[id_button] = newline

    #
    #
    button_text = {}
    buttons_table = db.Table("buttons")
    if (is_inline == False):
        buttons = buttons_table.select_many_key(["id", "text"], "id",
                                                list(button_next.keys()))
    buttons_table.close()

    ### end work with BD
    for button in buttons:
        id, text = button
        button_text[id] = text

    button_id_first = 0
    for button_id in list(button_next.keys()):
        if button_id not in list(button_next.items()):
            button_id_first = button_id
            break

    keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
    button_id_now = button_id_first
    line = []
    while button_next[button_id_now] != 0:
        if button_newline[button_id_now] == False:
            line.append(button_text[button_id_now])
        else:
            line.append(button_text[button_id_now])
            keyboard.row(*line)
            line = []
        button_id_now = button_next[button_id_now]
    line.append(button_text[button_id_now])
    keyboard.row(*line)

    return keyboard
Ejemplo n.º 2
0
    def test_shape(self):
        test_tuple_one_column = db.Table((("33",), ("a",), ("soie",), ("l",), ("ser",)))
        self.assertEqual(test_tuple_one_column.shape, [5, 1])

        test_tuple_two_columns = db.Table((("33", 8), ("a", 2), ("soie", 3), ("l", 1), ("ser", 9)))
        self.assertEqual(test_tuple_two_columns.shape, [5, 2])

        test_tuple_multidimensional = db.Table(((("33", 8), ("a", 2)), (("soie", 3), ("l", 1)), (("ser", 9),)))
        self.assertEqual(test_tuple_multidimensional.shape, [3, 2, 2])
Ejemplo n.º 3
0
Archivo: model.py Proyecto: oten/bebel
class Codes(db.TransversalModel):
    code = db.Table('codes_code', path='./db')
    language = db.Table('codes_language', path='./db')
    tags = db.Table('codes_tags', path='./db')
    codes_of_tag = db.Mirror(tags)
    codes_of_language = db.Mirror(language)

    def __init__(self):
        if self.code.keys():
            self.next_idx = max(map(int, self.code.keys())) + 1
        else:
            self.next_idx = 0

    def add(self, **kwarg):
        super(Codes, self).add(str(self.next_idx), **kwarg)
        self.next_idx += 1
Ejemplo n.º 4
0
def add(array, name=None, is_inline=False):

    array = to2Array(array)

    keyboard_table = db.Table("keyboard")
    keyboard_table.insert(["name", "is_inline"], [name, is_inline])
    keyboard_id = keyboard_table.select_by_max("id", "id")[0][0]

    keyboard_table.close()

    buttons_table = db.Table("buttons")
    buttons_ids = []
    for i, line in enumerate(array):
        buttons_ids.append([])
        for j, text in enumerate(line):
            buttons_table.insert("text", text)
            id = buttons_table.select_by_max("id", "id")
            id = id[0][0]
            buttons_ids[i].append(id)

    buttons_table.close()

    bundles_table = db.Table("bundles", True)

    for i, line in enumerate(buttons_ids):
        for j, id in enumerate(line):
            if j < len(line) - 1:
                newline = False
                next_id = line[j + 1]
            else:
                if i < len(buttons_ids) - 1:
                    newline = True
                    next_id = buttons_ids[i + 1][0]
                else:
                    newline = False
                    next_id = 0
            bundles_table.insert(
                ["id_keyboard", "id_button", "id_next_button", "newline"],
                [keyboard_id, id, next_id, newline])

    bundles_table.close()

    print(keyboard_id)
Ejemplo n.º 5
0
def prepare_db():
    buttons_table = db.Table("buttons")
    buttons_table.create(
        ["id", "text", "callback"],
        ["int(5) PRIMARY KEY AUTO_INCREMENT", "VARCHAR(128)", "VARCHAR(128)"])
    buttons_table.close()

    keyboard_table = db.Table("keyboard")
    keyboard_table.create(["id", "name", "is_inline"], [
        "int(5) PRIMARY KEY AUTO_INCREMENT", "VARCHAR(128)",
        "BOOLEAN DEFAULT false"
    ])
    keyboard_table.close()

    bundles_table = db.Table("bundles")
    bundles_table.create(
        ["id_keyboard", "id_button", "id_next_button", "newline"],
        ["INT(5)", "INT(5)", "INT(5)", "BOOLEAN DEFAULT false"])
    bundles_table.close()
Ejemplo n.º 6
0
def add(array, name=None, is_inline=False):
    array = to2Array(array)

    keyboard_table = db.Table("keyboard")
    if (name != None):
        keyboard_table.insert(["name", "is_inline"], [name, is_inline])
    else:
        keyboard_table.insert("is_inline", is_inline)
    keyboard_id = keyboard_table.select_by_max("id", "id")[0][0]
    keyboard_table.close()

    buttons_table = db.Table("buttons")
    id_buttons = []
    for line in array:
        id_buttons_line = []
        for element in line:
            buttons_table.insert("text", element)
            id_button = buttons_table.select_by_max("id", "id")[0][0]
            id_buttons_line.append(id_button)
        id_buttons.append(id_buttons_line)
    buttons_table.close()

    bundles_table = db.Table("bundles", True)
    id_prev = 0
    for id_line in id_buttons:
        for i, id in enumerate(id_line):
            last_in_line = False
            if i == len(id_line) - 1:
                last_in_line = True
            bundles_table.insert(
                ["id_keyboard", "id_prev_button", "id_button", "newline"],
                [keyboard_id, id_prev, id, last_in_line])
            id_prev = id
    bundles_table.close()

    print(keyboard_id)
Ejemplo n.º 7
0
import db

class Category(object):
    pass

categoryTable = db.Table(
    "category", db.metadata,
    db.Column('id', db.Integer, primary_key=True),
    db.Column('fid', db.Integer),
    db.Column('gid', db.Integer),
)

db.mapper(Category, categoryTable)
Ejemplo n.º 8
0
import db


class Magnet(object):
    pass


magnetTable = db.Table(
    "magnet",
    db.metadata,
    db.Column('id', db.Integer, primary_key=True),
    db.Column('fid', db.Integer),
    db.Column('magnet_uri', db.TEXT),
)

db.mapper(Magnet, magnetTable)
Ejemplo n.º 9
0
from {{cookiecutter.app_name}}.database import db

tags_posts = db.Table('tags_posts',
                        db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')),
                        db.Column('post_id', db.Integer, db.ForeignKey('posts.id'))
                        )
Ejemplo n.º 10
0
from sqlalchemy.ext.associationproxy import association_proxy
# from {{cookiecutter.app_name}}.utils import uuid


def _role_find_or_create(r):
    role = Role.query.filter_by(name=r).first()
    if not(role):
        role = Role(name=r)
        role.insert()
    return role


user_role_table = db.Table('user_roles',
                           db.Column(
                               'user_id', db.VARCHAR(36),
                               db.ForeignKey('users.id')),
                           db.Column(
                               'role_id', db.VARCHAR(36),
                               db.ForeignKey('roles.id'))
                           )

role_abilities_table = db.Table('role_abilities',
                              db.Column(
                                  'role_id', db.VARCHAR(36),
                                  db.ForeignKey('roles.id')),
                              db.Column(
                                  'ability_id', db.VARCHAR(36),
                                  db.ForeignKey('abilities.id'))
                              )


def is_sequence(arg):
Ejemplo n.º 11
0
import db

class Cast(object):
    pass

castTable = db.Table(
    "cast", db.metadata,
    db.Column('id', db.Integer, primary_key=True),
    db.Column('aid', db.Integer),
    db.Column('fid', db.Integer),
)

db.mapper(Cast, castTable)
Ejemplo n.º 12
0
 def test_id_dict(self):
     test_id_dict = db.Table(((8, "33"), (2, "a"), (3, "soie"), (1, "l"), (9, "ser"))).id_dict()
     self.assertEqual(test_id_dict, {"33": 8, "a": 2, "soie": 3, "l": 1, "ser": 9})
Ejemplo n.º 13
0
from flask_security import RoleMixin, UserMixin, SQLAlchemySessionUserDatastore
from {{cookiecutter.project_core_dir}}.extensions import db

role_users = db.Table(
    'role_users',
    db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
    db.Column('role _id', db.Integer(), db.ForeignKey('role.id'))
)


class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

    def __str__(self):
        return self.name

    def __hash__(self):
        return hash(self.name)


class User(db.Model, UserMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(255))
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    last_login_at = db.Column(db.DateTime())
    current_login_at = db.Column(db.DateTime())
Ejemplo n.º 14
0
 def test_flat_list(self):
     test_tuple_one_column = db.Table((("33",), ("a",), ("soie",), ("l",), ("ser",)))
     self.assertEqual(test_tuple_one_column.flat_list(), ("33", "a", "soie", "l", "ser"))
Ejemplo n.º 15
0
import db


class Genre(object):
    pass


genreTable = db.Table(
    "genre",
    db.metadata,
    db.Column('gid', db.Integer, primary_key=True),
    db.Column('genre_name', db.VARCHAR(50), nullable=False),
    db.Column('url', db.VARCHAR(100), nullable=True),
)

db.mapper(Genre, genreTable)
Ejemplo n.º 16
0
import db

class Film(object):
    pass

filmTable = db.Table(
    "film", db.metadata,
    db.Column('fid', db.Integer, primary_key=True),
    db.Column('tag', db.VARCHAR(40), nullable=False),
    db.Column('file_name', db.VARCHAR(1000), nullable=False),
    db.Column('cover', db.VARCHAR(1000), nullable=True),
    db.Column('url', db.VARCHAR(200), nullable=True),
    db.Column('length', db.Integer, nullable=True),
    db.Column('date', db.VARCHAR(100), nullable=True),
    db.Column('director', db.VARCHAR(100), nullable=True),
    db.Column('maker', db.VARCHAR(100), nullable=True),
    db.Column('producer', db.VARCHAR(100), nullable=True),
    db.Column('score', db.FLOAT, nullable=True),
    db.Column('magnet', db.VARCHAR(1000), nullable=True),
)

db.mapper(Film, filmTable)
Ejemplo n.º 17
0
import db


class TopArtist(object):
    pass


topArtistTable = db.Table(
    "topartist",
    db.metadata,
    db.Column('aid', db.Integer, primary_key=True),
    db.Column('artist_name', db.VARCHAR(50), nullable=False),
    db.Column('stag', db.VARCHAR(40), nullable=True),
    db.Column('url', db.VARCHAR(100), nullable=True),
    db.Column('img', db.VARCHAR(100), nullable=True),
    db.Column('rank', db.Integer, nullable=True),
)

db.mapper(TopArtist, topArtistTable)
Ejemplo n.º 18
0
import db
from .product import Product
from .customer import Customer
from model.constants import constants as cnst
from gui.locale import rus as locale

products = db.Table('products', locale.PRODUCT_SOURCE, Product)
products.add_column(
    db.Column('id', 1, 'INTEGER PRIMARY KEY AUTOINCREMENT', user_init=False))
products.add_column(db.Column('name', 4, 'VARCHAR(100) UNIQUE NUT NULL'))
products.add_column(
    db.Column('count',
              2,
              'INT NOT NULL CHECK (count >= 0)',
              max_length=cnst.MAX_INT_LENGTH))
products.add_column(
    db.Column('price',
              2,
              'INT CHECK (price >= 0)',
              max_length=cnst.MAX_INT_LENGTH))
products.add_column(
    db.Column('reserved',
              3,
              'INT DEFAULT 0 CHECK (reserved >= 0)',
              user_init=False,
              max_length=cnst.MAX_INT_LENGTH))

customers = db.Table('customers', locale.CUSTOMER_SOURCE, Customer)
customers.add_column(
    db.Column('id', 1, 'INTEGER PRIMARY KEY AUTOINCREMENT', user_init=False))
customers.add_column(db.Column('surname', 4, 'VARCHAR(100) NOT NULL'))
Ejemplo n.º 19
0
from flask_login import UserMixin

from {{ cookiecutter.project_name }}.external import db


roles_users = db.Table('roles_users',
                       db.Column('role_id', db.ForeignKey('role.id',
                                                          ondelete='CASCADE'),
                                 nullable=False),
                       db.Column('user_id', db.ForeignKey('user.id',
                                                          ondelete='CASCADE'),
                                 nullable=False),
                       db.UniqueConstraint('role_id', 'user_id',
                                           name='ux_role_user'))


class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)

    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))


class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True)