Commit 3e42e8e5 authored by DmitSPopov's avatar DmitSPopov
Browse files

Update

parent dc8c6bbb
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="Flask">
<option name="enabled" value="true" />
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
......
from web_setting import db, flask_app, Migrate
migrate = Migrate(flask_app, db)
from routers import entry_routers, \
admin_routers, users_routers, advertisement_routers
from models.all_models import *
Single-database configuration for Flask.
# A generic, single database configuration.
[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic,flask_migrate
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[logger_flask_migrate]
level = INFO
handlers =
qualname = flask_migrate
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
from __future__ import with_statement
import logging
from logging.config import fileConfig
from flask import current_app
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.get_engine().url).replace(
'%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')
connectable = current_app.extensions['migrate'].db.get_engine()
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}
"""empty message
Revision ID: 3bbc208a5aac
Revises: 5ae7135b6889
Create Date: 2022-06-11 19:46:19.922562
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '3bbc208a5aac'
down_revision = '5ae7135b6889'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('approved_customer', sa.Column('status', sa.Boolean(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('approved_customer', 'status')
# ### end Alembic commands ###
"""empty message
Revision ID: 5ae7135b6889
Revises: 738aaa18aac1
Create Date: 2022-06-11 19:44:58.968187
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '5ae7135b6889'
down_revision = '738aaa18aac1'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('approved_customer', 'status')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('approved_customer', sa.Column('status', sa.INTEGER(), autoincrement=False, nullable=True))
# ### end Alembic commands ###
"""empty message
Revision ID: 71ac0e4b2c0e
Revises: c6689aa6dbb4
Create Date: 2022-06-11 18:19:22.551640
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '71ac0e4b2c0e'
down_revision = 'c6689aa6dbb4'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('coordinates_event',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('address', sa.String(), nullable=False),
sa.Column('coordinates', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.add_column('advertisement', sa.Column('coordinates_id', sa.Integer(), nullable=True))
op.create_foreign_key(None, 'advertisement', 'coordinates_event', ['coordinates_id'], ['id'])
op.drop_column('advertisement', 'coordinates')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('advertisement', sa.Column('coordinates', sa.VARCHAR(), autoincrement=False, nullable=True))
op.drop_constraint(None, 'advertisement', type_='foreignkey')
op.drop_column('advertisement', 'coordinates_id')
op.drop_table('coordinates_event')
# ### end Alembic commands ###
"""empty message
Revision ID: 738aaa18aac1
Revises: 71ac0e4b2c0e
Create Date: 2022-06-11 18:25:45.259411
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '738aaa18aac1'
down_revision = '71ac0e4b2c0e'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('advertisement', sa.Column('time_event', sa.Time(), nullable=True))
op.add_column('advertisement', sa.Column('type_event', sa.String(), nullable=True))
op.add_column('advertisement', sa.Column('time_create', sa.DateTime(), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('advertisement', 'time_create')
op.drop_column('advertisement', 'type_event')
op.drop_column('advertisement', 'time_event')
# ### end Alembic commands ###
"""empty message
Revision ID: 790b44b0524b
Revises: 3bbc208a5aac
Create Date: 2022-06-11 22:11:23.064862
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '790b44b0524b'
down_revision = '3bbc208a5aac'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('click_history',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('tags_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['tags_id'], ['tags.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('click_history')
# ### end Alembic commands ###
"""empty message
Revision ID: c6689aa6dbb4
Revises:
Create Date: 2022-06-11 14:58:26.990126
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'c6689aa6dbb4'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('community',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('community_name', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tags',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('tag_name', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('users',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('user_name', sa.String(), nullable=True),
sa.Column('login', sa.String(), nullable=False),
sa.Column('password', sa.Unicode(), nullable=False),
sa.Column('role', sa.Integer(), nullable=True),
sa.Column('email', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('login')
)
op.create_table('categories',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.Column('tags_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['tags_id'], ['tags.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('customers',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('coordinates', sa.String(), nullable=True),
sa.Column('points', sa.Integer(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('community_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['community_id'], ['community.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('employer',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('discription', sa.String(), nullable=True),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tags_community',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('tags_id', sa.Integer(), nullable=True),
sa.Column('community_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['community_id'], ['community.id'], ),
sa.ForeignKeyConstraint(['tags_id'], ['tags.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tags_users',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('user_id', sa.Integer(), nullable=True),
sa.Column('tags_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['tags_id'], ['tags.id'], ),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('advertisement',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('adv_name', sa.String(), nullable=True),
sa.Column('employer_id', sa.Integer(), nullable=True),
sa.Column('confirmed', sa.Integer(), nullable=True),
sa.Column('coordinates', sa.String(), nullable=True),
sa.Column('categories_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['categories_id'], ['categories.id'], ),
sa.ForeignKeyConstraint(['employer_id'], ['employer.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tags_categories',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('categories_id', sa.Integer(), nullable=True),
sa.Column('tags_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['categories_id'], ['categories.id'], ),
sa.ForeignKeyConstraint(['tags_id'], ['tags.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('approved_customer',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('status', sa.Integer(), nullable=True),
sa.Column('adv_id', sa.Integer(), nullable=True),
sa.Column('customer_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['adv_id'], ['advertisement.id'], ),
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('community_adv',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('community_id', sa.Integer(), nullable=True),
sa.Column('adv_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['adv_id'], ['advertisement.id'], ),
sa.ForeignKeyConstraint(['community_id'], ['community.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('tags_advertisement',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('adv_id', sa.Integer(), nullable=True),
sa.Column('tags_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['adv_id'], ['advertisement.id'], ),
sa.ForeignKeyConstraint(['tags_id'], ['tags.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('tags_advertisement')
op.drop_table('community_adv')
op.drop_table('approved_customer')
op.drop_table('tags_categories')
op.drop_table('advertisement')
op.drop_table('tags_users')
op.drop_table('tags_community')
op.drop_table('employer')
op.drop_table('customers')
op.drop_table('categories')
op.drop_table('users')
op.drop_table('tags')
op.drop_table('community')
# ### end Alembic commands ###
"""empty message
Revision ID: c7e0efc2d4ec
Revises: 790b44b0524b
Create Date: 2022-06-11 23:38:10.156967
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'c7e0efc2d4ec'
down_revision = '790b44b0524b'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('categories_tags_id_fkey', 'categories', type_='foreignkey')
op.drop_column('categories', 'tags_id')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('categories', sa.Column('tags_id', sa.INTEGER(), autoincrement=False, nullable=True))
op.create_foreign_key('categories_tags_id_fkey', 'categories', 'tags', ['tags_id'], ['id'])
# ### end Alembic commands ###
......@@ -35,9 +35,35 @@ class Users(db.Model):
self.password = password
self.email = new_email
# def __init__(self, **kwargs):
# super(Users, self).__init__(**kwargs)
def __repr__(self):
return f"User name:'{self.user_name}', login {self.login}"
@staticmethod
def _bootstrap(count=100):
from mimesis.providers import Person
from mimesis.locales import Locale
from mimesis.enums import Gender
person = Person(locale=Locale.RU)
for _ in range(count):
from random import randint
gender = randint(0, 1)
role = randint(0, 1)
if gender == 1:
gender = Gender.MALE
else:
gender = Gender.FEMALE
user = Users(user_name=person.full_name(gender), login=person.username(), password=person.password(), email=person.email(), role=role)
db.session.add(user)
try:
db.session.commit()
except Exception:
db.session.rollback()
class Categories(db.Model):
"""Все категории"""
......@@ -256,3 +282,5 @@ class ClickHistory(db.Model):
def __init__(self, user_id, tags_id):
self.user_id = user_id
self.tags_id = tags_id
......@@ -88,9 +88,7 @@ def calc_recomendation_by_location(user_data, all_data, tags_recomendation):
keys = list(all_data.keys())
for key, i in zip(keys, range(len(all_data))):
all_data[key] = destination[i]
result = [all_data[i + 1] for i in symmertic_matrix[0].toarray().argsort()[0]]
interaction_Sparse_transpose = interaction_Sparse.transpose(copy=True)
normalized_matrix_transpose = normalize(interaction_Sparse_transpose, norm="l2", axis=1)
......@@ -131,10 +129,7 @@ def get_recomendation_by_tags(user_id: int, all_adv=all_advertisments()):
tag_id.append(val['connect_tags']['id'])
all_data[_['id']] = tag_id
# print("user data by tags", user_data)
# print("all data by tags: ", all_data)
result_id = calc_recomendation_by_tags(user_data, all_data)
# print("result_by_tags:", result_id)
return result_id
......@@ -149,8 +144,4 @@ def get_recomendation_by_location(user_id: int, location_str: str):
coords[0], coords[1] = float(coords[0]), float(coords[1])
distance_data[_['id']] = coords
# print(distance_user)
# print(distance_data)
return calc_recomendation_by_location(distance_user, distance_data, get_recomendation_by_tags(user_id, all_adv))
# print(get_recomendation_by_location(2, "55.53570015221644, 36.95149256704093"))
\ No newline at end of file
return calc_recomendation_by_location(distance_user, distance_data, get_recomendation_by_tags(user_id, all_adv))
\ No newline at end of file