# -*- 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 pyramid_mailer import get_mailer from pyramid_mailer.message import Message, Attachment from sqlalchemy.exc import DBAPIError from ..security import groupfinder from ..models.default import * from ..models.members import ( get_member_by_email ) # import datetime import time import hashlib import json import yfinance as yf 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('.', ',') + " €") 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, d): """Takes a float and returns a string""" if x == 0: pc = '' elif d == 2: pc = "%.2f " % x elif d == 3: pc = "%.3f " % x else: pc = "%.1f " % x if len(pc) > 0: pc += "%" return pc.replace('.', ',') @view_config(route_name='home', renderer='../templates/home.pt') def home(request): member = get_member_by_email(request, 'ctphuoc@gmail.com') id_photo = member.photo_instagram # lire toutes les docs items = get_docs_bytheme(request, 'BLOG') return { 'page_title': "Un geek à la retraite", 'items': items, 'id_photo': id_photo, } @view_config(route_name='archives', renderer='../templates/archives.pt') def archives(request): theme = 'blog' if 'theme' in request.params: theme = request.params["theme"] # lire la table des themes themes = get_docs_themes(request) # lire toutes les docs du theme docs = get_docs_bytheme(request, theme) return { 'page_title': "Archives de %s" % theme, 'docs': docs, 'themes': themes, 'theme': theme, } @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 = "" themes = get_docs_themes(request) tags = get_docs_tags(request) if doc_id == '0': titre = "Nouveau doc" # nouveau doc = {} doc['intitule'] = '' doc['texte'] = '' doc['theme'] = '' doc['tag1'] = '' doc['tag2'] = '' 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: 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('archives',theme=request.params['theme'])) 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('archives',theme=doc.theme)) return { 'page_title': titre, 'url': url, 'message': message, 'doc_id': doc_id, 'doc': doc, 'themes': themes, 'tags': tags, } @view_config(route_name='doc_search', renderer='../templates/doc_search.pt') def doc_search(request): critere = '' docs = [] if 'form.submitted' in request.params: critere = request.params['critere'] # si afficher tous les fiches ? docs = get_docs_bycritere(request, critere) return { 'page_title': "Rechercher", 'docs': docs, 'critere': critere, } @view_config(route_name='apropos', renderer='../templates/apropos.pt') def apropos(request): return { 'page_title': "A propos", } @view_config(route_name='doc_view', renderer='../templates/doc_view.pt') def doc_view(request): logged_in = request.authenticated_userid doc_id = request.matchdict['doc_id'] doc = get_docs(request, doc_id) if logged_in == None and doc.theme == 'memo': # si anonyme, interdire de voir les memo return HTTPFound(location=request.route_url('home')) url_retour = request.route_url('archives',theme=doc.theme) 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, 'texte': texte, 'modif_le': doc.modif_le.strftime('%d/%m/%Y'), 'tags': tags, 'url_retour':url_retour } def envoyerMail(request, destinataire, objet, corps): body = """ %s Cordialement, monaa.caotek.fr """ % (corps) message = Message(subject=u"[Mes Avoirs] %s" % objet, sender=request.registry.settings['caotek_mesavoirs.admin_email'], body=body) message.add_recipient(destinataire) mailer = get_mailer(request) mailer.send_immediately(message)