def add(): """ Imports data from the uploaded file and updates the stock values including BOM results """ form = FileForm(request.form) # WTF is NOT used for the file handling, since the file upload handling seems broken. if request.method == 'POST' and form.validate_on_submit(): # show the validation page if a file is uploaded # else process the tmpname parameter if request.files.get('file'): try: save_to_tmp(form) success, headers, values = _import_file(extract_filepath(form)) # Also add the current quantity to the review list for item in values: current_quantity = 0 obj = current_app.mongo.db.stock.find_one(item.get('partno')) if obj: current_quantity = obj.get('quantity') item['current_quantity'] = current_quantity item['new_quantity'] = item.get('quantity') + current_quantity headers.append('current_quantity') headers.append('new_quantity') return render_template('stock/validate_form.html', form=form, headers=headers, data=values, title='Verify Input Data', action='Add to Stock') except Exception as e: flash(e, 'error') elif form.tmpname.data: success, headers, values = _import_file(extract_filepath(form)) if success: try: for v in values: partno = v.get('partno') quantity = v.get('quantity') batchname = v.get('batch') comment = v.get('comment', 'added to stock') update_counts(partno, quantity, batchname, comment) flash('stock import successful', 'success') return redirect(url_for('stock.overview')) except Exception as e: flash(e, 'error') return render_template('stock/validate_form.html', form=form, headers=headers, data=values, title='Verify Input Data', action='Add to Stock') extract_errors(form) return render_template('stock/import_form.html', form=form, title='Add Stock Items')
def correct(): """ Imports data from the uploaded file and corrects the corresponding stock values """ form = FileForm(request.form) warning = dict(category='warning', message='You are about to override the exising stock! Are you sure you want to continue?') # WTF is NOT used for the file handling, since the file upload handling seems broken. if request.method == 'POST' and form.validate_on_submit(): # show the validation page if a file is uploaded # else process the tmpname parameter if request.files.get('file'): try: save_to_tmp(form) success, headers, values = _import_file(extract_filepath(form)) for item in values: current_quantity = 0 obj = current_app.mongo.db.stock.find_one(item.get('partno')) if obj: current_quantity = obj.get('quantity') item['current_quantity'] = current_quantity headers.append('current_quantity') return render_template('stock/validate_form.html', form=form, headers=headers, data=values, title='Verify Correction Data', action='Apply Correction', warning=warning) except Exception as e: flash(e, 'error') elif form.tmpname.data: success, headers, values = _import_file(extract_filepath(form)) if success: try: for v in values: partno = v.get('partno') quantity = v.get('quantity') comment = v.get('comment', 'manual correction') correct_counts(partno, quantity, comment) flash('stock correction successful', 'success') return redirect(url_for('stock.overview')) except Exception as e: flash(e, 'error') return render_template('stock/validate_form.html', form=form, headers=headers, data=values, title='Verify Correction Data', action='Apply Correction', warning=warning) extract_errors(form) return render_template('stock/import_form.html', form=form, title='Correct Stock Items', warning=warning)
def update_bom(): """ Imports data from the uploaded file and corrects the corresponding stock values """ form = FileForm(request.form) # WTF is NOT used for the file handling, since the file upload handling seems broken. if request.method == 'POST' and form.validate_on_submit(): # show the validation page if a file is uploaded # else process the tmpname parameter if request.files.get('file'): # a file was uploaded try: save_to_tmp(form) success, headers, values = _import_file(extract_filepath(form)) target_partno = 'unknown' if len(values) > 0: target_partno = values.pop(0).get('partno') return render_template('stock/validate_form.html', form=form, headers=headers, data=values, title='Verify BOM Data for '+target_partno, action='Update BOM') except Exception as e: flash(e, 'error') elif form.tmpname.data: success, headers, values = _import_file(extract_filepath(form)) target_partno = 'unknown' if len(values) > 0: target_partno = values.pop(0).get('partno') if success: try: set_bom(target_partno, values) flash('BOM update successful', 'success') return redirect(url_for('stock.overview')) except Exception as e: flash(e, 'error') return render_template('stock/validate_form.html', form=form, headers=headers, data=values, title='Verify BOM Data for '+target_partno, action='Update BOM') extract_errors(form) return render_template('stock/import_form.html', form=form, title='Update BOM')
def import_items(): form = FileForm(request.form) # WTF is NOT used for the file handling, since the file upload handling seems broken. if request.method == 'POST' and form.validate_on_submit(): # show the validation page if a file is uploaded # else process the tmpname parameter if request.files.get('file'): # a file was uploaded try: save_to_tmp(form) success, headers, values = _import_file(extract_filepath(form)) return render_template('items/validate_form.html', title='Verify Item Data', form=form, headers=headers, data=values) except Exception as e: flash(e, 'error') elif form.tmpname.data: # re-run the data import and insert items into the database success, headers, values = _import_file(extract_filepath(form)) if success: try: _store_items(values) flash('item import successful', 'success') return redirect(url_for('items.overview')) except Exception as e: flash(e, 'error') return render_template('items/validate_form.html', title='Verify Item Data', form=form, headers=headers, data=values) extract_errors(form) return render_template('items/import_form.html', title='Import Items File', form=form)