def test_student_list(self): """ Tests that students are properly listed. """ # add students to database Student(user=self.student_user, labgroup=self.group, wwuid='64').save() Student(user=self.instructor_user, labgroup=self.group, wwuid='12').save() # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response students = Student.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response_body['students'][0]['user'], students[0].user.id) self.assertEqual(response_body['students'][0]['labgroup'], students[0].labgroup.id) self.assertEqual(response_body['students'][0]['wwuid'], students[0].wwuid) self.assertEqual(response_body['students'][1]['user'], students[1].user.id) self.assertEqual(response_body['students'][1]['labgroup'], students[1].labgroup.id) self.assertEqual(response_body['students'][1]['wwuid'], students[1].wwuid)
def setUp(self): # create test user with permissions and test student user self.instructor_username = '******' self.student_username_1 = 'test student 1' self.student_username_2 = 'test student 2' self.student_username_3 = 'test student 3' self.password = '******' self.student_user_1 = User.objects.create_user( username=self.student_username_1, password=self.password) self.student_user_2 = User.objects.create_user( username=self.student_username_2, password=self.password) self.student_user_3 = User.objects.create_user( username=self.student_username_3, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) self.instructor_user.user_permissions.add( Permission.objects.get(codename='change_student')) self.instructor_user.user_permissions.add( Permission.objects.get(codename='delete_student')) self.client.login(username=self.instructor_username, password=self.password) # populate database self.instructor = Instructor(user=self.instructor_user, wwuid='9999999') self.instructor.save() self.course = Course(name='test course') self.course.save() self.group_1 = LabGroup(course=self.course, instructor=self.instructor, term='never', enroll_key='6') self.group_1.save() self.group_2 = LabGroup(course=self.course, instructor=self.instructor, term='ever', enroll_key='8') self.group_2.save() self.student_1 = Student(user=self.student_user_1, labgroup=self.group_1, wwuid='1111111') self.student_1.save() self.student_2 = Student(user=self.student_user_2, labgroup=self.group_1, wwuid='2222222') self.student_2.save() self.student_3 = Student(user=self.student_user_3, labgroup=self.group_1, wwuid='3333333') self.student_3.save() # retrieve the view self.view_name = 'api:student-rud'
def setUp(self): # create test users self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.instructor_user) group = permissions.get_or_create_student_permissions() group.user_set.add(self.student_user) self.client.login(username=self.student_username, password=self.password) # populate the database self.student = Student(labgroup=None, user=self.student_user, wwuid='1111111') self.student.save() self.instructor = Instructor(user=self.instructor_user, wwuid='2222222') self.instructor.save() self.course = Course(name='test name') self.course.save() self.labgroup = LabGroup(course=self.course, instructor=self.instructor, group_name='A', term='FALL2018', enroll_key='ABC') self.labgroup.save() # retrieve the view self.view_name = 'api:enroll'
def create(self, validated_data): password = validated_data['password'] re_password = validated_data['re_password'] if password != re_password: raise serializers.ValidationError( {'password': '******'}) student = Student( username=validated_data['username'], email=validated_data['email'], ) student.set_password(password) student.save() verification_token = random_token(16) from api.models import VerificationToken VerificationToken(student=student, token=verification_token, type=1).save() email_context = { 'token': verification_token, 'username': student.username, } sendemail(subject='Account Verification FIIMaterials', template='email_verification.html', context=email_context, email_to=[student.email]) return student
def post(self, request, format=None): serializer = StudentOtherSerializer(data=request.data) if serializer.is_valid(): student = Student(**serializer.data) student.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def setUp(self): # create test user with permissions self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) group = permissions.get_or_create_student_permissions() group.user_set.add(self.student_user) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) group = permissions.get_or_create_instructor_permissions() group.user_set.add(self.instructor_user) self.client.login(username=self.instructor_username, password=self.password) # populate test database self.course = Course(name='Bounty Hunting 101') self.course.save() self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term='before', enroll_key='4') self.group.save() self.student = Student(user=self.student_user, wwuid='9994141', labgroup=self.group) self.student.save() self.template = AssignmentTemplate(course=self.course, name='Royalty Kidnapping Section A') self.template.save() # retrieve the view self.view_name = 'api:assignment-lc'
def mutate(self, info, name, age): try: student = Student(name=name, age=age) student.save() return AddStudent(success=True) except: return AddStudent(success=False)
def test_student_list_not_shown_to_students(self): """ Tests that students are not listed to other students. """ # login the student self.client.logout() self.client.login(username=self.student_username, password=self.password) # add students to database Student(user=self.student_user, labgroup=self.group, wwuid='64').save() Student(user=self.instructor_user, labgroup=self.group, wwuid='12').save() # request response = self.client.get(reverse(self.view_name)) # test response self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def get_user(user_id, login): user = Student.objects.filter(login=login) if user.exists(): return user[0] else: user = Student(user_id=user_id, login=login) logger.info(f"Student {login} created") user.save() return user
def post(self, request): try: name = request.data['name'] age = request.data['age'] student = Student(name=name, age=age) student.save() return Response(status=HTTP_201_CREATED) except: return Response(status=HTTP_400_BAD_REQUEST)
def get_user(user_id): user = Student.objects.filter(user_id=user_id) if user.exists(): return user[0] else: res = client.get(f"/users/{user_id}") login = res.json()["login"] user = Student(user_id=user_id, login=login) user.save() logger.info(f"Student {login} created") return user
def post(self, request, format=None): form = self.form_class(data=request.data) if not form.is_valid(): raise ValueError("Invalid") serializer = StudentOtherSerializer(data=request.data) if serializer.is_valid(): student = Student(**serializer.data) student.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.error, status=status.HTTP_400_BAD_REQUEST)
def student_register(request): if request.method == "POST": first_name = request.POST.get('first-name', "") last_name = request.POST.get('last-name', "") parent_option = request.POST.get('parent-option', "") parent = User.objects.get(id=parent_option) s = Student(first_name=first_name, last_name=last_name, parent=parent) s.save() s.generate_qrcode() return HttpResponseRedirect('/')
def setUp(self): course_a = Course( name=f'Course A', description='', start_date=datetime.date.today(), end_date=datetime.date.today() + datetime.timedelta(days=1), ) course_b = Course( name=f'Course B', description='', start_date=datetime.date.today(), end_date=datetime.date.today() + datetime.timedelta(days=1), ) course_a.save() course_b.save() student_a = Student(first_name="Student A", last_name="aaa", email="*****@*****.**") student_b = Student(first_name="Student B", last_name="bbb", email="*****@*****.**") student_a.save() student_b.save() participants = [ CourseParticipant(course=course_a, student=student_a, completed=True), CourseParticipant(course=course_b, student=student_b, completed=False), CourseParticipant(course=course_a, student=student_b, completed=True), ] CourseParticipant.objects.bulk_create(participants)
def setUp(self): # create test user with permissions self.student_username = '******' self.instructor_username = '******' self.password = '******' self.student_user = User.objects.create_user( username=self.student_username, password=self.password) self.instructor_user = User.objects.create_user( username=self.instructor_username, password=self.password) self.client.login(username=self.student_username, password=self.password) # populate test database self.instructor = Instructor(user=self.instructor_user, wwuid='9994141') self.instructor.save() self.course = Course(name='Bounty Hunting 101') self.course.save() self.group = LabGroup(course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='4', group_name='Group A') self.group.save() self.student = Student(user=self.student_user, labgroup=self.group, wwuid='1111111') self.student.save() self.template = AssignmentTemplate(course=self.course, name='Royalty Kidnapping Section A') self.template.save() self.assignment = Assignment( assignment_template=self.template, labgroup=self.group, open_date=datetime.now(timezone(settings.TIME_ZONE)), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) self.assignment.save() self.assignment_entry = AssignmentEntry(student=self.student, assignment=self.assignment) self.assignment_entry.save() # retrieve the view self.view_name = 'api:assignment-entry-submit'
def post(self, request): """ Enroll in a labgroup. """ # ensure all parameters are present in the request data for param in ('wwuid', 'labgroup', 'enroll_key'): if param not in request.data.keys(): return Response(status=status.HTTP_400_BAD_REQUEST) # load the labgroup from the database try: labgroup = LabGroup.objects.get(id=request.data['labgroup']) except LabGroup.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) # check enroll key if request.data['enroll_key'] != labgroup.enroll_key: return Response(status=status.HTTP_403_FORBIDDEN) # delete student if it exists try: student = Student.objects.get(user=request.user) student.delete() except Student.DoesNotExist: pass # validate student data in request student_data = { 'user': request.user.id, 'labgroup': labgroup.id, 'wwuid': request.data['wwuid'], } serializer = StudentSerializer(data=student_data) if not serializer.is_valid(): return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) # create the student student = Student(user=request.user, labgroup=labgroup, wwuid=request.data['wwuid']) student.save() # add new student to the student group group = permissions.get_or_create_student_permissions() group.user_set.add(request.user) # return successful response return Response(status=status.HTTP_204_NO_CONTENT)
def post(self, request): statename = request.POST['statename'] collegename = request.POST['collegename'] hostelname = request.POST['hostelname'] branchname = request.POST['branchname'] studentname = request.POST['studentname'] studentrollno = request.POST['studentrollno'] studentemailid = request.POST['studentemailid'] studentpercentage = request.POST['studentpercentage'] studentbloodgp = request.POST['studentbloodgp'] studentyear = request.POST['studentyear'] studentroomno = request.POST['studentroomno'] candonateblood = request.POST['candonateblood'] print(statename + " \n" + collegename + " \n" + hostelname + " \n" + branchname + " \n" + studentname + " \n" + studentrollno + " \n" + studentemailid + " \n" + studentpercentage + " \n" + studentbloodgp + " \n" + studentyear + " \n" + studentroomno + " \n" + candonateblood) p = Student(statename=State.objects.get(statename=statename), collegename=College.objects.get(collegename=collegename), hostelname=Hostel.objects.get(hostelname=hostelname), branchname=Branch.objects.get(branchname=branchname), studentrollno=studentrollno, studentemailid=studentemailid, studentpercentage=studentpercentage, studentbloodgp=studentbloodgp, studentyear=studentyear, studentroomno=studentroomno, candonateblood=candonateblood) p.save() print(request.user.username) try: Postlogin.objects.get(username=request.user.username) except: Postlogin.objects.create(username=request.user.username) return render(request, "ui/profile.html")
def create_account (request): if request.method == "POST": print("POSTPOSTPOSTPOSTPOSTPOSTPOSTPOST") form = UserCreationForm(request.POST) print(form.is_valid()) print(request.POST) cleaned_data = form.clean() if cleaned_data.get('password') == cleaned_data.get('confirm_password'): token = Token.objects.get(token=cleaned_data.get('token')) username = token.username email = token.email first_name = token.first_name last_name = token.last_name isStudent = token.isStudent grade = token.grade git = cleaned_data.get('git') password = cleaned_data.get('password') user = User.objects.create_user(username=username, email=email, first_name=first_name, last_name=last_name, password=password) user.save() g, created = Group.objects.get_or_create(name='teachers') if isStudent: profile = Student(user=user, git=git, grade=grade, ion_user=username) else: profile = Teacher(user=user, git=git, ion_user=username) group = Group.objects.get(name='teachers') user.groups.add(group) profile.save() token.delete() print (user) messages.success(request, "Your SkoolOS account has successfully been created") return redirect(f'/login/?username={username}') else: print(form.errors) Token.objects.get(token=request.GET.get('token')).delete() messages.warning(request, "Passwords did not match!") return redirect('/register/') if request.method == "GET" and Token.objects.filter(token=request.GET.get('token')).count() == 1: print("GETGETGETGETGETGET") token = Token.objects.get(token=request.GET.get('token')) username = token.username email = token.email first_name = token.first_name last_name = token.last_name isStudent = token.isStudent grade = token.grade initial = { 'username': username, 'email': email, 'first_name': first_name, 'last_name': last_name, 'grade': grade, 'isStudent': isStudent, 'token': token.token, } form = UserCreationForm(initial=initial) return render(request, 'users/create_account.html', {'form': form}) messages.warning(request, "Invalid token") return redirect('/register/')
def import_data(request): form = ImportForm() if request.method == "POST": form = ImportForm(request.POST, request.FILES) if form.is_valid(): category = form.cleaned_data['category'] lines = process_csv_file(request, True) for line in lines: line = line.split(',') code = generate_student_code() admission_number = line[0] name = line[1].split(' ') first_name = name[0] last_name = name[1] date_of_birth = line[2] gender = line[3] school_class = line[4] stream = line[5] dormitory = line[6] religion = line[7] district = line[8] nationality = line[9] home_address = line[10] email = line[11] date_joined = line[12] class_joined = line[13] disabled = line[14] other_info = line[15] student_nin = line[16] school_id = 1 father_name = line[17] father_telephone = line[18] father_email = line[19] father_occupation = line[20] father_nin = line[21] mother_name = line[22] mother_telephone = line[23] mother_email = line[24] mother_occupation = line[25] mother_nin = line[26] if category == '1': student = Student(first_name=first_name, last_name=last_name, date_of_birth=date_of_birth, gender=gender, school_class=school_class, stream=stream, other_info=other_info, nin=student_nin, admission_number=admission_number, school_id=school_id, religion=religion, code=code) student.save() print(father_name) ''' add parents ''' if father_name: name = father_name.split(' ') user = User( email=father_email, username=father_email, is_active=False, first_name=name[0], last_name=name[1], ) user.save(commit=False) user.set_password('sw33th0m3') # user.save() father = Nok(name=father_name, student=student, occupation=father_occupation, relationship='Father', nin=father_nin, profile=Profile( user=user, type='Parent', )) father.save() if mother_name: name = mother_name.split(' ') user = User( email=mother_email, username=mother_email, is_active=False, first_name=name[0], last_name=name[1], ) user.save(commit=False) user.set_password('sw33th0m3') # user.save() mother = Nok(name=mother_name, student=student, occupation=mother_occupation, relationship='Mother', nin=mother_nin, profile=Profile( user=user, type='Parent', )) mother.save() return HttpResponseRedirect(reverse('thanks')) return render(request, 'admin/import.html', {'model': { 'form': form, 'action': 'import-data' }})
def create(self, validated_data): students = [Student(**item) for item in validated_data] return Student.objects.bulk_create(students)
def test_assignment_template_list_student(self): """ Tests that assignment templates are properly listed for students. """ # create a labgroup labgroup_1 = LabGroup(group_name='A', course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='ABC') labgroup_1.save() # create an unused labgroup labgroup_2 = LabGroup(group_name='B', course=self.course, instructor=self.instructor, term=get_current_term(), enroll_key='ABC') labgroup_2.save() # create student user student_user = User.objects.create_user(username='******', password=self.password) Student(user=student_user, wwuid='1111111', labgroup=labgroup_1).save() group = permissions.get_or_create_student_permissions() group.user_set.add(student_user) self.client.logout() self.client.login(username=student_user.username, password=self.password) # add assignment templates to database at1 = AssignmentTemplate(name='test name 1', course=self.course) at1.save() at2 = AssignmentTemplate(name='test name 2', course=self.course) at2.save() # assign student the first assignment template assignment = Assignment( assignment_template=at1, labgroup=labgroup_1, open_date=datetime.now(timezone(settings.TIME_ZONE)) - timedelta(days=1), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) assignment.save() # create different assignment Assignment(assignment_template=at2, labgroup=labgroup_2, open_date=datetime.now(timezone(settings.TIME_ZONE)) - timedelta(days=1), close_date=datetime.now(timezone(settings.TIME_ZONE)) + timedelta(days=1)) # request response = self.client.get(reverse(self.view_name)) response_body = json.loads(response.content.decode('utf-8')) # test response assignments = AssignmentTemplate.objects.all() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(len(response_body['templates']), 1) self.assertEqual(response_body['templates'][0]['pk'], assignments[0].id) self.assertEqual(response_body['templates'][0]['course'], assignments[0].course.id) self.assertEqual(response_body['templates'][0]['name'], assignments[0].name)