Beispiel #1
0
def edit_product(product_id):
    product = Product().fetch_product(product_id, current_user.get_id())
    form = ProductForm()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in Attachment().fetch_user_attachments():
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    if not product:
        flash(['Something went wrong. Please try again later'])
        return redirect(url_for('dashboard.products'))

    form.product_name.data = product.name

    if product.product_type == 'item':
        linked_product_items = ProductItem().fetch_active_product_items(
            product.product_items)
        form.items.data = ''.join(linked_product_items)

    else:
        form.attachment_file.data = product.product_attachment.attachment_filename
        form.stock.data = product.stock

    form.price.data = float(product.price)

    return render_template('/dashboard/edit-product.html',
                           product=product,
                           form=form,
                           flashed_message=get_flashed_messages(),
                           attachments=Attachment().fetch_user_attachments())
Beispiel #2
0
def attachments() -> render_template:
    return render_template(
        '/dashboard/attachments.html',
        form=AttachmentForm(),
        attachments=Attachment().fetch_user_attachments_pagination(),
        flashed_message=get_flashed_messages(),
        user=User().fetch_user_logged_in())
Beispiel #3
0
def remove_attachment(attachment_id) -> redirect:
    attachment_file = Attachment().fetch_attachment(attachment_id)

    if not attachment_file:
        flash(['Something went wrong. Please try again'])
        return redirect(url_for('dashboard.attachments'))

    if Attachment().check_if_linked_to_product(attachment_file.id):
        flash([
            "You can't delete an attachment whilst it belongs to an active product"
        ])
        return redirect(url_for('dashboard.attachments'))

    Attachment().remove_attachment(attachment_id)

    flash(['Attachment Successfully Deleted'])
    return redirect(url_for('dashboard.attachments'))
Beispiel #4
0
def upload_attachment() -> redirect:
    form = AttachmentForm()

    if not form.validate_on_submit():
        flash(list(form.errors.values())[0])
        return redirect(url_for('dashboard.attachments'))

    if Attachment().check_if_already_exists(
            request.files['attachment_upload'].filename):
        flash([
            'This attachment has already been uploaded. Please rename the attachment, or upload a different attachment'
        ])
        return redirect(url_for('dashboard.attachments'))

    if not Attachment().check_attachment_size(
            request.files['attachment_upload']):
        flash(['Attachment too large. Maximum file size is 5MB'])
        return redirect(url_for('dashboard.attachments'))

    upload = Attachment().upload_attachment(request.files['attachment_upload'])

    flash(['Attachment Successfully Uploaded'])
    return redirect(url_for('dashboard.attachments'))
Beispiel #5
0
def download_attachment(attachment_id) -> Response:
    attachment = Attachment().fetch_attachment(attachment_id)

    if not attachment:
        return abort(404)

    download_response = download_file(attachment.attachment_name,
                                      environ.get('AWS_ATTACHMENTS'))

    return Response(
        download_response['file'],
        mimetype='text/plain',
        headers={
            "Content-Disposition":
            f"attachment;filename={download_response['original_filename']}"
        })
Beispiel #6
0
def create_product() -> render_template:
    form = ProductForm()
    attachments = Attachment().fetch_user_attachments()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in attachments:
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    return render_template('/dashboard/create-product.html',
                           form=form,
                           flashed_message=get_flashed_messages(),
                           attachments=attachments)
Beispiel #7
0
def download_attachment(order_id, attachment_id):
    order = Order().fetch_order(order_id)

    if (not order) or (order.status != 'Completed'):
        return abort(404)

    attachment = Attachment().fetch_attachment(attachment_id)
    fetch_file = download_file(attachment.attachment_filename,
                               environ.get('AWS_ATTACHMENTS'))

    return Response(
        fetch_file['file'],
        mimetype='text/plain',
        headers={
            "Content-Disposition":
            f"attachment;filename={attachment.original_attachment_filename}"
        })
Beispiel #8
0
def submit_new_product() -> redirect:
    form = ProductForm()
    attachments = Attachment().fetch_user_attachments()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in attachments:
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    if not form.validate_on_submit():
        flash(list(form.errors.values())[0])
        return redirect(url_for('dashboard.create_product'))

    Product().add(request)

    flash(['Product Successfully Created'])
    return redirect(url_for('dashboard.products'))
Beispiel #9
0
def update_product(product_id):
    form = ProductForm()
    attachments = Attachment().fetch_user_attachments()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in attachments:
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    if not form.validate_on_submit():
        flash(list(form.errors.values())[0])
        return redirect(
            url_for('dashboard.edit_product', product_id=product_id))

    if not Product().update(request, product_id):
        flash(['Something went wrong. Please try again later'])
        return redirect(url_for('dashboard.products'))

    flash(['Product Successfully Updated'])
    return redirect(url_for('dashboard.edit_product', product_id=product_id))