def setUp(self): """ Add a admin user to the database. """ # Call the super class method. BaseTestCase.setUp(self) # Create a new user. staff_user = User() # Add Staff User staff_user.update_user_details( first_name="admin", last_name="admin", email_address="*****@*****.**", password=staff_user.create_password_hash("password1"), role="STA", ) db.session.add(staff_user) db.session.commit()
def add_user(): """ Add a new user to the database. """ # Empty error variable. error = None # Create the form object. add_user_form = AddUserForm() # Check to see if the method was post and the form was valid. if request.method == 'POST' and add_user_form.validate_on_submit() \ and add_user_form.validate_optional_form_fields(): # Create new user object. new_user = User() # Create password hash password_hash = new_user.create_password_hash(add_user_form.password.data) # Update generic details. new_user.update_user_details( first_name=add_user_form.first_name.data, last_name=add_user_form.last_name.data, email_address=add_user_form.email_address.data, role=add_user_form.role.data, password=password_hash ) # Check to see if the user is a student or a tutor/staff. if add_user_form.role.data == 'STU': # Check there isn't already a parent with these details. parent = Parent.query.filter_by( email_address=add_user_form.parent_email_address.data ).first() # If the parent exists. if parent is not None: # Parent already exists, lets use there details. # But check the phone number also matches. if parent.get_telephone_number() != add_user_form.parent_telephone_number.data: error = "A parent is already associated with this email address, \ but could not be added to this student as the telephone number \ provided did not match. Please either use a different email address \ or the same telephone number as previously used." return render_template( 'staff/add_user.html', add_user_form=add_user_form, error=error ) else: # If the telephone number matched, use this parent's ID. parent_id = parent.parent_id else: # Add new parent. new_parent = Parent() # Update the new parent's details. new_parent.update_parent_details( first_name=add_user_form.parent_first_name.data, last_name=add_user_form.parent_last_name.data, email_address=add_user_form.parent_email_address.data, telephone_number=add_user_form.parent_telephone_number.data ) # Add the new parent to the database. db.session.add(new_parent) # Commit the changes to the database. db.session.commit() # Get the parent_id. parent_id = new_parent.parent_id if add_user_form.musical_instrument.data == 'singing': new_user_musical_instrument = 'Voice' else: new_user_musical_instrument = add_user_form.musical_instrument.data # Update student specific details. new_user.update_user_details( tutor_group=add_user_form.tutor_group.data, musical_instrument_type=add_user_form.musical_instrument_type.data, musical_instrument=new_user_musical_instrument, musical_style=add_user_form.musical_style.data, musical_grade=int(add_user_form.musical_grade.data), lesson_type=add_user_form.lesson_type.data, lesson_pairing=add_user_form.lesson_pairing.data, parent_id=parent_id ) elif add_user_form.role.data == 'TUT' or add_user_form.role.data == 'STA': # Update staff/tutor specific details. new_user.update_user_details( speciality=add_user_form.speciality.data, telephone_number=add_user_form.telephone_number.data ) # Add the new user. db.session.add(new_user) # Commit the changes. db.session.commit() # Success message. flash("Successfully added new user.") # Redirect. return redirect(url_for('staff.add_user')) return render_template( 'staff/add_user.html', add_user_form=add_user_form, error=error )
from peri2organise import db from peri2organise.models import User # User Configuration Required: FIRST_NAME = 'Admin' LAST_NAME = 'Admin' EMAIL_ADDRESS = '*****@*****.**' PASSWORD = '******' TELEPHONE_NUMBER = '01373465353' SPECIALITY = 'Admin' # -------------------------- # # This script can only be run if there are no other admins! if not User.query.filter(User.role == 'STA').all(): # Create a new user object. admin_user = User() # Create a password hash of the desired password. password_hash = admin_user.create_password_hash(PASSWORD) # Update the admin user's details. admin_user.update_user_details( first_name=FIRST_NAME, last_name=LAST_NAME, email_address=EMAIL_ADDRESS, password=password_hash, role='STA', telephone_number=TELEPHONE_NUMBER, speciality=SPECIALITY ) # Add the new object to the database. db.session.add(admin_user) # Commit changes.
def setUp(self): """ Authenticate as staff before each request. """ # Call the super class setup method. BaseTestCase.setUp(self) # Add a student, tutor, parent and staff user to the database. staff_user = User() # Add Tutor User staff_user.update_user_details( first_name='staff', last_name='staff', email_address='*****@*****.**', password=staff_user.create_password_hash('password1'), role='STA', ) db.session.add(staff_user) tutor_user = User() # Add Tutor User tutor_user.update_user_details( first_name='tutor', last_name='tutor', email_address='*****@*****.**', password=tutor_user.create_password_hash('password1'), role='TUT', ) db.session.add(tutor_user) # Add a parent. parent = Parent() parent.update_parent_details( first_name='parent', last_name='parent', email_address='*****@*****.**', telephone_number='01234567890' ) db.session.add(parent) student_user = User() # Add Student User student_user.update_user_details( first_name='student', last_name='student', email_address='*****@*****.**', password=student_user.create_password_hash('password1'), role='STU', tutor_group='ABC', musical_instrument_type='Instrument', musical_instrument='Piano', musical_style='jazz', lesson_type='individual', parent_id=1, ) db.session.add(student_user) # Add a new room. room = Room() room.update_room_details( location='Room1', facilities='Piano' ) db.session.add(room) # Add a new lesson. lesson = Lesson() lesson.update_lesson_details( lesson_datetime=datetime.now() + timedelta(days=7), lesson_duration=3600, lesson_notes='Work Very Hard', room_id=1 ) lesson.users.append(tutor_user) lesson.users.append(student_user) db.session.add(lesson) # Commit the changes. db.session.commit() # Authenticate. with self.client: self.client.post( '/auth/login', data=dict(email_address='*****@*****.**', password='******'), follow_redirects=True ) # Set the IDs to use. self.STAFF_ID = staff_user.user_id self.TUTOR_ID = tutor_user.user_id self.STUDENT_ID = student_user.user_id self.PARENT_ID = parent.parent_id