def delete_new(id_user, id_new): ses = db_session.create_sessin() news = ses.query(News).filter( News.id == id_new, News.user_id == id_user).first() ses.delete(news) ses.commit() return redirect(f'/user/{id_user}')
def find_news(): ses = db_session.create_sessin() if request.method == 'GET': news = ses.query(News).all() return render_template('find_news.html', title='Find news', news=news, ses=ses, User=User, is_news=True) elif request.method == 'POST': req = request.form['req'] is_news = request.form.get('flexRadioDefault') == 'news' if is_news: news = ses.query(News).filter(News.title.like(f'%{req}%')) else: news = ses.query(User).filter(User.nickname.like(f'%{req}%')) k = 0 for _ in news: k += 1 if k == 1: break if k: return render_template('find_news.html', news=news, value=req, ses=ses, User=User, is_news=is_news) else: return render_template('find_news.html', message='nothing is found', value=req)
def delete_user(id): if id == 1: id = int(request.referrer.split('/')[-1]) ses = db_session.create_sessin() user = ses.query(User).get(id) ses.delete(user) ses.commit() return redirect('/main')
def news_feed(id): ses = db_session.create_sessin() user = ses.query(User).get(id) subs = [int(i) for i in user.subs.split(',')] news = ses.query(News).filter(News.user_id.in_(subs))[::-1] return render_template('news_feed.html', title='Feed', news=news, user=user.nickname, id=id, ses=ses, User=User)
def get_new(id): ses = db_session.create_sessin() new = ses.query(News).get(id) new.views += 1 ses.commit() user = ses.query(User).get(new.user_id) address_image = url_for('static', filename=f'img/{new.id}.jpg') address_author = f'/user/{new.user_id}' return render_template('get_new.html', title=new.title, form=new, author=user.nickname, address_image=address_image, address_author=address_author)
def get_user(id): ses = db_session.create_sessin() user = ses.query(User).get(id) if request.method == 'POST': cur_user = ses.query(User).get(current_user.id) if str(id) in current_user.subs: subs = current_user.subs.split(',') del subs[subs.index(str(id))] cur_user.subs = ','.join(subs) else: if cur_user.subs == '': cur_user.subs = str(id) else: cur_user.subs += ',' + str(id) ses.commit() return redirect(f'/user/{id}') elif request.method == 'GET': news = user.news is_user = str(id) in current_user.subs return render_template('get_user.html', news=news, user=user.nickname, title=user.nickname, id=id, is_user=is_user)
def log_up(): user = User() reg_form = New_User() if reg_form.validate_on_submit(): ses = db_session.create_sessin() params = dict() if reg_form.password.data != reg_form.password_again.data: params['message'] = 'passwords do not equal' if ses.query(User).filter( User.nickname == reg_form.nickname.data).first(): params['message'] = 'This nickname is already exist' if params: params['title'] = 'Log up' params['form'] = reg_form return render_template('log_up.html', **params) user.nickname = reg_form.nickname.data user.hashed_password = reg_form.password.data user.generate_password() ses.add(user) ses.commit() login_user(user, remember=reg_form.remember_me.data) return redirect('/') return render_template('log_up.html', title='Log up', form=reg_form)
def log_in(): in_form = LoginForm() if in_form.validate_on_submit(): ses = db_session.create_sessin() user = ses.query(User).filter( User.nickname == in_form.nickname.data).first() if not user: return render_template( 'log_in.html', message='Such nickname is not exist', form=in_form, title='Log in', ) if not user.check_password(in_form.password.data): return render_template( 'log_in.html', message='Password is not right', form=in_form, title='Log in', ) login_user(user, remember=in_form.remember_me.data) return redirect('/main') return render_template('log_in.html', title='Log in', form=in_form)
def add_new(): new_form = New_New() new = News() if new_form.validate_on_submit(): ses = db_session.create_sessin() if ses.query(News).filter(News.user == current_user, News.title == new_form.title.data).first(): return render_template('add_new.html', title='Add new', message='You already have such gossip', form=new_form) new.title = new_form.title.data new.content = new_form.content.data new.user_id = current_user.id ses.add(new) ses.commit() id = ses.query(News).filter(News.user_id == current_user.id, News.title == new.title).first() if new_form.file.data: new_form.file.data.save( url_for('static', filename=f'img/{id.id}.jpg')[1:]) return redirect(f'user/{current_user.id}') return render_template('add_new.html', title='Add new', form=new_form)
def loader_user(user_id): ses = db_session.create_sessin() return ses.query(User).get(user_id)