75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
# -*- coding: utf8 -*-
|
|
from sqlalchemy import text
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import (
|
|
scoped_session,
|
|
sessionmaker,
|
|
)
|
|
|
|
from zope.sqlalchemy import ZopeTransactionExtension, mark_changed
|
|
|
|
from datetime import *
|
|
import transaction
|
|
|
|
def execute_query(request, query, params):
|
|
"""Execute query and mark session as changed"""
|
|
request.dbsession.execute(query, params)
|
|
mark_changed(request.dbsession)
|
|
transaction.commit()
|
|
|
|
def get_docs(request, doc_id):
|
|
"""Lire les doc"""
|
|
if doc_id == 0:
|
|
query = "SELECT * FROM docs ORDER BY theme, intitule;"
|
|
results = request.dbsession.execute(query).fetchall()
|
|
elif doc_id == -1:
|
|
query = "SELECT * FROM docs where theme <> 'INTERNE' ORDER BY theme, intitule;"
|
|
results = request.dbsession.execute(query).fetchall()
|
|
else:
|
|
query = "SELECT * FROM docs where doc_id = :doc_id;"
|
|
results = request.dbsession.execute(query, {'doc_id': doc_id}).first()
|
|
return results
|
|
|
|
def update_doc(request, doc_id, intitule, texte, theme):
|
|
"""créér ou modifier le doc"""
|
|
if doc_id == '0':
|
|
query = "INSERT INTO docs (intitule, texte, theme) VALUES(:intitule, :texte, :theme);"
|
|
execute_query(request, query, {'intitule': intitule, 'texte': texte, 'theme': theme})
|
|
else:
|
|
query = "update docs set intitule=:intitule, texte=:texte, theme=:theme where doc_id = :doc_id;"
|
|
execute_query(request, query, {'doc_id': doc_id, 'intitule': intitule, 'texte': texte, 'theme': theme})
|
|
|
|
def delete_doc(request, doc_id):
|
|
"""supprimer la doc"""
|
|
query = "delete from docs where doc_id = :doc_id;"
|
|
results = request.dbsession.execute(query, {'doc_id': doc_id})
|
|
|
|
def get_categories(request, no_cat):
|
|
if no_cat == '0':
|
|
query = "SELECT * FROM categories ORDER BY categorie"
|
|
results = request.dbsession.execute(query).fetchall()
|
|
else:
|
|
# lire le actif par son identifiant
|
|
query = """SELECT * FROM categories WHERE no_cat=:no_cat;"""
|
|
results = request.dbsession.execute(query, {'no_cat': no_cat}).first()
|
|
return results
|
|
|
|
def update_categorie(request, no_cat, new_values):
|
|
# formater les champs
|
|
s = ''
|
|
for param in new_values.keys():
|
|
if s:
|
|
s += ",%s=:%s" % (param, param)
|
|
else:
|
|
s = "%s=:%s" % (param, param)
|
|
|
|
if no_cat == '0':
|
|
query = "INSERT INTO categories SET %s" % s
|
|
else:
|
|
new_values['no_cat'] = no_cat
|
|
query = "UPDATE categories SET %s WHERE no_cat = :no_cat;" % s
|
|
execute_query(request, query, new_values)
|
|
|
|
def delete_categorie(request, no_cat):
|
|
query = "DELETE FROM categories WHERE no_cat = :no_cat ;"
|
|
execute_query(request, query, {'no_cat': no_cat}) |