mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
Add customUpdateModel
This commit is contained in:
parent
6c268c3bb5
commit
a3c63e3c0f
3 changed files with 83 additions and 45 deletions
|
@ -196,46 +196,33 @@ class UserView(SecureAdminModelView):
|
||||||
"active",
|
"active",
|
||||||
]
|
]
|
||||||
|
|
||||||
def create_model(self, form):
|
_skip_session_addition_on_model_creation = True
|
||||||
try:
|
|
||||||
password = randomPassword()
|
|
||||||
hashedPassword = hash_password(password)
|
|
||||||
|
|
||||||
email = form.email.data.lower()
|
def customCreateModel(self, form):
|
||||||
|
password = randomPassword()
|
||||||
|
hashedPassword = hash_password(password)
|
||||||
|
|
||||||
roles = [role.name for role in form.roles.data]
|
email = form.email.data.lower()
|
||||||
|
|
||||||
model = user_datastore.create_user(
|
roles = [role.name for role in form.roles.data]
|
||||||
email=email,
|
|
||||||
password=hashedPassword,
|
|
||||||
roles=roles,
|
|
||||||
first_name=form.first_name.data,
|
|
||||||
last_name=form.last_name.data,
|
|
||||||
phone_number=form.phone_number.data,
|
|
||||||
mobile_phone_number=form.mobile_phone_number.data,
|
|
||||||
building=form.building.data,
|
|
||||||
room=form.room.data,
|
|
||||||
active=form.active.data,
|
|
||||||
active_semester=form.active_semester.data,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.on_model_change(form, model, True)
|
model = user_datastore.create_user(
|
||||||
self.session.commit()
|
email=email,
|
||||||
except Exception as ex:
|
password=hashedPassword,
|
||||||
if not self.handle_view_exception(ex):
|
roles=roles,
|
||||||
flash(str(ex), "error")
|
first_name=form.first_name.data,
|
||||||
|
last_name=form.last_name.data,
|
||||||
|
phone_number=form.phone_number.data,
|
||||||
|
mobile_phone_number=form.mobile_phone_number.data,
|
||||||
|
building=form.building.data,
|
||||||
|
room=form.room.data,
|
||||||
|
active=form.active.data,
|
||||||
|
active_semester=form.active_semester.data,
|
||||||
|
)
|
||||||
|
|
||||||
self.session.rollback()
|
flashRandomPassword(password)
|
||||||
else:
|
|
||||||
flash(
|
|
||||||
f"{email} registered with roles: {', '.join([role.name for role in model.roles])}.",
|
|
||||||
category="success",
|
|
||||||
)
|
|
||||||
|
|
||||||
flashRandomPassword(password)
|
return model
|
||||||
|
|
||||||
self.after_model_change(form, model, True)
|
|
||||||
return model
|
|
||||||
|
|
||||||
def on_model_delete(self, model):
|
def on_model_delete(self, model):
|
||||||
if model == current_user:
|
if model == current_user:
|
||||||
|
@ -268,6 +255,13 @@ class UserView(SecureAdminModelView):
|
||||||
admin = Admin(user=model)
|
admin = Admin(user=model)
|
||||||
self.session.add(admin)
|
self.session.add(admin)
|
||||||
|
|
||||||
|
def after_model_change(self, form, model, is_created):
|
||||||
|
if is_created:
|
||||||
|
flash(
|
||||||
|
f"{model.email} registered with roles: {', '.join([role.name for role in model.roles])}.",
|
||||||
|
category="success",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class RoleView(SecureAdminModelView):
|
class RoleView(SecureAdminModelView):
|
||||||
can_export = False
|
can_export = False
|
||||||
|
@ -1183,10 +1177,21 @@ class AppointmentView(SecureAdminModelView):
|
||||||
assistant=form.assistant.data,
|
assistant=form.assistant.data,
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_model_change(self, form, model, is_created):
|
def customUpdateModel(self, form, model):
|
||||||
if None not in (form.date, form.special): # No need to check or adjust assistant for editables
|
if form.date is None:
|
||||||
if not is_created:
|
# For editable
|
||||||
model.assistant = Appointment.checkAndGetAssistant(model.group_experiment, model.assistant)
|
model.special = form.special.data
|
||||||
|
elif form.special is None:
|
||||||
|
# For editable
|
||||||
|
model.date = form.date.data
|
||||||
|
else:
|
||||||
|
# Not editable, full form
|
||||||
|
model.custom_update(
|
||||||
|
date=form.date.data,
|
||||||
|
special=form.special.data,
|
||||||
|
group_experiment=form.group_experiment.data,
|
||||||
|
assistant=form.assistant.data,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ExperimentMarkView(SecureAdminModelView):
|
class ExperimentMarkView(SecureAdminModelView):
|
||||||
|
|
|
@ -96,6 +96,10 @@ class CustomModelView(ModelView):
|
||||||
|
|
||||||
refreshFiltersCache = False
|
refreshFiltersCache = False
|
||||||
|
|
||||||
|
# Used in the UserView because of create_user
|
||||||
|
# Should not be touched in other views
|
||||||
|
_skip_session_addition_on_model_creation = False
|
||||||
|
|
||||||
@expose("/")
|
@expose("/")
|
||||||
def index_view(self):
|
def index_view(self):
|
||||||
if self.refreshFiltersCache:
|
if self.refreshFiltersCache:
|
||||||
|
@ -124,14 +128,18 @@ class CustomModelView(ModelView):
|
||||||
|
|
||||||
return super().handle_view_exception(exc)
|
return super().handle_view_exception(exc)
|
||||||
|
|
||||||
def create_model(self, form):
|
def customCreateModel(self, form):
|
||||||
if not hasattr(self, "customCreateModel"):
|
model = self.build_new_instance()
|
||||||
return super().create_model(form)
|
form.populate_obj(model)
|
||||||
|
return model
|
||||||
|
|
||||||
|
def create_model(self, form):
|
||||||
try:
|
try:
|
||||||
model = self.customCreateModel(form)
|
model = self.customCreateModel(form)
|
||||||
|
|
||||||
self.session.add(model)
|
if not self._skip_session_addition_on_model_creation:
|
||||||
|
self.session.add(model)
|
||||||
|
|
||||||
self.on_model_change(form, model, True)
|
self.on_model_change(form, model, True)
|
||||||
self.session.commit()
|
self.session.commit()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
|
@ -141,9 +149,27 @@ class CustomModelView(ModelView):
|
||||||
self.session.rollback()
|
self.session.rollback()
|
||||||
else:
|
else:
|
||||||
self.after_model_change(form, model, True)
|
self.after_model_change(form, model, True)
|
||||||
|
|
||||||
return model
|
return model
|
||||||
|
|
||||||
|
def customUpdateModel(self, form, model):
|
||||||
|
form.populate_obj(model)
|
||||||
|
|
||||||
|
def update_model(self, form, model):
|
||||||
|
try:
|
||||||
|
self.customUpdateModel(form, model)
|
||||||
|
|
||||||
|
self.on_model_change(form, model, False)
|
||||||
|
self.session.commit()
|
||||||
|
except Exception as ex:
|
||||||
|
if not self.handle_view_exception(ex):
|
||||||
|
flash(str(ex), "error")
|
||||||
|
|
||||||
|
self.session.rollback()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
self.after_model_change(form, model, False)
|
||||||
|
return True
|
||||||
|
|
||||||
def create_form(self, obj=None):
|
def create_form(self, obj=None):
|
||||||
if hasattr(self, "CreateForm"):
|
if hasattr(self, "CreateForm"):
|
||||||
formClass = self.CreateForm
|
formClass = self.CreateForm
|
||||||
|
|
|
@ -384,12 +384,12 @@ class Appointment(db.Model):
|
||||||
semesterExperiment = groupExperiment.semester_experiment
|
semesterExperiment = groupExperiment.semester_experiment
|
||||||
semesterExperimentAssistants = semesterExperiment.assistants
|
semesterExperimentAssistants = semesterExperiment.assistants
|
||||||
|
|
||||||
if not semesterExperimentAssistants:
|
if semesterExperimentAssistants is None:
|
||||||
raise DataBaseException(f"{semesterExperiment} does not have assistants yet!")
|
raise DataBaseException(f"{semesterExperiment} does not have assistants yet!")
|
||||||
|
|
||||||
if assistant is not None:
|
if assistant is not None:
|
||||||
if assistant not in semesterExperimentAssistants:
|
if assistant not in semesterExperimentAssistants:
|
||||||
raise DataBaseException(f"{assistant} not responsible for {semesterExperiment}!")
|
raise DataBaseException(f"{assistant} is not responsible for {semesterExperiment}!")
|
||||||
else:
|
else:
|
||||||
if len(semesterExperimentAssistants) == 1:
|
if len(semesterExperimentAssistants) == 1:
|
||||||
assistant = semesterExperimentAssistants[0]
|
assistant = semesterExperimentAssistants[0]
|
||||||
|
@ -410,6 +410,13 @@ class Appointment(db.Model):
|
||||||
assistant=assistant,
|
assistant=assistant,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def custom_update(self, date, special, group_experiment, assistant=None):
|
||||||
|
self.assistant = Appointment.checkAndGetAssistant(group_experiment, assistant)
|
||||||
|
|
||||||
|
self.date = date
|
||||||
|
self.special = special
|
||||||
|
self.group_experiment = group_experiment
|
||||||
|
|
||||||
def repr(self):
|
def repr(self):
|
||||||
return f"{self.date} {self.group_experiment.repr()}"
|
return f"{self.date} {self.group_experiment.repr()}"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue