def regist(): form = RegistForm() if request.method == 'POST': username = form.username.data password = form.password.data email = form.email.data idcard = form.idcard.data bankcard = form.bankcard.data location = form.location.data tel = form.tel.data wallet = Wallet() shop = ShoppingCar() customer = User(username=username, email=email, password=password, idCard=idcard, bindBankCard=bankcard, location=location, role='user', wallet_id=wallet, shoppingcar=shop, tel=tel) #customer.encrypt_password(password) wallet.save() and shop.save() and customer.save() flash('注册成功 !') return redirect('/') return render_template('regist.html')
def createUser(request): if request.method == 'POST': form = UserCreationForm(request.POST) isAdmin = False if form.is_valid(): invite = None # Checking if user invite is enabled if getAdminOptions().inviteCodeEnabled: inviteCode = form.data.get('godFather') if InviteCode.objects.filter(code=inviteCode).count() == 1: invite = InviteCode.objects.get(code=inviteCode) if UserPreferences.objects.get( user=invite.user).group.permissions.filter( code="SPON").count() != 1: return render(request, 'signup.html', {'form': form}) else: return render(request, 'signup.html', {'form': form}) # FIXME: TOO LONG! Make smaller functions form.save() # Special condition for the first user to be administrator if User.objects.all().count() == 1: isAdmin = True populateDB() # Creating user if tests are ok username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) user.is_superuser = isAdmin user.save() # Setting the user preferences userPref = UserPreferences() userPref.user = user if isAdmin: userPref.group = Groups.objects.get(rank=5) else: userPref.group = Groups.objects.get(rank=1) wallet = Wallet() wallet.save() userPref.wallet = wallet if invite is not None: userPref.inviteCode = invite # creating user avatar userPref.avatar = identicon.create_identicon(username) userPref.save() createUserInviteCode(user) login(request, user) return HttpResponseRedirect(reverse('app:index')) else: form = UserCreationForm() return render(request, 'signup.html', {'form': form})
def get_or_create_wallet(user): try: wallet = Wallet.objects.get(user=user) except Wallet.DoesNotExist: # creating new wallet wallet = Wallet() wallet.user = user wallet.local_id = random.randint(1000000, 9999999) wallet.title = 'DUCK.{}'.format(wallet.local_id) wallet.display_name = None wallet.balance = 0 wallet.save() # print("New wallet id =", wallet.pk) return wallet