Beispiel #1
0
def delete_news(news_id):
    if 'username' not in session:
        return redirect(
            '/login'
        )  # if user is not logged in, user will get redirected to /login
    nm = NewsModel(db.get_connection())
    nm.delete(news_id)
    return redirect("/index")
Beispiel #2
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'])
Beispiel #3
0
def stats():
    um = UsersModel(db.get_connection())
    users_list = um.get_all()
    nm = NewsModel(db.get_connection())
    news_list = nm.get_all()
    stats_list = []
    for item in users_list:
        hobosti = list(
            filter(lambda x: x == item[0], map(lambda x: x[3], news_list)))
        stats_list.append([item[1], len(hobosti)])
    return render_template('stats.html', stats_list=stats_list)
Beispiel #4
0
def add_news():
    form = AddNewsForm()
    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        nm = NewsModel(db.get_connection())
        nm.insert(title, content)
        return redirect("/news_list")
    return render_template('add_news.html',
                           username=session['username'],
                           form=form)
Beispiel #5
0
def subscriptions():
    subscribes = SubscribeModel(db.get_connection())
    user_subscriptions = subscribes.get_all(session['user_id'])
    user_model = UsersModel(db.get_connection())
    news = NewsModel(db.get_connection())
    for_post = []
    for _, user in user_subscriptions:
        for_post.extend([
            list(i) + [user_model.get(user)[1]]
            for i in reversed(news.get_all(user))
        ])
    return render_template('subscriptions.html', news=for_post)
Beispiel #6
0
def index():
    nm = NewsModel(db.get_connection())
    news = nm.get_all()
    if len(news) >= 3:
        new = [news[-1], news[-2], news[-3]]
    else:
        new = news
    if 'username' not in session:
        return render_template('index.html', text="Авторизироваться", news=new)
    return render_template('index.html',
                           text='Личный кабинет',
                           news=new,
                           username=session['username'])
Beispiel #7
0
def add_news(book):
    if book == "none":
        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'],
                               admins=admins,
                               book="")
    else:
        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(book, title, content, session['user_id'])
            return redirect("/index")
        return render_template('add_news.html',
                               title='Добавление заметки',
                               form=form,
                               username=session['username'],
                               admins=admins,
                               book=book)
Beispiel #8
0
def add_news():
    if 'username' not in session:  # if you try to get to /add_news without logging in you will be redirected to login page
        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']
                           )  # get all news if something/nothing changed
Beispiel #9
0
def my_page():
    if 'username' not in session:
        return redirect('/login')
    if request.method == 'GET':
        nm = NewsModel(db.get_connection())
        nm.init_table()
        um = UsersModel(db.get_connection())
        um.init_table()
        em = um.get_email(session['username'])
        uname = session['username']
        image = um.get_avatar(uname)
        return render_template('account.html',
                               username=uname,
                               news=nm.get_all(uname),
                               email=em,
                               own="True",
                               image=image)
Beispiel #10
0
def index():
    if 'username' not in session:
        usname = None
    else:
        usname = session['username']

    news = NewsModel(db.get_connection()).get_all()
    return render_template('index.html', username=usname, news=news)
Beispiel #11
0
def index():
    if 'username' not in session:  # if you are not loginned you should login
        return redirect('/login')
    form = PostForm()
    if form.validate_on_submit():  # if news_title is not empty
        title = form.title.data
        content = form.content.data
        nm = NewsModel(db.get_connection())
        nm.insert(title, content, session['user_id'])
        return redirect("/index")
    news = NewsModel(db.get_connection()).get_all(
        session['user_id'])  # add note to news db
    return render_template(
        'index.html',
        username=session['username'],
        # update page because of the new note added
        news=reversed(news),
        form=form)
Beispiel #12
0
def index():
    if request.method == 'GET':
        form = cgi.FieldStorage()
        if 'username' not in session or not flag_perm and not session.permanent:
            if "username" in session:
                return redirect("/logout")
            return redirect('/login')
        news = NewsModel(db.get_connection()).get_all(session['user_id'])
        user_model = UsersModel(db.get_connection())
        all_news = NewsModel(db.get_connection()).get_all()
        all_users = user_model.get_all()
        if session['username'] in admins:
            return render_template('index.html',
                                   news=reversed(all_news),
                                   admins=admins,
                                   username=session['username'],
                                   all_users=all_users,
                                   adm_n=news)
        return render_template('index.html',
                               username=session['username'],
                               news=reversed(news),
                               admins=admins)
Beispiel #13
0
def get_user_page(user_id):
    users = UsersModel(db.get_connection())
    if users.exists_only_by_id(user_id):
        name = users.get(user_id)[1]
        subscribes = SubscribeModel(db.get_connection())
        news = NewsModel(db.get_connection()).get_all(user_id)
        return render_template(
            'view_page.html',
            news=reversed(news),
            current_user_id=user_id,
            user_name=name,
            user_photo="/static/avas/" + str(user_id) + ".jpg",
            subscribed=subscribes.check_subscription(
                session['user_id'],
                user_id))  # return user template if user exists
    return "Sorry. User not found."  # if you go on /user/something_not_in_db you will see page with text "Sorry. User not found."
Beispiel #14
0
def main():
    if 'username' not in session:
        return redirect('/login')
    nm = NewsModel(db.get_connection())
    nm.init_table()
    um = UsersModel(db.get_connection())
    um.init_table()
    # nm.delete_all()
    if request.method == "POST":
        content = request.form["comment"]
        # content = request.files["uploadingfiles"]
        avatar = um.get_avatar(session['username'])
        print(avatar)
        nm.insert(str(time.asctime(time.localtime(time.time()))), content,
                  session['username'], avatar)
        for i in nm.get_all():
            check_if_avatar_exists(i)
        return redirect("/main")
    else:
        print(nm.get_all())
        return render_template('home.html',
                               title='Добавление новости',
                               username=session['username'],
                               news=nm.get_all())
Beispiel #15
0
def show_user(uname):
    if 'username' not in session:
        return redirect('/login')
    nm = NewsModel(db.get_connection())
    nm.init_table()
    um = UsersModel(db.get_connection())
    um.init_table()
    em = um.get_email(session['username'])
    image = um.get_avatar(uname)
    if uname == session['username']:
        owning = 'True'
    else:
        owning = 'False'
    if request.method == "GET":
        print(nm.get_all(uname))
        return render_template('account.html',
                               username=uname,
                               news=nm.get_all(uname),
                               email=em,
                               own=owning,
                               image=image)
Beispiel #16
0
from db import DB
from user_model import UserModel
from news_model import NewsModel
from items_model import ItemsModel

db = DB()
users_model = UserModel(db.get_connection())
users_model.init_table()
users_model.insert("test1", "11111111", "*****@*****.**", "admin")
users_model.insert("admin", "password11", "*****@*****.**", "admin")
news_model = NewsModel(db.get_connection())
news_model.init_table()
items_model = ItemsModel(db.get_connection())
items_model.init_table()
items_model.insert("Defaul candy", "www.yandex.ru", "30",
                   "Classic candy with honey and cream.", "25")
Beispiel #17
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})
Beispiel #18
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)
Beispiel #19
0
 def delete(self, news_id):
     abort_if_news_not_found(news_id)
     NewsModel(db.get_connection()).delete(news_id)
     return jsonify({'success': 'OK'})
Beispiel #20
0
 def get(self):
     news = NewsModel(db.get_connection()).get_all()
     return jsonify({'news': news})
Beispiel #21
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'})
Beispiel #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))
Beispiel #23
0
def index():
    '''if 'username' not in session:
        username="******"'''
    news = NewsModel(db.get_connection()).get_all()
    return render_template('index.html', news=news)  # username=session['username']
Beispiel #24
0
def news_list():
    nm = NewsModel(db.get_connection())
    news = nm.get_all()
    return render_template('news_list.html',
                           username=session['username'],
                           news=news)
Beispiel #25
0
from flask import Flask, redirect, render_template, session, request
from login_form import LoginForm
from news_model import NewsModel
from users_model import UsersModel
from register_form import RegisterModel
from book_model import BookModel
from books_content import content
from books_form import BooksForm

import json
import cgi

app = Flask(__name__)
app.config['SECRET_KEY'] = 'yandexlyceum_secret_key'
db = DB()
NewsModel(db.get_connection()).init_table()
UsersModel(db.get_connection()).init_table()

flag_perm = False

admins = json.loads(open('static/admins.txt', 'r', encoding='utf-8').read())

image = []
# print(admins)
# user_model_2 = UsersModel(db.get_connection())
# for i in admins:
#     if user_model_2.is_username_busy(i):
#         print(i)
#         user_model_2.insert(i, admins[i])

# один пользователь: test - username; qwerty123 - password
Beispiel #26
0
import argparse
from news_model import NewsHTMLParser, NewsModel


class StubRandomWriter:
    def __init__(self):
        self.lst = []

    def train(self, data):
        self.lst.append(data)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('url', type=str)
    parser.add_argument('headline_class', type=str)
    args = parser.parse_args()
    rw = StubRandomWriter()
    model = NewsModel()
    model.update_models([rw], 'http://' + args.url,
                        NewsHTMLParser.identify_headline_class(
                            args.headline_class))
    print(args.url, '=', args.headline_class)
    print(rw.lst)
    print(len(rw.lst))
Beispiel #27
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")
Beispiel #28
0
def delete_news(news_id):
    nm = NewsModel(db.get_connection())
    nm.delete(news_id)
    return redirect("/news_list")
Beispiel #29
0
    if (not tweet or tweet[-2] in '.!?') and not (text[0].isupper() or text[0].isdigit()):
        text = ""
    return text

if __name__ == '__main__':
    config = config_reader.read_configs()
    models = config['MODEL_WEIGHTS']
    distribution = [model for model, count in models.items() for i in range(int(count))]
    default_model = random.choice(distribution)

    parser = argparse.ArgumentParser(description="Tweet random news")
    parser.add_argument('--notweet', action='store_true', default=False)
    parser.add_argument('--model', default=default_model, choices=models.keys(), dest='model_name')
    args = parser.parse_args()
    print("Using", args.model_name)
    model = NewsModel()
    rw = model.get_news_model(args.model_name)
    if not rw.trained:
        model.update_news_models(args.model_name)
    tweet = ""
    tweet_len = 50
    if args.model_name in config['CHARACTER_MODELS']:
        tweet_len = random.randint(-12, 12) + 46
    elif args.model_name in config['WORD_MODELS']:
        tweet_len = random.randint(-20, 20) + 60

    while can_add_to_tweet(tweet, tweet_len):
        text = ""
        for ch in rw.generate_tokens():
            text += ch
            if args.model_name in config['CHARACTER_MODELS']:
Beispiel #30
0
import argparse
from news_model import NewsHTMLParser, NewsModel

class StubRandomWriter:
    def __init__(self):
        self.lst = []


    def train(self, data):
        self.lst.append(data)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('url', type=str)
    parser.add_argument('headline_class', type=str)
    args = parser.parse_args()
    rw = StubRandomWriter()
    model = NewsModel()
    model.update_models([rw], 'http://' + args.url, NewsHTMLParser.identify_headline_class(args.headline_class))
    print(args.url, '=', args.headline_class)
    print(rw.lst)
    print(len(rw.lst))