Exemple #1
0
def comm_add(new_id):
    """
    Обработка добавления комментария
    :param new_id: id новости в DB, к которой добавляется комментарий
    :return: форма для добавления комментария
    """
    form = ComForm()
    if form.validate_on_submit():
        db_sess = db_session.create_session()
        to_new = db_sess.query(News).filter(News.id == new_id).first()
        idd = len(db_sess.query(Comments).all()) + 1
        if len(to_new.comms) > 0:
            to_new.comms = str(to_new.comms) + ';{}'.format(idd)
        else:
            to_new.comms = idd
        com = Comments()
        com.user_id = current_user.id
        com.content = form.content.data
        com.created_date = datetime.datetime.now()
        db_sess.add(com)
        db_sess.add(to_new)
        db_sess.commit()
        return redirect('/')
    return render_template("add_com.html",
                           form=form,
                           title='Добавить комментарий')
def publication(id):
    status = 0
    if current_user.is_authenticated:
        status = current_user.status + 1
    session = db_session.create_session()
    publication = session.query(Publications).get(id)
    session.close()
    if publication:
        form = CommentsForm()
        if form.submit.data:
            if current_user.is_authenticated:
                session = db_session.create_session()
                user = session.query(User).get(current_user.id)
                publication = session.query(Publications).get(id)
                comment = Comments()
                comment.text = form.text.data
                comment.created_date = datetime.datetime.now()
                form.text.data = ""
                user.comments.append(comment)
                session.merge(user)
                publication.comments.append(comment)
                session.merge(publication)
                session.commit()
                session = db_session.create_session()
                comments = session.query(Comments).filter(Comments.publication_id == id).order_by(
                    Comments.created_date.desc()).all()
                templ = render_template("/Publication.html", publication=publication, status=status, user=current_user,
                                        style=url_for('static', filename='css/style.css'), form=form, comments=comments,
                                        count_commentaries=len(comments), base_href=f"Publication/{id}/",
                                        bgimg=get_image_profile(current_user))
                session.close()
                return templ
        else:
            comments = session.query(Comments).filter(Comments.publication_id == id).order_by(
                Comments.created_date.desc()).all()
            templ = render_template("/Publication.html", publication=publication, status=status, user=current_user,
                                    style=url_for('static', filename='css/style.css'), form=form, comments=comments,
                                    count_commentaries=len(comments), base_href=f"Publication/{id}/",
                                    bgimg=get_image_profile(current_user))
            session.close()
            return templ
    session.close()
    return redirect('/Publications')
def developers_diary(id):
    status = 0
    if current_user.is_authenticated:
        status = current_user.status + 1
    session = db_session.create_session()
    ds_diary = session.query(DevelopersDiary).filter(DevelopersDiary.id == id).first()
    session.close()
    ds_diary.created_date = ":".join(str(ds_diary.created_date).split(":")[:-1])
    if status >= ds_diary.availability_status:
        form = CommentsForm()
        if form.submit.data:
            if current_user.is_authenticated:
                session = db_session.create_session()
                user = session.query(User).get(current_user.id)
                diary = session.query(DevelopersDiary).get(id)
                comment = Comments()
                comment.text = form.text.data
                comment.created_date = datetime.datetime.now()
                form.text.data = ""
                user.comments.append(comment)
                session.merge(user)
                diary.comments.append(comment)
                session.merge(diary)
                session.commit()
                session = db_session.create_session()
                comments = session.query(Comments).filter(Comments.developers_diary_publication_id == id).order_by(
                    Comments.created_date.desc()).all()
                templ = render_template("/DevelopersDiary.html", publication=ds_diary, status=status, user=current_user,
                                        style=url_for('static', filename='css/style.css'), form=form, comments=comments,
                                        count_commentaries=len(comments), base_href=f"DevelopersDiaryPublication/{id}/",
                                        bgimg=get_image_profile(current_user))
                session.close()
                return templ
        else:
            comments = session.query(Comments).filter(Comments.developers_diary_publication_id == id).order_by(
                Comments.created_date.desc()).all()
            templ = render_template("/DevelopersDiary.html", publication=ds_diary, status=status, user=current_user,
                                    style=url_for('static', filename='css/style.css'), form=form, comments=comments,
                                    count_commentaries=len(comments), base_href=f"DevelopersDiaryPublication/{id}/",
                                    bgimg=get_image_profile(current_user))
            session.close()
            return templ
    return redirect('/DevelopersDiary')