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

Done Semester and Part views

This commit is contained in:
Mo 2021-04-24 13:38:03 +02:00
parent f4de3df51d
commit ff0f5cc48c
3 changed files with 38 additions and 33 deletions

View file

@ -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")

View file

@ -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))

View file

@ -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"<Student: {self.first_name} {self.last_name}>"
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"<Group {self.number} {Part.query.get(self.part_id)}>"
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"<Exp {self.number}>"
# 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"<Assistant: {self.first_name} {self.last_name}>"
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"<Part {self.label} {Semester.query.get(self.semester_id).label}>"
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"<User {self.email}>"
class Role(db.Model, FsRoleMixin):