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"
|
edit_template = "admin_edit.html"
|
||||||
|
|
||||||
queryFilter = None
|
queryFilter = None
|
||||||
|
customCreateModel = None
|
||||||
|
|
||||||
def is_accessible(self):
|
def is_accessible(self):
|
||||||
return adminViewIsAccessible()
|
return adminViewIsAccessible()
|
||||||
|
@ -58,3 +59,24 @@ class SecureModelView(ModelView):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
return super().handle_view_exception(exc)
|
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="-",
|
blank_text="-",
|
||||||
)
|
)
|
||||||
|
|
||||||
generate_new_password = None
|
|
||||||
|
|
||||||
class EditForm(CreateForm):
|
class EditForm(CreateForm):
|
||||||
generate_new_password = BooleanField("Generate new random password", default=False)
|
generate_new_password = BooleanField("Generate new random password", default=False)
|
||||||
|
|
||||||
|
@ -82,16 +80,14 @@ class UserView(SecureModelView):
|
||||||
flash(f"Random password: {password}", category="warning")
|
flash(f"Random password: {password}", category="warning")
|
||||||
|
|
||||||
def create_model(self, form):
|
def create_model(self, form):
|
||||||
|
try:
|
||||||
password = randomPassword()
|
password = randomPassword()
|
||||||
hashedPassword = hash_password(password)
|
hashedPassword = hash_password(password)
|
||||||
|
|
||||||
email = form.email.data.lower()
|
email = form.email.data.lower()
|
||||||
|
|
||||||
roles = [role.name for role in form.roles.data]
|
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(
|
model = user_datastore.create_user(
|
||||||
email=email, password=hashedPassword, roles=roles, active_semester=form.active_semester.data
|
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!")
|
raise ModelViewException("Tried to delete yourself as user!")
|
||||||
|
|
||||||
def on_model_change(self, form, model, is_created):
|
def on_model_change(self, form, model, is_created):
|
||||||
|
if not is_created:
|
||||||
if model == current_user and not form.active.data:
|
if model == current_user and not form.active.data:
|
||||||
raise ModelViewException("Tried to deactiavte yourself as user!")
|
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()
|
password = randomPassword()
|
||||||
|
|
||||||
UserView.flashPassword(password)
|
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):
|
class RoleView(SecureModelView):
|
||||||
|
@ -163,9 +162,8 @@ class SemesterView(SecureModelView):
|
||||||
column_details_list = column_list + ["semester_experiments", "active_users", "groups"]
|
column_details_list = column_list + ["semester_experiments", "active_users", "groups"]
|
||||||
column_searchable_list = ["label", "year"]
|
column_searchable_list = ["label", "year"]
|
||||||
|
|
||||||
def create_model(self, form):
|
def customCreateModel(self, form):
|
||||||
try:
|
return Semester.customInit(
|
||||||
model = Semester.customInit(
|
|
||||||
label=form.label.data,
|
label=form.label.data,
|
||||||
year=form.year.data,
|
year=form.year.data,
|
||||||
oldSemester=userActiveSemester(),
|
oldSemester=userActiveSemester(),
|
||||||
|
@ -173,17 +171,6 @@ class SemesterView(SecureModelView):
|
||||||
transferAssistants=form.transfer_assistants.data,
|
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):
|
def after_model_change(self, form, model, is_created):
|
||||||
setUserActiveSemester(model.id)
|
setUserActiveSemester(model.id)
|
||||||
|
|
||||||
|
@ -196,17 +183,30 @@ class SemesterView(SecureModelView):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def programQueryFactory():
|
||||||
|
return Program.query
|
||||||
|
|
||||||
|
|
||||||
class PartView(SecureModelView):
|
class PartView(SecureModelView):
|
||||||
can_view_details = True
|
can_view_details = True
|
||||||
|
|
||||||
column_sortable_list = []
|
column_sortable_list = []
|
||||||
column_list = ["program", "number", "semester"]
|
column_list = ["program", "number", "semester"]
|
||||||
column_details_list = column_list + ["part_students"]
|
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):
|
def queryFilter(self):
|
||||||
return Part.semester == userActiveSemester()
|
return Part.semester == userActiveSemester()
|
||||||
|
|
||||||
|
def customCreateModel(self, form):
|
||||||
|
return Part(program=form.program.data, number=form.number.data, semester=userActiveSemester())
|
||||||
|
|
||||||
|
|
||||||
class StudentView(SecureModelView):
|
class StudentView(SecureModelView):
|
||||||
can_view_details = True
|
can_view_details = True
|
||||||
|
@ -326,20 +326,8 @@ class GroupView(SecureModelView):
|
||||||
def queryFilter(self):
|
def queryFilter(self):
|
||||||
return Group.semester == userActiveSemester()
|
return Group.semester == userActiveSemester()
|
||||||
|
|
||||||
def create_model(self, form):
|
def customCreateModel(self, form):
|
||||||
try:
|
return Group.customInit(form.part_students.data)
|
||||||
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 update_model(self, form, model):
|
def update_model(self, form, model):
|
||||||
try:
|
try:
|
||||||
|
@ -359,9 +347,6 @@ class GroupView(SecureModelView):
|
||||||
|
|
||||||
|
|
||||||
class ExperimentView(SecureModelView):
|
class ExperimentView(SecureModelView):
|
||||||
def programQueryFactory():
|
|
||||||
return Program.query
|
|
||||||
|
|
||||||
can_view_details = True
|
can_view_details = True
|
||||||
|
|
||||||
column_filters = ["active"]
|
column_filters = ["active"]
|
||||||
|
@ -420,24 +405,11 @@ class SemesterExperimentView(SecureModelView):
|
||||||
def queryFilter(self):
|
def queryFilter(self):
|
||||||
return SemesterExperiment.semester == userActiveSemester()
|
return SemesterExperiment.semester == userActiveSemester()
|
||||||
|
|
||||||
def create_model(self, form):
|
def customCreateModel(self, form):
|
||||||
try:
|
return SemesterExperiment(
|
||||||
model = SemesterExperiment(
|
|
||||||
semester=userActiveSemester(), experiment=form.experiment.data, assistants=form.assistants.data
|
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):
|
class AssistantView(SecureModelView):
|
||||||
def assistantUserQueryFactory():
|
def assistantUserQueryFactory():
|
||||||
|
@ -522,12 +494,11 @@ class GroupExperimentView(SecureModelView):
|
||||||
[group.id for group in Group.query.filter(Group.semester == userActiveSemester())]
|
[group.id for group in Group.query.filter(Group.semester == userActiveSemester())]
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_model(self, form):
|
def customCreateModel(self, form):
|
||||||
try:
|
return GroupExperiment.customInit(semester_experiment=form.semester_experiment.data, group=form.group.data)
|
||||||
model = GroupExperiment.customInit(semester_experiment=form.semester_experiment.data, group=form.group.data)
|
|
||||||
|
|
||||||
self.session.add(model)
|
|
||||||
|
|
||||||
|
def on_model_change(self, form, model, is_created):
|
||||||
|
if is_created:
|
||||||
for date, special, assistant in zip(
|
for date, special, assistant in zip(
|
||||||
[form.appointment1_date.data, form.appointment2_date.data],
|
[form.appointment1_date.data, form.appointment2_date.data],
|
||||||
[form.appointment1_special.data, form.appointment2_special.data],
|
[form.appointment1_special.data, form.appointment2_special.data],
|
||||||
|
@ -538,19 +509,11 @@ class GroupExperimentView(SecureModelView):
|
||||||
|
|
||||||
self.session.add(appointment)
|
self.session.add(appointment)
|
||||||
|
|
||||||
self.on_model_change(form, model, True)
|
def after_model_change(self, form, model, is_created):
|
||||||
self.session.commit()
|
if is_created:
|
||||||
except Exception as ex:
|
|
||||||
flash(str(ex), "error")
|
|
||||||
|
|
||||||
self.session.rollback()
|
|
||||||
else:
|
|
||||||
if model.appointments:
|
if model.appointments:
|
||||||
flash(f"Appointments {model.appointments} added.", "success")
|
flash(f"Appointments {model.appointments} added.", "success")
|
||||||
|
|
||||||
self.after_model_change(form, model, True)
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def groupExperimentQueryFactory():
|
def groupExperimentQueryFactory():
|
||||||
return GroupExperiment.query.filter(
|
return GroupExperiment.query.filter(
|
||||||
|
@ -598,24 +561,11 @@ class AppointmentView(SecureModelView):
|
||||||
groupExperiment.id for groupExperiment in groupExperimentQueryFactory()
|
groupExperiment.id for groupExperiment in groupExperimentQueryFactory()
|
||||||
)
|
)
|
||||||
|
|
||||||
def create_model(self, form):
|
def customCreateModel(self, form):
|
||||||
try:
|
return Appointment.customInit(
|
||||||
model = Appointment.customInit(
|
|
||||||
form.date.data, form.special.data, form.assistant.data, form.group_experiment.data
|
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):
|
def update_model(self, form, model):
|
||||||
try:
|
try:
|
||||||
model.customUpdate(form.date.data, form.special.data, form.assistant.data, form.group_experiment.data)
|
model.customUpdate(form.date.data, form.special.data, form.assistant.data, form.group_experiment.data)
|
||||||
|
|
Loading…
Reference in a new issue