def student_create(request): body_unicode = request.body.decode('utf-8') body = json.loads(body_unicode) major = Major.objects.get(name=body['major']) level = Level.objects.get(level=body['level']) modeOfEntry = ModeOfEntry.objects.get(name=body['modeOfEntry']) dateBirth = body['dateBirth'] user = User() user.username = body['username'] user.password = dateBirth user.first_name = body['first_name'] user.last_name = body['last_name'] user.email = body['email'] user.type = '7' user.sex = body['sex'] user.save() student = Student() student.user = user student.major = major student.level = level student.modeOfEntry = modeOfEntry student.status = '1' student.dateBirth = dateBirth student.save() serial = StudentSerializer(student) return JsonResponse(serial.data, safe=False, status=200)
def form_valid(self, form): data = form.cleaned_data user = User(username=data.get('username')) user.first_name = data.get('firstname') user.last_name = data.get('lastname') user.email = data.get('email') user.set_password(data.get('password')) user.is_active = False user.save() if data.get('role') == RESEARCHER: researcher = Researcher() researcher.user = user researcher.phone = data.get('phone') researcher.save() elif data.get('role') == STUDENT: student = Student() student.user = user student.school = School.objects.get( id=data.get('school')) # <select name=xxxx> defines this student.role = PEERLEADER student.save() else: advisor = Advisor() advisor.user = user advisor.school = School.objects.get(id=data.get('school')) advisor.save() return super(RegisterView, self).form_valid(form)
def create_member(*args, **kwargs): if kwargs['is_new']: user = kwargs['user'] new_student = Student(name=user.first_name, surname=user.last_name, email=user.email) new_student.save(user=user)
def test_student_save_userprofile(self): """ Tests whether the save method for the Student class properly saves the user profile along with the Student object. """ s = Student(**self.student_data) s.save() self.assertEquals( s.userprofile.user.username, self.student_data['username'])
def home_(request): # return HttpResponse("<h1>Hello from home</h1>") queryset = Student.objects.all() plan = [] user = request.user.username print(request.FILES) if request.method == "POST" and request.POST['action'] == "Submit Info": degree = request.POST['major'] concentration = request.POST['concentration'] qua = request.POST['quarter'] numOfCourses = request.POST['numcourses'] summer = request.POST['summer?'] student_obj = Student(user=user, degree=degree, concentration=concentration, quarter=qua, numOfCourses=numOfCourses, summer=summer) student_obj.save() elif request.method == "POST" and request.POST['action'] == "Submit": some_var = request.POST.getlist('checks[]') plan = Algorithm(user, some_var).run() print(plan) return render(request, "algoOutput.html", {"plan": plan}) elif request.method == "POST" and request.POST['action'] == "Change Info": Student.objects.filter(user=user).delete() return render(request, "profile.html", {"title": "Profile"}) ##elif counter > 0: ## if request.POST['action'] == "Submit": ## counter = 1 ## else: ## counter = 0 ## Student.objects.filter(user).delete() ## return render(request, "profile.html", context) context = { "title": "Profile", "students": queryset, "plan": plan, } query = Student.objects.all().filter(user=user) if (query.count() > 0): queryUser = str(query[0]) else: return render(request, "profile.html", context) if (queryUser == user): return render(request, "EditProfile.html", context) else: return render(request, "profile.html", context)
def test_student_save_group(self): """ Tests whether the save method for the Student class properly initializes the user's group to Students. """ s = Student(**self.student_data) s.save() # Just one default group self.assertEqual( s.userprofile.user.groups.all().count(), 1, "Student not associated with one and only one group.") self.assertEqual( s.userprofile.user.groups.all()[0].name, u'Students', "Student not associated with 'Students' group.")
def test_active_property(self): s = Student(**self.student_data) s.save() m = Member.objects.get(pk=s.pk) # Check default self.assertFalse(m.active) m.active = True m.save() self.assertTrue(m.active) del m m = Member.objects.get(pk=s.pk) self.assertTrue(m.active) s = Student.objects.get(pk=m.pk) self.assertTrue(s.userprofile.user.is_active)
def test_username_property(self): """ Tests the behavior of the username property: it needs to properly change the user's username and return the new value. """ s = Student(**self.student_data) s.save() m = Member.objects.get(pk=s.pk) self.assertEqual(m.username, s.username) new_username = '******' m.username = '******' m.save() self.assertEqual(m.username, new_username) del m m = Member.objects.get(pk=s.pk) self.assertEqual(m.username, new_username) s = Student.objects.get(pk=m.pk) self.assertEqual(m.username, s.userprofile.user.username)
def form_valid(self, form): data = form.cleaned_data phones = [] first_names = [] last_names = [] default_name = 'N/A' for phone in data['phone_numbers'].split(','): phones.append(phone.strip()) for fn in data['first_names'].split(','): if not (fn.strip()): # if empty string, then default to 'N/A' first_names.append(default_name) else: first_names.append(fn.strip()) for ln in data['last_names'].split(','): if not (ln.strip()): # if empty string, then default to 'N/A' last_names.append(default_name) else: last_names.append(ln.strip()) #qs = User.objects.filter(username__in=phones) # this is not specific to school, so commented out qs = Student.objects.filter(user__username__in=phones, school__id=self.request.GET['school_id']) if qs: # TODO: refactor duplicate_nums = set(student.user.username for student in qs) duplicate_nums_str = ", ".join(duplicate_nums) error_msg = "Phone number(s): " + duplicate_nums_str + " already belong(s) to this organization. " + \ "If you'd like to update the info--such as first/last names--of these " + \ "number(s), please delete/remove first from the table above, and add them as a new entry. " + \ "Otherwise, remove the phone number(s) from the list below, and try again." form._errors[forms.forms.NON_FIELD_ERRORS] = ErrorList([error_msg]) return self.form_invalid(form) # We'll base block data entry on phone numbers because # they are the life-blood of our program len_first_names = len(first_names) len_last_names = len(last_names) for idx, phone in enumerate(phones): if not User.objects.filter(username=phone).exists(): user = User(username=phone) user.first_name = first_names[idx] if len_first_names > idx else default_name user.last_name = last_names[idx] if len_last_names > idx else default_name user.save() person = Student(user=user) person.school = School.objects.get(id=self.request.GET['school_id']) person.role = data['role'] person.save() else: # TODO: refactor this error_msg = "PHONE NUMBER ALREADY EXISTS: (" + phone + ") already exist in the database." # means user exists in the DB, but is NOT advisor (probably student or something else) if Advisor.objects.filter(user__username=phone).exists(): school_name = Advisor.objects.filter(user__username=phone).last().school.name error_msg += " We located it in organization named: " + school_name + "." elif Student.objects.filter(user__username=phone).exists(): school_name = Student.objects.filter(user__username=phone).last().school.name error_msg += " We located it in organization named: " + school_name + "." error_msg += " We do not allow duplicate phone numbers in the user database." + \ " If you'd like to add the user to this organization, please remove " + \ "him/her from the other organization and try again." form._errors[forms.forms.NON_FIELD_ERRORS] = ErrorList([error_msg]) return self.form_invalid(form) return HttpResponseRedirect(self.get_success_url())
else: print(student.name + " has no change") if no_insert_flag == 1: # skips to next. continue try: print('Creating user {0}.'.format(username)) user = User.objects.create_user(username=username) user.set_password(password) user.is_student=True user.save() assert authenticate(username=username, password=password) print('User {0} successfully created.'.format(username)) # create student - onetoneField with user student = Student(user=user) student.student_no = student_no student.name = name student.major = Major.objects.get(title=major) student.section = Section.objects.get(section_no=section, course__year=year, course__title=title) student.group = Group.objects.get(section__section_no=section, section__course__year=year, section__course__title=title, group_no=group) student.save() except: print('There was a problem creating the user: {0}. Error: {1}.' \ .format(username, sys.exc_info()[1]))