def signup(request): if request.user.is_authenticated(): return HttpResponseRedirect(reverse('core:index')) state = None if request.method == 'POST': password = request.POST.get('password', '') repeat_password = request.POST.get('repeat_password', '') if password == '' or repeat_password == '': state = 'empty' elif password != repeat_password: state = 'repeat_error' else: username = request.POST.get('username', '') if User.objects.filter(username=username): state = 'user_exist' else: new_user = User.objects.create_user(username=username, password=password, email=request.POST.get('email', '')) new_user.save() new_my_user = MyUser(user=new_user, nickname=request.POST.get('nickname', '')) new_my_user.save() state = 'success' content = { 'state': state, 'user': None, } return render(request, 'core/signup.html', content)
def setUp(self): email = '*****@*****.**' raw_password = '******' user = MyUser(email=email) user.set_password(raw_password) user.save() self.user = user
def get_headers_for_user(*, client: APIClient, user: MyUser) -> List[Tuple[bytes, bytes]]: password = "******" user.set_password(password) user.save() client.login(username=user.email, password=password) assert user.is_authenticated return [ (b"origin", b"localhost"), (b"cookie", client.cookies.output(header="", sep="; ").encode()), ]
def create(self, validated_data): """Create a new user with encrypted password and return it""" user = MyUser( name=validated_data['name'], username=validated_data['username'], email=validated_data['email'], gender=validated_data['gender'], ) user.set_password(validated_data['password']) user.save() return user
def home(request): regform = MyUserCreationForm() signinform = SigninForm() if request.user.is_authenticated: return redirect('wall') if request.method == 'POST': regform = MyUserCreationForm(request.POST) if regform.is_valid(): data = regform.cleaned_data valid = True if MyUser.objects.filter(username=data['username']): regform.add_error('username', 'We already have a user with this email!') valid = False if MyUser.objects.filter(mefi_handle=data['mefi_handle']): regform.add_error( 'mefi_handle', 'We already have a user with this MeFi username!') valid = False if valid: user = MyUser( username=data['username'], mefi_handle=data['mefi_handle'], platform=data['platform'], ) user.set_password(data['password1']) user.save() group, created = AdminGroup.objects.get_or_create( name='participants') user.groups.add(group) user.save() login(request, user) return redirect('mixcreate') return render(request, 'core/home.html', locals())
class DiagnosticTestCase(TestCase): def setUp(self): self.client = Client() self.user = MyUser( username='******', email='*****@*****.**', is_active=True, is_staff=True, is_superuser=True, ) self.user.set_password('test') self.user.save() self.client.login(username='******', password='******') teacher = MyUser.objects.create(username='******') students_lst = [] groups_lst = [] for counter in range(10): students_lst.append(Student(id=counter)) groups_lst.append(Group(id=counter, teacher=teacher)) Student.objects.bulk_create(students_lst) for group in groups_lst: group.students.set(students_lst) Group.objects.bulk_create(groups_lst) def test_groupsandstudents_page(self): response = self.client.get( reverse_lazy('core:groupsandstudents', kwargs={'username': '******'})) self.assertIn('user', response.context) self.assertEqual(response.status_code, 200) def test_groupsandstudents_groups_count(self): response = self.client.get( reverse_lazy('core:groupsandstudents', kwargs={'username': '******'})) self.assertEqual(len(response.context['groups']), 10) def test_groupsandstudents_students_count(self): response = self.client.get( reverse_lazy('core:groupsandstudents', kwargs={'username': '******'})) groups = response.context['groups'] for group in groups: self.assertEqual(group.students.count(), 10) def test_home_page(self): response = self.client.get(reverse_lazy('core:home')) self.assertEqual(response.status_code, 200) def test_teachers_page(self): response = self.client.get(reverse_lazy('core:teachers')) self.assertEqual(response.status_code, 200) def test_allteachers_page(self): response = self.client.get(reverse_lazy('core:allteachers')) self.assertEqual(response.status_code, 200) def test_students_page(self): response = self.client.get(reverse_lazy('core:students')) self.assertEqual(response.status_code, 200) def test_create_teacher_page(self): response = self.client.get(reverse_lazy('core:create-teacher')) self.assertEqual(response.status_code, 200) def test_create_student_page(self): response = self.client.get(reverse_lazy('core:create-student')) self.assertEqual(response.status_code, 200) def test_create_group_page(self): response = self.client.get(reverse_lazy('core:create-group')) self.assertEqual(response.status_code, 200) def test_update_teacher_page(self): response = self.client.get( reverse_lazy('core:update-teacher', kwargs={'username': '******'})) self.assertEqual(response.status_code, 200) def test_update_group_page(self): group = Group.objects.first() response = self.client.get( reverse_lazy('core:update-group', kwargs={'group_id': group.id})) self.assertEqual(response.status_code, 200) def test_update_student_page(self): student = Student.objects.first() response = self.client.get( reverse_lazy('core:update-student', kwargs={'student_id': student.id})) self.assertEqual(response.status_code, 200) def test_delete_student_page(self): student = Student.objects.first() response = self.client.get( reverse_lazy('core:delete-student', kwargs={'student_id': student.id})) self.assertEqual(response.status_code, 200) def test_delete_group_page(self): group = Group.objects.first() response = self.client.get( reverse_lazy('core:delete-group', kwargs={'group_id': group.id})) self.assertEqual(response.status_code, 200) def test_delete_teacher_page(self): response = self.client.get( reverse_lazy('core:delete-teacher', kwargs={'username': '******'})) self.assertEqual(response.status_code, 200) def test_contact_page(self): response = self.client.get(reverse_lazy('core:contact')) self.assertEqual(response.status_code, 200) def test_success_page(self): response = self.client.get(reverse_lazy('core:success')) self.assertEqual(response.status_code, 200) def test_registration_page(self): response = self.client.get(reverse_lazy('core:registration')) self.assertEqual(response.status_code, 200) def test_admin_page(self): response = self.client.get('/admin/') self.assertEqual(response.status_code, 200)