Skip to content
Merged

Seo #35

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion blog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from blog.admin import create_admin
from blog.config import config
from blog.extensions import admin_ext, cache, db, login_manager, migrate
from blog.extensions import admin_ext, cache, db, login_manager, migrate, flask_sitemap
from blog.post.views import post
from blog.tags.views import tagsb
from blog.user.views import user_blueprint
Expand All @@ -25,6 +25,7 @@ def configure_extensions(app):
migrate.init_app(app=app, db=db)
login_manager.init_app(app=app)
login_manager.login_view = "userb.index" # type: ignore
flask_sitemap.init_app(app)


def create_app():
Expand Down
31 changes: 18 additions & 13 deletions blog/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,39 @@


class Config(object):
CACHE_TYPE = "NullCache"
PORT = environ.get("PORT") or "5555"
SECRET_KEY = environ.get("SECRET_KEY") or "hard to guess string"
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ECHO = False
CACHE_TYPE: str = "NullCache"
PORT: str = environ.get("PORT") or "5555"
SECRET_KEY: str = environ.get("SECRET_KEY") or "hard to guess string"
SQLALCHEMY_TRACK_MODIFICATIONS: bool = False
SQLALCHEMY_ECHO: bool = False
YANDEX_VERIFICATION: str | None = environ.get("YANDEX_VERIFICATION", None)
YANDEX_METRIKA: str = environ.get("YANDEX_METRIKA", "76938046")

PAGE_CATEGORY: list[int] = [
int(c) for c in environ.get("PAGE_CATEGORY", "0").split(",")
]


class DevelopmentConfig(Config):
DEBUG = True
default_db_uri = f"sqlite:///{path.join(basedir, '../tmp/dev.db')}"
SQLALCHEMY_DATABASE_URI = environ.get("SQLALCHEMY_DATABASE_URI", default_db_uri)
DEBUG: bool = True
default_db_uri: str = f"sqlite:///{path.join(basedir, '../tmp/dev.db')}"
SQLALCHEMY_DATABASE_URI: str = environ.get(
"SQLALCHEMY_DATABASE_URI", default_db_uri
)


class TestingConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"
TESTING: bool = True
SQLALCHEMY_DATABASE_URI: str = "sqlite:///:memory:"
PAGE_CATEGORY: list[int] = [
1,
]


class ProductionConfig(Config):
SQLALCHEMY_DATABASE_URI = environ.get("SQLALCHEMY_DATABASE_URI", None)
CACHE_TYPE = "SimpleCache"
CACHE_DEFAULT_TIMEOUT = 300
SQLALCHEMY_DATABASE_URI: str | None = environ.get("SQLALCHEMY_DATABASE_URI", None)
CACHE_TYPE: str = "SimpleCache"
CACHE_DEFAULT_TIMEOUT: int = 300


config = {
Expand Down
2 changes: 2 additions & 0 deletions blog/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
from flask_login import LoginManager
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
from flask_sitemap import Sitemap

db = SQLAlchemy()
admin_ext = Admin(base_template="admin/index.html", template_mode="bootstrap3")
cache = Cache()
migrate = Migrate()
login_manager: LoginManager = LoginManager()
flask_sitemap = Sitemap()
25 changes: 24 additions & 1 deletion blog/post/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
make_response,
render_template,
request,
url_for,
)
from sqlalchemy import or_
from blog.extensions import flask_sitemap

from blog import cache, db
from blog.post.models import Post
Expand Down Expand Up @@ -61,11 +63,32 @@ def view(alias=None, **kwargs):
page_category_obj = db.session.scalars(
sa.select(Category).where(Category.id == post.category_id)
).first()
if page_category_obj:
if page_category_obj and page_category_obj.template:
return render_template(page_category_obj.template, post=post, **kwargs)
return render_template("post.html", post=post, **kwargs)


@flask_sitemap.register_generator
def site_map_gen():
page_category = current_app.config["PAGE_CATEGORY"]
pages_query = sa.select(Post).where(Post.category_id.in_(page_category))
pages = db.session.scalars(pages_query).all()
for page in pages:
yield url_for("postb.view", alias=page.alias)
post_query = (
sa.select(Post)
.where(
Post.publishedon.isnot(None),
Post.category_id.is_(None),
)
.order_by(Post.publishedon.desc())
)

posts = db.session.scalars(post_query).all()
for post in posts:
yield url_for("postb.view", alias=post.alias)


@post.route("/md/", methods=["POST", "GET"])
def getmd():
post_data = request.form.get("data", "")
Expand Down
4 changes: 4 additions & 0 deletions blog/templates/footer.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<footer class="footer">

<div class="footer__content">
<div class="footer__links">
<a href="https://t.me/gunlinuxlog" class="nav__link" target="_blank" aria-label="Telegram">
Expand All @@ -19,4 +20,7 @@
</div>
</div>
</footer>
{% if config['YANDEX_METRIKA'] %}
{% include "snippets/yandex_metrika.html" %}
{% endif %}

4 changes: 4 additions & 0 deletions blog/templates/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
<meta charset="utf-8">
<title>{% if post %}{{post.pagetitle}}{% else %} Неразумный перфекционизм{% endif %}</title>
<meta name="description" content="{% if post %}{{post.pagetitle}}{% endif %}">
{% if config['YANDEX_VERIFICATION'] %}
<meta name="yandex-verification" content="{{config['YANDEX_VERIFICATION']}}"/>
{% endif %}
<link href="/static/img/favicon.ico" rel="shortcut icon" />
<base href="{{config['BASE']}}">
<link rel="stylesheet" href="/static/src/global/normalize.css">
<link rel="stylesheet" href="/static/src/global/fonts.css">
<link rel="stylesheet" href="/static/src/global/vars.css">

<link rel="stylesheet" href="/static/src/global/reboot.css">
<link rel="stylesheet" href="/static/src/global/global.css">
<link rel="stylesheet" href="/static/src/components/header.css">
Expand Down
14 changes: 14 additions & 0 deletions blog/templates/snippets/yandex_metrika.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!-- Yandex.Metrika counter -->
<script type="text/javascript">
(function(m,e,t,r,i,k,a){
m[i]=m[i]||function(){(m[i].a=m[i].a||[]).push(arguments)};
m[i].l=1*new Date();
for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
k=e.createElement(t),a=e.getElementsByTagName(t)[0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)
})(window, document,'script','https://mc.yandex.ru/metrika/tag.js', 'ym');

ym({{config['YANDEX_METRIKA']}}, 'init', {clickmap:true, accurateTrackBounce:true, trackLinks:true});
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/{{config['YANDEX_METRIKA']}}" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dependencies = [
"flask-caching>=2.3.1",
"flask-login>=0.6.3",
"flask-migrate>=4.1.0",
"flask-sitemap>=0.4.0",
"flask-sqlalchemy>=3.1.1",
"flask-wtf>=1.2.2",
"gevent>=25.5.1",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_posts.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def post_helper(prefix="post", page=False):

def test_post(test_client):
with test_client.application.app_context():
post = post_helper()
post = post_helper(page=False)
db.session.add(post)
db.session.commit()
rv = test_client.get("/post_alias")
Expand Down
15 changes: 15 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.