def edit_profile(): form = edit_profile_form() if request.method == 'POST': if form.validate_on_submit(): current_user.name = form.name.data current_user.lastname = form.lastname.data current_user.day = form.day.data current_user.month = form.month.data current_user.year = form.year.data current_user.blood_type = form.blood_type.data current_user.weight = form.weight.data current_user.height = form.height.data session = db.getSession(engine) session.merge(current_user) session.commit() return redirect('/profile') elif request.method == 'GET': form.name.data = current_user.name form.lastname.data = current_user.lastname form.day.data = current_user.day form.month.data = current_user.month form.year.data = current_user.year form.blood_type.data = current_user.blood_type form.weight.data = current_user.weight form.height.data = current_user.height return render_template('edit_profile.html', form=form)
def buy_with_bonuses(): session = db_session.create_session() basket_ = [int(i) for i in current_user.basket.split()] price = 0 for i in basket_: price += session.query(Products).filter(Products.id == i).first().price p = price if current_user.is_vip: price -= 0.2 * p if current_user.bonuses >= 500: current_user.bonuses -= 500 price -= 0.5 * p if price > current_user.money: current_user.bonuses += 500 else: price -= current_user.bonuses / 1000 * p if price >= current_user.money: current_user.bonuses = 0 if price > current_user.money: return redirect('/not_enough') current_user.money -= price current_user.basket = '' session.merge(current_user) session.commit() return render_template("buy.html", basket=basket_)
def add_task(_id): if current_user.is_authenticated: form = NewTaskForm() if form.validate_on_submit(): session = db_session.create_session() board = session.query( Board.Board).filter(Board.Board.id == _id).first() TASKS = board.tasks if len(TASKS) == 0: taskId = 1 else: taskId = int(TASKS[-1].id) + 1 task = Task.Task( # id=taskId, title=form.title.data, content=form.content.data, author=current_user.name, user_id=current_user.id, # board=_id ) # taskId = task.id # print(taskId) # board.tasks = board.tasks + str(task.id) + ',' board.tasks.insert(0, task) print(board.tasks) session.merge(board) session.commit() return redirect('/board/' + str(_id)) return render_template('new_task.html', form=form) return redirect('/login')
def clicker(): if str(current_user) == 'real': session = db_session.create_session() current_user.bonuses += 0.1 session.merge(current_user) session.commit() return render_template("bot.html")
def basket_delete(id): session = db_session.create_session() s = current_user.basket.split() s.remove(str(id)) current_user.basket = ' '.join(s) session.merge(current_user) session.commit() return redirect('/basket')
def vip(): session = db_session.create_session() if current_user.money >= 500: current_user.money -= 500 current_user.is_vip = True else: return redirect('/not_enough') session.merge(current_user) session.commit() return redirect('/account')
def commit_parameter_to_ecs(deviceId, tempara, humidity, gas, pm, fire, smoke, update_time): parameter_update = parameter(parameterId=deviceId, temprature=tempara, humidity=humidity, gas=gas, pm=pm, fire=fire, smoke=smoke, update_time=update_time) session.merge(parameter_update) session.commit()
def products(id): session = db_session.create_session() products = session.query(Products).filter(Products.id == id).first() if str(products.id) not in str(current_user.basket).split(): if current_user.basket: current_user.basket = str(current_user.basket) + ' ' + str( products.id) else: current_user.basket = str(products.id) session.merge(current_user) session.commit() return redirect('/')
def add_to_search(game, types): check_last_page() if not "/searchmates" in current_user.last_page: session = db_session.create_session() current_user.last_page = f"/searchmates/{game}/{types}" session.merge(current_user) session.commit() with open("static/json/searching_mates.json") as file: data = json.load(file) if current_user.id not in data[game][types]: data[game][types] += [current_user.id] with open("static/json/searching_mates.json", "w") as file: json.dump(data, file) elif ("/searchmates" in current_user.last_page and (game != current_user.last_page.split("/")[1] or types != current_user.last_page.split("/")[2])): with open("static/json/searching_mates.json") as file: data = json.load(file) games, typess = current_user.last_page.split( "/")[2], current_user.last_page.split("/")[3] if current_user.id in data[games][typess]: del data[games][typess][data[games][typess].index(current_user.id)] with open("static/json/searching_mates.json", "w") as file: json.dump(data, file) session = db_session.create_session() current_user.last_page = f"/searchmates/{game}/{types}" session.merge(current_user) session.commit() with open("static/json/searching_mates.json") as file: data = json.load(file) if current_user.id not in data[game][types]: data[game][types] += [current_user.id] with open("static/json/searching_mates.json", "w") as file: json.dump(data, file) mates_id = data[game][types] mates = [] session = db_session.create_session() for mate_id in mates_id: mate = session.query(users.User).get(mate_id) mates.append([mate.name, mate.reputation, mate.avatar, mate.id, 0]) colors = choice(["primary", "success", "danger", "info"]) sessions = db_session.create_session() try: settings_info = sessions.query(settings_db.Settings_db).filter( settings_db.Settings_db.user_id == current_user.id).first() main_color = settings_info.theme except AttributeError: main_color = "white" return render_template("search_table.html", colors=colors, mates=mates, main_color=main_color)
def add_news(): form = NewsForm() if form.validate_on_submit(): session = db_session.create_session() news = News() news.title = form.title.data news.content = form.content.data news.is_private = form.is_private.data current_user.news.append(news) session.merge(current_user) session.commit() return redirect('/') return render_template('news.html', title='Добавление новости', form=form)
def add_vacancy(): form = VacancyForm() if form.validate_on_submit(): session = db_session.create_session() vacancys = Vacancys() vacancys.title = form.title.data vacancys.content = form.content.data current_user.vacancys.append(vacancys) session.merge(current_user) session.commit() return redirect('/') return render_template('vacancy.html', title='Добавление вакансии', form=form)
def add_job(): form = JobForm() if form.validate_on_submit(): session = db_session.create_session() job = Jobs() job.title = form.title.data job.team_leader = form.id.data job.size = form.size.data job.is_finished = form.is_finished.data current_user.job.append(job) session.merge(current_user) session.commit() return redirect('/') return render_template('jobs.html', title='Adding Job', form=form)
def check_last_page(): if current_user.is_authenticated and "/searchmates" in current_user.last_page: with open("static/json/searching_mates.json") as file: data = json.load(file) game, types = current_user.last_page.split( "/")[2], current_user.last_page.split("/")[3] if current_user.id in data[game][types]: del data[game][types][data[game][types].index(current_user.id)] with open("static/json/searching_mates.json", "w") as file: json.dump(data, file) session = db_session.create_session() current_user.last_page = "/" session.merge(current_user) session.commit()
def add_goods(): form = GoodsForm() if form.validate_on_submit(): session = db_session.create_session() goods = Goods() goods.name = form.name.data goods.content = form.content.data goods.is_fragile = form.is_fragile.data goods.place_come = form.place_come.data goods.place_going = form.place_going.data current_user.goods.append(goods) session.merge(current_user) session.commit() return redirect('/') return render_template('goods.html', title='Добавление товара', form=form)
def add_tasks(): form = TasksForm() if form.validate_on_submit(): session = db_session.create_session() tasks = Tasks() tasks.title = form.title.data tasks.content = form.content.data tasks.reusable = form.reusable.data tasks.cost = form.cost.data tasks.answeroftask = form.answer.data current_user.tasks.append(tasks) session.merge(current_user) session.commit() return redirect('/') return render_template('tasks.html', title='Добавление задачи', form=form)
def product_delete(id): session = db_session.create_session() products = session.query(Products).filter( Products.id == id, Products.user == current_user).first() if products: s = str(current_user.basket).split() if str(id) in s: s.remove(str(id)) current_user.basket = ' '.join(s) session.delete(products) session.merge(current_user) session.commit() else: abort(404) return redirect('/')
def add_Jobs(): form = JobsForm() if form.validate_on_submit(): session = db_session.create_session() jobs = Jobs() jobs.team_leader = form.team_leader.data jobs.job = form.job.data jobs.work_size = form.work_size.data jobs.collaborators = form.collaborators.data jobs.is_finished = form.is_finished.data current_user.jobs.append(jobs) session.merge(current_user) session.commit() return redirect('/') return render_template('news.html', title='Дбавление работы', form=form)
def join(id): if current_user.is_authenticated: session = db_session.create_session() board = session.query(Board.Board).filter(Board.Board.id == id).first() if board: board.workers = board.workers + str(current_user.id) + ',' user = session.query( User.User).filter(User.User.id == current_user.id).first() user.board.insert(0, board) session.merge(user) session.commit() return redirect('/board/' + str(id)) else: abort(404) else: return redirect('/login')
def add_products(): form = ProductsForm() if form.validate_on_submit(): session = db_session.create_session() products = Products() products.title = form.title.data products.price = form.price.data products.description = form.description.data products.user = current_user current_user.products.append(products) session.merge(current_user) session.commit() return redirect('/') return render_template('products.html', title='Добавление товара', form=form)
def add_job(): form = JobsForm() if form.validate_on_submit(): session = db_session.create_session() jobs = Jobs() jobs.job = form.title.data jobs.team_leader = int(form.teamlead_id.data) jobs.work_size = int(form.work_size.data) jobs.collaborators = form.collaborators.data jobs.is_finished = form.is_finished.data current_user.jobs.append(jobs) session.merge(current_user) session.commit() return redirect('/') return render_template('job.html', title='Adding a Job', form=form)
def settings_user(): form = RegisterForm() session = db_session.create_session() form.account_type.render_kw['disabled'] = 'disabled' form.password.render_kw = { **form.about_me.render_kw, 'disabled': 'disabled', 'hidden': 'hidden' } form.password_repeat.render_kw = { **form.about_me.render_kw, 'disabled': 'disabled', 'hidden': 'hidden' } form.photo.render_kw = { **form.about_me.render_kw, 'disabled': 'disabled', 'hidden': 'hidden' } form.account_type.choices = [[ current_user.account_type, session.query(AccountType).get(current_user.account_type).name ]] if form.is_submitted() is False: form.user_name.data = current_user.user_name form.name.data = current_user.name form.surname.data = current_user.surname form.email.data = current_user.email form.birthday.data = str(current_user.birthday).split()[0] form.about_me.data = current_user.about_me if form.is_submitted(): current_user.user_name = form.user_name.data current_user.name = form.name.data current_user.surname = form.surname.data current_user.email = form.email.data current_user.birthday = datetime.datetime.strptime( form.birthday.data, '%Y-%m-%d') current_user.about_me = form.about_me.data session.merge(current_user) session.commit() return render_template('register.html', action_title="Редактирование", form=form)
def buy(): session = db_session.create_session() products = session.query(Products) basket_ = [int(i) for i in current_user.basket.split()] price = 0 for i in basket_: price += float( session.query(Products).filter(Products.id == i).first().price) if current_user.is_vip: price *= 0.8 if price > current_user.money: return redirect('/not_enough') current_user.money -= price current_user.basket = '' session.merge(current_user) session.commit() return render_template("buy.html", products=products, basket=basket_)
def index(): global visits_count form = NewsForm() if form.validate_on_submit(): session = db_session.create_session() news = News() news.title = form.title.data news.content = form.content.data news.count = form.count.data file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save( os.path.join(app.config['UPLOAD_FOLDER'], str(session.query(News)[-1].id + 1)) + filename[-4:]) news.filename = '/static/img/' + str( session.query(News)[-1].id + 1) + str(filename[-4:]) current_user.news.append(news) session.merge(current_user) session.commit() return redirect('/') session = db_session.create_session() news = session.query(News)[::-1] visits_count = int(request.cookies.get("index", 0)) if visits_count: res = make_response( render_template('index.html', news=news, title='Маринчка', form=form)) res.set_cookie("index", str(visits_count + 1), max_age=60 * 60 * 24 * 365 * 2) else: res = make_response( render_template('about_us.html', news=news, title='Маринчка', form=form)) res.set_cookie("index", str(visits_count + 1), max_age=60 * 60 * 24 * 365 * 2) return res
def subscribe(): args = dict(request.args) session = db_session.create_session() user = session.query(User).filter(User.id == args['user_id']).first() cur_user = session.query(User).filter(User.id == current_user.id).first() if user: if user.id != current_user.id: if user.private_account: if user not in cur_user.requested_subscriptions: cur_user.requested_subscriptions.append(user) if cur_user not in user.requested_subscribers: user.requested_subscribers.append(cur_user) else: if user not in current_user.subscriptions: session.merge(current_user).subscriptions.append(user) if current_user not in user.subscribers: user.subscribers.append(session.merge(current_user)) session.commit() return redirect(args['back_url']) else: return redirect('/')
def edit_profile(): check_last_page() form = edit_profile_form.EditProfileForm() message = "If you want to change your password enter your old password" if request.method == "POST": session = db_session.create_session() print(form.old_password.data, form.new_password.data) if form.old_password.data != "" and form.new_password.data != "": if not current_user.check_password(form.old_password.data): message = "Invalid old password" elif form.new_password.data != form.new_password_again: message = "The new password is incorrectly repeated" elif form.old_password.data == form.new_password.data: message = "The new password matches the old one" else: current_user.set_password(form.new_password.data) print(request.files["file"]) if str(request.files["file"] ) != "<FileStorage: '' ('application/octet-stream')>": file = request.files["file"] name = "static/img/avatar_img/avatar_" + \ str(1 + len(os.listdir("static/img/avatar_img"))) + ".jpg" file.save(name) current_user.avatar = "/" + name current_user.favorite_games = form.favorite_games.data if form.name.data != "": current_user.name = form.name.data session.merge(current_user) session.commit() return redirect(f'/profile/{current_user.id}') form.avatar.data = current_user.avatar form.favorite_games.data = current_user.favorite_games form.name.data = current_user.name colors = choice(["primary", "success", "danger", "info"]) return render_template("edit_profile.html", colors=colors, form=form, message=message)
def tasks_asnwer(id): form = AnswerForm() answ = 0 cost = 0 people = 0 istrue = [] session = db_session.create_session() for task in session.query(Tasks).all(): if task.id == id: answ = task.answeroftask cost = task.cost people = task.reusable if type(task.correctUsers).__name__ != 'NoneType': istrue = task.correctUsers.split() break if form.answer.data == answ and people > 0 and str( current_user.id) not in istrue: task = session.query(Tasks).filter(Tasks.id == id).first() if task: minusUser = task.reusable - 1 task.reusable = minusUser if type(task.correctUsers).__name__ != 'NoneType': text = task.correctUsers + str(current_user.id) + ' ' else: text = str(current_user.id) + ' ' task.correctUsers = text session.commit() current_user.answer = form.answer.data current_user.schoolcoins = current_user.schoolcoins + cost current_user.numberoftasks = current_user.numberoftasks + 1 if form.validate_on_submit(): session = db_session.create_session() user = User() user.schoolcoins = current_user.schoolcoins user.numberoftasks = current_user.numberoftasks session.merge(current_user) session.commit() return render_template('answer.html', title='Отправить ответ', form=form)
def new_board(): if current_user.is_authenticated: form = NewBoardForm() if form.validate_on_submit(): session = db_session.create_session() if session.query(Board.Board).filter( Board.Board.title == form.title.data, Board.Board.user == current_user).first(): return render_template( 'new_board.html', message="Доска с таким именем уже существует", form=form) user = session.query( User.User).filter(User.User.id == current_user.id).first() board = Board.Board( title=form.title.data, isPrivate=form.isPrivate.data, user_id=user.id, workers=str(current_user.id) + ',', author=user.name, user=user, # tasks="#," ) # board.workers.append(str(current_user.id)) # TODO: Не забыть добавлять ЗАПЯТУЮ при приглашениях user.board.insert(0, board) session.merge(user) id = board.id # session.add(board) session.commit() return redirect( '/board/' + str(id)) #TODO: Сделать перенаправление на страницу доски return render_template("new_board.html", title="Новая доска", form=form) return redirect('/login')
def add_post(): """ Обработчик страницы создания поста """ form = PostForm() if form.validate_on_submit(): session = db_session.create_session() post = Post() post.title = form.title.data post.content = form.content.data post.string_created_date = str(datetime.datetime.now())[0:16] current_user.posts.append(post) session.merge(current_user) session.commit() if bool(form.attachment.data): session = db_session.create_session() post = session.query(Post).order_by(Post.id.desc()).first() f = form.attachment.data type = f.filename.split('.')[-1] filename = f'post_attachment_{post.id}.{type}' path = os.path.join('static', 'attachments', filename) f.save(path) post.attachment = path session.commit() post_id = session.query(Post).order_by(Post.id.desc()).first().id # Параллельным процессом запускаем рассыку новости всем зарегистрированным пользователям thread.start_new_thread( os.system, (f'python send_news_emails.pyw --id {post_id}', )) return redirect('/blog') return render_template('post.html', title='Новый пост', form=form, css=url_for('static', filename='css/post_style.css'))
def add_post(): form = AddPostForm() session = db_session.create_session() if form.validate_on_submit(): new_post = Post() new_post.author = session.merge(current_user) new_post.title = form.title.data new_post.text = form.text.data now_datetime = datetime.datetime.now() new_post.creating_date = datetime.datetime.now() new_post.creating_date_norm_view = date_to_normal_view(now_datetime) session.add(new_post) session.commit() return redirect(f'/user/{current_user.id}/1') return render_template('add_edit_post_page.html', form=form, add=True)
def money(): session = db_session.create_session() current_user.money += 50 session.merge(current_user) session.commit() return redirect('/account')