from flask_login import LoginManager, login_user, logout_user, login_required, current_user
from flask_wtf import FlaskForm
from werkzeug.exceptions import abort
from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField
from wtforms.fields.html5 import EmailField
from wtforms.validators import DataRequired
from flask import Flask, redirect, render_template, request, make_response
from flask import Flask
from data import db_session
from flask import Flask, current_app
mas = ''
from data import db_session
from data.users import User, News
app = Flask(__name__)
app.config['SECRET_KEY'] = 'yandexlyceum_secret_key'
db_session.global_init('C:/Users/Tim/112/db/blogs.sqlite')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'yandexlyceum_secret_key'
session = db_session.create_session()
class NewsForm(FlaskForm):
title = StringField('zagolovok', validators=[DataRequired()])
content = TextAreaField("soderzanie")
is_private = BooleanField("lichnoe")
submit = SubmitField('primenit')
@app.route('/news', methods=['GET', 'POST'])
@login_required
def add_news():
form = NewsForm()
if form.validate_on_submit():
print(1)
session = db_session.create_session()
news = News()
news.title = form.title.data
news.content = form.content.data
news.is_private = form.is_private.data
current_user.news.append(news)
session.merge(current_user)
session.commit()
return redirect('/news')
return render_template('news.html', title='add news',
form=form)
@app.route('/news/<int:id>', methods=['GET', 'POST'])
@login_required
def edit_news(id):
form = NewsForm()
if request.method == "GET":
session = db_session.create_session()
news = session.query(News).filter(News.id == id,
News.user == current_user).first()
if news:
form.title.data = news.title
form.content.data = news.content
form.is_private.data = news.is_private
else:
abort(404)
if form.validate_on_submit():
session = db_session.create_session()
news = session.query(News).filter(News.id == id,
News.user == current_user).first()
if news:
news.title = form.title.data
news.content = form.content.data
news.is_private = form.is_private.data
session.commit()
return redirect('/')
else:
abort(404)
return render_template('news.html', title='redak news', form=form)
if __name__ == '__main__':
app.run(port=8080, host='127.0.0.1')
import datetime
import sqlalchemy
from flask_login import UserMixin
from sqlalchemy import orm
from werkzeug.security import check_password_hash, generate_password_hash
from . import db_session
from .db_session import SqlAlchemyBase
import requests
#!/usr/bin/env python
# -*- coding: utf-8 -*-
s = u"RU"
class News(SqlAlchemyBase):
__tablename__ = 'news'
id = sqlalchemy.Column(sqlalchemy.Integer,
primary_key=True, autoincrement=True)
title = sqlalchemy.Column(sqlalchemy.String, nullable=True)
content = sqlalchemy.Column(sqlalchemy.String, nullable=True)
created_date = sqlalchemy.Column(sqlalchemy.DateTime,
default=datetime.datetime.now)
is_private = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
user_id = sqlalchemy.Column(sqlalchemy.Integer,
sqlalchemy.ForeignKey("users.id"))
user = orm.relation('User')
{% extends "base.html" %}
{% block content %}
<h1>Добавление новости</h1>
<form action="" method="post">
{{ form.hidden_tag() }}
{{ form.csrf_token }}
<p>
{{ form.title.label }}<br>
{{ form.title(class="form-control") }}<br>
{% for error in form.title.errors %}
<p class="alert alert-danger" role="alert">
{{ error }}
</p>
{% endfor %}
</p>
<p>
{{ form.content.label }}<br>
{{ form.content(class="form-control") }}<br>
{% for error in form.content.errors %}
<p content="alert alert-danger" role="alert">
{{ error }}
</p>
{% endfor %}
</p>
<p>{{ form.is_private() }} {{ form.is_private.label }}</p>
<p>{{ form.submit(type="submit", class="btn btn-primary") }}</p>
{{message}}
</form>
{% endblock %}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
crossorigin="anonymous">
<title>{{title}}</title>
</head>
<body>
<header>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Наше приложение</a>
</nav>
</header>
<!-- Begin page content -->
<main role="main" class="container">
{% block content %}{% endblock %}
</main>
</body>
</html>