def test_login_with_valid_credentials(self): User.create(*self.CREDENTIALS) username, password = self.CREDENTIALS response = self.app.post('/login', data={ 'username': username, 'password': password }) self.assertEqual(302, response.status_code)
def test_login_with_invalid_credentials(self): User.create(*self.CREDENTIALS) username, password = self.CREDENTIALS response = self.app.post('/login', data={ 'username': username, 'password': '******' }) self.assertEqual(200, response.status_code) self.assertIn('Invalid login', response.data)
def edit_user(): form = ShopOwnerForm() if form.validate_on_submit(): User.create( account=form.account.data, password=generate_password_hash(form.password.data), name=form.name.data, gender=form.gender.data, birthday=form.birthday.data, phone=form.phone.data, workaddress=form.workaddress.data, role=form.role.data, ) flash('修改成功') return render_template('senior/edit_user.html', user=form)
def signup(): form = SignupForm() if form.validate_on_submit(): user = User.create(form.username.data, form.password.data, 'student') student = Student(user=user.key, name=form.name.data, email_address=form.email_address.data, dob=datetime.datetime.strptime( form.dob.data, '%Y-%m-%d'), status=form.status.data, gender=form.gender.data, year=int(form.year.data)) student_courses = models.StudentCourse.query().filter( models.StudentCourse.email_address == form.email_address.data).get() if student_courses is not None: for course in student_courses.courses: thing = Course.query().filter(Course.code == course).get().key student.courses.append( Class.query().filter(Class.course == thing).get().key) student.put() return redirect(url_for('login')) return render_template('signup.haml', form=form, email_address=request.args.get('email_address'))
def add_user(): form = WorkerForm() if form.validate_on_submit(): User.create( number=form.number.data, account=form.account.data, password=generate_password_hash(form.password.data), name=form.name.data, gender=form.gender.data, birthday=form.birthday.data, phone=form.phone.data, workaddress=form.workaddress.data, role=form.role.data, ) flash('增加成功') return render_template('worker/add_user.html', user=form)
def register(): """Register user""" # Clear session from previous login clearSessionExcept("csrf_token", "_flashes") form = RegistrationForm() if request.method == "POST": # use Flask-WTF's validation: if form.validate_on_submit(): # Insert user and hashed password into database try: User.create(form.username.data, form.password.data) flash(f"account created for {form.username.data}!", "success") return redirect("/login") except Exception: flash(u"Something went wrong.", "danger") return render_template("register.html", form=form)
def add_lecturer(): if current_user.user_type != 'admin': return abort(403) departments = Department.query() form = AddLecturerForm() if form.validate_on_submit(): try: department = ndb.Key(urlsafe=form.department.data).get() except db.BadKeyError: department = None if department is None: return abort(400) temporary_password = str(uuid.uuid4()) user = User.create(form.email_address.data, temporary_password, 'lecturer') lecturer = Lecturer(name=form.name.data, title=form.title.data, department=department.key, email_address=form.email_address.data, user=user.key) lecturer.put() mail.send_mail(sender='*****@*****.**', to=form.email_address.data, subject='Your UWI Lecturer Account', body="""Click this link to be activated. <a href='http://surveymailer.appspot.com/validate?username=%s&value=%s""" % (form.username, temporary_password)) return redirect(request.referrer) return render_template('add_lecturer.haml', form=form, departments=departments)
def populate(): import datetime admin = User.create('admin', 'password', 'admin') admin.put() principal_user = User.create('principal', 'password', 'lecturer') principal = Lecturer(name='Principal', title='Dr', user=principal_user.key) principal.put() school = School(name='University of The West Indies - Mona', principal=principal.key) school.put() hof_user1 = User.create('hof1', 'password', 'lecturer') hof_user2 = User.create('hof2', 'password', 'lecturer') hof1 = Lecturer(name='Head Of Pure and Applied', title='Dr', user=hof_user1.key) hof2 = Lecturer(name='Head Of Medical Sciences', title='Dr', user=hof_user2.key) hof1.put() hof2.put() faculty1 = Faculty(name='Pure and Applied Science', school=school.key, head_of_faculty=hof1.key) faculty2 = Faculty(name='Medical Sciences', school=school.key, head_of_faculty=hof2.key) faculty1.put() faculty2.put() hod_user1 = User.create('hod1', 'password', 'lecturer') hod_user2 = User.create('hod2', 'password', 'lecturer') hod_user3 = User.create('hod3', 'password', 'lecturer') hod_user4 = User.create('hod4', 'password', 'lecturer') hod1 = Lecturer(name='Head Of Computing', title='Dr', user=hod_user1.key) hod2 = Lecturer(name='Head Of Mathematics', title='Dr', user=hod_user2.key) hod3 = Lecturer(name='Head Of Medicine', title='Dr', user=hod_user3.key) hod4 = Lecturer(name='Head Of Microbiology', title='Dr', user=hod_user4.key) hod1.put() hod2.put() hod3.put() hod4.put() department1 = Department(name='Computing', faculty=faculty1.key, head_of_department=hod1.key) department2 = Department(name='Mathematics', faculty=faculty1.key, head_of_department=hod2.key) department3 = Department(name='Medicine', faculty=faculty2.key, head_of_department=hod3.key) department4 = Department(name='Microbiology', faculty=faculty2.key, head_of_department=hod4.key) department1.put() department2.put() department3.put() department4.put() principal.department = department4.key hof1.department = department2.key hof2.department = department3.key hod1.department = department1.key hod2.department = department2.key hod3.department = department3.key hod4.department = department4.key principal.put() hof1.put() hof2.put() hod1.put() hod2.put() hod3.put() hod4.put() student_user = User.create('student', 'password', 'student') student = Student(name='Kevin Leyow', email_address='*****@*****.**', user=student_user.key, dob=datetime.date(year=1992, month=4, day=12), year=3, status='FT', gender='M') lecturer_user = User.create('lecturer', 'password', 'lecturer') lecturer = Lecturer(name='Jimmy', title='Dr', user=lecturer_user.key, department=department1.key) course = Course(name='Database Management Systems', code='COMP3161', total_students=90, department=department1.key, faculty=faculty1.key) course2 = Course(name='Theory Of Computation', code='COMP3702', total_students=20, department=department1.key, faculty=faculty1.key) ndb.put_multi([lecturer, course]) ndb.put_multi([lecturer, course2]) class_ = Class(course=course.key, lecturer=lecturer.key) class2_ = Class(course=course2.key, lecturer=lecturer.key) class_.put() class2_.put() student.courses = [class_.key, class2_.key] lecturer.courses = [class_.key, class2_.key] ndb.put_multi([student, lecturer]) survey = Survey(title='General survey', description='A general survey') survey_key = survey.put() with open('application/questions.txt') as f: questions = [] for number, line in enumerate(f.readlines()): question_type, dimension, question = line.split('|') questions.append( Question(question_type=question_type, dimension=dimension, question=question, is_active=True, number=number + 1, parent=survey_key)) ndb.put_multi(questions) return 'Done.'
def create_admin(name='navy', password='******', email='*****@*****.**'): from application.models import User user = User.create(email=email, password=password, name=name) user.roles.append('ADMIN') user.save()