Example #1
0
def get_product(id):
    product = utils.select_one_or_404('products', {'id': id})
    images = utils.select('product_images', {'product_id': id})

    c = Product(product.id, product.name, product.price, product.description,
                [image.filename for image in images])
    return c
Example #2
0
def edit(id):
    user = select_one_or_404('users', {'id': id})
    form = UserForm(obj=user)
    if form.validate_on_submit():
        try:
            data = {}
            if form.data['password'] != '' and form.data['confirm'] != '':
                m = hashlib.md5()
                password = form.data['password'].encode('utf-8')
                m.update(password)
                hashpass = m.hexdigest()
                data['password'] = hashpass

            if form.data['username'] != user.username:
                data['username'] = form.data['username']

            if data:
                update('users', {'id': id}, data)
        except IntegrityError as e:
            if 'username_unique' in e.pgerror:
                getattr(form, 'username').errors = (
                    'User with given name already exists.',
                )
            else:
                flash('Something went wrong, try again.', 'error')
    return render_template('users/create.html', form=form)
Example #3
0
def edit(id):
    user = select_one_or_404('users', {'id': id})
    form = UserForm(obj=user)
    if form.validate_on_submit():
        try:
            data = {}
            if form.data['password'] != '' and form.data['confirm'] != '':
                m = hashlib.md5()
                password = form.data['password'].encode('utf-8')
                m.update(password)
                hashpass = m.hexdigest()
                data['password'] = hashpass

            if form.data['username'] != user.username:
                data['username'] = form.data['username']

            if data:
                update('users', {'id': id}, data)
        except IntegrityError as e:
            if 'username_unique' in e.pgerror:
                getattr(form, 'username').errors = (
                    'User with given name already exists.', )
            else:
                flash('Something went wrong, try again.', 'error')
    return render_template('users/create.html', form=form)
Example #4
0
def get_order(id):
    order = select_one_or_404('orders', {'id': id})
    order_products = select('order_products', {'order_id': id})
    status = select(
        'order_statuses',
        {'id': order.status_id}
    )[0].status_name
    return Order(order.id, order.client_id, order_products, status)
Example #5
0
def get_product(id):
    product = utils.select_one_or_404('products', {'id': id})
    images = utils.select('product_images', {'product_id': id})

    c = Product(
        product.id,
        product.name,
        product.price,
        product.description,
        [image.filename for image in images]
    )
    return c
Example #6
0
def get_client(id):
    client = utils.select_one_or_404('clients', {'id': id})
    phones = utils.select('client_phones', {'client_id': id})
    billing_address = utils.select(
        'client_addresses', {'client_id': id, 'type': 'billing'}
    )
    delivery_address = utils.select(
        'client_addresses', {'client_id': id, 'type': 'delivery'}
    )
    b_address = None
    if billing_address:
        billing_addr = utils.select('addresses', {'id': billing_address[0].id})
        if billing_addr:
            b_address = Address(billing_addr[0])

    d_addr = None
    if delivery_address:
        delivery_addr = utils.select(
            'addresses', {'id': delivery_address[0].id}
        )
        if delivery_addr:
            d_addr = Address(delivery_addr[0])

    Client = namedtuple(
        'Client',
        [
            'id',
            'name',
            'surname',
            'email',
            'phones',
            'billing_address',
            'delivery_address'
        ]
    )
    client = Client(
        id=client.id,
        name=client.name,
        surname=client.surname,
        email=client.email,
        phones=[{'id': phone.id, 'phone': phone.phone} for phone in phones],
        billing_address=b_address,
        delivery_address=d_addr
    )
    c = Cl(client)
    return c
Example #7
0
def get_client(id):
    client = utils.select_one_or_404('clients', {'id': id})
    phones = utils.select('client_phones', {'client_id': id})
    billing_address = utils.select('client_addresses', {
        'client_id': id,
        'type': 'billing'
    })
    delivery_address = utils.select('client_addresses', {
        'client_id': id,
        'type': 'delivery'
    })
    b_address = None
    if billing_address:
        billing_addr = utils.select('addresses', {'id': billing_address[0].id})
        if billing_addr:
            b_address = Address(billing_addr[0])

    d_addr = None
    if delivery_address:
        delivery_addr = utils.select('addresses',
                                     {'id': delivery_address[0].id})
        if delivery_addr:
            d_addr = Address(delivery_addr[0])

    Client = namedtuple('Client', [
        'id', 'name', 'surname', 'email', 'phones', 'billing_address',
        'delivery_address'
    ])
    client = Client(id=client.id,
                    name=client.name,
                    surname=client.surname,
                    email=client.email,
                    phones=[{
                        'id': phone.id,
                        'phone': phone.phone
                    } for phone in phones],
                    billing_address=b_address,
                    delivery_address=d_addr)
    c = Cl(client)
    return c
Example #8
0
def get_order(id):
    order = select_one_or_404('orders', {'id': id})
    order_products = select('order_products', {'order_id': id})
    status = select('order_statuses', {'id': order.status_id})[0].status_name
    return Order(order.id, order.client_id, order_products, status)