def show_performance(performance_id): if 'username' not in session: return redirect('/login') performances_model = PerformancesModel(db.get_connection()) exists = performances_model.exists(performance_id) if not exists[0]: return redirect('/performances') performance = performances_model.get(performance_id) actors_model = ActorsModel(db.get_connection()) res_actors = [] actors = performance[4].split(',') for actor in actors: actor = actor.strip() exists = actors_model.exists(actor) if exists[0]: res_actors.append(exists[1:]) if session['username'] == ADMIN_NAME: admin = True else: admin = False users_model = UsersModel(db.get_connection()) if str(performance_id) in users_model.get_favourites( session['user_id']).split(','): in_favourites = True else: in_favourites = False return render_template('performance.html', title=performance[1], performance=performance, actors=res_actors, admin=admin, in_favourites=in_favourites)
def delete_performance(performance_id): if 'username' not in session: return redirect('/login') elif session['username'] != ADMIN_NAME: return redirect('/performances') performances_model = PerformancesModel(db.get_connection()) exists = performances_model.exists(performance_id) if not exists[0]: return redirect('/performances') performances_model.delete(performance_id) return redirect('/performances')
def show_favourites(): if 'username' not in session: return redirect('/login') performances_model = PerformancesModel(db.get_connection()) users_model = UsersModel(db.get_connection()) res = [] favourites = users_model.get_favourites(session['user_id']) for item in favourites.split(','): exists = performances_model.exists(item) if exists[0]: res.append(exists[1:5]) return render_template('favourites.html', title='Избранное', username=session['username'], favourites=res)
def add_performance(): if 'username' not in session: return redirect('/login') elif session['username'] != ADMIN_NAME: return redirect('/performances') form = PerformanceForm() if form.validate_on_submit(): title = form.title.data genre = form.genre.data time = form.time.data actors = form.actors.data description = form.description.data performances_model = PerformancesModel(db.get_connection()) performances_model.insert(title, genre, time, actors, description) return redirect('/performances') return render_template('performance_form.html', title='Добавление спектакля', form=form)
def add_to_favourites(perf_id): if 'username' not in session: return redirect('/login') user_id = session['user_id'] performances_model = PerformancesModel(db.get_connection()) exists = performances_model.exists(perf_id)[0] if not exists: return redirect('/performances') users_model = UsersModel(db.get_connection()) if str(perf_id) in users_model.get_favourites( session['user_id']).split(','): return redirect('/performances') favourites = users_model.get_favourites(user_id) if len(favourites) > 0: favourites += ',{}'.format(perf_id) else: favourites += str(perf_id) users_model.edit_favourites(user_id, favourites) return redirect('/favourites')
def edit_performance(performance_id): if 'username' not in session: return redirect('/login') elif session['username'] != ADMIN_NAME: return redirect('/performances') form = PerformanceForm() performances_model = PerformancesModel(db.get_connection()) if form.validate_on_submit(): title = form.title.data genre = form.genre.data time = form.time.data actors = form.actors.data description = form.description.data performances_model.edit(performance_id, title, genre, time, actors, description) return redirect('/performances') exists = performances_model.exists(performance_id) if not exists[0]: return redirect('/performances') performance = performances_model.get(performance_id) form.title.process_data(performance[1]) form.genre.process_data(performance[2]) form.time.process_data( datetime.datetime.strptime(performance[3], '%d.%m.%Y %H:%M')) form.actors.process_data(performance[4]) form.description.process_data(performance[5]) return render_template('performance_form.html', title='Изменение данных спектакля', form=form)
def show_performances(): if 'username' not in session: return redirect('/login') performances_model = PerformancesModel(db.get_connection()) performances = performances_model.get_all() if session['username'] == ADMIN_NAME: admin = True else: admin = False res = {} users_model = UsersModel(db.get_connection()) for item in performances: if str(item[0]) in users_model.get(session['user_id'])[3].split(','): res[item[0]] = True else: res[item[0]] = False return render_template('performances.html', username=session['username'], title='Спектакли', performances=performances, admin=admin, in_favourites=res)
title='Избранное', username=session['username'], favourites=res) @app.route('/favourites/<int:perf_id>/delete') def delete_from_favourites(perf_id): if 'username' not in session: return redirect('/login') user_id = session['user_id'] users_model = UsersModel(db.get_connection()) favourites = users_model.get_favourites(user_id).split(',') if str(perf_id) not in favourites: return redirect('/performances') del favourites[favourites.index(str(perf_id))] users_model.edit_favourites(user_id, ','.join(favourites)) return redirect('/performances') if __name__ == '__main__': db = DataBase() ActorsModel(db.get_connection()).init_table() PerformancesModel(db.get_connection()).init_table() um = UsersModel(db.get_connection()) um.init_table() if not um.exists(ADMIN_NAME)[0]: um.insert(ADMIN_NAME, ADMIN_PASSWORD_HASH) app.run(port=8080, host='127.0.0.1')
from db import DataBase, ActorsModel, PerformancesModel, UsersModel db = DataBase() actors_model = ActorsModel(db.get_connection()) actors_model.init_table() performances_model = PerformancesModel(db.get_connection()) performances_model.init_table() users_model = UsersModel(db.get_connection()) users_model.init_table() performances_model.insert('Щелкунчик', 'Балет', '2019-04-26 16:00', '1', 'Описание первого спектакля') performances_model.insert('Евгений Онегин', 'Опера', '2019-04-26 20:00', '2,3', 'Описание второго спектакля') performances_model.insert('Травиата', 'Опера', '2019-04-27 16:00', '2,3', 'Описание третьего спектакля') actors_model.insert('Ольга', 'Иванова', 'Балерина', 'Биография первого актёра') actors_model.insert('Иван', 'Петров', 'Певец', 'Биография второго актёра') actors_model.insert('Дарья', 'Сидорова', 'Певица', 'Биография третьего актёра')