added actif_edit template

This commit is contained in:
2023-01-30 15:53:59 +01:00
parent f8023701a4
commit e60212239e
7 changed files with 364 additions and 8 deletions

View File

@@ -3,12 +3,13 @@ from pyramid.view import (
)
from pyramid.httpexceptions import HTTPFound
from ..services.portfolio import PFService
from ..forms import AllocationForm, HistoForm
from ..models.portfolio import Histo, Allocation
from ..forms import ActifForm, Actif2Form, AllocationForm, HistoForm
from ..models.portfolio import Actifs, Histo, Allocation
import datetime #<- will be used to set default dates on models
import yfinance as yf
import json
import html
@view_config(route_name='portfolio', renderer='../templates/portfolio/portfolio.jinja2', permission='view')
def portfolio(request):
@@ -113,7 +114,7 @@ def portfolio(request):
@view_config(route_name='histo_list', renderer='../templates/portfolio/histo_list.jinja2', permission='view')
def histo_list(request):
# lire l historique
items = PFService.get_histo(request, '0')
items = PFService.get_histo(request, '-1')
return {
'page_title': 'Historique des parts',
@@ -213,3 +214,145 @@ def histo_edit(request):
'form': form,
'item': entry,
}
@view_config(route_name='actif_edit', renderer='../templates/portfolio/actif_edit.jinja2', permission='view')
def actif_edit(request):
no_id = request.matchdict['no_id']
url = request.route_url('actif_edit', no_id=no_id)
# get the list of classes
classes = PFService.get_allocation_byType(request, 'ACTION')
if no_id == '0':
# create a new allocation
entry = Actifs()
entry.pru = 0
entry.cours = 0
entry.pc_rdt = 0
entry.ter = 0
form = ActifForm(request.POST, entry)
form.classe.choices = [(row.classe, row.classe) for row in classes]
page_title = "Nouvel actif"
else:
# modify post
entry = PFService.get_actifs(request, no_id)
if not entry:
request.session.flash(u"Actif non trouvé : %s" % no_id, 'warning')
return HTTPFound(location=request.route_url('portfolio'))
form = ActifForm(request.POST, entry)
form.classe.choices = [(row.classe, row.classe) for row in classes]
page_title = "Modifier Actif : " + entry.symbole
if 'form.submitted' in request.params :
if no_id == '0':
form.populate_obj(entry)
# récupérer le cours du symbole de Yahoo finance
ticker = yf.Ticker(entry.symbole)
entry.cours = ticker.fast_info.last_price
entry.devise = ticker.info.get('currency')
entry.libelle = html.unescape(ticker.info.get('shortName'))
# raccourcir le libelle
entry.libelle = entry.libelle.replace('UCITS ','')
entry.libelle = entry.libelle.replace('World U','World')
entry.pc_allocation = 1.0
entry.valeur = float(entry.cours) * entry.parite * entry.nombre;
entry.plus_value = entry.valeur - float(entry.pru * entry.nombre);
entry.pc_plusvalue = entry.plus_value / entry.valeur * 100;
entry.rendement = entry.valeur * float(entry.pc_rdt) / 100;
request.dbsession.add(entry)
return HTTPFound(location=request.route_url('portfolio'))
else:
del form.no_id # SECURITY: prevent overwriting of primary key
form.populate_obj(entry)
# récupérer le cours du symbole de Yahoo finance
ticker = yf.Ticker(entry.symbole)
entry.cours = ticker.fast_info.last_price
entry.devise = ticker.info.get('currency')
entry.libelle = html.unescape(ticker.info.get('shortName'))
# raccourcir le libelle
entry.libelle = entry.libelle.replace('UCITS ','')
entry.libelle = entry.libelle.replace('World U','World')
entry.valeur = float(entry.cours) * entry.parite * entry.nombre;
entry.plus_value = entry.valeur - float(entry.pru * entry.nombre);
entry.pc_plusvalue = entry.plus_value / entry.valeur * 100;
entry.rendement = entry.valeur * float(entry.pc_rdt) / 100;
return HTTPFound(location=request.route_url('portfolio'))
if 'form.deleted' in request.params:
PFService.delete_actif(request, entry.no_id)
request.session.flash("La fiche a été supprimée avec succès.", 'success')
return HTTPFound(location=request.route_url('portfolio'))
return {
'page_title': page_title,
'url': url,
'form': form,
'item': entry,
}
@view_config(route_name='actif2_edit', renderer='../templates/portfolio/actif2_edit.jinja2', permission='view')
def actif2_edit(request):
no_id = request.matchdict['no_id']
url = request.route_url('actif2_edit', no_id=no_id)
# get the list of classes
classes = PFService.get_allocation_byType(request, 'AUTRE')
if no_id == '0':
# create a new allocation
entry = Actifs()
entry.pru = 0
entry.cours = 0
entry.pc_rdt = 0
entry.ter = 0
entry.devise = 'EUR'
form = Actif2Form(request.POST, entry)
form.classe.choices = [(row.classe, row.classe) for row in classes]
page_title = "Nouvel actif"
else:
# modify post
entry = PFService.get_actifs(request, no_id)
if not entry:
request.session.flash(u"Actif non trouvé : %s" % no_id, 'warning')
return HTTPFound(location=request.route_url('portfolio'))
form = Actif2Form(request.POST, entry)
form.classe.choices = [(row.classe, row.classe) for row in classes]
page_title = "Modifier Actif : " + entry.symbole
if 'form.submitted' in request.params :
if no_id == '0':
form.populate_obj(entry)
entry.nombre = 1000
entry.devise = 'EUR'
entry.parite = 1.0
entry.pc_allocation = 1.0
entry.valeur = float(entry.cours) * entry.parite * entry.nombre;
entry.plus_value = entry.valeur - float(entry.pru * entry.nombre);
entry.pc_plusvalue = entry.plus_value / entry.valeur * 100;
entry.rendement = entry.valeur * float(entry.pc_rdt) / 100;
request.dbsession.add(entry)
return HTTPFound(location=request.route_url('portfolio'))
else:
del form.no_id # SECURITY: prevent overwriting of primary key
form.populate_obj(entry)
entry.valeur = float(entry.cours) * entry.parite * entry.nombre;
entry.plus_value = entry.valeur - float(entry.pru * entry.nombre);
entry.pc_plusvalue = entry.plus_value / entry.valeur * 100;
entry.rendement = entry.valeur * float(entry.pc_rdt) / 100;
return HTTPFound(location=request.route_url('portfolio'))
if 'form.deleted' in request.params:
PFService.delete_actif(request, entry.no_id)
request.session.flash("La fiche a été supprimée avec succès.", 'success')
return HTTPFound(location=request.route_url('portfolio'))
return {
'page_title': page_title,
'url': url,
'form': form,
'item': entry,
}