Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions TelegrammBotProject/Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bot: python3 telegabot.py
19 changes: 19 additions & 0 deletions TelegrammBotProject/categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{"categories":[
{
"id": "1",
"name": "Первые блюда"
},
{
"id": "2",
"name": "Салаты"
},
{
"id": "3",
"name": "Основные блюда"
},
{
"id": "4",
"name": "Дессерты"
}
]
}
170 changes: 170 additions & 0 deletions TelegrammBotProject/check_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
from database_connection import engine
from db_classes import Categories, Dishes, Clients, Marks,Base
from sqlalchemy.sql import select, update, join
from sqlalchemy import and_, or_, between, asc, desc,update
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import func
import plotly.express as px
import plotly.graph_objects as go



# Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

# функция для добавления нового пользователя
def check_user_exist(username):

try:
# q = session.query(Clients).filter_by(Clients.id == '1')

if bool(session.query(Clients.id).filter_by(id = '1').first()) is False:

row = Clients(id = '1',client_username = username)
session.add(row)
session.commit()
search_query = (session.query(Clients).filter_by(id = '1').one()).client_username
print('Hello,', search_query)

else:

if bool(session.query(Clients.client_username).filter_by(client_username = username).first()) is True:
print('User already exist')

else:
id_value = session.query(Clients.id).count()
row = Clients(id = id_value+1, client_username = username)
session.add(row)
session.commit()
print('We successfully added you to our database')

except:
print('This user already exsist')

# функция для проверки значения оценки
def check_mark_value(id,mark,values,id_client,dish_id,text):
if mark > 5:
print('Max value is 5')
else:
row = Marks(id=id, mark_value=values,client_id = id_client,dish_id = dish_id,text_reviw = text)
session.add(row)
session.commit()
# bool(User.query.filter_by(name='John Smith').first())

# функция для проверки существования блюда
def check_is_dish_exist(dish_name):
# exsist = session.query(Dishes).filter(Dishes.dish_name == dish_name)
if bool(session.query(Dishes.dish_name).filter_by(dish_name = dish_name).first()) is False:
return False

else:
return True
#
def if_not_exist_dish():

result = session.query(Dishes.dish_name).all()
return 'Такого блюда не существует в нашей базе данных, посмотрите блюда, которые доступны',result

def result_of_search(dish_name):

result = (session.query(Dishes).filter(Dishes.dish_name == dish_name).one())
return result.ingridients, result.text_recipe

# функция для получения видео рецепта
def get_video_recipe(dish_name):
# exsist = session.query(Dishes).filter(Dishes.dish_name == dish_name)
if bool(session.query(Dishes.dish_name).filter_by(dish_name = dish_name).first()) is False:
return False
else:
return True

def get_exsist_video_recipe(dish_name):
result = (session.query(Dishes).filter(Dishes.dish_name == dish_name).one()).video_recept
return result
# функция для добавления оценки пользователя
def add_user_mark(mark,client_username,dish_name):

if bool(session.query(Marks.id).filter_by(id='1').first()) is False:
id_found = (session.query(Clients).filter(Clients.client_username == client_username).first()).id
dish_id = (session.query(Dishes).filter(Dishes.dish_name == dish_name).first()).id


row = Marks(id= '1', mark_value = mark, client_id = id_found, dish_id = dish_id,user_name = client_username)
session.add(row)
session.commit()
return 'Вы наш первый пользователь.Мы успешно добавили вашу первую оценку'

elif bool(session.query(Marks).filter(Marks.user_name == client_username).first()) is True:
session.query(Marks).filter(Marks.user_name == client_username).update({Marks.mark_value: mark},synchronize_session=False)
session.commit()
return 'Мы успешно обновили вашу оценку'
else:
id_found = (session.query(Clients).filter(Clients.client_username == client_username).one()).id
dish_id = (session.query(Dishes).filter(Dishes.dish_name == dish_name).one()).id
id_value = session.query(Marks.id).count()
row = Marks(id=id_value + 1, mark_value = mark, client_id = id_found, dish_id = dish_id, user_name = client_username)
session.add(row)
session.commit()
return 'Мы успешно добавили вашу оценку'
# функция для проверки принадлежности блюда к категории
def check_dish_category(dish_name):
id_dish = (session.query(Dishes).filter(Dishes.dish_name == dish_name).one()).id
dish_category = (session.query(Categories).filter(Categories.id == id_dish).one()).category_name
return dish_category
# функция добавления комментария пользователя в бд
def add_comments(message,username):

session.query(Marks).filter(Marks.user_name == username).update({Marks.text_reviw: message},synchronize_session = False)
session.commit()
return 'Мы успешно добавили ваш комментарий'
# функции для просмотре блюд и коментариев
def see_users_comments():
return session.query(Marks.user_name,Marks.text_reviw).all()

def see_all_soups():
return (session.query(Dishes.dish_name).filter(Dishes.id_category == 1).all())

def see_all_main_dishes():
return (session.query(Dishes.dish_name).filter(Dishes.id_category == 3).all())

def see_all_salads():
return (session.query(Dishes.dish_name).filter(Dishes.id_category == 2).all())

def see_all_desserts():
return (session.query(Dishes.dish_name).filter(Dishes.id_category == 4).all())

# график распределения оценок пользователей
def statistic_of_marks():
result = []
for i in range(1,6):
result.append(session.query(Marks).filter(Marks.mark_value== i).count())

return result
def avarage_of_mark():

result = session.query(func.avg(Marks.mark_value).label('average')).all()
#
# sum_of_marks = session.query(Marks.mark_value).all()
return round((result[0][0]),1)
# num_of_marks = len(sum_of_marks)
# result = sum(sum_of_marks)/num_of_marks
# print(result)


















11 changes: 11 additions & 0 deletions TelegrammBotProject/clients.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{"clients":[
{
"id": "2",
"name": "dima"
},
{
"id": "3",
"name": "olya"
}
]
}
7 changes: 7 additions & 0 deletions TelegrammBotProject/database_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from sqlalchemy.engine import create_engine

# для создания таблиц Вам неободимо прописать путь к своей базе данніх oracle
DATABASE_URI = 'postgres+psycopg2://postgres:msn1973msn@localhost:5432/bot'

engine = create_engine(DATABASE_URI)

144 changes: 144 additions & 0 deletions TelegrammBotProject/db_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
from sqlalchemy import Column, Integer, String, ForeignKey,Text
from sqlalchemy.ext.declarative import declarative_base
from database_connection import engine
import json

Base = declarative_base()

from sqlalchemy.orm import relationship, sessionmaker
# создание таблиц
class Categories(Base):

__tablename__ = 'dish_categories'

id = Column(Integer, primary_key=True)
category_name = Column(Text)

styles = relationship('Dishes', backref='dish_categories', lazy=True)

class Dishes(Base):

__tablename__ = 'dishes'

id = Column(Integer, primary_key=True)
dish_name = Column(Text)
id_category = Column(Integer, ForeignKey("dish_categories.id"))
text_recipe = Column(Text)
video_recept = Column(Text)
ingridients = Column(Text)


class Clients(Base):

__tablename__ = 'clients'

id = Column(Integer, primary_key=True)
client_username = Column(Text)

class Marks(Base):

__tablename__ = 'marks'

id = Column(Integer, primary_key=True)
mark_value = Column(Integer)
client_id = Column(Integer, ForeignKey("clients.id"))
dish_id = Column(Integer, ForeignKey("dishes.id"))
text_reviw = Column(Text)
user_name = Column(Text)

styles = relationship('Clients', backref='marks', lazy=True)
styles_1 = relationship('Dishes', backref='marks', lazy=True)

Base.metadata.create_all(engine)

# добавления данных в бд
# Session = sessionmaker(bind = engine)
# session = Session()
# считывание данных с файла, на другом компьютере прийдеся вставить другой путь к файлам

# categories = open("C:/Pyprojects/venv/categories.json",encoding="utf-8")
# data_categories = json.load(categories)
# dishes = open("C:/Pyprojects/venv/recipes.json",encoding="utf8")
# data_dishes = json.load(dishes)
# marks = open("C:/Pyprojects/venv/marks.json",encoding="utf8")
# data_marks = json.load(marks)
# clients = open("C:/Pyprojects/venv/clients.json",encoding="utf8")
# data_clients = json.load(clients)

# добавление категорий блюд
# session.add_all([
# Categories(id = data_categories['categories'][0]['id'], category_name = data_categories['categories'][0]['name']),
# Categories(id = data_categories['categories'][1]['id'], category_name = data_categories['categories'][1]['name']),
# Categories(id = data_categories['categories'][2]['id'], category_name = data_categories['categories'][2]['name']),
# Categories(id = data_categories['categories'][3]['id'], category_name = data_categories['categories'][3]['name'])]
# )
# session.commit()
#
# # добавление рецептов и названий блюд
# session.add_all([
# Dishes(id = data_dishes['dish'][0]['id'],
# dish_name = data_dishes['dish'][0]['name'],
# id_category=data_dishes['dish'][0]['id_category'],
# text_recipe = data_dishes['dish'][0]['text_recipe'],
# video_recept=data_dishes['dish'][0]['video_recipe'],
# ingridients=data_dishes['dish'][0]['ingridients']),
# Dishes(id = data_dishes['dish'][1]['id'],
# dish_name = data_dishes['dish'][1]['name'],
# id_category=data_dishes['dish'][1]['id_category'],
# text_recipe = data_dishes['dish'][1]['text_recipe'],
# video_recept=data_dishes['dish'][1]['video_recipe'],
# ingridients=data_dishes['dish'][1]['ingridients']),
# Dishes(id = data_dishes['dish'][2]['id'],
# dish_name = data_dishes['dish'][2]['name'],
# id_category=data_dishes['dish'][2]['id_category'],
# text_recipe = data_dishes['dish'][2]['text_recipe'],
# video_recept=data_dishes['dish'][2]['video_recipe'],
# ingridients=data_dishes['dish'][2]['ingridients']),
# Dishes(id = data_dishes['dish'][3]['id'],
# dish_name = data_dishes['dish'][3]['name'],
# id_category=data_dishes['dish'][3]['id_category'],
# text_recipe = data_dishes['dish'][3]['text_recipe'],
# video_recept=data_dishes['dish'][3]['video_recipe'],
# ingridients=data_dishes['dish'][3]['ingridients']),
# Dishes(id = data_dishes['dish'][4]['id'],
# dish_name = data_dishes['dish'][4]['name'],
# id_category=data_dishes['dish'][4]['id_category'],
# text_recipe = data_dishes['dish'][4]['text_recipe'],
# video_recept=data_dishes['dish'][4]['video_recipe'],
# ingridients=data_dishes['dish'][4]['ingridients']),
# Dishes(id = data_dishes['dish'][5]['id'],
# dish_name = data_dishes['dish'][5]['name'],
# id_category=data_dishes['dish'][5]['id_category'],
# text_recipe = data_dishes['dish'][5]['text_recipe'],
# video_recept=data_dishes['dish'][5]['video_recipe'],
# ingridients=data_dishes['dish'][5]['ingridients']),
# Dishes(id = data_dishes['dish'][6]['id'],
# dish_name = data_dishes['dish'][6]['name'],
# id_category=data_dishes['dish'][6]['id_category'],
# text_recipe = data_dishes['dish'][6]['text_recipe'],
# video_recept=data_dishes['dish'][6]['video_recipe'],
# ingridients=data_dishes['dish'][6]['ingridients']),
# Dishes(id = data_dishes['dish'][7]['id'],
# dish_name = data_dishes['dish'][7]['name'],
# id_category=data_dishes['dish'][7]['id_category'],
# text_recipe = data_dishes['dish'][7]['text_recipe'],
# video_recept=data_dishes['dish'][7]['video_recipe'],
# ingridients=data_dishes['dish'][7]['ingridients']),
# Dishes(id = data_dishes['dish'][8]['id'],
# dish_name = data_dishes['dish'][8]['name'],
# id_category=data_dishes['dish'][8]['id_category'],
# text_recipe = data_dishes['dish'][8]['text_recipe'],
# video_recept=data_dishes['dish'][8]['video_recipe'],
# ingridients=data_dishes['dish'][8]['ingridients']),
# ]
# )
# session.commit()
#
# session.add_all([
# Categories(id = data_categories['categories'][0]['id'], category_name = data_categories['categories'][0]['name']),
# Categories(id = data_categories['categories'][1]['id'], category_name = data_categories['categories'][1]['name']),
# Categories(id = data_categories['categories'][2]['id'], category_name = data_categories['categories'][2]['name']),
# Categories(id = data_categories['categories'][3]['id'], category_name = data_categories['categories'][3]['name'])]
# )
# session.commit()

Loading