Exemple #1
0
def register(par):
    if par == "user":
        if current_user.is_authenticated:
            return flask.redirect("/feed")
        form = RegistrationForm()
        if form.validate_on_submit():
            print(form.username.data)
            print(form.email.data)
            print(form.password.data)
            user = User()
            user.nickname = form.username.data
            user.email = form.email.data
            user.hashed_password = generate_password_hash(form.password.data)
            #if allowed_file(form.ava.file.filename):
            #    file_name = secure_filename(form.ava.file.filename)
            #    form.ava.file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))
            #    user.avatar = os.path.join(app.config['UPLOAD_FOLDER'], file_name)
            session = create_session()
            session.add(user)
            print(type(form.username.data), type(form.email.data),
                  type(generate_password_hash(form.password.data)))
            session.commit()

            return flask.redirect("/login/user")
        return flask.render_template('registration_creator.html',
                                     action=False,
                                     title='Register',
                                     form=form)
    elif par == "author":
        form = RegistrationForm()
        if form.validate():
            author = User()
            author.nickname = form.username.data
            author.email = form.email.data
            author.hashed_password = generate_password_hash(form.password.data)
            author.utype = True
            #if allowed_file(form.ava.file.filename):
            #    file_name = secure_filename(form.ava.file.filename)
            #    form.ava.file.save(os.path.join(app.config['UPLOAD_FOLDER'], file_name))
            #    author.avatar = os.path.join(app.config['UPLOAD_FOLDER'], file_name)
            session = create_session()
            session.add(author)
            session.commit()

            return flask.redirect("/login/author")
        else:
            print(form.errors)
        return flask.render_template('registration_creator.html',
                                     action=True,
                                     form=form)
Exemple #2
0
def post():
    if current_user.is_authenticated:
        if flask.request.method == 'GET':
            return flask.render_template('create_post.html')
        else:
            text = flask.request.form.get('editordata')
            post_name = flask.request.form.get('post-name')
            checkbox1 = flask.request.form.get('radio1')
            checkbox2 = flask.request.form.get('radio2')
            checkbox3 = flask.request.form.get('radio3')
            if checkbox1:
                color = "/static/img/white.jpg"
            elif checkbox2:
                color = "/static/img/red.jpg"
            elif checkbox3:
                color = "/static/img/green.jpg"
            else:
                color = "/static/img/white.jpg"
            post = Story()
            post.content = text
            post.head = post_name
            post.author_id = current_user.id
            post.cover = color
            session = create_session()
            session.add(post)
            session.commit()
            return flask.redirect("/dashboard")
    else:
        return flask.redirect('/')
Exemple #3
0
def card(aid):
    session = create_session()
    author = session.query(User).filter(User.id == aid).first()
    user = session.query(User).filter(User.id == current_user.id).first()
    if flask.request.method == 'GET':
        if not current_user.is_authenticated:
            return flask.redirect("/")
        subscribed = author in user.followed
        return flask.render_template("card.html",
                                     name=author.nickname,
                                     fc=author.followers,
                                     sub=subscribed)
    else:
        req = flask.request.form
        if req.get('subscribe-button'):
            subscribed = author in user.followed
            req = flask.request.form
            if req.get('subscribe-button'):

                if author in user.followed:
                    user.followed.remove(author)
                    session.commit()
                    author.followers -= 1
                    session.commit()
                else:
                    user.followed.append(author)
                    session.commit()
                    author.followers += 1
                    session.commit()
        return flask.redirect('/feed')

        return flask.render_template("card.html",
                                     name=author.nickname,
                                     fc=author.followers,
                                     sub=subscribed)
Exemple #4
0
def dashboard():
    session = create_session()
    author = session.query(User).filter(User.id == current_user.id).first()
    stories = author.stories
    followers_count = author.followers
    return flask.render_template("dashboard.html",
                                 stories=stories,
                                 followers=followers_count)
Exemple #5
0
def login(par):
    if par == "user":
        res = flask.make_response("Setting a cookie")
        res.set_cookie('utype', 'user', max_age=60 * 60 * 24 * 365 * 2)
        if current_user.is_authenticated:
            return flask.redirect("/feed")
        form = LoginForm()
        if form.validate_on_submit():
            session = create_session()
            user = session.query(User).filter(
                User.email == form.email.data).first()
            if user and user.check_password(form.password.data):
                login_user(user, remember=form.remember_me.data)
                return flask.redirect("/")
            print(generate_password_hash(form.password.data))
            return flask.render_template(
                'login_template.html',
                message="Неправильный логин или пароль",
                form=form)
        else:
            print(form.errors)
        return flask.render_template('login_template.html',
                                     action=False,
                                     title='Авторизация',
                                     form=form)
    elif par == "author":
        res = flask.make_response("Setting a cookie")
        res.set_cookie('utype', 'author', max_age=60 * 60 * 24 * 365 * 2)
        if current_user.is_authenticated:
            return flask.redirect("/dashboard")
        form = LoginForm()
        if form.validate_on_submit():
            session = create_session()
            author = session.query(User).filter(
                User.email == form.email.data).first()
            if author and author.check_password(form.password.data):
                login_user(author, remember=form.remember_me.data)
                return flask.redirect("/dashboard")
            return flask.render_template('login_template.html', form=form)
        else:
            print(form.errors)
        return flask.render_template('login_template.html',
                                     action=True,
                                     form=form)
Exemple #6
0
def story(sid):
    session = create_session()
    story = session.query(Story).filter(Story.id == sid).first()
    if flask.request.method == 'GET':
        content = story.content
        comments = story.commented
        header = story.head
        name = session.query(User).filter(User.id == story.author_id).first()
        can_delete = (current_user.id == story.author_id)
        author_link = story.author_id

        return flask.render_template("post_template.html",
                                     content=content,
                                     comments=comments,
                                     header=header,
                                     name=name.nickname,
                                     likes=story.likes_count,
                                     can_delete=can_delete,
                                     author_href=author_link)
    else:
        if not current_user.utype:
            content = story.content
            comments = story.commented
            header = story.head
            name = session.query(User).filter(
                User.id == story.author_id).first()
            req = flask.request.form
            if req.get('editordata'):
                print('ok')
                comment = Comment()
                comment.content = req.get('editordata')
                comment.head = current_user.nickname
                comment.story = story
                session.commit()
            if req.get('post-like'):
                story.likes_count += 1
                session.commit()
            return flask.render_template('post_template.html',
                                         content=content,
                                         comments=comments,
                                         header=header,
                                         name=name.nickname,
                                         likes=story.likes_count)
        else:
            req = flask.request.form
            if req.get('post-delete'):
                session.delete(story)
                session.commit()
                return flask.redirect('/dashboard')
    return flask.redirect(f'/story/{sid}')
Exemple #7
0
def cabinet():
    session = create_session()
    change_nick = ChangeNickname()
    disf = DisFollowed()
    if change_nick.validate_on_submit():
        user = session.query(User).filter(User.id == current_user.id)
        user.nickname = change_nick.change.data
        session.commit()
    if disf.validate_on_submit():
        user = session.query(User).filter(User.id == current_user.id)
        author = session.query(User).filter(User.id == disf.author.data)
        user.followed.remove(author)
        session.commit()
    user = session.query(User).filter(User.id == current_user.id).first()
    follows = user.followed
    name = user.nickname
    return flask.render_template("account.html", follows=follows, name=name)
Exemple #8
0
def feed():
    stories_for_watching = []
    session = create_session()
    user = session.query(User).filter(User.id == current_user.id).one()
    if flask.request.method == "POST" and flask.request.form.get('accept'):
        search_key = flask.request.form.get("search-input")  # поисковой движок
        results = session.query(Story).filter(
            Story.head.like(f'%{search_key}%'))
        if results:
            return flask.render_template("search_results.html", res=results)
        else:
            results = session.query(Story).filter(
                Story.headlike(f"%{search_key[0].upper() + search_key[1:]}%"))
        return flask.render_template("search_results.html", res=results)

    for i in user.followed.all():
        stories_for_watching += i.stories
    if not stories_for_watching:
        stories_for_watching = session.query(Story).all()
    return flask.render_template("feed.html", stories=stories_for_watching)
Exemple #9
0
 def validate_email(self, email):
     session = create_session()
     user = session.query(User).filter_by(email=email.data).first()
     if user is not None:
         raise ValidationError('Please use a different email address.')
Exemple #10
0
 def validate_username(self, username):
     session = create_session()
     user = session.query(User).filter_by(nickname=username.data).first()
     if user is not None:
         raise ValidationError('Please use a different username.')
Exemple #11
0
def load_user(user_id):
    session = db_session.create_session()
    return session.query(User).get(user_id)