Skip to content

Commit

Permalink
new service file for crating base user types
Browse files Browse the repository at this point in the history
  • Loading branch information
gaskeo committed Oct 25, 2022
1 parent 93e226f commit de84bbf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 28 deletions.
10 changes: 3 additions & 7 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
from handlers import index, grant, \
helpdesk, \
error_handlers, user, channel, mixin, service
from redis_storage.params_creator import get_limits_params

import limits

from storage.db_session import base_init
from redis_storage.redis_session import base_init as redis_base_init
from version import version
from content_limits import init_limit

# Flask init
app = Flask(__name__)
Expand All @@ -40,11 +40,7 @@ def add_header(response):
SessObject = base_init()
RedisSessObject = redis_base_init()

redis_params = get_limits_params()
storage_uri = f"redis://{redis_params['host']}:" \
f"{redis_params['port']}"

limit_generator = init_limit(app, storage_uri, redis_params)
limit_generator = limits.limit_generator(app)

# Blueprints registration
app.register_blueprint(index.create_handler(limit_generator))
Expand Down
15 changes: 15 additions & 0 deletions limits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# _ _ _ _ _ __ __ ____
# | | (_) | | | | (_) | \/ |/ __ \
# | | _ | |_ | |__ _ _ _ _ __ ___ | \ / | | | |
# | | | | | __| | "_ \ | | | | | | | "_ ` _ \ | |\/| | | | |
# | |____ | | | |_ | | | | | | | |_| | | | | | | | | | | | |__| |
# |______| |_| \__| |_| |_| |_| \__,_| |_| |_| |_| |_| |_|\___\_\

from redis_storage.params_creator import get_redis_link
from content_limits import init_limit
from flask import Flask


def limit_generator(app: Flask):
uri, params = get_redis_link()
return init_limit(app, uri, params)
11 changes: 11 additions & 0 deletions redis_storage/params_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,14 @@ def get_limits_params() -> Params:
password = getenv('redis_limit_password') or ''

return Params(host=host, port=port, db=db, password=password)


redis_uri = str


def get_redis_link() -> (redis_uri, Params):
redis_params = get_limits_params()
storage_uri = f"redis://{redis_params['host']}:" \
f"{redis_params['port']}"

return storage_uri, redis_params
45 changes: 45 additions & 0 deletions service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# _ _ _ _ _ __ __ ____
# | | (_) | | | | (_) | \/ |/ __ \
# | | _ | |_ | |__ _ _ _ _ __ ___ | \ / | | | |
# | | | | | __| | "_ \ | | | | | | | "_ ` _ \ | |\/| | | | |
# | |____ | | | |_ | | | | | | | |_| | | | | | | | | | | | |__| |
# |______| |_| \__| |_| |_| |_| \__,_| |_| |_| |_| |_| |_|\___\_\

from storage.db_session import base_init
from storage.user_type import UserType

# TODO in json or something else
USER_TYPES = [
UserType(
name='Free',
max_channel_count=2,
max_message_size=256,
bufferization=True,
max_bufferred_message_count=256,
buffered_data_persistency=12,
end_to_end_data_encryption=False
)
]


# init data in tables
def init_db_data():
sess_object = base_init()
session = sess_object()
accounts = session.query(UserType).first()
if accounts:
print('db inited')
return
print('generate db data...')
for user_type in USER_TYPES:
session.add(user_type)
session.commit()
print('db init done')


def main():
init_db_data()


if __name__ == "__main__":
main()
22 changes: 1 addition & 21 deletions storage/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,4 @@ CREATE TABLE messages
tag VARCHAR(128),
msg_type int,
content bytea
);

CREATE TABLE user_types (
type_id integer NOT NULL,
name character varying(32),
max_channel_count integer NOT NULL,
max_message_size integer NOT NULL,
bufferization boolean NOT NULL,
max_bufferred_message_count integer NOT NULL,
buffered_data_persistency integer NOT NULL,
end_to_end_data_encryption boolean NOT NULL
);

INSERT INTO user_types (name,
max_channel_count,
max_message_size,
bufferization,
max_bufferred_message_count,
buffered_data_persistency,
end_to_end_data_encryption)
VALUES ('Free', 2, 256, true, 256, 12, false);
);

0 comments on commit de84bbf

Please sign in to comment.