def test_user_profile(self): UserModel = get_user_model() user = UserModel(first_name=b'Sviatoslav', last_name='') instance = models.UserProfile() instance.user = user self.assertEqual(six.text_type(instance), u'User profile for Sviatoslav') user = UserModel(first_name=u'Sventoslav\u016d') instance = models.UserProfile() instance.user = user self.assertEqual(six.text_type(instance), u'User profile for Sventoslav\u016d')
def fill_user_profile(self, user): if not user.is_authenticated: user_profile = models.UserProfile() elif hasattr(user, 'profile'): user_profile = user.profile else: user_profile = models.UserProfile(user=user) for field_name in user_profile.get_field_names(): if field_name in self.cleaned_data: field_value = self.cleaned_data.get(field_name) # has_accepted_terms is a BooleanField in the database, so we need to # transform list to bool if field_name == 'has_accepted_terms': field_value = field_value[0] setattr(user_profile, field_name, field_value) return user_profile
def post(self, request): register_form = forms.RegisterForm(request.POST) if register_form.is_valid(): user_name = request.POST.get("username", "") if models.UserProfile.objects.filter(Q(username=user_name), Q(email=user_name)): return render(request, 'register.html', {"msg": "用户名已存在!"}) user_tel = request.POST.get("phone", "") user_pwd1 = request.POST.get("psw1", "") user_pwd2 = request.POST.get("psw2", "") if user_pwd1 != user_pwd2: return render(request, 'register.html', {"msg": "密码不一致!"}) user_profile = models.UserProfile() user_profile.username = user_name user_profile.mobile = user_tel user_profile.password = make_password(user_pwd1) user_profile.save() return HttpResponseRedirect(reverse("login")) return render(request, 'register.html', {'register_form': register_form})
def test_last_name(self): p = models.UserProfile(user=self.user) self.assertEqual(p.last_name(), "Last")
def test_first_name(self): p = models.UserProfile(user=self.user) self.assertEqual(p.first_name(), "First")
def test_email(self): p = models.UserProfile(user=self.user) self.assertEqual(p.email(), "*****@*****.**")