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/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] page_title = 'Nouvelle page' 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] 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 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) entry.edited = datetime.datetime.now() 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 += '
| %s | ' % (request.route_url('blog', id=item.id, slug=item.slug), item.title) liste += '%s | ' % item.author liste += '%s | ' % item.created.strftime("%d-%m-%Y") if item.status == 'brouillon': liste += '%s | ' % item.status liste += '