def save(self, user=None): if not user: user = User() if not user.pk: action_flag = ADDITION else: action_flag = CHANGE user = self.save_user(user) if has_active_business_profile(user): businessprofile = user.get_business_profile() else: businessprofile = BusinessProfile() businessprofile.creator = user.userprofile LogEntry.objects.log_action( user_id = user.pk, content_type_id = ContentType.objects.get_for_model(user.userprofile).pk, object_id = user.userprofile.pk, object_repr = '(Frontend)' + force_unicode(user.userprofile), action_flag = action_flag ) businessprofile = self.save_businessprofile(businessprofile) if not has_active_business_profile(user): user.userprofile.active_profile = businessprofile user.userprofile.business_profiles.add(businessprofile) user.userprofile = self.save_userprofile(user.userprofile) return user
def register(request): if request.method == 'POST': print("method is post.") login_form = LoginForm(request.POST) register_form = RegistrationForm(request.POST) if register_form.is_valid(): user = User() user.username = register_form.cleaned_data['username'] user.set_password(register_form.cleaned_data['password']) user.email = register_form.cleaned_data['email'] user.save() userprofile = UserProfile() userprofile.user = user userprofile.birthday = register_form.cleaned_data['birthday'] print(userprofile.birthday) userprofile.alias = register_form.cleaned_data['alias'] userprofile.save() user.userprofile = userprofile print("New user created.") return HttpResponseRedirect('/profile/') return HttpResponseRedirect('/login/')
def signup(req: HttpRequest): user_exists = 'نام کاربری شما در سیستم موجود است' password_not_match = 'گذرواژه و تکرار گذرواژه یکسان نیستند' if req.method == "POST": data = req.POST my_user_name = data.get('username') pass1 = data.get('password1') pass2 = data.get('password2') if User.objects.filter(username=my_user_name).count() != 0: return render(req, "signup.html", context={'error': user_exists}) if pass1 != pass2: return render(req, "signup.html", context={'error': password_not_match}) user = User(username=my_user_name, first_name=data.get('first_name'), last_name=data.get('last_name'), email=data.get('email')) user.set_password(pass1) user.save() profile = UserProfile() user.userprofile = profile user.save() return redirect('/') else: return render(req, "signup.html", context={})
def test_o2o_cross_database_protection(self): "Operations that involve sharing FK objects across databases raise an error" # Create a user and profile on the default database alice = User.objects.db_manager('default').create_user( 'alice', '*****@*****.**') # Create a user and profile on the other database bob = User.objects.db_manager('other').create_user( 'bob', '*****@*****.**') # Set a one-to-one relation with an object from a different database alice_profile = UserProfile.objects.using('default').create( user=alice, flavor='chocolate') try: bob.userprofile = alice_profile self.fail("Shouldn't be able to assign across databases") except ValueError: pass # BUT! if you assign a FK object when the base object hasn't # been saved yet, you implicitly assign the database for the # base object. bob_profile = UserProfile.objects.using('other').create( user=bob, flavor='crunchy frog') new_bob_profile = UserProfile(flavor="spring surprise") charlie = User(username='******', email='*****@*****.**') charlie.set_unusable_password() # initially, no db assigned self.assertEquals(new_bob_profile._state.db, None) self.assertEquals(charlie._state.db, None) # old object comes from 'other', so the new object is set to use 'other'... new_bob_profile.user = bob charlie.userprofile = bob_profile self.assertEquals(new_bob_profile._state.db, 'other') self.assertEquals(charlie._state.db, 'other') # ... but it isn't saved yet self.assertEquals( list( User.objects.using('other').values_list('username', flat=True)), [u'bob']) self.assertEquals( list( UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy frog']) # When saved (no using required), new objects goes to 'other' charlie.save() bob_profile.save() new_bob_profile.save() self.assertEquals( list( User.objects.using('default').values_list('username', flat=True)), [u'alice']) self.assertEquals( list( User.objects.using('other').values_list('username', flat=True)), [u'bob', u'charlie']) self.assertEquals( list( UserProfile.objects.using('default').values_list('flavor', flat=True)), [u'chocolate']) self.assertEquals( list( UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy frog', u'spring surprise']) # This also works if you assign the O2O relation in the constructor denise = User.objects.db_manager('other').create_user( 'denise', '*****@*****.**') denise_profile = UserProfile(flavor="tofu", user=denise) self.assertEquals(denise_profile._state.db, 'other') # ... but it isn't saved yet self.assertEquals( list( UserProfile.objects.using('default').values_list('flavor', flat=True)), [u'chocolate']) self.assertEquals( list( UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy frog', u'spring surprise']) # When saved, the new profile goes to 'other' denise_profile.save() self.assertEquals( list( UserProfile.objects.using('default').values_list('flavor', flat=True)), [u'chocolate']) self.assertEquals( list( UserProfile.objects.using('other').values_list('flavor', flat=True)), [u'crunchy frog', u'spring surprise', u'tofu'])
def test_o2o_cross_database_protection(self): "Operations that involve sharing FK objects across databases raise an error" # Create a user and profile on the default database alice = User.objects.db_manager('default').create_user('alice', '*****@*****.**') # Create a user and profile on the other database bob = User.objects.db_manager('other').create_user('bob', '*****@*****.**') # Set a one-to-one relation with an object from a different database alice_profile = UserProfile.objects.using('default').create(user=alice, flavor='chocolate') try: bob.userprofile = alice_profile self.fail("Shouldn't be able to assign across databases") except ValueError: pass # BUT! if you assign a FK object when the base object hasn't # been saved yet, you implicitly assign the database for the # base object. bob_profile = UserProfile.objects.using('other').create(user=bob, flavor='crunchy frog') new_bob_profile = UserProfile(flavor="spring surprise") new_bob_profile.save(using='other') charlie = User(username='******',email='*****@*****.**') charlie.set_unusable_password() charlie.save(using='other') # old object comes from 'other', so the new object is set to use 'other'... new_bob_profile.user = bob charlie.userprofile = bob_profile self.assertEqual(new_bob_profile._state.db, 'other') self.assertEqual(charlie._state.db, 'other') # When saved (no using required), new objects goes to 'other' bob_profile.save() self.assertEqual(list(User.objects.using('default').values_list('username',flat=True)), ['alice']) self.assertEqual(list(User.objects.using('other').values_list('username',flat=True)), ['bob', 'charlie']) self.assertEqual(list(UserProfile.objects.using('default').values_list('flavor',flat=True)), ['chocolate']) self.assertEqual(list(UserProfile.objects.using('other').values_list('flavor',flat=True)), ['crunchy frog', 'spring surprise']) # This also works if you assign the O2O relation in the constructor denise = User.objects.db_manager('other').create_user('denise','*****@*****.**') denise_profile = UserProfile(flavor="tofu", user=denise) self.assertEqual(denise_profile._state.db, 'other') # ... but it isn't saved yet self.assertEqual(list(UserProfile.objects.using('default').values_list('flavor',flat=True)), ['chocolate']) self.assertEqual(list(UserProfile.objects.using('other').values_list('flavor',flat=True)), ['crunchy frog', 'spring surprise']) # When saved, the new profile goes to 'other' denise_profile.save() self.assertEqual(list(UserProfile.objects.using('default').values_list('flavor',flat=True)), ['chocolate']) self.assertEqual(list(UserProfile.objects.using('other').values_list('flavor',flat=True)), ['crunchy frog', 'spring surprise', 'tofu'])