mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-12-20 23:41:20 +00:00
Add changing password for assistants
This commit is contained in:
parent
82e7bc9d23
commit
cbda7e63ae
5 changed files with 44 additions and 24 deletions
File diff suppressed because one or more lines are too long
|
@ -38,6 +38,7 @@ from advlabdb.models import (
|
||||||
User,
|
User,
|
||||||
)
|
)
|
||||||
from advlabdb.utils import (
|
from advlabdb.utils import (
|
||||||
|
flashRandomPassword,
|
||||||
initActiveSemesterMenuLinks,
|
initActiveSemesterMenuLinks,
|
||||||
randomPassword,
|
randomPassword,
|
||||||
setUserActiveSemester,
|
setUserActiveSemester,
|
||||||
|
@ -72,8 +73,8 @@ class UserView(SecureAdminModelView):
|
||||||
last_name = StringField("Last Name", validators=[DataRequired()])
|
last_name = StringField("Last Name", validators=[DataRequired()])
|
||||||
phone_number = StringField("Phone Number")
|
phone_number = StringField("Phone Number")
|
||||||
mobile_phone_number = StringField("Mobile Phone Number")
|
mobile_phone_number = StringField("Mobile Phone Number")
|
||||||
room = StringField("Room")
|
|
||||||
building = StringField("Building")
|
building = StringField("Building")
|
||||||
|
room = StringField("Room")
|
||||||
|
|
||||||
semester_experiments = QuerySelectMultipleField(
|
semester_experiments = QuerySelectMultipleField(
|
||||||
"Semester Experiments",
|
"Semester Experiments",
|
||||||
|
@ -114,8 +115,8 @@ class UserView(SecureAdminModelView):
|
||||||
column_details_list = column_list + [
|
column_details_list = column_list + [
|
||||||
"phone_number",
|
"phone_number",
|
||||||
"mobile_phone_number",
|
"mobile_phone_number",
|
||||||
"room",
|
|
||||||
"building",
|
"building",
|
||||||
|
"room",
|
||||||
"create_datetime",
|
"create_datetime",
|
||||||
"update_datetime",
|
"update_datetime",
|
||||||
]
|
]
|
||||||
|
@ -136,9 +137,6 @@ class UserView(SecureAdminModelView):
|
||||||
form = self.CreateForm
|
form = self.CreateForm
|
||||||
return form(get_form_data(), obj=obj)
|
return form(get_form_data(), obj=obj)
|
||||||
|
|
||||||
def flashPassword(password):
|
|
||||||
flash(f"Random password: {password}", category="warning")
|
|
||||||
|
|
||||||
def create_model(self, form):
|
def create_model(self, form):
|
||||||
try:
|
try:
|
||||||
password = randomPassword()
|
password = randomPassword()
|
||||||
|
@ -156,8 +154,8 @@ class UserView(SecureAdminModelView):
|
||||||
last_name=form.last_name.data,
|
last_name=form.last_name.data,
|
||||||
phone_number=form.phone_number.data,
|
phone_number=form.phone_number.data,
|
||||||
mobile_phone_number=form.mobile_phone_number.data,
|
mobile_phone_number=form.mobile_phone_number.data,
|
||||||
room=form.room.data,
|
|
||||||
building=form.building.data,
|
building=form.building.data,
|
||||||
|
room=form.room.data,
|
||||||
active=form.active.data,
|
active=form.active.data,
|
||||||
active_semester=form.active_semester.data,
|
active_semester=form.active_semester.data,
|
||||||
)
|
)
|
||||||
|
@ -174,7 +172,7 @@ class UserView(SecureAdminModelView):
|
||||||
category="success",
|
category="success",
|
||||||
)
|
)
|
||||||
|
|
||||||
UserView.flashPassword(password)
|
flashRandomPassword(password)
|
||||||
|
|
||||||
self.after_model_change(form, model, True)
|
self.after_model_change(form, model, True)
|
||||||
return model
|
return model
|
||||||
|
@ -193,12 +191,11 @@ class UserView(SecureAdminModelView):
|
||||||
|
|
||||||
if hasattr(form, "generate_new_password") and form.generate_new_password.data:
|
if hasattr(form, "generate_new_password") and form.generate_new_password.data:
|
||||||
password = randomPassword()
|
password = randomPassword()
|
||||||
|
|
||||||
UserView.flashPassword(password)
|
UserView.flashPassword(password)
|
||||||
|
|
||||||
admin_change_password(
|
admin_change_password(
|
||||||
model, password, notify=False
|
model, password, notify=False
|
||||||
) # Password is automatically hashed with this method
|
) # Password is automatically hashed with this function
|
||||||
|
|
||||||
if model.has_role("assistant") and not model.assistant:
|
if model.has_role("assistant") and not model.assistant:
|
||||||
semester_experiments = form.semester_experiments.data if form.semester_experiments else []
|
semester_experiments = form.semester_experiments.data if form.semester_experiments else []
|
||||||
|
@ -522,8 +519,8 @@ class ExperimentView(SecureAdminModelView):
|
||||||
column_details_list = column_list + [
|
column_details_list = column_list + [
|
||||||
"description",
|
"description",
|
||||||
"wiki_link",
|
"wiki_link",
|
||||||
"room",
|
|
||||||
"building",
|
"building",
|
||||||
|
"room",
|
||||||
"responsibility",
|
"responsibility",
|
||||||
"duration_in_days",
|
"duration_in_days",
|
||||||
"oral_weighting",
|
"oral_weighting",
|
||||||
|
@ -614,8 +611,8 @@ class AssistantView(SecureAdminModelView):
|
||||||
column_details_list = column_list + [
|
column_details_list = column_list + [
|
||||||
"user.phone_number",
|
"user.phone_number",
|
||||||
"user.mobile_phone_number",
|
"user.mobile_phone_number",
|
||||||
"user.room",
|
|
||||||
"user.building",
|
"user.building",
|
||||||
|
"user.room",
|
||||||
"appointments",
|
"appointments",
|
||||||
"experiment_marks",
|
"experiment_marks",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from flask import flash
|
from flask import flash
|
||||||
from flask_admin import expose
|
from flask_admin import expose
|
||||||
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
|
from flask_admin.contrib.sqla.fields import QuerySelectField, QuerySelectMultipleField
|
||||||
from flask_security import current_user
|
from flask_security import admin_change_password, current_user
|
||||||
from sqlalchemy import and_
|
from sqlalchemy import and_
|
||||||
from wtforms import BooleanField, Form, RadioField, SelectField, StringField
|
from wtforms import BooleanField, Form, RadioField, SelectField, StringField
|
||||||
from wtforms.fields import DateField
|
from wtforms.fields import DateField
|
||||||
|
@ -26,7 +26,12 @@ from advlabdb.models import (
|
||||||
Student,
|
Student,
|
||||||
User,
|
User,
|
||||||
)
|
)
|
||||||
from advlabdb.utils import initActiveSemesterMenuLinks, userActiveSemester
|
from advlabdb.utils import (
|
||||||
|
flashRandomPassword,
|
||||||
|
initActiveSemesterMenuLinks,
|
||||||
|
randomPassword,
|
||||||
|
userActiveSemester,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AssistantAppointmentView(SecureAssistantModelView):
|
class AssistantAppointmentView(SecureAssistantModelView):
|
||||||
|
@ -127,30 +132,44 @@ class AssistantExperimentMarkView(SecureAssistantModelView):
|
||||||
|
|
||||||
|
|
||||||
class AssistantUserView(SecureAssistantModelView):
|
class AssistantUserView(SecureAssistantModelView):
|
||||||
|
class EditForm(Form):
|
||||||
|
phone_number = StringField("Phone Number")
|
||||||
|
mobile_phone_number = StringField("Mobile Phone Number")
|
||||||
|
|
||||||
|
building = StringField("Building")
|
||||||
|
room = StringField("Room")
|
||||||
|
|
||||||
|
generate_new_password = BooleanField("Generate new random password", default=False)
|
||||||
|
|
||||||
|
form = EditForm
|
||||||
|
|
||||||
|
can_edit = True
|
||||||
|
column_display_actions = True
|
||||||
|
|
||||||
column_sortable_list = []
|
column_sortable_list = []
|
||||||
|
|
||||||
column_list = [
|
column_list = [
|
||||||
"email",
|
"email",
|
||||||
"phone_number",
|
"phone_number",
|
||||||
"mobile_phone_number",
|
"mobile_phone_number",
|
||||||
"room",
|
|
||||||
"building",
|
"building",
|
||||||
|
"room",
|
||||||
"assistant.semester_experiments",
|
"assistant.semester_experiments",
|
||||||
]
|
]
|
||||||
column_labels = {
|
column_labels = {
|
||||||
"assistant.semester_experiments": "Semester Experiments",
|
"assistant.semester_experiments": "Semester Experiments",
|
||||||
}
|
}
|
||||||
|
|
||||||
column_editable_list = [
|
|
||||||
"phone_number",
|
|
||||||
"mobile_phone_number",
|
|
||||||
"room",
|
|
||||||
"building",
|
|
||||||
]
|
|
||||||
|
|
||||||
def queryFilter(self):
|
def queryFilter(self):
|
||||||
return User.id == current_user.id
|
return User.id == current_user.id
|
||||||
|
|
||||||
|
def on_model_change(self, form, model, is_created):
|
||||||
|
if form.generate_new_password.data:
|
||||||
|
password = randomPassword()
|
||||||
|
flashRandomPassword(password)
|
||||||
|
|
||||||
|
admin_change_password(model, password, notify=False) # Password is automatically hashed with this function
|
||||||
|
|
||||||
|
|
||||||
class AssistantDocsView(SecureAssistantBaseView):
|
class AssistantDocsView(SecureAssistantBaseView):
|
||||||
@expose("/", methods=["GET"])
|
@expose("/", methods=["GET"])
|
||||||
|
|
|
@ -234,8 +234,8 @@ class Experiment(db.Model):
|
||||||
title = db.Column(db.String(200), nullable=False)
|
title = db.Column(db.String(200), nullable=False)
|
||||||
description = db.Column(db.Text, nullable=True)
|
description = db.Column(db.Text, nullable=True)
|
||||||
wiki_link = db.Column(db.String(300), nullable=True)
|
wiki_link = db.Column(db.String(300), nullable=True)
|
||||||
room = db.Column(db.String(100), nullable=False)
|
|
||||||
building = db.Column(db.String(100), nullable=False)
|
building = db.Column(db.String(100), nullable=False)
|
||||||
|
room = db.Column(db.String(100), nullable=False)
|
||||||
responsibility = db.Column(db.String(200), nullable=True)
|
responsibility = db.Column(db.String(200), nullable=True)
|
||||||
duration_in_days = db.Column(db.Integer, db.CheckConstraint("duration_in_days > -1"), nullable=False)
|
duration_in_days = db.Column(db.Integer, db.CheckConstraint("duration_in_days > -1"), nullable=False)
|
||||||
active = db.Column(db.Boolean, default=True, nullable=False)
|
active = db.Column(db.Boolean, default=True, nullable=False)
|
||||||
|
@ -522,8 +522,8 @@ class User(db.Model, FsUserMixin):
|
||||||
last_name = db.Column(db.String(100), nullable=False)
|
last_name = db.Column(db.String(100), nullable=False)
|
||||||
phone_number = db.Column(db.String(50), nullable=True)
|
phone_number = db.Column(db.String(50), nullable=True)
|
||||||
mobile_phone_number = db.Column(db.String(50), nullable=True)
|
mobile_phone_number = db.Column(db.String(50), nullable=True)
|
||||||
room = db.Column(db.String(100), nullable=True)
|
|
||||||
building = db.Column(db.String(100), nullable=True)
|
building = db.Column(db.String(100), nullable=True)
|
||||||
|
room = db.Column(db.String(100), nullable=True)
|
||||||
|
|
||||||
active_semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=True)
|
active_semester_id = db.Column(db.Integer, db.ForeignKey("semester.id"), nullable=True)
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,10 @@ def randomPassword():
|
||||||
return "".join(choice(ascii_letters + digits) for i in range(12))
|
return "".join(choice(ascii_letters + digits) for i in range(12))
|
||||||
|
|
||||||
|
|
||||||
|
def flashRandomPassword(password):
|
||||||
|
flash(f"Random password: {password}", category="warning")
|
||||||
|
|
||||||
|
|
||||||
def userActiveSemester(flashWarning=False):
|
def userActiveSemester(flashWarning=False):
|
||||||
lastSemesterId = (
|
lastSemesterId = (
|
||||||
Semester.query.order_by(Semester.year.desc()).order_by(Semester.label.desc()).first().id
|
Semester.query.order_by(Semester.year.desc()).order_by(Semester.label.desc()).first().id
|
||||||
|
|
Loading…
Reference in a new issue