Rechentrainer/app.py
2021-06-06 14:12:43 +02:00

62 lines
2.2 KiB
Python

from flask import Flask, render_template, url_for, request, redirect, Response, abort, session
from flask_sqlalchemy import SQLAlchemy
from werkzeug.middleware.proxy_fix import ProxyFix
from datetime import datetime
import magic
import random
import string
app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app, x_proto=1, x_host=1)
app.secret_key = 'eng5iikeiwah3lae4idoo0woh4eiy6Th'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///homeschooling.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class rt_rechenreihen(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(200), nullable=False)
typ = db.Column(db.String(1), nullable=False)
class rt_reihe(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
set = db.Column(db.Integer, db.ForeignKey('rt_rechenreihen.id'), nullable=False)
a = db.Column(db.Integer, nullable=False)
b = db.Column(db.Integer, nullable=False)
t = db.relationship("rt_rechenreihen")
@app.context_processor
def get_date():
date = datetime.now()
return { "now": date.strftime("%Y-%m-%d") }
@app.route('/', methods = ['GET', 'POST'])
def index():
if request.method == 'POST':
alle = []
zufall = []
for r in request.form.getlist('rechenreihen'):
alle.extend(rt_reihe.query.filter_by(set = r).all())
if request.form.get('anzahl'):
count = (int(request.form.get('anzahl')) + 1)
else:
count = len(alle)
for i in range(1,count):
if len(alle) == 0:
break
term = alle.pop(random.randint(0,len(alle)-1))
term.c = ''
if request.form.get('gemischteAufgaben'):
maxValue = eval(str(term.a) + term.t.typ + str(term.b))
term.c += ' + ' if random.randint(0,1) else ' - '
term.c += str(random.randint(1,maxValue))
zufall.append(term)
return render_template('arbeitsblatt.html',aufgaben=zufall,maxPerPage=int(request.form.get('maxPerPage')))
else:
return render_template('generator.html', reihen=rt_rechenreihen)
if __name__ == "__main__":
app.run(debug=True)