Files
caotek_monaa/caotek_mesavoirs/views/contents.py

150 lines
4.4 KiB
Python

# -*- coding: utf8 -*-
from pyramid.response import Response
from pyramid.renderers import render, get_renderer
from pyramid.view import (
view_config,
forbidden_view_config,
)
from pyramid.httpexceptions import (
HTTPFound,
HTTPNotFound,
HTTPForbidden,
)
from ..models.contents import *
def isAnonymous(request):
logged_in = request.authenticated_userid
return logged_in is None
@view_config(route_name='folder', renderer='../templates/contents/folder.pt')
def folder(request):
topic = request.matchdict['topic']
logged_in = request.authenticated_userid
# lire toutes les docs du topic
docs = get_docs_bytopic(request, topic, logged_in)
return {
'page_title': "Contenu de %s" % topic.upper(),
'docs': docs,
'topic': topic,
}
@view_config(route_name='doc_edit', renderer='../templates/contents/doc_edit.pt', permission='view')
def doc_edit(request):
topic = request.matchdict['topic']
doc_id = request.matchdict['doc_id']
url = request.route_url('doc_edit', topic=topic, doc_id=doc_id)
logged_in = request.authenticated_userid
# si anonyme, interdire de modifier les docs
if isAnonymous(request):
return HTTPFound(location=request.route_url('home'))
message = ""
tags = get_d_tags(request)
statuts = ['private', 'public']
if doc_id == '0':
titre = "Nouveau doc"
# nouveau
doc = {}
doc['intitule'] = ''
doc['texte'] = ''
doc['topic'] = topic
doc['tag1'] = ''
doc['tag2'] = ''
doc['statut'] = 'private'
else:
titre = "Modifier : %s" % str(doc_id)
doc = get_docs(request, doc_id)
if 'form.submitted' in request.params:
new_values = {}
for param, db_value in doc.items():
if param in request.params and request.params[param] != db_value:
new_values[param] = request.params[param]
if new_values:
new_values['topic'] = topic
update_doc(request, doc_id, new_values)
if doc_id != '0':
return HTTPFound(location=request.route_url('doc_view', doc_id=doc_id))
else:
return HTTPFound(location=request.route_url('folder',topic=topic))
if 'form.deleted' in request.params:
if doc_id != '0':
delete_doc(request, doc_id)
request.session.flash(u"<%s> est supprimée avec succès." % doc.intitule, 'success')
return HTTPFound(location=request.route_url('folder',topic=topic))
return {
'page_title': titre,
'url': url,
'message': message,
'doc_id': doc_id,
'doc': doc,
'topic': topic,
'tags': tags,
'statuts': statuts,
}
@view_config(route_name='doc_search', renderer='../templates/contents/doc_search.pt')
def doc_search(request):
logged_in = request.authenticated_userid
critere = ''
docs = []
if 'form.submitted' in request.params:
critere = request.params['critere']
# si afficher tous les fiches ?
docs = get_docs_bycritere(request, critere, logged_in)
return {
'page_title': "Rechercher",
'docs': docs,
'critere': critere,
}
@view_config(route_name='doc_view', renderer='../templates/contents/doc_view.pt')
def doc_view(request):
doc_id = request.matchdict['doc_id']
# lire le document
doc = get_docs(request, doc_id)
# si anonyme, interdire de voir les docs privés
if isAnonymous(request) and doc.statut == 'private':
return HTTPFound(location=request.route_url('home'))
if doc_id == '2':
# mouvements portfolio, retour vers portfolio
url_retour = request.route_url('portfolio')
else:
url_retour = request.route_url('folder',topic=doc.topic)
tags = doc.tag1
if doc.tag2:
tags += ', ' + doc.tag2
# insèrer le path de static/img
texte = doc.texte.replace('static/img/', "%s/static/img/" % request.application_url)
# convertir mardown en HTML
from markdown2 import Markdown
markdowner = Markdown()
texte = markdowner.convert(texte)
return {
'page_title': doc.intitule,
'doc_id': doc_id,
'doc': doc,
'texte': texte,
'topic': doc.topic,
'tags': tags,
'url_retour':url_retour,
}