diff --git a/advlabdb/forms.py b/advlabdb/forms.py index fcdceb5..e69de29 100644 --- a/advlabdb/forms.py +++ b/advlabdb/forms.py @@ -1,24 +0,0 @@ -from flask_wtf import FlaskForm -from wtforms import StringField, PasswordField, SubmitField, BooleanField -from wtforms.validators import DataRequired, Email, ValidationError -from advlabdb.models import User - - -class RegistrationForm(FlaskForm): - email = StringField("Email", - validators=[DataRequired(), Email()]) - assistant = BooleanField("Assistant", default=True) - admin = BooleanField("Admin", default=False) - submit = SubmitField("Register") - - def validate_email(form, field): - user = User.query.filter_by(email=field.data.lower()).first() - if user: - raise ValidationError("A user with this email address is already registered!") - - -class LoginForm(FlaskForm): - email = StringField("Email", - validators=[DataRequired(), Email()]) - password = PasswordField("Password", validators=[DataRequired()]) - submit = SubmitField("Login") diff --git a/advlabdb/modelViews.py b/advlabdb/modelViews.py index 6e005d6..43c10ed 100644 --- a/advlabdb/modelViews.py +++ b/advlabdb/modelViews.py @@ -4,7 +4,7 @@ from flask_admin.menu import MenuLink from flask_security import hash_password from wtforms import BooleanField -from advlabdb import admin, app, user_datastore +from advlabdb import admin, app, user_datastore, db from advlabdb.models import * from advlabdb.utils import randomPassword @@ -25,16 +25,39 @@ class UserModelView(SecureModelView): if "admin" in roles: flash("You have registered a new admin!", "danger") - newUser = user_datastore.create_user(email=email, password=passwordHash, roles=roles) + model = user_datastore.create_user(email=email, password=passwordHash, roles=roles) db.session.commit() flash(f"{email} registered with roles: {', '.join([role.name for role in form.roles.data])}.", category="success") flash(f"Random password: {password}", category="warning") - return newUser + return model class RoleModelView(SecureModelView): column_exclude_list = ["update_datetime"] + +class SemesterModelView(SecureModelView): + column_list = ["label", "parts"] + form_columns = ["label", "create_parts"] + + parts = ["A/1", "A/2", "B/1", "B/2"] + + form_extra_fields = { + "create_parts": BooleanField("Create parts:" + ", ".join(parts) + ".", default=True) + } + + def after_model_change(self, form, model, is_created): + if is_created and form.create_parts.data: + for part in self.parts: + db.session.add(Part(label=part, semester=model)) + db.session.commit() + + +class PartModelView(SecureModelView): + can_view_details = True + column_details_list = ["label", "semester", "part_experiments", "part_students", "groups"] + form_columns = ["label", "semester"] + admin.add_view(SecureModelView(Student, db.session)) admin.add_view(SecureModelView(PartStudent, db.session)) admin.add_view(SecureModelView(Group, db.session)) @@ -43,8 +66,8 @@ admin.add_view(SecureModelView(Experiment, db.session)) admin.add_view(SecureModelView(PartExperiment, db.session)) admin.add_view(SecureModelView(Assistant, db.session)) admin.add_view(SecureModelView(Appointment, db.session)) -admin.add_view(SecureModelView(Part, db.session)) -admin.add_view(SecureModelView(Semester, db.session)) +admin.add_view(PartModelView(Part, db.session)) +admin.add_view(SemesterModelView(Semester, db.session)) admin.add_view(SecureModelView(ExperimentMark, db.session)) admin.add_view(UserModelView(User, db.session)) admin.add_view(RoleModelView(Role, db.session)) diff --git a/advlabdb/models.py b/advlabdb/models.py index fa4abcf..a4dfcb2 100644 --- a/advlabdb/models.py +++ b/advlabdb/models.py @@ -25,7 +25,7 @@ class Student(db.Model): part_students = db.relationship("PartStudent", backref="student", lazy=True) def __repr__(self): - return f"<{self.first_name} {self.last_name}>" + return f"" class PartStudent(db.Model): @@ -45,6 +45,9 @@ class Group(db.Model): part_students = db.relationship("PartStudent", backref="group", lazy=True) group_experiments = db.relationship("GroupExperiment", backref="group", lazy=True) + def __repr__(self): + return f"" + class GroupExperiment(db.Model): # An experiment specified to a group @@ -71,7 +74,7 @@ class Experiment(db.Model): part_experiments = db.relationship("PartExperiment", backref="experiment", lazy=True) def __repr__(self): - return f"<{self.number}>" + return f"" # Helper table for the many to many relationship between Assistant and PartExperiment @@ -106,7 +109,7 @@ class Assistant(db.Model): experiment_marks = db.relationship("ExperimentMark", backref="assistant", lazy=True) def __repr__(self): - return f"<{self.first_name} {self.last_name}>" + return f"" class Appointment(db.Model): @@ -125,6 +128,9 @@ class Part(db.Model): part_students = db.relationship("PartStudent", backref="part", lazy=True) groups = db.relationship("Group", backref="part", lazy=True) + def __repr__(self): + return f"" + class Semester(db.Model): id = db.Column(db.Integer, primary_key=True) @@ -150,7 +156,7 @@ class User(db.Model, FsUserMixin): assistant = db.relationship("Assistant", backref="user", lazy=True, uselist=False) def __repr__(self): - return f"<{self.email}>" + return f"" class Role(db.Model, FsRoleMixin):