Files
cao_sunyata/cao_blogr/views/blog.py
2022-10-08 11:58:53 +02:00

208 lines
7.5 KiB
Python

from pyramid.view import view_config
from pyramid.httpexceptions import HTTPNotFound, HTTPFound
from markdown2 import Markdown
import datetime #<- will be used to set default dates on models
from ..models.blog_record import BlogRecord
from ..services.blog_record import BlogRecordService
from ..forms import BlogCreateForm, BlogUpdateForm, BlogSearchForm
@view_config(route_name='blog',
renderer='cao_blogr:templates/blog.jinja2')
def blog(request):
# get post id from request
blog_id = request.matchdict['id']
entry = BlogRecordService.by_id(request, blog_id)
if not entry:
request.session.flash(u"Page non trouvée : %s" % blog_id, 'warning')
return HTTPFound(location=request.route_url('home'))
# insèrer le path de static/img
body = entry.body.replace('static/', "%s/static/" % request.application_url)
# convertir mardown en HTML
markdowner = Markdown()
body_html = markdowner.convert(body)
return {
'page_title': entry.title,
'entry': entry,
'body_html': body_html,
}
@view_config(route_name='blog_copy',
renderer='cao_blogr:templates/blog_copy.jinja2', permission='view')
def blog_copy(request):
# get post parameters from request
topic = request.matchdict['topic']
blog_id = request.matchdict['id']
# get the post
entry = BlogRecordService.by_id(request, blog_id)
if not entry:
request.session.flash("Page non trouvée : %s" % blog_id, 'warning')
return HTTPFound(location=request.route_url('topic', topic=topic))
# create a new post
dup_entry = BlogRecord()
dup_entry.title = entry.title + ' - copie'
dup_entry.body = entry.body
dup_entry.topic_id = entry.topic_id
dup_entry.tag = entry.tag
dup_entry.author = entry.author
dup_entry.status = entry.status
request.dbsession.add(dup_entry)
request.session.flash("La page a été duppliquée avec succès", 'success')
return HTTPFound(location=request.route_url('topic', topic=topic))
@view_config(route_name='blog_edit',
renderer='cao_blogr:templates/blog_edit.jinja2', permission='view')
def blog_edit(request):
# get post parameters from request
topic = request.matchdict['topic']
blog_id = request.matchdict['id']
url = request.route_url('blog_edit',topic=topic, id=blog_id)
# get the list of tags of this topic
tags = BlogRecordService.get_tags_byTopic(request, topic)
if blog_id == '0':
# create a new post
entry = BlogRecord()
entry.topic_id = topic
form = BlogCreateForm(request.POST, entry)
form.tag.choices = [(row.tag, row.tag_name) for row in tags]
page_title = 'Nouvelle page'
else:
# modify post
entry = BlogRecordService.by_id(request, blog_id)
if not entry:
request.session.flash("Page non trouvée : %s" % blog_id, 'warning')
return HTTPFound(location=request.route_url('topic', topic=topic))
form = BlogUpdateForm(request.POST, entry)
form.tag.choices = [(row.tag, row.tag_name) for row in tags]
page_title = 'Modifier : ' + entry.title
if 'form.submitted' in request.params and form.validate():
if blog_id == '0':
form.populate_obj(entry)
entry.topic_id = topic
# interdire le car '/' dans le titre à cause du slug
entry.title = entry.title.replace('/','.')
entry.creator = request.authenticated_userid
entry.editor = entry.creator
request.dbsession.add(entry)
return HTTPFound(location=request.route_url('topic', topic=topic))
else:
del form.id # SECURITY: prevent overwriting of primary key
form.populate_obj(entry)
# interdire le car '/' dans le titre à cause du slug
entry.title = entry.title.replace('/','.')
entry.edited = datetime.datetime.now()
entry.editor = request.authenticated_userid
return HTTPFound(location=request.route_url('blog', id=entry.id, slug=entry.slug))
if 'form.deleted' in request.params:
BlogRecordService.delete(request, blog_id)
request.session.flash("La page a été supprimée avec succès.", 'success')
return HTTPFound(location=request.route_url('topic', topic=topic))
return {
'page_title': page_title,
'url': url,
'form': form,
'blog_id': blog_id,
'entry': entry,
}
@view_config(route_name='blog_search',
renderer='cao_blogr:templates/blog_search.jinja2')
def blog_search(request):
criteria = ''
liste = ''
# generate a list of all tags of all topics
if request.authenticated_userid:
# get all topics
topics = BlogRecordService.get_topics(request)
for topic in topics:
liste += '<h4><a href="{0}">{1}</a></h4>'.format(
request.route_url('topic', topic=topic.topic), topic.topic_name)
# get all the tags of the topic
tags = BlogRecordService.get_tags_byTopic(request, topic.topic)
if tags:
liste += '<ul>'
for tag in tags:
count = len(BlogRecordService.by_topic(request, tag.topic, tag.tag))
liste += '<button type="button" class="btn">{0}&nbsp;&nbsp;&nbsp;<span class="badge">{1}</span></button>&nbsp;'.format(
tag.tag_name, count)
liste += '</ul>'
form = BlogSearchForm(request.POST)
items = []
if 'form.submitted' in request.params and form.validate():
criteria = request.params['criteria']
# si afficher tous les fiches ?
items = BlogRecordService.by_criteria(request, criteria)
return {
'page_title': "Rechercher",
'form': form,
'items': items,
'criteria': criteria,
'liste': liste,
}
@view_config(route_name='topic',
renderer='cao_blogr:templates/topic.jinja2')
def topic(request):
topic = request.matchdict['topic']
# get the topic record
topic_record = BlogRecordService.get_topic_byTopic(request, topic)
# convertir champ topic_quote en HTML
markdowner = Markdown()
topic_quote = markdowner.convert(topic_record.topic_quote)
# insèrer le path de static/img
topic_quote = topic_quote.replace('static/', "%s/static/" % request.application_url)
# get all the tags of this topic
tags = BlogRecordService.get_tags_byTopic(request, topic)
# generate the items list group by tag
liste = ''
for tag in tags:
liste += '<h3>' + tag.tag_name + '</h3>'
# lire toutes les docs du topic
items = BlogRecordService.by_topic(request, topic, tag.tag)
if items:
liste += '<ul><table class="table table-condensed">'
for item in items:
liste += '<tr>'
liste += '<td><a href="%s">%s</a></td>' % (request.route_url('blog', id=item.id, slug=item.slug), item.title)
liste += '<td>%s</td>' % item.author
liste += '<td>%s</td>' % item.created.strftime("%d-%m-%Y")
if item.status == 'brouillon':
liste += '<td><span class="label label-danger">%s</span></td>' % item.status
liste += '</tr>'
liste += '</table></ul>'
else:
liste += '<ul><li> </li></ul>'
return {
'page_title': topic_record.topic_name,
'topic': topic,
'topic_quote': topic_quote,
'liste': liste,
}