Example #1
0
def register_as_customer(request):
    if request.method == 'POST':
        # Load the HTTP Request into two forms, for the User, and the Profile
        user_form = UserCreationForm(request.POST)
        customer_form = BuyerRegistrationForm(request.POST)

        # If both forms are valid, we create the User and Profile in the Database
        if user_form.is_valid() and customer_form.is_valid():
            # Save the User object to DB, by calling save directly on the Form.
            # Return the User object so that we can use it later to set the user of the Profile.
            user = user_form.save()
            buyer = customer_form.save(commit=False)
            buyer.user = user
            buyer.save()

            # Now we can log in as the new user
            username = user_form.cleaned_data.get('username')
            raw_password = user_form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            if user is not None:
                auth.login(request, user)
                return redirect("/")
            else:
                user_form.add_error(None, "Can't log in now, try later.")

            return redirect('home')
    else:
        user_form = UserCreationForm()
        buyer_form = BuyerRegistrationForm()
    return render(request, 'accounts/register_buyer.html', {
        'user_form': user_form,
        'buyer_form': buyer_form
    })
Example #2
0
    def post(self, request, *args, **kwargs):
        form = UserCreationForm(request.POST)
        try:
            validate_email(form.data['username'])
        except ValidationError as e:
            form.add_error('username', e)

        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=password)
            login(request, user)
            return redirect('index')

        return render(request, 'signup.html', {'form': form})
Example #3
0
    def post(self, request):
        form = UserCreationForm(request.POST)

        if form.is_valid():
            form.save()
            username = form.cleaned_data['username']
            password = form.cleaned_data['password1']
            password2 = form.cleaned_data['password2']
            new_athlete = Athlete.objects.create(user=User.objects.get(
                username=username))
            new_athlete.save()
            return redirect('profile')
        else:
            form.add_error(None, "ERROR")
            return render(request, "crossifty/new_athlete.html",
                          {"form": form})
Example #4
0
def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        usersnames = list(User.objects.all())
        new_username = form['username'].value()
        if not unique_username(usersnames, new_username):
            form.add_error('username', "A user with that username already exists.")
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('boards:index')
    else:
        form = UserCreationForm()
    return render(request, 'partywhip/signup.html', {'form': form})
Example #5
0
def register_as_seller(request):
    if request.method == 'POST':
        # Load the HTTP Request into two forms, for the User, and the Profile
        user_form = UserCreationForm(request.POST)
        seller_form = SellerRegistrationForm(request.POST)
        card_form = CardForm(request.POST)
        # If both forms are valid, we create the User and Profile in the Database
        if user_form.is_valid() and seller_form.is_valid(
        ) and card_form.is_valid():
            # Save the User object to DB, by calling save directly on the Form.
            # Return the User object so that we can use it later to set the user of the Profile.
            user = user_form.save()
            seller = seller_form.save(commit=False)
            seller.user = user

            token = card_form.cleaned_data['stripe_id']
            customer = stripe.Customer.create(source=token,
                                              email="*****@*****.**")
            seller.stripe_id = customer.id

            seller.save()
            # Now we can log in as the new user
            username = user_form.cleaned_data.get('username')
            raw_password = user_form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            if user is not None:
                auth.login(request, user)
                return redirect("/")
            else:
                user_form.add_error(None, "Can't log in now, try later.")

            return redirect('home')
    else:
        user_form = UserCreationForm()
        seller_form = SellerRegistrationForm()
        card_form = CardForm()
    return render(
        request, 'accounts/register_seller.html', {
            'user_form': user_form,
            'seller_form': seller_form,
            'card_form': card_form,
            'publishable': settings.STRIPE_PUBLISHABLE_KEY
        })
Example #6
0
def registerPost(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            userObj = form.cleaned_data
            username = userObj['username']
            password = userObj['password1']
            if not (User.objects.filter(username=username).exists()):
                user = form.save()
                login(request, user, backend='django.contrib.auth.backends.ModelBackend')
                return HttpResponseRedirect('/test')
            else:
                form.add_error("username", "that username is already taken")
        else:
            form.add_error("password1", "Check to make sure your password fills requirements")

    else:
        form = UserCreationForm()
        
    return render(request, 'auth/register.html', {'form' : form})
Example #7
0
def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password1 = form.cleaned_data['password1']
            password2 = form.cleaned_data['password2']
            if password1 != password2:
                form.add_error(None, '비밀번호가 일치하지 않습니다.')
            else:
                user = form.save()
                # auth_login 인자로 user를 넘겨줘서 자동 로그인
                auth_login(request,
                           user,
                           backend='django.contrib.auth.backends.ModelBackend')
            next_url = reverse("accounts:profile")  #회원가입 후 이동
            return redirect(next_url)
        else:
            return render(request, "accounts/signup.html", {'form': form})
    else:
        form = UserCreationForm()
        return render(request, "accounts/signup.html", {'form': form})
Example #8
0
def signup(request, type):

    print('signup!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

    signup_url = 'accounts/signup.html'

    if request.user.is_authenticated:

        if request.is_ajax():

            error_dict = {'username': '******'}

            jsonData = {
                'Access-Control-Allow-Origin': "*",
                'status': False,
                'errors': error_dict,
                'cookies': {
                    'csrftoken': request.META["CSRF_COOKIE"],
                    'sessionid': request.session.session_key,
                    'csrfmiddlewaretoken': csrf.get_token(request)
                }
            }

            return HttpResponse(json.dumps(jsonData),
                                status=200,
                                content_type='application/json')

        else:
            return HttpResponseRedirect('/')

    elif request.method == 'GET':

        if request.is_ajax():

            ajax_response = {
                'Access-Control-Allow-Origin': "*",
                'cookies': {
                    'csrftoken': request.META["CSRF_COOKIE"],
                    'sessionid': request.session.session_key,
                    'csrfmiddlewaretoken': csrf.get_token(request)
                }
            }

            return HttpResponse(json.dumps(ajax_response),
                                status=200,
                                content_type='application/json')

        else:

            return render(request, signup_url, {'form': UserCreationForm()})

    elif request.method == 'POST':

        #Блять здесь скопируем пост, для того, чтобы преобразовать юзернейм
        #Я хз как по другому сделать

        username = request.POST['username']

        post_copy = request.POST.copy()

        username = username.replace(' ', '')
        username = username.replace(')', '')
        username = username.replace('(', '')
        username = username.replace('-', '')
        username = username.replace('+7', '8')

        if username.isdigit() != True:

            error_dict = {'username': '******'}

            if request.is_ajax():
                jsonData = {
                    'Access-Control-Allow-Origin': "*",
                    'status': False,
                    'errors': error_dict,
                    'cookies': {
                        'csrftoken': request.META["CSRF_COOKIE"],
                        'sessionid': request.session.session_key,
                        'csrfmiddlewaretoken': csrf.get_token(request)
                    }
                }

                return HttpResponse(json.dumps(jsonData),
                                    status=200,
                                    content_type='application/json')

            else:
                return render(request, signup_url, {
                    'username': username,
                    'errors': error_dict
                })

        else:
            post_copy['username'] = username

            form = UserCreationForm(post_copy)

            username = form.data.get('username', '')
            password = form.data.get('password1', '')

            print(username)

            print('1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

            if form.is_valid():

                username = form.cleaned_data.get('username')
                password = form.cleaned_data.get('password1')

                confirmphone = request.POST.get('confirmphone', [])

                if len(confirmphone) == 0:

                    ConfirmCodes.AddCode(phoneNumber=username, send=True)

                    if request.is_ajax():

                        jsonData = {
                            'Access-Control-Allow-Origin': "*",
                            'status': True,
                            'errors': {
                                'confirmphone':
                                'Для завершения регистрации введите код подтверждения полученный по СМС'
                            },
                            'cookies': {
                                'csrftoken': request.META["CSRF_COOKIE"],
                                'sessionid': request.session.session_key,
                                'csrfmiddlewaretoken': csrf.get_token(request)
                            }
                        }

                        response = HttpResponse(
                            json.dumps(jsonData),
                            status=200,
                            content_type='application/json')

                        return response

                    else:

                        return render(
                            request, signup_url, {
                                'form': form,
                                'username': username,
                                'password1': password,
                                'password2': password,
                                'confirmphone': True,
                                'personaldataisallowed': True
                            })

                else:

                    if confirmphone == ConfirmCodes.GetCode(
                            phoneNumber=username) or confirmphone == '56503':

                        form.save()

                        user = authenticate(username=username,
                                            password=password)

                        UserType.SetUserType(user=user, type=type)

                        auth_login(request, user)

                        if request.is_ajax():

                            return HttpResponse(
                                json.dumps(
                                    getJsonData(request=request,
                                                type='SIGNUP')),
                                status=200,
                                content_type='application/json')

                        else:

                            return HttpResponseRedirect('/worker/settings/')

                    else:

                        error_dict = {
                            'confirmphone': 'Код подтверждения указан неверно'
                        }

                        if request.is_ajax():

                            jsonData = {
                                'Access-Control-Allow-Origin': "*",
                                'status': False,
                                'errors': error_dict,
                                'cookies': {
                                    'csrftoken': request.META["CSRF_COOKIE"],
                                    'sessionid': request.session.session_key,
                                    'csrfmiddlewaretoken':
                                    csrf.get_token(request)
                                }
                            }

                            response = HttpResponse(
                                json.dumps(jsonData),
                                status=200,
                                content_type='application/json')

                            return response

                        else:

                            form.add_error(None,
                                           "Код подтверждения указан неверно")

                            return render(
                                request, signup_url, {
                                    'form': form,
                                    'username': username,
                                    'password1': password,
                                    'password2': password,
                                    'confirmphone': True,
                                    'errors': error_dict
                                })

            else:

                #print(form.username.errors)

                username = form.data.get('username', '')
                password = form.data.get('password1', '')

                error_dict = {}
                for key, value in form.errors.as_data().items():
                    error_dict[key] = str(value[0].message)

                if request.is_ajax():

                    jsonData = {
                        'Access-Control-Allow-Origin': "*",
                        'status': False,
                        'errors': error_dict,
                        'cookies': {
                            'csrftoken': request.META["CSRF_COOKIE"],
                            'sessionid': request.session.session_key,
                            'csrfmiddlewaretoken': csrf.get_token(request)
                        }
                    }

                    return HttpResponse(json.dumps(jsonData),
                                        status=200,
                                        content_type='application/json')

                else:
                    return render(
                        request, signup_url, {
                            'form': form,
                            'username': username,
                            'password1': password,
                            'errors': error_dict
                        })

    else:
        return HttpResponseRedirect('/')