1
0
Fork 0
mirror of https://codeberg.org/Mo8it/AdvLabDB.git synced 2024-12-20 23:41:20 +00:00

Fixed adding students to a group

This commit is contained in:
Mo 2021-07-29 23:18:24 +02:00
parent 1787a84d30
commit 58d1ec73b5

View file

@ -3,7 +3,7 @@ from flask_admin.contrib.sqla.filters import BaseSQLAFilter
from flask_admin.menu import MenuLink from flask_admin.menu import MenuLink
from flask_admin.model.template import EndpointLinkRowAction from flask_admin.model.template import EndpointLinkRowAction
from flask_security import current_user, hash_password, admin_change_password from flask_security import current_user, hash_password, admin_change_password
from sqlalchemy import func from sqlalchemy import func, or_
from wtforms import Form, BooleanField, SelectField, TextField, RadioField from wtforms import Form, BooleanField, SelectField, TextField, RadioField
from wtforms.validators import DataRequired, Email, Optional, URL from wtforms.validators import DataRequired, Email, Optional, URL
from flask_admin.contrib.sqla.fields import QuerySelectMultipleField, QuerySelectField from flask_admin.contrib.sqla.fields import QuerySelectMultipleField, QuerySelectField
@ -297,17 +297,28 @@ class PartStudentView(SecureModelView):
return super().update_model(form, model) return super().update_model(form, model)
def partStudentsQueryFactory(): def partStudentQueryFactory():
return PartStudent.query.filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts])) return PartStudent.query.filter(PartStudent.part_id.in_([part.id for part in userActiveSemester().parts]))
class GroupView(SecureModelView): class GroupView(SecureModelView):
class CreateForm(Form): def formFactory(create, group):
part_students = QuerySelectMultipleField( if create:
"Part Students", query_factory=partStudentsQueryFactory, validators=[DataRequired()]
)
form = CreateForm def query_factory():
return partStudentQueryFactory().filter(PartStudent.group == None)
else:
def query_factory():
return partStudentQueryFactory().filter(or_(PartStudent.group == None, PartStudent.group == group))
class CustomForm(Form):
part_students = QuerySelectMultipleField(
"Part Students", query_factory=query_factory, validators=[DataRequired()]
)
return CustomForm
column_list = ["number", "semester", "program", "part_students", "group_experiments"] column_list = ["number", "semester", "program", "part_students", "group_experiments"]
column_filters = ["number", "semester", "program"] column_filters = ["number", "semester", "program"]
@ -338,6 +349,14 @@ class GroupView(SecureModelView):
else: else:
return super().update_model(form, model) return super().update_model(form, model)
def create_form(self, obj=None):
form = GroupView.formFactory(create=True, group=None)
return form(get_form_data(), obj=obj)
def edit_form(self, obj=None):
form = GroupView.formFactory(create=False, group=obj)
return form(get_form_data(), obj=obj)
class ExperimentView(SecureModelView): class ExperimentView(SecureModelView):
can_view_details = True can_view_details = True
@ -623,7 +642,7 @@ class ExperimentMarkView(SecureModelView):
class CreateForm(Form): class CreateForm(Form):
part_student = QuerySelectField( part_student = QuerySelectField(
"Part Student", "Part Student",
query_factory=partStudentsQueryFactory, query_factory=partStudentQueryFactory,
validators=[DataRequired()], validators=[DataRequired()],
allow_blank=True, allow_blank=True,
blank_text="-", blank_text="-",