1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-09-19 18:31:16 +00:00

Add step to weighting fields

This commit is contained in:
Mo 2022-04-04 18:47:53 +02:00
parent c43382ac7a
commit 34ee938636
2 changed files with 17 additions and 8 deletions

View file

@ -16,7 +16,6 @@ from wtforms import Form
from wtforms.fields import (
BooleanField,
DateField,
DecimalField,
IntegerField,
RadioField,
SelectField,
@ -27,6 +26,7 @@ from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional
from advlabdb import adminSpace, app, assistantSpace, db, user_datastore
from advlabdb.configUtils import getConfig
from advlabdb.customClasses import (
CustomDecimalFieldFactory,
CustomIdEndpointLinkRowAction,
SecureAdminBaseView,
SecureAdminModelView,
@ -696,21 +696,21 @@ class SemesterExperimentView(SecureAdminModelView):
blank_text="-",
)
oral_weighting = DecimalField(
oral_weighting = CustomDecimalFieldFactory(0.01)(
"Oral weighting",
validators=[DataRequired(), NumberRange(min=0, max=1)],
default=0.5,
description="Between 0 and 1",
places=2,
)
protocol_weighting = DecimalField(
protocol_weighting = CustomDecimalFieldFactory(0.01)(
"Protocol weighting",
validators=[DataRequired(), NumberRange(min=0, max=1)],
default=0.5,
description="Between 0 and 1. Oral and protocol weightings have to add to 1! Both are rounded to 2 decimal digits.",
places=2,
)
final_weighting = DecimalField(
final_weighting = CustomDecimalFieldFactory(0.01)(
"Final weighting",
validators=[DataRequired(), NumberRange(min=0, max=1)],
default=1.0,
@ -772,10 +772,10 @@ class SemesterExperimentView(SecureAdminModelView):
model.checkAndRoundWeightings()
def update_model(self, form, model):
weightingsChanged = not (
form.oral_weighting.data == model.oral_weighting
and form.protocol_weighting.data == model.protocol_weighting
and form.final_weighting.data == model.final_weighting
weightingsChanged = (
form.oral_weighting.data != model.oral_weighting
or form.protocol_weighting.data != model.protocol_weighting
or form.final_weighting.data != model.final_weighting
)
updateSuccessful = super().update_model(form, model)

View file

@ -5,6 +5,8 @@ from flask_admin.model.helpers import get_mdict_item_or_list
from flask_admin.model.template import EndpointLinkRowAction
from flask_security import current_user
from sqlalchemy import and_
from wtforms.fields import DecimalField
from wtforms.widgets import NumberInput
from advlabdb.exceptions import DataBaseException, ModelViewException
from advlabdb.models import (
@ -269,3 +271,10 @@ class CustomIdEndpointLinkRowAction(EndpointLinkRowAction):
def render(self, context, row_id, row):
return super().render(context, self.customId(row), row)
def CustomDecimalFieldFactory(step):
class CustomDecimalField(DecimalField):
widget = NumberInput(step=step)
return CustomDecimalField