diff --git a/app/rollerverbrauch/__init__.py b/app/rollerverbrauch/__init__.py index a815566..3a79605 100644 --- a/app/rollerverbrauch/__init__.py +++ b/app/rollerverbrauch/__init__.py @@ -31,13 +31,15 @@ from rollerverbrauch.forms import \ SelectVehicleForm, \ DeleteAccountForm, \ DeletePitStopForm, \ - EditPitstopForm + EditPitstopForm, \ + CreateConsumableForm from rollerverbrauch.entities import \ User, \ Role, \ Pitstop, \ - Vehicle + Vehicle, \ + Consumable # required to activate the filters import rollerverbrauch.filters @@ -65,6 +67,11 @@ def user_registered_sighandler(app, user, confirm_token): @app.before_first_request def before_first_request(): db.create_all() + print(""" + ========================= + TEST + ======================== + """) user_datastore.find_or_create_role(name='admin', description='Role for administrators') user_datastore.find_or_create_role(name='user', description='Role for all users.') db.session.commit() @@ -105,6 +112,7 @@ def index(): def edit_vehicle(vid): vehicle = Vehicle.query.filter(Vehicle.id == vid).first() form = EditVehicleForm() + #form.consumables.choices = [(g.id, '%s (%s)' % (g.name, g.unit)) for g in Consumable.query.all()] if form.validate_on_submit(): if not tools.check_vehicle_name_is_unique(current_user, form.name): @@ -115,8 +123,10 @@ def edit_vehicle(vid): return redirect(url_for('get_account_page')) form.name.default = vehicle.name + #form.consumables.default = [g.id for g in vehicle.consumables] + #print(form.consumables.name) form.process() - return render_template('editVehicleForm.html', form=form) + return render_template('editVehicleForm.html', form=form, vehicle=vehicle) @app.route('/account/delete_vehicle/', methods=['GET', 'POST']) @@ -287,8 +297,24 @@ def get_manual(): @app.route('/admin', methods=['GET']) @roles_required('admin') def get_admin_page(): - g.data['users'] = User.query.all() - return render_template('admin.html', data=g.data) + users = User.query.all() + consumables = Consumable.query.all() + return render_template('admin.html', users=users, consumables=consumables) + + +@app.route('/admin/create_consumable', methods=['GET', 'POST']) +@login_required +def create_consumable(): + form = CreateConsumableForm() + + if form.validate_on_submit(): + new_consumable = Consumable(form.name.data, form.unit.data) + db.session.add(new_consumable) + db.session.commit() + tools.db_log_add(new_consumable) + return redirect(url_for('get_admin_page')) + + return render_template('createConsumableForm.html', form=form) @app.route('/account', methods=['GET']) diff --git a/app/rollerverbrauch/entities.py b/app/rollerverbrauch/entities.py index 0a72a29..83e5cd7 100644 --- a/app/rollerverbrauch/entities.py +++ b/app/rollerverbrauch/entities.py @@ -5,6 +5,10 @@ roles_users = db.Table('roles_users', db.Column('user_id', db.Integer(), db.ForeignKey('user.id')), db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))) +vehicles_consumables = db.Table('vehicles_consumables', + db.Column('vehicle_id', db.Integer(), db.ForeignKey('vehicle.id')), + db.Column('consumable_id', db.Integer(), db.ForeignKey('consumable.id'))) + class Role(db.Model, RoleMixin): id = db.Column(db.Integer(), primary_key=True) @@ -44,6 +48,11 @@ class Vehicle(db.Model): pitstops = db.relationship( 'Pitstop' ) + consumables = db.relationship( + 'Consumable', + secondary=vehicles_consumables, + backref=db.backref('consumes', lazy='dynamic') + ) __table_args__ = (db.UniqueConstraint('owner_id', 'name', name='_owner_name_uniq'),) def __init__(self, name): @@ -68,4 +77,18 @@ class Pitstop(db.Model): self.costs = costs def __repr__(self): - return '' % (self.odometer, self.litres, self.date, self.vehicle_id) \ No newline at end of file + return '' % \ + (self.odometer, self.litres, self.date, self.vehicle_id) + + +class Consumable(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(255)) + unit = db.Column(db.String(255)) + + def __init__(self, name, unit): + self.name = name + self.unit = unit + + def __repr__(self): + return '' % (self.name, self.unit) diff --git a/app/rollerverbrauch/forms.py b/app/rollerverbrauch/forms.py index c6bb7c2..60efdb9 100644 --- a/app/rollerverbrauch/forms.py +++ b/app/rollerverbrauch/forms.py @@ -1,5 +1,5 @@ from flask_wtf import Form -from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField +from wtforms import DateField, IntegerField, DecimalField, StringField, SelectField, SubmitField, SelectMultipleField, BooleanField from wtforms.validators import ValidationError, Length from datetime import date @@ -31,6 +31,7 @@ def edit_costs_check(form, field): if costs_check_required and field.data is not None and field.data <= 0: raise ValidationError('Costs must be above 0.01 €.') + class SelectVehicleForm(Form): vehicle = SelectField('Vehicle', coerce=int) submit = SubmitField(label='Do it!') @@ -50,6 +51,7 @@ class CreatePitstopForm(Form): class EditVehicleForm(Form): name = StringField('Name', validators=[Length(1, 255)]) + #consumables = SelectMultipleField('Consumables') submit = SubmitField(label='Do it!') @@ -77,3 +79,8 @@ class EditPitstopForm(Form): self.last_pitstop = last_pitstop +class CreateConsumableForm(Form): + name = StringField('Name', validators=[Length(1, 255)]) + unit = StringField('Unit', validators=[Length(1, 255)]) + submit = SubmitField(label='Do it!') + diff --git a/app/rollerverbrauch/templates/admin.html b/app/rollerverbrauch/templates/admin.html index 8b25d30..4323d83 100644 --- a/app/rollerverbrauch/templates/admin.html +++ b/app/rollerverbrauch/templates/admin.html @@ -1,18 +1,52 @@ {% extends "layout.html" %} {% block body %} -
-
+

Admin

+
Users
-

Admin

- We have {{ data.users|length }} users so far: + We have {{ users|length }} users so far:
    - {% for user in data.users %} + {% for user in users %}
  • {{user.email}}
  • {% endfor %} -
-
+ +
+
Consumables
+ + + + + + + + + {% for consumable in consumables %} + + + + + + {% endfor %} + +
+ Name + + Unit + + Actions +
+ {{ consumable.name }} + + {{ consumable.unit }} + +
+
+ {% endblock %}