diff --git a/pyproger/app.py b/pyproger/app.py index 8c27951..8399e50 100644 --- a/pyproger/app.py +++ b/pyproger/app.py @@ -93,6 +93,10 @@ def create_app(test_config=None): app.register_blueprint(bp_errors) + from pyproger.robots.robots import bp as bp_robots + + app.register_blueprint(bp_robots) + @security.context_processor def security_context_processor(): return dict( diff --git a/pyproger/config.py b/pyproger/config.py index 6700c3d..5263fe3 100755 --- a/pyproger/config.py +++ b/pyproger/config.py @@ -45,4 +45,4 @@ CKEDITOR_SERVE_LOCAL = True CKEDITOR_ENABLE_CODESNIPPET = True CKEDITOR_CODE_THEME = "monokai_sublime" # Настройки блога -POSTS_ON_PAGE = 6 +POSTS_ON_PAGE = 2 diff --git a/pyproger/dbase/database.py b/pyproger/dbase/database.py index 7c6e443..0169898 100644 --- a/pyproger/dbase/database.py +++ b/pyproger/dbase/database.py @@ -1,3 +1,7 @@ +from datetime import datetime, timezone + +from sqlalchemy import func + from . import db from .models import Page, Post, Tag, User @@ -55,3 +59,21 @@ def get_page(slug): def get_menu_items(): menu_items = db.session.query(Page.name, Page.slug).all() return menu_items + + +def get_posts_for_sitemap(): + posts = ( + db.session.query( + Post.slug, + Post.update_datetime, + ) + .filter(Post.published.is_(True)) + .all() + ) + return posts + + +def get_pages_for_sitemap(): + pages = db.session.query(Page.slug).all() + date = datetime.utcnow().strftime("%Y-%m-%d") + return pages, date diff --git a/pyproger/dbase/models.py b/pyproger/dbase/models.py index c51dc8c..abb25d9 100644 --- a/pyproger/dbase/models.py +++ b/pyproger/dbase/models.py @@ -2,6 +2,7 @@ import datetime from flask_security.models import fsqla from sqlalchemy import Boolean, Column, DateTime, Integer, String, Text +from sqlalchemy.sql import func from . import db @@ -78,12 +79,13 @@ class Post(db.Model): create_datetime = Column( DateTime(), nullable=True, - default=datetime.datetime.utcnow(), + default=func.now(), ) update_datetime = Column( DateTime(), nullable=True, - onupdate=datetime.datetime.utcnow(), + default=func.now(), + onupdate=func.now(), ) text = Column(Text) diff --git a/pyproger/robots/__init__.py b/pyproger/robots/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pyproger/robots/robots.py b/pyproger/robots/robots.py new file mode 100644 index 0000000..b269a15 --- /dev/null +++ b/pyproger/robots/robots.py @@ -0,0 +1,10 @@ +from flask import Blueprint + +bp = Blueprint( + "bp_robots", + __name__, + template_folder="templates/robots", + static_folder="static", +) + +from . import urls diff --git a/pyproger/robots/urls.py b/pyproger/robots/urls.py new file mode 100644 index 0000000..b12642b --- /dev/null +++ b/pyproger/robots/urls.py @@ -0,0 +1,31 @@ +from flask import make_response, render_template +from sqlalchemy.sql import func + +from pyproger.dbase.database import get_pages_for_sitemap, get_posts_for_sitemap + +from .robots import bp + + +@bp.route("/sitemap.xml", methods=["GET"]) +def sitemap_xml(): + sm_posts = get_posts_for_sitemap() + sm_pages, date = get_pages_for_sitemap() + sm_render = render_template( + "robots/sitemap.xml", + sm_pages=sm_pages, + sm_posts=sm_posts, + date=date, + ) + response = make_response(sm_render) + response.headers["Content-Type"] = "application/rss+xml" + response.mimetype = "application/xml" + return response + + +@bp.route("/robots.txt", methods=["GET"]) +def robots_txt(): + rt_render = render_template("robots/robots.txt") + response = make_response(rt_render) + response.headers["Content-Type"] = "text/plain; charset=utf-8" + response.mimetype = "text/plain" + return response diff --git a/pyproger/templates/robots/robots.txt b/pyproger/templates/robots/robots.txt new file mode 100644 index 0000000..8e3dbd4 --- /dev/null +++ b/pyproger/templates/robots/robots.txt @@ -0,0 +1,37 @@ +User-agent: Yandex +Disallow: /? +Disallow: /admin/ +Disallow: /static/ +Disallow: /post/$ +Disallow: /tag/ +Disallow: /tags/ +Allow: /static/*.css +Allow: /static/*.js +Allow: /static/*.png +Allow: /static/*.jpg +Allow: /static/*.gif + +User-agent: GoogleBot +Disallow: /? +Disallow: /admin/ +Disallow: /static/ +Disallow: /post/$ +Disallow: /tag/ +Disallow: /tags/ +Allow: /static/*.css +Allow: /static/*.js +Allow: /static/*.png +Allow: /static/*.jpg +Allow: /static/*.gif + +User-agent: * +Disallow: /? +Disallow: /admin/ +Disallow: /static/ +Disallow: /post/$ +Disallow: /tag/ +Disallow: /tags/ +Crawl-Delay: 5 + +Sitemap: https://pyproger.ru/sitemap.xml +Host: https://pyproger.ru diff --git a/pyproger/templates/robots/sitemap.xml b/pyproger/templates/robots/sitemap.xml new file mode 100644 index 0000000..a05e521 --- /dev/null +++ b/pyproger/templates/robots/sitemap.xml @@ -0,0 +1,12 @@ + + + + {% for p in sm_posts %} + + http://www.pyproger.ru/post/{{ p.slug }} + {{ p.update_datetime.strftime('%Y-%m-%d') }} + daily + 1.0 + + {% endfor %} +