From ab1eded4b7f12087d897390ee8fd013bf105f212 Mon Sep 17 00:00:00 2001 From: Mo8it Date: Wed, 18 Aug 2021 20:06:18 +0200 Subject: [PATCH] Fixed filtering for GroupView's form --- advlabdb/adminModelViews.py | 9 +++++++-- advlabdb/models.py | 6 +----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/advlabdb/adminModelViews.py b/advlabdb/adminModelViews.py index 548de33..c7907ef 100644 --- a/advlabdb/adminModelViews.py +++ b/advlabdb/adminModelViews.py @@ -5,7 +5,7 @@ from flask_admin.helpers import get_form_data from flask_admin.menu import MenuLink from flask_admin.model.template import EndpointLinkRowAction from flask_security import admin_change_password, current_user, hash_password -from sqlalchemy import func, or_ +from sqlalchemy import func, or_, and_ from wtforms import BooleanField, Form, RadioField, SelectField, TextField from wtforms.fields.html5 import DateField from wtforms.validators import URL, DataRequired, Email, NumberRange, Optional @@ -371,7 +371,12 @@ class GroupView(SecureAdminModelView): else: def query_factory(): - return partStudentQueryFactory().filter(or_(PartStudent.group == None, PartStudent.group == group)) + return partStudentQueryFactory().filter( + or_( + and_(PartStudent.group == None, PartStudent.part.has(Part.program == group.program)), + PartStudent.group == group, + ) + ) class CustomForm(Form): part_students = QuerySelectMultipleField( diff --git a/advlabdb/models.py b/advlabdb/models.py index 6e00f72..18c19a3 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -95,18 +95,14 @@ class Group(db.Model): def check(part_students, program=None): commonProgram = part_students[0].part.program + if program and program != commonProgram: raise DataBaseException("Group's program and students' program mismatch!") - commonSemester = part_students[0].part.semester - for partStudent in part_students[1:]: if partStudent.part.program != commonProgram: raise DataBaseException(f"Part Students {part_students} are not in the same program!") - if partStudent.part.semester != commonSemester: - raise DataBaseException(f"Part Students {part_students} are not in the same semester!") - def customInit(part_students): Group.check(part_students)