mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
Added customCreateModel
This commit is contained in:
parent
189998c7d3
commit
6fab47591f
2 changed files with 76 additions and 104 deletions
|
@ -32,6 +32,7 @@ class SecureModelView(ModelView):
|
|||
edit_template = "admin_edit.html"
|
||||
|
||||
queryFilter = None
|
||||
customCreateModel = None
|
||||
|
||||
def is_accessible(self):
|
||||
return adminViewIsAccessible()
|
||||
|
@ -58,3 +59,24 @@ class SecureModelView(ModelView):
|
|||
return True
|
||||
|
||||
return super().handle_view_exception(exc)
|
||||
|
||||
def create_model(self, form):
|
||||
if not self.customCreateModel:
|
||||
return super().create_model(form)
|
||||
else:
|
||||
try:
|
||||
model = self.customCreateModel(form)
|
||||
|
||||
self.session.add(model)
|
||||
|
||||
self.on_model_change(form, model, True)
|
||||
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(str(ex), "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
self.after_model_change(form, model, True)
|
||||
|
||||
return model
|
||||
|
|
|
@ -60,8 +60,6 @@ class UserView(SecureModelView):
|
|||
blank_text="-",
|
||||
)
|
||||
|
||||
generate_new_password = None
|
||||
|
||||
class EditForm(CreateForm):
|
||||
generate_new_password = BooleanField("Generate new random password", default=False)
|
||||
|
||||
|
@ -82,16 +80,14 @@ class UserView(SecureModelView):
|
|||
flash(f"Random password: {password}", category="warning")
|
||||
|
||||
def create_model(self, form):
|
||||
try:
|
||||
password = randomPassword()
|
||||
hashedPassword = hash_password(password)
|
||||
|
||||
email = form.email.data.lower()
|
||||
|
||||
roles = [role.name for role in form.roles.data]
|
||||
if "admin" in roles:
|
||||
flash("You have registered a new admin!", "danger")
|
||||
|
||||
try:
|
||||
model = user_datastore.create_user(
|
||||
email=email, password=hashedPassword, roles=roles, active_semester=form.active_semester.data
|
||||
)
|
||||
|
@ -118,15 +114,18 @@ class UserView(SecureModelView):
|
|||
raise ModelViewException("Tried to delete yourself as user!")
|
||||
|
||||
def on_model_change(self, form, model, is_created):
|
||||
if not is_created:
|
||||
if model == current_user and not form.active.data:
|
||||
raise ModelViewException("Tried to deactiavte yourself as user!")
|
||||
|
||||
if form.generate_new_password and form.generate_new_password.data:
|
||||
if form.generate_new_password.data:
|
||||
password = randomPassword()
|
||||
|
||||
UserView.flashPassword(password)
|
||||
|
||||
admin_change_password(model, password, notify=False) # Password is automatically hashed with this method
|
||||
admin_change_password(
|
||||
model, password, notify=False
|
||||
) # Password is automatically hashed with this method
|
||||
|
||||
|
||||
class RoleView(SecureModelView):
|
||||
|
@ -163,9 +162,8 @@ class SemesterView(SecureModelView):
|
|||
column_details_list = column_list + ["semester_experiments", "active_users", "groups"]
|
||||
column_searchable_list = ["label", "year"]
|
||||
|
||||
def create_model(self, form):
|
||||
try:
|
||||
model = Semester.customInit(
|
||||
def customCreateModel(self, form):
|
||||
return Semester.customInit(
|
||||
label=form.label.data,
|
||||
year=form.year.data,
|
||||
oldSemester=userActiveSemester(),
|
||||
|
@ -173,17 +171,6 @@ class SemesterView(SecureModelView):
|
|||
transferAssistants=form.transfer_assistants.data,
|
||||
)
|
||||
|
||||
self.session.add(model)
|
||||
self.on_model_change(form, model, True)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(str(ex), "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
self.after_model_change(form, model, True)
|
||||
return model
|
||||
|
||||
def after_model_change(self, form, model, is_created):
|
||||
setUserActiveSemester(model.id)
|
||||
|
||||
|
@ -196,17 +183,30 @@ class SemesterView(SecureModelView):
|
|||
)
|
||||
|
||||
|
||||
def programQueryFactory():
|
||||
return Program.query
|
||||
|
||||
|
||||
class PartView(SecureModelView):
|
||||
can_view_details = True
|
||||
|
||||
column_sortable_list = []
|
||||
column_list = ["program", "number", "semester"]
|
||||
column_details_list = column_list + ["part_students"]
|
||||
form_columns = ["program", "number", "semester"]
|
||||
form_columns = ["program", "number"]
|
||||
|
||||
form_extra_fields = {
|
||||
"program": QuerySelectField(
|
||||
"Program", query_factory=programQueryFactory, validators=[DataRequired()], allow_blank=True, blank_text="-"
|
||||
)
|
||||
}
|
||||
|
||||
def queryFilter(self):
|
||||
return Part.semester == userActiveSemester()
|
||||
|
||||
def customCreateModel(self, form):
|
||||
return Part(program=form.program.data, number=form.number.data, semester=userActiveSemester())
|
||||
|
||||
|
||||
class StudentView(SecureModelView):
|
||||
can_view_details = True
|
||||
|
@ -326,20 +326,8 @@ class GroupView(SecureModelView):
|
|||
def queryFilter(self):
|
||||
return Group.semester == userActiveSemester()
|
||||
|
||||
def create_model(self, form):
|
||||
try:
|
||||
model = Group.customInit(form.part_students.data)
|
||||
|
||||
self.session.add(model)
|
||||
self.on_model_change(form, model, True)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(str(ex), "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
self.after_model_change(form, model, True)
|
||||
return model
|
||||
def customCreateModel(self, form):
|
||||
return Group.customInit(form.part_students.data)
|
||||
|
||||
def update_model(self, form, model):
|
||||
try:
|
||||
|
@ -359,9 +347,6 @@ class GroupView(SecureModelView):
|
|||
|
||||
|
||||
class ExperimentView(SecureModelView):
|
||||
def programQueryFactory():
|
||||
return Program.query
|
||||
|
||||
can_view_details = True
|
||||
|
||||
column_filters = ["active"]
|
||||
|
@ -420,24 +405,11 @@ class SemesterExperimentView(SecureModelView):
|
|||
def queryFilter(self):
|
||||
return SemesterExperiment.semester == userActiveSemester()
|
||||
|
||||
def create_model(self, form):
|
||||
try:
|
||||
model = SemesterExperiment(
|
||||
def customCreateModel(self, form):
|
||||
return SemesterExperiment(
|
||||
semester=userActiveSemester(), experiment=form.experiment.data, assistants=form.assistants.data
|
||||
)
|
||||
|
||||
self.session.add(model)
|
||||
|
||||
self.on_model_change(form, model, True)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(str(ex), "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
self.after_model_change(form, model, True)
|
||||
return model
|
||||
|
||||
|
||||
class AssistantView(SecureModelView):
|
||||
def assistantUserQueryFactory():
|
||||
|
@ -522,12 +494,11 @@ class GroupExperimentView(SecureModelView):
|
|||
[group.id for group in Group.query.filter(Group.semester == userActiveSemester())]
|
||||
)
|
||||
|
||||
def create_model(self, form):
|
||||
try:
|
||||
model = GroupExperiment.customInit(semester_experiment=form.semester_experiment.data, group=form.group.data)
|
||||
|
||||
self.session.add(model)
|
||||
def customCreateModel(self, form):
|
||||
return GroupExperiment.customInit(semester_experiment=form.semester_experiment.data, group=form.group.data)
|
||||
|
||||
def on_model_change(self, form, model, is_created):
|
||||
if is_created:
|
||||
for date, special, assistant in zip(
|
||||
[form.appointment1_date.data, form.appointment2_date.data],
|
||||
[form.appointment1_special.data, form.appointment2_special.data],
|
||||
|
@ -538,19 +509,11 @@ class GroupExperimentView(SecureModelView):
|
|||
|
||||
self.session.add(appointment)
|
||||
|
||||
self.on_model_change(form, model, True)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(str(ex), "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
def after_model_change(self, form, model, is_created):
|
||||
if is_created:
|
||||
if model.appointments:
|
||||
flash(f"Appointments {model.appointments} added.", "success")
|
||||
|
||||
self.after_model_change(form, model, True)
|
||||
return model
|
||||
|
||||
|
||||
def groupExperimentQueryFactory():
|
||||
return GroupExperiment.query.filter(
|
||||
|
@ -598,24 +561,11 @@ class AppointmentView(SecureModelView):
|
|||
groupExperiment.id for groupExperiment in groupExperimentQueryFactory()
|
||||
)
|
||||
|
||||
def create_model(self, form):
|
||||
try:
|
||||
model = Appointment.customInit(
|
||||
def customCreateModel(self, form):
|
||||
return Appointment.customInit(
|
||||
form.date.data, form.special.data, form.assistant.data, form.group_experiment.data
|
||||
)
|
||||
|
||||
self.session.add(model)
|
||||
|
||||
self.on_model_change(form, model, True)
|
||||
self.session.commit()
|
||||
except Exception as ex:
|
||||
flash(str(ex), "error")
|
||||
|
||||
self.session.rollback()
|
||||
else:
|
||||
self.after_model_change(form, model, True)
|
||||
return model
|
||||
|
||||
def update_model(self, form, model):
|
||||
try:
|
||||
model.customUpdate(form.date.data, form.special.data, form.assistant.data, form.group_experiment.data)
|
||||
|
|
Loading…
Reference in a new issue