added urlified title in title_url

This commit is contained in:
2022-05-01 16:55:15 +02:00
parent 0a307fc953
commit 7c7a0c3a54
16 changed files with 884 additions and 144 deletions

View File

@@ -1,46 +0,0 @@
"""init
Revision ID: 85284752d1d5
Revises: bbacde35234d
Create Date: 2022-04-30 14:21:08.576738
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '85284752d1d5'
down_revision = 'bbacde35234d'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('tags',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('topic', sa.Unicode(length=25), nullable=True),
sa.Column('tag', sa.Unicode(length=25), nullable=True),
sa.Column('title', sa.Unicode(length=25), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_tags')),
sa.UniqueConstraint('title', name=op.f('uq_tags_title'))
)
op.create_index('topic_index', 'tags', ['topic', 'tag'], unique=False)
op.create_table('topics',
sa.Column('topic', sa.Unicode(length=25), nullable=False),
sa.Column('title', sa.Unicode(length=25), nullable=False),
sa.PrimaryKeyConstraint('topic', name=op.f('pk_topics')),
sa.UniqueConstraint('title', name=op.f('uq_topics_title'))
)
op.create_index(op.f('ix_entries_tag'), 'entries', ['tag'], unique=False)
op.create_index(op.f('ix_entries_topic'), 'entries', ['topic'], unique=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_entries_topic'), table_name='entries')
op.drop_index(op.f('ix_entries_tag'), table_name='entries')
op.drop_table('topics')
op.drop_index('topic_index', table_name='tags')
op.drop_table('tags')
# ### end Alembic commands ###

View File

@@ -1,8 +1,8 @@
"""init
Revision ID: 5899f27f265f
Revision ID: 07fa8fad6cc3
Revises:
Create Date: 2018-12-23 16:39:13.677058
Create Date: 2022-05-01 10:48:25.244455
"""
from alembic import op
@@ -10,21 +10,25 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '5899f27f265f'
revision = '07fa8fad6cc3'
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('entries',
op.create_table('tags',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('title', sa.Unicode(length=255), nullable=False),
sa.Column('body', sa.UnicodeText(), nullable=True),
sa.Column('created', sa.DateTime(), nullable=True),
sa.Column('edited', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id', name=op.f('pk_entries')),
sa.UniqueConstraint('title', name=op.f('uq_entries_title'))
sa.Column('topic', sa.Unicode(length=25), nullable=True),
sa.Column('tag', sa.Unicode(length=25), nullable=True),
sa.Column('tag_name', sa.Unicode(length=25), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_tags'))
)
op.create_index('topic_index', 'tags', ['topic', 'tag'], unique=False)
op.create_table('topics',
sa.Column('topic', sa.Unicode(length=25), nullable=False),
sa.Column('topic_name', sa.Unicode(length=25), nullable=False),
sa.PrimaryKeyConstraint('topic', name=op.f('pk_topics'))
)
op.create_table('users',
sa.Column('id', sa.Integer(), nullable=False),
@@ -34,10 +38,25 @@ def upgrade():
sa.PrimaryKeyConstraint('id', name=op.f('pk_users')),
sa.UniqueConstraint('name', name=op.f('uq_users_name'))
)
op.create_table('entries',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('title', sa.Unicode(length=255), nullable=False),
sa.Column('body', sa.UnicodeText(), nullable=True),
sa.Column('body_html', sa.UnicodeText(), nullable=True),
sa.Column('created', sa.DateTime(), nullable=True),
sa.Column('edited', sa.DateTime(), nullable=True),
sa.Column('topic_id', sa.Unicode(length=25), nullable=False),
sa.ForeignKeyConstraint(['topic_id'], ['topics.topic'], name=op.f('fk_entries_topic_id_topics')),
sa.PrimaryKeyConstraint('id', name=op.f('pk_entries')),
sa.UniqueConstraint('title', name=op.f('uq_entries_title'))
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('users')
op.drop_table('entries')
op.drop_table('users')
op.drop_table('topics')
op.drop_index('topic_index', table_name='tags')
op.drop_table('tags')
# ### end Alembic commands ###

View File

@@ -1,8 +1,8 @@
"""init
Revision ID: e7889eab89c0
Revises: 5899f27f265f
Create Date: 2022-04-19 16:21:57.531003
Revision ID: 1a99bede8b76
Revises: 1c62fb741d2f
Create Date: 2022-05-01 16:50:53.674187
"""
from alembic import op
@@ -10,19 +10,17 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'e7889eab89c0'
down_revision = '5899f27f265f'
revision = '1a99bede8b76'
down_revision = '1c62fb741d2f'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('entries', sa.Column('tag', sa.Unicode(), nullable=True))
op.add_column('entries', sa.Column('topic', sa.Unicode(), nullable=True))
op.add_column('entries', sa.Column('title_url', sa.Unicode(length=255), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('entries', 'topic')
op.drop_column('entries', 'tag')
op.drop_column('entries', 'title_url')
# ### end Alembic commands ###

View File

@@ -1,8 +1,8 @@
"""init
Revision ID: bbacde35234d
Revises: e7889eab89c0
Create Date: 2022-04-19 17:09:50.728285
Revision ID: 1c62fb741d2f
Revises: 07fa8fad6cc3
Create Date: 2022-05-01 11:16:29.476581
"""
from alembic import op
@@ -10,17 +10,17 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'bbacde35234d'
down_revision = 'e7889eab89c0'
revision = '1c62fb741d2f'
down_revision = '07fa8fad6cc3'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('entries', sa.Column('body_html', sa.UnicodeText(), nullable=True))
op.add_column('entries', sa.Column('tag', sa.Unicode(length=25), nullable=True))
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('entries', 'body_html')
op.drop_column('entries', 'tag')
# ### end Alembic commands ###

View File

@@ -1,32 +0,0 @@
"""init
Revision ID: de7d23a4b139
Revises: 85284752d1d5
Create Date: 2022-05-01 08:41:03.244262
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'de7d23a4b139'
down_revision = '85284752d1d5'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('tags', sa.Column('tag_name', sa.Unicode(length=25), nullable=False))
op.create_unique_constraint(op.f('uq_tags_tag_name'), 'tags', ['tag_name'])
op.add_column('topics', sa.Column('topic_name', sa.Unicode(length=25), nullable=False))
op.create_unique_constraint(op.f('uq_topics_topic_name'), 'topics', ['topic_name'])
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(op.f('uq_topics_topic_name'), 'topics', type_='unique')
op.drop_column('topics', 'topic_name')
op.drop_constraint(op.f('uq_tags_tag_name'), 'tags', type_='unique')
op.drop_column('tags', 'tag_name')
# ### end Alembic commands ###

View File

@@ -7,7 +7,10 @@ from sqlalchemy import (
UnicodeText, #<- will provide Unicode text field
DateTime, #<- time abstraction field
Index,
ForeignKey,
)
from sqlalchemy.orm import relationship
from webhelpers2.text import urlify #<- will generate slugs
from webhelpers2.date import distance_of_time_in_words #<- human friendly dates
@@ -16,13 +19,16 @@ class BlogRecord(Base):
__tablename__ = 'entries'
id = Column(Integer, primary_key=True)
title = Column(Unicode(255), unique=True, nullable=False)
title_url = Column(Unicode(255))
body = Column(UnicodeText, default='')
body_html = Column(UnicodeText, default='')
tag = Column(Unicode(25), index=True)
topic = Column(Unicode(25), index=True)
created = Column(DateTime, default=datetime.datetime.utcnow)
edited = Column(DateTime, default=datetime.datetime.utcnow)
topic_id = Column(ForeignKey('topics.topic'), nullable=False)
topic = relationship('Topics', backref='topic_pages')
tag = Column(Unicode(25))
@property
def slug(self):
return urlify(self.title)
@@ -37,7 +43,6 @@ class Topics(Base):
topic = Column(Unicode(25), primary_key=True)
topic_name = Column(Unicode(25), nullable=False)
class Tags(Base):
__tablename__ = 'tags'
id = Column(Integer, primary_key=True)

View File

@@ -11,10 +11,13 @@ class BlogRecordService(object):
@classmethod
def by_topic(cls, request, topic):
query = request.dbsession.query(BlogRecord).join(Tags, Tags.topic == BlogRecord.topic, Tags.tag == BlogRecord.tag)
query = query.filter(BlogRecord.topic == topic)
query = query.order_by(BlogRecord.tag, BlogRecord.title).all()
return query
query = """SELECT entries.*, entries.slug, topics.topic_name, tags.tag_name FROM entries
JOIN topics ON topics.topic = entries.topic_id
JOIN tags ON tags.topic = entries.topic_id AND tags.tag = entries.tag
WHERE entries.topic_id = :topic ORDER BY tags.tag, entries.title
"""
results = request.dbsession.execute(query, {'topic': topic}).fetchall()
return results
@classmethod
def by_criteria(cls, request, criteria):
@@ -31,7 +34,7 @@ class BlogRecordService(object):
@classmethod
def get_last_five(cls, request):
# gest the last 5 items modified
query = request.dbsession.query(BlogRecord)
query = request.dbsession.query(BlogRecord.id, BlogRecord.title, BlogRecord.edited, Topics.topic_name).join(Topics, Topics.topic == BlogRecord.topic_id)
query = query.order_by(sa.desc(BlogRecord.edited)).limit(5).all()
return query
@@ -42,14 +45,15 @@ class BlogRecordService(object):
query = query.order_by(Tags.tag).all()
return query
@classmethod
def get_topic_name(cls, request, id):
# gest the last 5 items modified
query = request.dbsession.query(Topics).filter(Topics.topic == id).first()
return query.topic_name.upper()
@classmethod
def proc_after_create(cls, request, _id):
entry = request.dbsession.query(BlogRecord).get(_id)
# set default values
if entry.tag == '':
entry.tag = 'pyramid'
if entry.topic == '':
entry.topic = 'blog'
# convertir mardown en HTML
markdowner = Markdown()
entry.body_html = markdowner.convert(entry.body)
@@ -59,6 +63,7 @@ class BlogRecordService(object):
def proc_after_update(cls, request, _id):
entry = request.dbsession.query(BlogRecord).get(_id)
entry.edited = datetime.datetime.now()
entry.title_url = entry.slug
# convertir mardown en HTML
markdowner = Markdown()
entry.body_html = markdowner.convert(entry.body)

View File

@@ -160,7 +160,7 @@
</a>
</div>
<div class="col-xs-2">
<span class="glyphicon glyphicon-triangle-left"></span>&nbsp;{{ entry.topic }}
[&nbsp;{{ entry.topic_name }} ]
</div>
{% endfor %}

View File

@@ -9,14 +9,14 @@
{% endif%}
<div class="row">
{% for entry in items %}
<div class="col-xs-10">
{{ entry.edited.strftime("%d-%m-%Y") }}&nbsp;&nbsp;
<a href="{{ request.route_url('blog', id=entry.id, slug=entry.slug) }}">
<span class="glyphicon glyphicon-triangle-right"></span>&nbsp;{{ entry.title }}
</a>
<div class="col-xs-9">
<li>
<a href="{{ request.route_url('blog', id=entry.id, slug=entry.slug) }}">{{ entry.title }}</a>
</li>
</div>
<div class="col-xs-2">
<span class="glyphicon glyphicon-triangle-left"></span>&nbsp;{{ entry.topic }}
<div class="col-xs-3">
{{ entry.edited[:10] }}&nbsp;-&nbsp;
{{ entry.tag_name }}
</div>
{% endfor %}

View File

@@ -52,6 +52,8 @@ def blog_edit(request):
if 'form.submitted' in request.params and form.validate():
if blog_id == '0':
form.populate_obj(entry)
entry.title_url = entry.slug
request.dbsession.add(entry)
return HTTPFound(location=request.route_url('home'))
@@ -97,11 +99,14 @@ def blog_search(request):
def topic(request):
topic = request.matchdict['topic']
# get the topic_name
topic_name = BlogRecordService.get_topic_name(request, topic)
# lire toutes les docs du topic
items = BlogRecordService.by_topic(request, topic)
return {
'page_title': topic.upper(),
'page_title': topic_name,
'topic': topic,
'items': items,
}

View File

@@ -25,6 +25,7 @@ def home(request):
page = int(request.params.get('page', 1))
# get the 5 last modified posts
last_five = BlogRecordService.get_last_five(request)
return {
'page_title': "",
'last_five': last_five,