def edit(id): is_new = False form = EditForm() session = sessions.create_session() track = session.query(Track).filter(Track.id == id).first() if not track: track, is_new = Track(), True if form.validate_on_submit(): track.name = form.name.data.lower() if is_new: track.artist_id = current_user.id session.add(track) session.commit() with open(TEXTS_PATH + str(track.id) + '.txt', 'w') as file: text = str(form.text.data).split() file.write('\n'.join( list(' '.join(text[i:i + WORDS_IN_LINE]) for i in range(0, len(text), WORDS_IN_LINE)))) return redirect('/home') try: form.name.data = track.name form.text.data = ' '.join( open(TEXTS_PATH + str(track.id) + '.txt', 'r').readlines()) except Exception: form.text.data, form.name.data = '', '' return render_template('edit.html', form=form, id=id)
def delete(self, id): abort_if_track_not_found(id) session = sessions.create_session() track = session.query(Track).get(id) session.delete(track) session.commit() return json.dumps({'success': 'OK'})
def lyric(id): session = sessions.create_session() track = session.query(Track).filter(Track.id == id).first() artist = get_artist_by_track(track, session) text = open(TEXTS_PATH + str(id) + '.txt', 'r').readlines() return render_template('lyric.html', name=track.name, artist=artist, text=text)
def get(self, id): abort_if_track_not_found(id) session = sessions.create_session() track = session.query(Track).get(id) text = open(TEXTS_PATH + str(id) + '.txt', 'r').readlines() return json.dumps({ 'track': track.to_dict(), 'text': [text[i].encode('utf-8') for i in range(len(text))] })
def home(): session = sessions.create_session() tracks = session.query(Track).filter(Track.artist_id == current_user.id) texts = dict() for track in tracks: try: texts[track] = ''.join( open(TEXTS_PATH + str(track.id) + '.txt', 'r').readlines()[:TEXT_SHORTVIEW]) except Exception as e: texts[track] = ERROR_MESSAGE + str(e) return render_template('home.html', tracks=tracks, texts=texts)
def login(): if current_user.is_authenticated: return redirect('/home') form = LoginForm() message = 0 if form.validate_on_submit(): user = sessions.create_session().query(User).filter( User.email == form.login.data).first() if user and user.check_password(form.password.data): login_user(user) return redirect('/home') else: message = REGISTER_FAILED return render_template('login.html', form=form, message=message)
def start_page(): form = SearchForm() result = [] artists = dict() if form.validate_on_submit(): request = form.query.data.lower() session = sessions.create_session() result = session.query(Track).filter(Track.name.like(f'%{request}%')) artists = { track: get_artist_by_track(track, session) for track in result } return render_template('start.html', form=form, results=result, artists=artists)
def signin(): if current_user.is_authenticated: return redirect('/home') form = SignForm() message = 0 if form.validate_on_submit(): sesssion = sessions.create_session() user = User() if form.password.data == form.check_password.data and not sesssion.query( User).filter(User.email == form.login.data).first(): user.name, user.email = form.name.data, form.login.data user.create_password(form.password.data) sesssion.add(user) sesssion.commit() login_user(user) return redirect('/home') else: message = REGISTER_FAILED return render_template('signin.html', form=form, message=message)
def load_user(user_id): db_sess = sessions.create_session() return db_sess.query(User).get(user_id)
def get(self): session = sessions.create_session() tracks = session.query(Track).all() print(tracks) return json.dumps({'tracks': [track.to_dict() for track in tracks]})
def main(): sessions.global_init(DB_PATH) session = sessions.create_session() app.run(ADRESS, PORT)
def delete(id): session = sessions.create_session() track = session.query(Track).filter(Track.id == id).first() session.delete(track) session.commit() return redirect('/home')
def abort_if_track_not_found(track_id): session = sessions.create_session() tracks = session.query(Track).get(track_id) if not tracks: abort(404, message=f"Track {track_id} not found")