Commit 65368bcf authored by Eliss-good's avatar Eliss-good
Browse files

update

parent cd287556
......@@ -63,22 +63,10 @@ def filter_advertisments(id_adv=None):
if id_adv is not None:
data_query = data_query.filter(Advertisement.id == id_adv)
api_all_adv = AdvertisementSchema(many=True)
return api_all_adv.dump(data_query)
# def filter_by_community_advertisments(id_adv=None):
# data_query = _base_query()
# if id_adv is not None:
# data_query = data_query.filter(Advertisement.id == id_adv)
# api_all_adv = AdvertisementSchema(many=True)
# return api_all_adv.dump(data_query)
def indexes_advertisments(indexes: list = None):
data_query = _base_query()
......@@ -88,3 +76,6 @@ def indexes_advertisments(indexes: list = None):
api_all_adv = AdvertisementSchema(many=True)
return api_all_adv.dump(data_query)
from web_setting import flask_app, ma, db
from models.all_models import Community
from models.all_models import Community, CommunityAdv
#from api.api_tags import TagsSchema
from api.api_employer import EmployerSchema
from api.api_categories import CategoriesSchema
from api.api_customers import CustomersSchema
from api.api_tags import TagsGlobalSchema
from api.api_advertisment import AdvertisementSchema
class CoommunityAdvSchema(ma.SQLAlchemyAutoSchema):
class Meta:
model = CommunityAdv
load_instance = True
include_relationships = True
connect_reposted_by = ma.Nested(CustomersSchema(exclude=('comm_adv_reposted_by_customers','connect_com', )))
connect_adv_id = ma.Nested(AdvertisementSchema(only=('connect_coordinates', 'adv_id_tagsadv', 'time_create',
'wait_size_users', 'id', 'adv_name', )))
class CommunitySchema(ma.SQLAlchemyAutoSchema):
......@@ -13,9 +25,11 @@ class CommunitySchema(ma.SQLAlchemyAutoSchema):
model = Community
load_instance = True
include_relationships = True
exclude = ['community_cus', 'tags_community_community']
exclude = ['tags_community_community']
# connect_customer = ma.Nested(CustomersSchema(exclude=('connect_com', 'customer_approve', 'id', )))
comm_adv_comm_id_community = ma.List(ma.Nested(CoommunityAdvSchema(exclude=('connect_community_id',) )))
community_cus = ma.List(ma.Nested(CustomersSchema()))
connect_customer = ma.Nested(CustomersSchema(exclude=('connect_com', 'customer_approve', 'id',)))
def _base_query():
......@@ -34,6 +48,5 @@ def filter_communities(community_id=None):
if community_id is not None:
data_query = data_query.filter(Community.id == community_id)
api_all_adv = CommunitySchema(many=True)
return api_all_adv.dump(data_query)
......@@ -11,7 +11,7 @@ class CustomersSchema(ma.SQLAlchemyAutoSchema):
include_relationships = True
exclude = ['customer_approve']
connect_users = ma.Nested(UsersSchema(exclude=('users_emp', 'users_cus', 'password','id', )))
connect_users = ma.Nested(UsersSchema(exclude=('users_click','users_emp', 'users_cus', 'password','id', )))
def _base_query():
......
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 *
from flask import jsonify
from backend.models.all_models import Advertisement
from web_setting import flask_app, db
def create_advertisement(adv_info):
new_advertisement = Advertisement(
new_adv_name=adv_info.adv_name,
new_time_event=adv_info.time_event,
new_type_event=adv_info.type_event,
new_employer_id=adv_info.employer_id,
new_coordinates_id=adv_info.coordinates_id,
new_categories_id=adv_info.categories_id,
new_wait_size_users=adv_info.wait_size_users
)
db.session.add(new_advertisement)
db.session.commit()
return jsonify('advert add success')
......@@ -11,7 +11,6 @@ def location_from_address(default_location='Москва, Беговая, 17 к1
if location is None:
return json.dumps("Cannot find this address")
return location.address, location.latitude, location.longitude
except:
......@@ -21,10 +20,9 @@ def location_from_address(default_location='Москва, Беговая, 17 к1
def location_from_coordinates(default_coordinates="52.509669, 13.376294"):
try:
location = geolocator.reverse(default_coordinates)
if location is None:
return json.dumps("Wrong coordinates")
return location.address, location.latitude, location.longitude
except:
return json.dumps(f"Cannot make location from coordinates ({default_coordinates})")
from buisness_logic.coordinates_to_adress import location_from_address, location_from_coordinates
from models.all_models import Advertisement, CommunityAdv, СoordinatesEvent
from web_setting import flask_app, db
from models.all_models import Users, Employer, Customers, TagsUsers, ClickHistory
from models.all_models import Users, Employer, Customers, TagsUsers, ClickHistory, CommunityAdv
from werkzeug.security import generate_password_hash, check_password_hash
from routers.all_dataclasses import EntryDate, TagsIndex
from routers.all_dataclasses import EntryDate, TagsIndex, RepostEvent
from typing import List
......@@ -33,6 +31,16 @@ def add_new_users(RegisterData: EntryDate):
return "successful registration"
def add_new_repost(new_repost: RepostEvent):
repost = CommunityAdv(new_community_id=new_repost.community_id_id,
new_reposted_by=new_repost.custimers_id,
new_adv_id=new_repost.adv_id,
new_is_repost=True)
db.session.add(repost)
db.session.commit()
def add_new_click(NewClick: TagsIndex):
clicks_queue = []
......@@ -45,83 +53,6 @@ def add_new_click(NewClick: TagsIndex):
def create_advertisement(adv_name, time_event, type_event, employer_id, coordinates_id, categories_id, wait_size_users):
new_advertisement = Advertisement(
new_adv_name=adv_name,
new_time_event=time_event,
new_type_event=type_event,
new_employer_id=employer_id,
new_coordinates_id=coordinates_id,
new_categories_id=categories_id,
new_wait_size_users=wait_size_users
)
db.session.add(new_advertisement)
db.session.commit()
return 'advert add success'
# def add_new_advertisement_to_community(adv_info, community_info):
# new_advertisement = Advertisement(
# new_adv_name=adv_info.adv_name,
# new_time_event=adv_info.time_event,
# new_type_event=adv_info.type_event,
# new_employer_id=adv_info.employer_id,
# new_coordinates_id=adv_info.coordinates_id,
# new_categories_id=adv_info.categories_id,
# new_wait_size_users=adv_info.wait_size_users
# )
# new_community_advertisement = CommunityAdv(
# new_community_id=,
# new_adv_id=,
# new_is_repost=False,
# new_reposted_by=
# )
# db.session.add(new_advertisement)
# db.session.commit()
# return 'advert for community add success'
def add_new_repost(adv_id, repost_to_community_id, reposter_user_id):
new_repost = CommunityAdv(
new_community_id=adv_id,
new_adv_id=repost_to_community_id,
new_is_repost=True,
new_reposted_by=reposter_user_id
)
# add new unique tags
db.session.add(new_repost)
db.session.commit()
return 'repost add success'
def add_new_address_from_coordinates(coordinates):
address = location_from_coordinates(coordinates)
new_data = СoordinatesEvent(
address=address,
coordinates=coordinates
)
db.session.add(new_data)
db.session.commit()
def add_new_coordinates_from_address(address):
coordinates = location_from_address(address)
new_data = СoordinatesEvent(
address=address,
coordinates=coordinates
)
db.session.add(new_data)
db.session.commit()
""" 1) Подписка на комьюнити и инскрт новых коммьюнити
2) Создание новых объявлений
3) обработка друзей и лоби"""
"""empty message
Revision ID: dc7444888d26
Revises:
Create Date: 2022-06-12 20:21:35.036304
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'dc7444888d26'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('categories',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('name', sa.String(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('community',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('community_name', sa.String(), nullable=True),
sa.Column('community_description', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
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.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('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')
)
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_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('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('time_event', sa.Time(), nullable=True),
sa.Column('type_event', sa.String(), nullable=True),
sa.Column('time_create', sa.DateTime(), nullable=True),
sa.Column('wait_size_users', sa.Integer(), nullable=True),
sa.Column('employer_id', sa.Integer(), nullable=True),
sa.Column('coordinates_id', sa.Integer(), nullable=True),
sa.Column('categories_id', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['categories_id'], ['categories.id'], ),
sa.ForeignKeyConstraint(['coordinates_id'], ['coordinates_event.id'], ),
sa.ForeignKeyConstraint(['employer_id'], ['employer.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('approved_customer',
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
sa.Column('status', sa.Boolean(), 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.Column('is_repost', sa.Boolean(), nullable=True),
sa.Column('reposted_by', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(['adv_id'], ['advertisement.id'], ),
sa.ForeignKeyConstraint(['community_id'], ['community.id'], ),
sa.ForeignKeyConstraint(['reposted_by'], ['customers.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('advertisement')
op.drop_table('tags_users')
op.drop_table('tags_community')
op.drop_table('tags_categories')
op.drop_table('employer')
op.drop_table('customers')
op.drop_table('click_history')
op.drop_table('users')
op.drop_table('tags')
op.drop_table('coordinates_event')
op.drop_table('community')
op.drop_table('categories')
# ### end Alembic commands ###
"""empty message
Revision ID: f5959aecd282
Revises: dc7444888d26
Create Date: 2022-06-12 20:23:35.847915
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision = 'f5959aecd282'
down_revision = 'dc7444888d26'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('advertisement', sa.Column('end_event', sa.DateTime(), nullable=True))
op.add_column('advertisement', sa.Column('discription', sa.Text(), nullable=True))
op.add_column('advertisement', sa.Column('status_end', sa.Boolean(), nullable=True))
op.drop_column('advertisement', 'time_event')
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('advertisement', sa.Column('time_event', postgresql.TIME(), autoincrement=False, nullable=True))
op.drop_column('advertisement', 'status_end')
op.drop_column('advertisement', 'discription')
op.drop_column('advertisement', 'end_event')
# ### end Alembic commands ###
......@@ -146,9 +146,11 @@ class Advertisement(db.Model):
__tablename__ = 'advertisement'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
adv_name = db.Column(db.String)
time_event = db.Column(db.Time)
end_event = db.Column(db.DateTime)
type_event = db.Column(db.String)
time_create = db.Column(db.DateTime)
discription = db.Column(db.Text)
status_end = db.Column(db.Boolean)
wait_size_users = db.Column(db.Integer)
......@@ -161,14 +163,14 @@ class Advertisement(db.Model):
categories_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
connect_categories = db.relationship('Categories', backref='categories_advertisement', uselist=False)
def __init__(self, new_adv_name, new_employer_id, new_type_event, new_coordinates_id, new_categories_id, new_wait_size_users, new_time_event):
def __init__(self, new_adv_name, new_employer_id, new_coordinates, new_categories_id, new_tags_id,
new_confirmed=default_confirmed_status):
self.adv_name = new_adv_name
self.time_event = new_time_event
self.type_event = new_type_event
self.employer_id = new_employer_id
self.coordinates_id = new_coordinates_id
self.confirmed = new_confirmed
self.coordinates = new_coordinates
self.categories_id = new_categories_id
self.wait_size_users = new_wait_size_users
self.tags_id = new_tags_id
def __repr__(self):
return f"Advertisement name:'{self.adv_name}"
......
from datetime import datetime
from multiprocessing.connection import wait
from buisness_logic.coordinates_to_adress import location_from_coordinates
from buisness_logic.insert_models import create_advertisement
from web_setting import flask_app, cross_origin
from api.api_advertisment import all_advertisments, indexes_advertisments, filter_advertisments
from api.api_customers import filter_customers
......@@ -11,7 +7,7 @@ from flask import jsonify, request
from routers.all_dataclasses import TagsIndex, FilterRad
from buisness_logic.insert_models import add_new_click
from recomendation_system import get_recomendation_by_tags, get_recomendation_by_location
from recomendation_system import get_recomendation_by_tags, get_recomendation_by_location, get_adv_in_radius
@flask_app.route('/api/advertisement', methods=["POST", "GET"])
......@@ -22,10 +18,12 @@ def advertisement():
return jsonify('XER')
@flask_app.route('/api/advertisement/get/<int:adv_id>_<int:user_id>/', methods=["GET"])
def get_advertisement(adv_id=None, user_id=None):
if adv_id is not None:
js_result = filter_advertisments(id_adv=adv_id)
@flask_app.route('/api/advertisement/get/<int:adv_id>/', methods=["GET", "POST"])
def get_advertisement(adv_id=None):
js_result = filter_advertisments(id_adv=adv_id)
if request.method == 'POST':
user_id = request.json.get('user_id')
for _ in js_result:
tag_id = []
......@@ -34,22 +32,39 @@ def get_advertisement(adv_id=None, user_id=None):
con_tags = TagsIndex(tags_id=tag_id, index_connect=user_id)
add_new_click(con_tags)
return jsonify(js_result)
return jsonify(js_result)
return jsonify('Error'), 401
@flask_app.route('/api/advertisement/recommendations/<int:user_id>/', methods=["GET"])
def rec_advertisement(user_id=None):
@flask_app.route('/api/advertisement/recomendate/<int:user_id>/', methods=["GET"])
def recomendate_advertisement(user_id=None):
if user_id is not None:
advertisments_ids = get_recomendation_by_tags(user_id)
return jsonify(indexes_advertisments(advertisments_ids))
return jsonify('Error rec_advertisement'), 401
@flask_app.route('/api/advertisement/recommendations_geo/', methods=["POST"])
def rec_geo_advertisement(user_id=None):
user_id = request.json.get('user_id')
coordinate = request.json.get('coodinate')
if user_id is not None:
if coordinate is None:
try:
coordinate = filter_customers(user_id=user_id)[0]['coordinates']
except:
jsonify('Error rec_geo_advertisement'), 401
advertisments_ids = get_recomendation_by_location(user_id, coordinate)
return indexes_advertisments(advertisments_ids)
return jsonify('Error'), 401
return jsonify('Error rec_geo_advertisement'), 401
@flask_app.route('/api/advertisement/filter/rad/', methods=["POST"])
@flask_app.route('/api/advertisement/filter/radius/', methods=["POST"])
def routers_advertisement():
if request.method == 'POST':
data_user = FilterRad(**request.get_json())
......@@ -58,23 +73,10 @@ def routers_advertisement():
data_user.coordinate = filter_customers(user_id=data_user.user_id)[0]['coordinates']
try:
id_adv = []#вызов функции(FilterRad.coordinate, FilterRad.rad)
id_adv = get_adv_in_radius(FilterRad.coordinate, FilterRad.rad)
except:
jsonify('XER'), 401
indexes_advertisments(id_adv)
return jsonify('XER')
@flask_app.route('/api/advertisement/create/<string:adv_name>_<string:time_event>_<string:type_event>_<int:employer_id>_<int:coordinates>_<int:categories_id>_<int:wait_size_users>/', methods=["GET","POST"])
def create_adv(adv_name=None, time_event=None, type_event=None, employer_id=None, coordinates=None, categories_id=None, wait_size_users=None):
if adv_name is not None and time_event is not None and type_event is not None and employer_id is not None and coordinates_id is not None and categories_id is not None and wait_size_users:
print('\n\n', time_event)
parsed_time_event = datetime.strptime(time_event, '%H:%M-%d-%m-%Y')
address = location_from_coordinates(coordinates)
jsonify('Error routers_advertisement'), 401
return jsonify(indexes_advertisments(id_adv))
create_advertisement(adv_name, parsed_time_event, type_event, employer_id, coordinates_id, categories_id, wait_size_users)
return jsonify("event created successfully")
return jsonify('Error'), 401
......@@ -26,5 +26,8 @@ class FilterRad(BaseModel):
coordinate: Optional[str] = None