def test_add_courses_and_group_to_users(self): User.objects.bulk_create([ User(username='******', moodle_id=3, email='*****@*****.**', first_name='Tom', last_name='Tree', is_staff=True, version_time=datetime.datetime(2020, 1, 28)), User(username='******', moodle_id=5, email='*****@*****.**', first_name='Sam', last_name='Smith', is_staff=True, version_time=datetime.datetime(2020, 1, 28)) ]) course_list = {2: 1, 4: 2} Group.objects.create(name='Teachers') users = User.objects.all() add_courses_and_group_to_users(course_list, Group.objects.get(name='Teachers'), test_data.test_parse_get_teachers, users) self.assertEqual( User.objects.annotate(group_count=Count('groups')).count(), 2)
def add_rate(request): if request.method == "POST": user_select = request.POST.get("user_select") page = request.POST.get("page") user_ip = request.POST.get("user_ip") try: User.objects.get(page=page, ip=user_ip) except User.DoesNotExist: new_user = User() new_user.ip = user_ip page = Page.objects.get(id=page) new_user.page = page new_user.save() page.update_rate(user_select) page.save() return index(request)
def create_teachers(teachers_list): """ Create teachers and add them to Django database. :param teachers_list: Dictionary of teachers. :type teachers_list: dict(int, dict(str, str)) :return: A QuerySet of teachers. :rtype: QuerySet """ user_list = [] for teacher in teachers_list.values(): user = User(username=teacher['username'], moodle_id=teacher['id'], email=teacher['email'], first_name=teacher['firstname'], last_name=teacher['lastname'], is_staff=True, version_time=timezone.now()) user.set_password("Test123!") user_list.append(user) User.objects.bulk_create(user_list) users = User.objects.all() return users
def user_assigned(user): """ Check if a user is assigned the editteacher or manager role and if so, add them to the database. :param user: information about the user. :type user: dict(str, str) """ if user['role'] == 'editingteacher' or user['role'] == 'manager': try: course = Course.objects.get(courseId=user['course_id']) except ObjectDoesNotExist: log_assistants('Course does not exist.', 'Sync_Agent') raise ObjectDoesNotExist('Course does not exist.') user_exists = User.objects.filter(moodle_id=user['id']).exists() if user_exists: userdb = User.objects.get(moodle_id=user['id']) if not userdb.role_set.filter(course_id=course, role_type=user['role']).exists(): Role.objects.create(user_id=userdb, course_id=course, role_type=user['role']) else: userdb = User(username=user['username'], moodle_id=user['id'], email=user['email'], first_name=user['firstname'], last_name=user['lastname'], is_staff=True, version_time=timezone.now()) userdb.set_password("Test123!") userdb.save() teacher_group = Group.objects.get(name='Teachers') userdb.groups.add(teacher_group) Role.objects.create(user_id=userdb, course_id=course, role_type=user['role'])
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): # If the credentials are valid # Generate hash password for the user hashed_pass = bcrypt.generate_password_hash(form.password.data).decode('utf-8') my_role = form.role.data # Store in the database the hashed password and not the plain text user = User(name=form.name.data, email=form.email.data, password=hashed_pass, role_id=my_role) db.session.add(user) db.session.commit() flash(f'Your account has been created!', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def setUp(self) -> None: Group.objects.create(name='Teachers') Course.objects.create(name='test-course', courseId=2, version_time=datetime.datetime(2020, 1, 28)) user = User(username='******', first_name='Test', last_name='User', email='*****@*****.**', moodle_id=3, is_staff=True, version_time=datetime.datetime(2020, 1, 28)) user.set_password('Test123!') user.save() group = Group.objects.get(name='Teachers') user.groups.add(group)
def test_unassigned_user_course(self): course = Course.objects.create(name='test-course', courseId=2, version_time=datetime.datetime( 2020, 1, 28)) user = User(username='******', first_name='Test', last_name='User', email='*****@*****.**', moodle_id=3, is_staff=True, version_time=datetime.datetime(2020, 1, 28)) user.set_password('Test123!') user.save() group = Group.objects.get(name='Teachers') user.groups.add(group) Role.objects.create(user_id=user, course_id=course, role_type='editingteacher') db_update.user_unassigned(test_data.test_database_unassign_user) self.assertEqual(0, user.courses.count())
def delete(self, user: User): User.delete(user)
def save(self, user: User): User.save(user)