Beispiel #1
0
def register():
    form = RegisterForm(request.form)
    if current_user.is_authenticated:
        return redirect(url_for('client.index'))
    if request.method == 'GET':
        return render_template('create-account.html', form=form)
    else:
        # if form.validate_on_submit():
        if request.method == 'POST':
            email = request.form['email']
            if Customer.get(email=email):
                flash("This email has been used", 'danger')
                return redirect(url_for('.register'))
            else:
                user = Customer.create(first_name=request.form['name'],
                                email=request.form['email'], is_active=True,
                                password=generate_password_hash(request.form['password']))
                login_user(user, remember=True)
                g.user = user
                if session.get('session_id'):
                    wishlist_products = WishList.all(session_id=session.get('session_id'))
                    cart_products = Cart.all(session_id=session.get('session_id'))
                    for product in wishlist_products:
                        product.update(customer_id=current_user.id)
                    for product in cart_products:
                        product.update(customer_id=current_user.id)
                return redirect(url_for('.index'))
        return render_template('create-account.html', form=form)
Beispiel #2
0
def put(customer_id, body):
    customer_data = body
    customer = Customer.get(id=customer_id)
    customer.update(**customer_data)
    response = {'data': serializer.dump(customer)}

    return response, 200
Beispiel #3
0
 def post(self):
     form = self.form
     if form.validate_on_submit():
         phone_number = form.data['phone_number']
         user = Customer.get(phone_number=phone_number)
         code = generate_code()
         if user is None:
             message = send_whatsapp(phone_number, code)
             if message.error_code is None:
                 sms = CustomerSms.create(phone_number=phone_number, code=code)
             else:
                 flash('Couldnt send sms', 'danger')
                 return redirect(url_for('.auth'))
             customer = Customer.create(phone_number=phone_number)
             return redirect(url_for('.verify', next_page=request.args.get('next')))
         else:
             message = send_whatsapp(phone_number, code)
             if message.error_code is None:
                 sms = CustomerSms.create(phone_number=phone_number, code=code)
             else:
                 flash('Couldnt send sms', 'danger')
                 return redirect(url_for('.auth'))
             return redirect(url_for('.verify', next_page=request.args.get('next')))
     return self.get()
Beispiel #4
0
 def post(self):
     form = self.form
     if form.validate_on_submit():
         sms = CustomerSms.get(code=request.form.get('code'))
         if sms is None:
             flash("Invalid code", 'danger')
             return redirect(url_for('.verify'))
         user = Customer.get(phone_number=sms.phone_number)
         login_user(user, remember=True)
         g.user = user
         sms.delete()
         if session.get('order') is not None:
             order = Order.get(id=session['order'])
             payload = {"customer_id": user.id}
             order.update(**payload)
             session.pop("order", None)
         next_page = request.args.get('next')
         if not next_page or url_parse(next_page).netloc != '':
             next_page = url_for('.index')
         return redirect(next_page)
     return self.get()
Beispiel #5
0
def login():
    if current_user.is_authenticated:
        return redirect(url_for('client.index'))
    if request.method == 'GET':
        return render_template('login.html')
    else:
        email = request.form['email']
        user = Customer.get(email=email)
        if user is None:
            flash("Invalid username or password", 'danger')
            return redirect(url_for('.login'))
        else:
            if user.check_password(request.form['password']):
                login_user(user, remember=True)
                g.user = user
                next_page = request.args.get('next')
                if not next_page or url_parse(next_page).netloc != '':
                    next_page = url_for('.index')
                return redirect(next_page)
            else:
                return 'invalid'
                flash("Invalid username or password", 'danger')
                return redirect(url_for('.login'))
Beispiel #6
0
def post(body):
    customer_data = body
    customer = Customer.create(**customer_data)
    response = {'data': serializer.dump(customer)}

    return response, 201
Beispiel #7
0
def get(customer_id):
    customer = Customer.get(id=customer_id)
    response = {'data': serializer.dump(customer)}
    return response, 200
Beispiel #8
0
def load_user(user_id):
    return Customer.get(id=user_id)