137 lines
4.5 KiB
Python
137 lines
4.5 KiB
Python
from pyramid.view import view_config
|
|
from pyramid.httpexceptions import HTTPNotFound, HTTPFound
|
|
from markdown2 import Markdown
|
|
|
|
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/img/', "%s/static/img/" % 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_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]
|
|
|
|
else:
|
|
# modify post
|
|
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'))
|
|
form = BlogUpdateForm(request.POST, entry)
|
|
form.tag.choices = [(row.tag, row.tag_name) for row in tags]
|
|
|
|
if 'form.submitted' in request.params and form.validate():
|
|
if blog_id == '0':
|
|
form.populate_obj(entry)
|
|
entry.title_url = entry.slug
|
|
entry.topic_id = topic
|
|
|
|
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)
|
|
|
|
# after update procedure
|
|
BlogRecordService.proc_after_update(request, blog_id)
|
|
|
|
return HTTPFound(location=request.route_url('blog', id=entry.id, slug=entry.slug))
|
|
|
|
return {
|
|
'page_title': entry.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 = ''
|
|
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,
|
|
}
|
|
|
|
|
|
@view_config(route_name='topic',
|
|
renderer='cao_blogr:templates/topic.jinja2')
|
|
def topic(request):
|
|
topic = request.matchdict['topic']
|
|
|
|
# get the topic_name
|
|
topic_name = BlogRecordService.get_topic_name(request, topic)
|
|
# 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>'
|
|
for item in items:
|
|
liste += '<li>{0} <a href="{1}"><span class="glyphicon glyphicon-menu-right"></span> {2}</a></li>'.format(
|
|
item.edited.strftime("%d-%m-%Y"), request.route_url('blog', id=item.id, slug=item.slug), item.title)
|
|
liste += '</ul>'
|
|
else:
|
|
liste += '<ul><li> </li></ul>'
|
|
return {
|
|
'page_title': topic_name,
|
|
'topic': topic,
|
|
'liste': liste,
|
|
}
|