def delete_news(news_id): if 'username' not in session: return redirect('/login') nm = NewsModel(db.get_connection()) print(news_id, 'jj') nm.delete(news_id) return redirect("/admin")
def sort_by(sort): if 'username' not in session: return redirect('/login') nm = NewsModel(db.get_connection()) news = nm.sort(sort, user_id=session['user_id']) return render_template('index.html', username=session['username'], news=news)
def admin_only(): if 'username' not in session: return redirect('/login') news = NewsModel(news_db.get_connection()).get_all() news.sort(key=lambda x: x[3]) return render_template('index.html', username=session['username'], news=news)
def delete_news(news_id): if 'username' not in session: return redirect('/ban') nm = NewsModel(news_db.get_connection()) nm.delete(news_id) cm = CommentsModel(comments_db.get_connection()) cm.news_delete(news_id) return redirect("/index")
def create_news(): if not request.json: return jsonify({'error': 'Empty request'}) elif not all(key in request.json for key in ['title', 'content', 'user_id']): return jsonify({'error': 'Bad request'}) news = NewsModel(db.get_connection()) news.insert(request.json['title'], request.json['content'], request.json['user_id']) return jsonify({'success': 'OK'})
def background_thread(): count = 0 news_model = NewsModel() while True: time.sleep(300) count += 1 news = news_model.get_rand_news() sock.emit( 'server_response', {'owner': news_bot_name, "text": news, 'count': count}, namespace='/c', room="news" )
def add_news(): if 'username' not in session: return redirect('/login') form = AddNewsForm() if form.validate_on_submit(): title = form.title.data content = form.content.data nm = NewsModel(db.get_connection()) nm.insert(title, content, session['user_id']) return redirect("/index") return render_template('add_news.html', title='Добавление новости', form=form, username=session['username'])
def admin_only(): if 'username' not in session: return redirect('/login') news = NewsModel(news_db.get_connection()).get_all() news.sort(key=lambda x: x[3]) user_model = UsersModel(users_db.get_connection()) usernames = {} for item in news: data = user_model.get(item[4]) if item[4] not in usernames: usernames.update( {item[4]: [str(data[2] + ' ' + data[3]), data[1]]}) return render_template('index.html', username=session['username'], news=news, users=usernames)
def admin(): if 'username' not in session: return redirect('/login') products = NewsModel(db.get_connection()).get_all(session['user_id']) return render_template('index.html', username=session['username'], prods=products)
def index(): if 'username' not in session: return redirect('/login') news = NewsModel(db.get_connection()).get_all(session['user_id']) return render_template('index.html', username=session['username'], news=news)
def add_news(): if 'username' not in session: return redirect('/login') form = AddNewsForm() if form.validate_on_submit(): title = form.title.data content = form.content.data date = datetime.datetime.now() nm = NewsModel(news_db.get_connection()) nm.insert(title, content, str(date.day) + '.' + str(date.month) + '.' + str(date.year), session['user_id']) return redirect("/index") return render_template('add_news.html', title='Добавить новость', cur_user_id=session['user_id'], form=form, username=session['username'])
def admin(): if session['username'] != 'admin': return redirect('/login') else: c = [] users = UsersModel(db.get_connection()).get_all() for user in users: news = NewsModel(db.get_connection()).get_all(user[0]) c.append([user[1], len(news)]) return render_template('admin.html', username=session['username'], news=c)
def add_task(): if 'username' not in session: return redirect('/ban') if session['block'] == 'blocked': return redirect('/ban') form = AddNewsForm() print('!!!!!!!!!!!!') if form.validate_on_submit(): title = form.title.data content = form.content.data date = form.date.data nm = NewsModel(news_db.get_connection()) nm.insert(title, content, date, session['user_id']) print('!!!!!!!!!!!!') return redirect("/index") print('!!!!!!!!!!!!') return render_template('add_news.html', title='Добавление задачи', cur_user_id=session['user_id'], form=form, username=session['username'])
def news_edit(news_id): if 'username' not in session: return redirect('/ban') if request.method == 'GET': news = NewsModel(news_db.get_connection()).get(news_id) return render_template('edit_news.html', cur_user_id=session['user_id'], title=news[1], content=news[2], date=news[3]) elif request.method == 'POST': news = NewsModel(news_db.get_connection()) news.update(request.form['title'], request.form['content'], request.form['date'], news_id) return redirect('/index')
def user_data(user_id): if 'username' not in session: return redirect('/ban') user_model = UsersModel(users_db.get_connection()) data = user_model.get(user_id) news = NewsModel(news_db.get_connection()).get_all(user_id) path = data[6] if session['user_id'] == user_id: my_page = True else: my_page = False name_surname = data[2] + ' ' + data[3] return render_template('user_data.html', title=name_surname, my_page=my_page, cur_user_id=session['user_id'], path=path, name=data[2], surname=data[3], status=data[4], news=news)
def news_data(news_id): if 'username' not in session: return redirect('/ban') form = CommentForm() if form.validate_on_submit(): content = form.content.data cm = CommentsModel(comments_db.get_connection()) author_data = UsersModel(users_db.get_connection()).get( session['user_id']) username = author_data[2] + ' ' + author_data[3] cm.insert(session['user_id'], news_id, content, username) return redirect("/news/" + str(news_id)) news = NewsModel(news_db.get_connection()).get(news_id) author_data = UsersModel(users_db.get_connection()).get(news[4]) username = author_data[2] + ' ' + author_data[3] cm = CommentsModel(comments_db.get_connection()).get_all(news[0]) cm.reverse() return render_template('news_data.html', title=news[1], author=username, cur_user_id=session['user_id'], news=news, cm=cm, form=form)
def index(): if 'username' not in session: return redirect('/ban') news = NewsModel(news_db.get_connection()).get_all(session['user_id']) news.sort(key=lambda x: x[3]) news.reverse() user_model = UsersModel(users_db.get_connection()) usernames = {} for item in news: data = user_model.get(item[4]) if item[4] not in usernames: usernames.update( {item[4]: [str(data[2] + ' ' + data[3]), data[1]]}) return render_template('index.html', title='a', username=session['username'], cur_user_id=session['user_id'], news=news, users=usernames)
from db import DB from flask import Flask, jsonify, make_response, request from newsmodel import NewsModel app = Flask(__name__) app.config['SECRET_KEY'] = 'yandexlyceum_secret_key' db = DB() NewsModel(db.get_connection()).init_table() # Получение списка всех новостей @app.route('/news', methods=['GET']) def get_news(): news = NewsModel(db.get_connection()).get_all() return jsonify({'news': news}) # Получение одной новости по id @app.route('/news/<int:news_id>', methods=['GET']) def get_one_news(news_id): news = NewsModel(db.get_connection()).get(news_id) if not news: return jsonify({'error': 'Not found'}) return jsonify({'news': news}) # Добавление новости @app.route('/news', methods=['POST']) def create_news(): if not request.json: return jsonify({'error': 'Empty request'})
def get(self, news_id): abort_if_news_not_found(news_id) news = NewsModel(db.get_connection()).get(news_id) return jsonify({'news': news})
def delete_news(news_id): if 'username' not in session: return redirect('/login') nm = NewsModel(db.get_connection()) nm.delete(news_id) return redirect("/index")
from usersmodel import UsersModel from newsmodel import NewsModel from commentsmodel import CommentsModel from editform import EditForm from shutil import copy from db import DB import datetime app = Flask(__name__) app.config['SECRET_KEY'] = 'yandexlyceum_secret_key' users_db = DB('users.db') news_db = DB('news.db') comments_db = DB('comments.db') users_init = UsersModel(users_db.get_connection()) users_init.init_table() news_init = NewsModel(news_db.get_connection()) news_init.init_table() comments_init = CommentsModel(comments_db.get_connection()) comments_init.init_table() @app.route('/') @app.route('/index') def index(): if 'username' not in session: return redirect('/ban') news = NewsModel(news_db.get_connection()).get_all(session['user_id']) news.sort(key=lambda x: x[3]) news.reverse() user_model = UsersModel(users_db.get_connection()) usernames = {}
def abort_if_news_not_found(news_id): if not NewsModel(db.get_connection()).get(news_id): abort(404, message="News {} not found".format(news_id))
def post(self): args = parser.parse_args() news = NewsModel(db.get_connection()) news.insert(args['title'], args['content'], args['user_id']) return jsonify({'success': 'OK'})
def get_news(): news = NewsModel(db.get_connection()).get_all() return jsonify({'news': news})
def get_one_news(news_id): news = NewsModel(db.get_connection()).get(news_id) if not news: return jsonify({'error': 'Not found'}) return jsonify({'news': news})
from flask import Flask, request, render_template, redirect from db import DB from newsmodel import NewsModel from usermodel import UsersModel from add_news import AddNewsForm, LoginForm import sqlite3 from werkzeug.security import generate_password_hash, check_password_hash app = Flask(__name__) session = {} db = DB() con = db.get_connection() print(con) nws = NewsModel(con) nws.init_table() print(nws.get_all()) cursor = nws.connection.cursor() cursor.execute("SELECT * FROM news ") rows = cursor.fetchall() print(rows) app.config.update( dict(SECRET_KEY="powerful secretkey", WTF_CSRF_SECRET_KEY="a csrf secret key")) @app.route('/login', methods=['GET', 'POST']) def login(): form = LoginForm() if request.method == 'POST':
def delete_news(news_id): news = NewsModel(db.get_connection()) if not news.get(news_id): return jsonify({'error': 'Not found'}) news.delete(news_id) return jsonify({'success': 'OK'})
def delete(self, news_id): abort_if_news_not_found(news_id) NewsModel(db.get_connection()).delete(news_id) return jsonify({'success': 'OK'})