def _common_comments(exercise_id=None, user_id=None): """ Most common comments throughout all exercises. Filter by exercise id when specified. """ is_moderator_comments = ( (Comment.commenter.role == Role.get_staff_role().id) | (Comment.commenter.role == Role.get_admin_role().id), ) query = (CommentText.select( CommentText.id, CommentText.text).join(Comment).join(User).join(Role).where( CommentText.flake8_key.is_null(True), is_moderator_comments, ).switch(Comment)) if exercise_id is not None: query = (query.join(SolutionFile).join(Solution).join(Exercise).where( Exercise.id == exercise_id)) if user_id is not None: query = (query.filter(Comment.commenter == user_id)) query = (query.group_by(CommentText.id).order_by( fn.Count(CommentText.id).desc()).limit(5)) return tuple(query.dicts())
def create_student_user(index=0): return User.create( # NOQA: S106 username=f'student-{index}', fullname='Astudent', mail_address=f'so-student-{index}@mail.com', password='******', role=Role.get_student_role(), )
def admin_user(): admin_role = Role.get(Role.name == RoleOptions.ADMINISTRATOR.value) return User.create( # NOQA: B106, S106 username='******', fullname='Buya', mail_address='*****@*****.**', password='******', role=admin_role, )
def staff_user(staff_password): staff_role = Role.get(Role.name == RoleOptions.STAFF.value) return User.create( # NOQA: S106 username='******', fullname='Elk', mail_address='*****@*****.**', password=staff_password, role=staff_role, )
def admin_user(): admin_role = Role.get(Role.name == RoleOptions.ADMINISTRATOR.value) username = '******' return User.create( # NOQA: B106, S106 username=username, fullname='Buya', mail_address='*****@*****.**', password=FAKE_PASSWORD, api_key='fake key', role=admin_role, )
def create_user( role_name: str = RoleOptions.STUDENT.value, index: int = 1, ) -> User: return User.create( # NOQA: S106 username=f'{role_name}-{index}', fullname=f'A{role_name}', mail_address=f'so-{role_name}-{index}@mail.com', password='******', role=Role.by_name(role_name), )
def confirm_email(user_id: int, token: str): user = User.get_or_none(User.id == user_id) if user is None: return fail(404, 'The authentication code is invalid.') if not user.role.is_unverified: return fail(403, 'User has been already confirmed.') try: SERIALIZER.loads( token, salt=retrieve_salt(user), max_age=CONFIRMATION_TIME, ) except SignatureExpired: send_confirmation_mail(user) return redirect(url_for( 'login', login_message=( _( 'The confirmation link is expired, new link has been ' 'sent to your email', ), ), )) except BadSignature: return fail(404, 'The authentication code is invalid.') else: update = User.update( role=Role.get_student_role(), ).where(User.username == user.username) update.execute() return redirect(url_for( 'login', login_message=( _( 'Your user has been successfully confirmed, ' 'you can now login', ), ), ))
def signup(): if not webapp.config.get('REGISTRATION_OPEN', False): return redirect(url_for( 'login', login_message=_('Can not register now'), )) form = RegisterForm() if not form.validate_on_submit(): return render_template('signup.html', form=form) user = User.create(**{ User.mail_address.name: form.email.data, User.username.name: form.username.data, User.fullname.name: form.fullname.data, User.role.name: Role.get_unverified_role(), User.password.name: form.password.data, User.api_key.name: User.random_password(), }) send_confirmation_mail(user) return redirect(url_for( 'login', login_message=_('Registration successfully'), ))
def populate_roles(): for role in RoleOptions: Role.create(name=role.value)