243 lines
6.9 KiB
Python
243 lines
6.9 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.security import (
|
|
authenticated_userid,
|
|
remember,
|
|
forget,
|
|
)
|
|
from pyramid.httpexceptions import (
|
|
HTTPFound,
|
|
HTTPNotFound,
|
|
HTTPForbidden,
|
|
)
|
|
from pyramid_mailer import get_mailer
|
|
from pyramid_mailer.message import Message, Attachment
|
|
from datetime import *
|
|
import hashlib
|
|
from docutils.core import publish_parts
|
|
from yahoo_finance import Share, Currency
|
|
|
|
from sqlalchemy.exc import DBAPIError
|
|
from ..security import groupfinder
|
|
|
|
from ..models.default import *
|
|
from ..models.actifs import (
|
|
get_actifs,
|
|
)
|
|
from ..models.members import (
|
|
get_member_by_email,
|
|
)
|
|
|
|
import json
|
|
|
|
def to_decimal(x):
|
|
import decimal
|
|
return decimal.Decimal(str(x))
|
|
|
|
def to_euro(x):
|
|
"""Takes a float and returns a string"""
|
|
#if x == 0:
|
|
# return ""
|
|
#else:
|
|
return (u"{:,.2f}".format(x).replace(',', ' ').replace('.', ',') + u" €")
|
|
|
|
def to_usd(x):
|
|
"""Takes a float and returns a string"""
|
|
#if x == 0:
|
|
# return ""
|
|
#else:
|
|
return (u"%.2f $" % x).replace('.', ',')
|
|
|
|
|
|
def to_int(x):
|
|
try:
|
|
number = int(x.replace(',', '.'))
|
|
return number
|
|
except ValueError:
|
|
return 0
|
|
|
|
def to_percent(x):
|
|
"""Takes a float and returns a string"""
|
|
return (u"%.1f " % x).replace('.', ',') + "%"
|
|
|
|
|
|
@view_config(route_name='home', renderer='../templates/home.pt', permission='view')
|
|
def home(request):
|
|
message = ''
|
|
|
|
# lire les categories
|
|
items = get_categories(request, '0')
|
|
# calculer % total
|
|
total = 0
|
|
for item in items:
|
|
total += item.pc_cible
|
|
if total <> 100:
|
|
message = u'Attention, le total de votre répartition cible ne fait pas 100%.'
|
|
|
|
# lire les actifs
|
|
actifs = get_actifs(request, '0')
|
|
|
|
total_valeur = 0
|
|
total_pv = 0
|
|
for item in actifs:
|
|
total_valeur += item.valeur
|
|
total_pv += item.plus_value
|
|
total_pc_value = total_pv / total_valeur * 100
|
|
|
|
return {
|
|
'page_title': u"Allocation d'actifs",
|
|
'message': message,
|
|
'items': items,
|
|
'actifs': actifs,
|
|
'total_valeur': total_valeur,
|
|
'total_pv': total_pv,
|
|
'total_pc_value': total_pc_value,
|
|
}
|
|
|
|
@view_config(route_name='doc_list', renderer='../templates/doc_list.pt', permission='view')
|
|
def doc_list(request):
|
|
|
|
# lire toutes les docs
|
|
docs = get_docs(request, 0)
|
|
|
|
return {
|
|
'page_title': u"Documents",
|
|
'docs': docs,
|
|
}
|
|
|
|
@view_config(route_name='doc_edit', renderer='../templates/doc_edit.pt', permission='view')
|
|
def doc_edit(request):
|
|
doc_id = request.matchdict['doc_id']
|
|
url = request.route_url('doc_edit',doc_id=doc_id)
|
|
|
|
message = u""
|
|
if doc_id == '0':
|
|
titre = "Nouvelle doc"
|
|
intitule = u""
|
|
texte = u""
|
|
theme = u""
|
|
else:
|
|
titre = "Modifier la doc : %s" % str(doc_id)
|
|
doc = get_docs(request, doc_id)
|
|
intitule = doc.intitule
|
|
texte = doc.texte
|
|
theme = doc.theme
|
|
|
|
if 'form.submitted' in request.params:
|
|
intitule = request.params["intitule"]
|
|
texte = request.params["texte"]
|
|
theme = request.params["theme"]
|
|
|
|
if len(intitule) > 0 and len(texte) > 0:
|
|
update_doc(request, doc_id, intitule, texte, theme)
|
|
return HTTPFound(location=request.route_url('doc_list'))
|
|
else:
|
|
message = "Veuillez saisir un intitule et un texte."
|
|
|
|
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." % intitule, 'success')
|
|
return HTTPFound(location=request.route_url('doc_list'))
|
|
|
|
return {
|
|
'page_title': titre,
|
|
'url': url,
|
|
'message': message,
|
|
'doc_id': doc_id,
|
|
'intitule': intitule,
|
|
'texte': texte,
|
|
'theme': theme,
|
|
'themes': ["CONDUITE","EXAMEN","RESULTAT","INTERNE"],
|
|
}
|
|
|
|
@view_config(route_name='doc_view', renderer='../templates/doc_view.pt', permission='view')
|
|
def doc_view(request):
|
|
doc_id = request.matchdict['doc_id']
|
|
current_route_path = request.current_route_path()
|
|
|
|
doc = get_docs(request, doc_id)
|
|
intitule = doc.intitule
|
|
# insèrer le path de static/img
|
|
img_path = 'image:: %s/static/img/' % request.application_url
|
|
|
|
texte = doc.texte.replace('image:: static/img/', img_path)
|
|
# convertir reST en HTML
|
|
texte = publish_parts(texte, writer_name='html')['html_body']
|
|
return {
|
|
'page_title': intitule,
|
|
'texte': texte,
|
|
'doc_id': doc_id,
|
|
}
|
|
|
|
def envoyerMail(request, destinataire, objet, corps):
|
|
body = u"""
|
|
|
|
%s
|
|
|
|
Cordialement,
|
|
gestion.entreprise-dumas.com
|
|
|
|
""" % (corps)
|
|
|
|
message = Message(subject=u"[Mes Avoirs] %s" % objet,
|
|
sender=request.registry.settings['mondumas.admin_email'],
|
|
body=body)
|
|
message.add_recipient(destinataire)
|
|
mailer = get_mailer(request)
|
|
|
|
mailer.send_immediately(message)
|
|
|
|
|
|
@view_config(route_name='categorie_edit', renderer='../templates/categorie_edit.pt', permission='view')
|
|
def categorie_edit(request):
|
|
no_cat = request.matchdict['no_cat']
|
|
url = request.route_url('categorie_edit', no_cat=no_cat)
|
|
message = ''
|
|
types_list = ['ACTION', 'AUTRE']
|
|
|
|
if no_cat == '0':
|
|
# nouveau
|
|
categorie = {}
|
|
categorie['no_cat'] = '0'
|
|
categorie['categorie'] = ''
|
|
categorie['type'] = 'ACTION'
|
|
categorie['pc_cible'] = '0'
|
|
page_title= u'Nouvelle catégorie'
|
|
else:
|
|
# lire la fiche de la categorie
|
|
categorie = get_categories(request, no_cat)
|
|
if not categorie:
|
|
request.session.flash(u"Catégorie non trouvé : %s" % no_cat, 'warning')
|
|
return HTTPFound(location=request.route_url('home'))
|
|
page_title= u"Catégorie : %s" % (categorie.categorie)
|
|
|
|
if 'form.submitted' in request.params:
|
|
new_values = {}
|
|
for param, db_value in categorie.items():
|
|
if param in request.params and request.params[param] != db_value:
|
|
new_values[param] = request.params[param]
|
|
|
|
if new_values:
|
|
update_categorie(request, no_cat, new_values)
|
|
request.session.flash(u"La fiche a été mise à jour avec succès.", 'success')
|
|
return HTTPFound(location=request.route_url('home'))
|
|
|
|
|
|
if 'form.deleted' in request.params:
|
|
delete_categorie(request, no_cat)
|
|
request.session.flash(u"La fiche a été supprimée avec succès.", 'success')
|
|
return HTTPFound(location=request.route_url('home'))
|
|
|
|
return {
|
|
'page_title': page_title,
|
|
'url': url,
|
|
'categorie': categorie,
|
|
'types_list': types_list,
|
|
'message': message,
|
|
} |