Ejemplo n.º 1
0
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")
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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")
Ejemplo n.º 5
0
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'})
Ejemplo n.º 6
0
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"
        )
Ejemplo n.º 7
0
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'])
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
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'])
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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'])
Ejemplo n.º 14
0
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')
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
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)
Ejemplo n.º 17
0
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)
Ejemplo n.º 18
0
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'})
Ejemplo n.º 19
0
 def get(self, news_id):
     abort_if_news_not_found(news_id)
     news = NewsModel(db.get_connection()).get(news_id)
     return jsonify({'news': news})
Ejemplo n.º 20
0
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")
Ejemplo n.º 21
0
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 = {}
Ejemplo n.º 22
0
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))
Ejemplo n.º 23
0
 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'})
Ejemplo n.º 24
0
def get_news():
    news = NewsModel(db.get_connection()).get_all()
    return jsonify({'news': news})
Ejemplo n.º 25
0
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})
Ejemplo n.º 26
0
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':
Ejemplo n.º 27
0
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'})
Ejemplo n.º 28
0
 def delete(self, news_id):
     abort_if_news_not_found(news_id)
     NewsModel(db.get_connection()).delete(news_id)
     return jsonify({'success': 'OK'})