Skip to content

Instantly share code, notes, and snippets.

@pascale64
Last active March 7, 2020 12:18
Show Gist options
  • Select an option

  • Save pascale64/b774de18c3deba47edca62fe7a6a882b to your computer and use it in GitHub Desktop.

Select an option

Save pascale64/b774de18c3deba47edca62fe7a6a882b to your computer and use it in GitHub Desktop.
from datetime import datetime
from flask import render_template, flash, redirect, url_for, request, g, \
jsonify, current_app
from bdf.books.forms import AddBookForm, SearchAuxForm, SearchCreditForm, SearchDateForm, SearchDebitForm,\
SearchJNForm, SearchMontantForm, SearchPIDForm, SearchREForm, SearchTPForm,\
EditBookForm, SearchCTForm, SearchDescriptionForm, EditBookForm AddBookRowForm,
AddachatForm
from flask_login import current_user, login_required
from flask_paginate import Pagination, get_page_args
from bdf import db
from bdf.models import Book, Period
from bdf.books import bp
from io import TextIOWrapper
import csv
def pid(date):
pid = Period.query.filter(Period.end>=date).first()
return pid.id
def BB_REF():
bb_ref = Book.query.order_by(Book.id.desc()).filter(Book.JN=='BB').first()
if not bb_ref:
return 0
else:
return 'B'+str(int(bb_ref.REF[1:])+1)
def AM_REF():
am_ref = Book.query.order_by(Book.id.desc()).filter(Book.TP=='AM').first()
if not am_ref:
return 0
else:
return 'B'+str(int(am_ref.REF[1:])+1)
def CA_REF():
ca_ref = Book.query.order_by(Book.id.desc()).filter(Book.TP=='C').first()
if not ca_ref:
return 0
else:
return 'C'+str(int(ca_ref.REF[1:])+1)
def BQ_REF():
bq_ref = Book.query.order_by(Book.id.desc()).filter(Book.TP=='B').first()
if not bq_ref:
return 0
else:
return 'BQ'+str(int(bq_ref.REF[2:])+1)
def ref(euros):
ref = Book.query.order_by(Book.id.desc()).Filter(Book.montant==euros).first()
if not ref:
return 0
else:
return ref.REF
def aux(euros):
aux = Book.query.order_by(Book.id.desc()).Filter(Book.montant==euros).first()
if not aux:
return 0
else:
return ref.AUX
@bp.route('/books', methods=['GET', 'POST'])
@login_required
def books():
date_form = SearchDateForm()
des_form = SearchDescriptionForm()
debit_form = SearchDebitForm()
credit_form = SearchCreditForm()
montant_form = SearchMontantForm()
aux_form = SearchAuxForm()
tp_form = SearchTPForm()
ref_form = SearchREForm()
jn_form = SearchJNForm()
pid_form = SearchPIDForm()
ct_form = SearchCTForm()
books = Book.query.order_by(Book.date.desc()).all()
def get_books(offset=0, per_page=20):
return books[offset: offset + per_page]
add_form = AddBookForm()
if add_form.validate_on_submit():
obj = Book(id=add_form.id.data, date=add_form.date.data, description=add_form.description.data,\
debit=add_form.debit.data, credit=add_form.credit.data, montant=add_form.montant.data,\
AUX=add_form.AUX.data, TP=add_form.TP.data, REF=add_form.REF.data, JN=add_form.JN.data,\
PID=add_form.PID.data, CT=add_form.CT.data)
db.session.add(obj)
db.session.commit()
return redirect(url_for('books.books'))
page, per_page, offset = get_page_args(page_parameter='page',
per_page_parameter='per_page')
total = len(books)
pagination_books = get_books(offset=offset, per_page=per_page)
pagination = Pagination(page=page, per_page=per_page, total=total)
return render_template('books/books.html', title='Books', add_form=add_form, date_form=date_form,
desc_form=desc_form, debit_form=debit_form, credit_form=credit_form,
montant_form=montant_form, aux_form=aux_form, tp_form=tp_form, ref_form=ref_form,
jn_form=jn_form, pid_form=pid_form, ct_form=ct_form, page=page,
books=pagination_books, per_page=per_page, pagination=pagination)
<form method="post" action="">
{{ form.hidden_tag() }}
{{ form.date}}
{{ form.description}}
{{ form.TP}}
{{ form.REF}}
{{ form.PID}}
{{ form.CT}}
<br/>
{% for entry in form.rows %}
{{ entry() }}
{% endfor %}
<input type="submit"/>
</form>
from flask import Flask, render_template
from flask_wtf import Form
from wtforms import StringField, FormField, FieldList, IntegerField, FloatField
from wtforms.validators import Optional
app = Flask(__name__)
app.secret_key = 'SCRATCH'
class Book(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.DateTime)
description = db.Column(String(255))
debit = db.Column(db.Integer, db.ForeignKey("general.id"))
credit = db.Column(db.Integer, db.ForeignKey("general.id"))
montant = db.Column(db.Float)
AUX = db.Column(db.String(6), db.ForeignKey("auxilliere.id"))
TP = db.Column(db.String(3), db.ForeignKey("type.id"))
REF = db.Column(db.String(6))
JN = db.Column(db.String(6), db.ForeignKey("journal.id"))
PID = db.Column(db.String(6), db.ForeignKey("period_id.id"))
CT = db.Column(db.Float)
Debit = db.relationship("General", foreign_keys=[debit])
Credit = db.relationship("General", foreign_keys=[credit])
AUX = db.relationship("Auxilliere")
Type = db.relationship("Type")
JN = db.relationship("Journal")
PID = db.relationship("Period_id")
def __repr__(self):
return '{}'.format(self.id)
class RowsForm(Form):
debit = IntegerField('Debit', validators=[Optional()])
credit = IntegerField('Credit', validators=[Optional()])
montant = FloatField('Montant', validators=[Optional()])
AUX = StringField('Auxilliare', validators=[Optional()])
class DataInputForm(Form):
date = StringField('date')
description = StringField('Description')
TP = StringField('Type')
REF = StringField('REF')
JN = StringField('JN')
PID = StringField('PID')
CT = IntegerField('CT')
rows = FieldList(FormField(RowsForm), min_entries=1, max_entries=5)
@app.route('/', methods=['post','get'])
def home():
form = DataInputForm()
if form.validate_on_submit():
book = Book(debit=form.debit.data)# totally lost here!!
for row_data in form.rows.data:
# totally lost here!!
db.session.add(book)
db.session.commit
return render_template('results.html', book=book)
print(form.errors)
return render_template('home.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
<ul>
{% for line in results %}
<li>{{ line }}</li>
{% endfor %}
</ul>
@pascale64
Copy link
Author

Hi, This is the start, but I am lost at the view function to take the data from the form and send it to the db. I want to be able to add row by row to the database each row will include "date", "description", "TP", "REF", "JN","PID" and "CT" plus each row of "debit", "credit", "montant" and "AUX" . All of this data needs to be added each row, so if I can fill out the first seven items on the form once and the fill in the four items of "row" for each different row, it will save me loads of time, but if a row "debit" is empty the loop stops, as there is a minimum of one row up to some times six rows. the file books.py is my present view function to add one row at a time.
Any ideas greatly needed.
Paul

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment