def view_product(design_number, part_identifier, serial_number): product = Product.get_product_by_product_number(design_number, part_identifier, serial_number) serial_numbers = Product.get_serial_numbers_for_design_number_and_part_identifier(product.part.design.design_number, product.part.part_identifier) projects = Project.query.all() companies = Company.get_all_with_pri_on_top() hardware_types = HardwareType.query.all() users = User.query.all() dispositions = Disposition.query.all() components_array = arrange_product_components(product) # Group installed components so can show them grouped on page extra_components_array = arrange_extra_product_components(product) installed_ins = defaultdict(list) for pc in product.get_installed_ins(): installed_ins[pc.parent.product_number].append(pc) variables = { 'product': product, 'projects': projects, 'hardware_types': hardware_types, 'users': users, 'serial_numbers': serial_numbers, 'components_array': components_array, 'extra_components_array': extra_components_array, 'dispositions': dispositions, 'companies': companies, 'installed_ins': installed_ins } return render_template('product/view_product.html', **variables)
def advanced_search(): params = request.args.to_dict() users = User.query.all() projects = Project.query.all() materials = Material.query.all() material_specs = MaterialSpecification.query.all() hardware_types = HardwareType.query.all() criticalities = Criticality.query.all() vendors = Company.get_all_with_pri_on_top() variables = { 'users': users, 'projects': projects, 'vendors': vendors, 'materials': materials, 'material_specs': material_specs, 'hardware_types': hardware_types, 'criticalities': criticalities, 'column_names': ADVANCED_SEARCH_COLUMNS, 'record_type': params['record_type'] if 'record_type' in params else 'design', 'Design': Design, # TODO: Consider making these globally accessible if needed 'VendorPart': VendorPart, 'Product': Product, 'VendorProduct': VendorProduct, 'Procedure': Procedure, 'Anomaly': Anomaly, 'ECO': ECO, 'Specification': Specification } return render_template('backend/advanced_search.html', **variables)
def update_build(): id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] # TODO: Check if build exists build = Build.get_by_id(id) original_value = None if field == 'notes': original_value = build.notes build.update(notes=field_value) elif field == 'owner': if build.owner: original_value = build.owner.get_name() owner = User.get_by_id(field_value) build.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'purchase_order': original_value = build.purchase_order build.update(purchase_order=field_value) elif field == 'vendor': if build.vendor: original_value = build.vendor.name vendor = Company.get_by_id(field_value) build.update(vendor=vendor) field_value = vendor.name if vendor else None build.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def view_vendor_part(part_number): """View existing vendor part.""" vendor_part = VendorPart.get_by_part_number(part_number) projects = Project.query.all() materials = Material.query.all() users = User.query.all() vendors = Company.get_all_with_pri_on_top() variables = { 'vendor_part': vendor_part, 'projects': projects, 'materials': materials, 'vendors': vendors, 'users': users } return render_template('vendorpart/view_vendor_part.html', **variables)
def view_vendor_product(part_number, serial_number): vendor_product = VendorProduct.get_vendor_product_by_product_number(part_number, serial_number) index = 1 back_index = -1 product_number = part_number + '-' + serial_number product_number_parts = product_number.split('-') max_index = len(product_number.split('-')) while not vendor_product: # Probably due to a dash in serial_number causing the above match to fail. Do some magic and try again part_number = '-'.join(product_number_parts[0:max_index - index]) serial_number = '-'.join(product_number_parts[back_index:]) vendor_product = VendorProduct.get_vendor_product_by_product_number(part_number, serial_number) index += 1 back_index -= 1 if index == max_index: break # TODO: Make this error gracefully rather than nastily serial_numbers = VendorProduct.get_serial_numbers_for_vendor_part(vendor_product.vendor_part) projects = Project.query.all() companies = Company.get_all_with_pri_on_top() hardware_types = HardwareType.query.all() users = User.query.all() dispositions = Disposition.query.all() installed_ins = defaultdict(list) for pc in vendor_product.get_installed_ins(): installed_ins[pc.parent.product_number].append(pc) variables = { 'vendor_product': vendor_product, 'serial_numbers': serial_numbers, 'projects': projects, 'hardware_types': hardware_types, 'users': users, 'companies': companies, 'dispositions': dispositions, 'installed_ins': installed_ins } return render_template('vendorproduct/view_vendor_product.html', **variables)
def get_vendors(): return Company.get_all_with_pri_on_top()
def update_vendor_part(): # TODO: Check that field should actually be allowed to change. As in, don't change design number for instance id = request.form['pk'] # UID for field will be ala [fieldname]-[classname]-[id]-editable, field name will be first section always field = request.form['name'].split('-')[0] field_value = request.form['value'] original_value = None # TODO: Check if design exists vendor_part = VendorPart.get_by_id(id) if field == 'name': original_value = vendor_part.name vendor_part.update(name=field_value) elif field == 'summary': original_value = vendor_part.summary vendor_part.update(summary=field_value) elif field == 'project': if vendor_part.project: original_value = vendor_part.project.name project = Project.get_by_id(field_value) vendor_part.update(project=project) field_value = project.name if project else None elif field == 'owner': if vendor_part.owner: original_value = vendor_part.owner.get_name() if vendor_part.owner.padawan: for approver in vendor_part.approvers: if approver.approver == vendor_part.owner.supervisor and approver.capacity == 'Supervisor': vendor_part.approvers.remove(approver) approver.delete() owner = User.get_by_id(field_value) if owner.padawan: approver = Approver.create(approver_id=owner.supervisor_id, capacity='Supervisor') vendor_part.approvers.append(approver) vendor_part.update(owner=owner) field_value = owner.get_name() if owner else None elif field == 'vendor': if vendor_part.vendor: original_value = vendor_part.vendor.name vendor = Company.get_by_id(field_value) vendor_part.update(vendor=vendor) field_value = vendor.name if vendor else None elif field == 'notes': original_value = vendor_part.notes vendor_part.update(notes=field_value) elif field == 'material': original_material = None original_material_spec = None if vendor_part.material: original_material = vendor_part.material.name if vendor_part.material_specification: original_material_spec = vendor_part.material_specification.name material = Material.get_by_id(field_value) vendor_part.update( material=material, material_specification=None ) # To ensure we don't have a mat_spec linked with old material material_name = None material_specifications = None if material: material_name = material.name material_specifications = material.specifications vendor_part.add_change_log_entry(action='Edit', field='Material', original_value=original_material, new_value=material_name) if original_material_spec: vendor_part.add_change_log_entry( action='Edit', field='Material Specification', original_value=original_material_spec) variables = { 'vendor_part': vendor_part, 'material_specifications': material_specifications } return render_template( 'vendorpart/ajax_select_material_specification.html', **variables) elif field == 'material_specification': if vendor_part.material_specification: original_value = vendor_part.material_specification.name material_specification = MaterialSpecification.get_by_id(field_value) vendor_part.update(material_specification=material_specification) field_value = material_specification.name if material_specification else None elif field == 'thumbnail_id': thumbnail_id = None if field_value == 'default' else field_value vendor_part.update(thumbnail_id=thumbnail_id) return render_template('shared/thumbnail_return.html', record=vendor_part) elif field == 'current_best_estimate': original_value = vendor_part.current_best_estimate original_predicted_best_estimate = vendor_part.predicted_best_estimate current_best_estimate = float(field_value) predicted_best_estimate = current_best_estimate * ( 1 + (vendor_part.uncertainty / 100)) # PBE = CBE * (1+%Unc) vendor_part.update(current_best_estimate=current_best_estimate, predicted_best_estimate=predicted_best_estimate) vendor_part.update_parents_mass( ) # Updates parts where this is a component vendor_part.add_change_log_entry(action='Edit', field='Current Best Estimate', original_value=original_value, new_value=current_best_estimate) vendor_part.add_change_log_entry( action='Edit', field='Predicted Best Estimate', original_value=original_predicted_best_estimate, new_value=predicted_best_estimate) return render_template('vendorpart/mass_fields.html', vendor_part=vendor_part) elif field == 'uncertainty': original_value = vendor_part.uncertainty original_predicted_best_estimate = vendor_part.predicted_best_estimate uncertainty = float(field_value) predicted_best_estimate = vendor_part.current_best_estimate * ( 1 + (uncertainty / 100)) # PBE = CBE * (1+%Unc) vendor_part.update(uncertainty=uncertainty, predicted_best_estimate=predicted_best_estimate) vendor_part.update_parents_mass( ) # Updates parts where this is a component vendor_part.add_change_log_entry(action='Edit', field='Uncertainty', original_value=original_value, new_value=uncertainty) vendor_part.add_change_log_entry( action='Edit', field='Predicted Best Estimate', original_value=original_predicted_best_estimate, new_value=predicted_best_estimate) return render_template('vendorpart/mass_fields.html', vendor_part=vendor_part) vendor_part.add_change_log_entry(action='Edit', field=field.title().replace('_', ' '), original_value=original_value, new_value=field_value) return jsonify({'success': True}), 200, {'ContentType': 'application/json'}
def create_vendor_build(): """Create new VendorBuild. Also creates one or more VendorProducts.""" form = CreateVendorBuildForm(request.form) if request.method == 'GET': vendor_part = VendorPart.get_by_id(request.args.get('vendor_part_id')) existing_build_id = request.args.get('existing_build_id') if existing_build_id: existing_build = VendorBuild.get_by_id(existing_build_id) build_identifier = existing_build.build_identifier else: build_identifier = VendorBuild.get_next_build_identifier_for_vendor_part(vendor_part) lot_identifier = VendorProduct.get_next_lot_number_for_vendor_part(vendor_part) existing_serial_numbers = ','.join(VendorProduct.get_serial_numbers_for_vendor_part(vendor_part)) # Pre-populate with values from vendor_part form.project.data = vendor_part.project form.vendor.data = vendor_part.vendor form.manufacturer.data = Company.get_company_by_name('N/A') variables = { 'form': form, 'vendor_part': vendor_part, 'build_identifier': build_identifier, 'lot_identifier': lot_identifier, 'existing_serial_numbers': existing_serial_numbers, 'existing_build_id': existing_build_id } return render_template('vendorproduct/create_vendor_build_modal.html', **variables) validated, data = form.validate_on_submit() if validated: vendor_part = VendorPart.get_by_id(form.vendor_part_id.data) vendor = vendor_part.vendor manufacturer = form.manufacturer.data owner = form.owner.data build_identifier = form.build_identifier.data # Create build if form.existing_build_id.data: build = VendorBuild.get_by_id(form.existing_build_id.data) else: build = VendorBuild.create(vendor_part=vendor_part, vendor=vendor, manufacturer=manufacturer, owner=owner, build_identifier=build_identifier) # For each s/n in s/n, create product summary = vendor_part.summary hardware_type = form.hardware_type.data project = form.project.data # Create serial numbers serial_numbers = data.get('serial_numbers', []) for sn in serial_numbers: product = VendorProduct.create(serial_number=sn, vendor_part=vendor_part, vendor_build=build, summary=summary, hardware_type=hardware_type, project=project, owner=owner) # Or create LOT product lot_record = data.get('lot_record', None) if lot_record: product = VendorProduct.create(serial_number=lot_record, vendor_part=vendor_part, vendor_build=build, summary=summary, hardware_type=hardware_type, product_type='LOT', project=project, owner=owner) # Or create STOCK product is_stock = data.get('is_stock', False) if is_stock: product = VendorProduct.create(serial_number='STCK', vendor_part=vendor_part, vendor_build=build, summary=summary, hardware_type=hardware_type, product_type='STOCK', project=project, owner=owner) jsonData = { 'success': True, 'product_number': product.product_number.replace(' ', '-'), # Slugify product_number 'url': product.get_url() } return jsonify(jsonData), 200, {'ContentType': 'application/json'} else: vendor_part = VendorPart.get_by_id(request.form['vendor_part_id']) build_identifier = VendorBuild.get_next_build_identifier_for_vendor_part(vendor_part) lot_identifier = VendorProduct.get_next_lot_number_for_vendor_part(vendor_part) existing_serial_numbers = ','.join(VendorProduct.get_serial_numbers_for_vendor_part(vendor_part)) variables = { 'form': form, 'vendor_part': vendor_part, 'build_identifier': build_identifier, 'lot_identifier': lot_identifier, 'existing_serial_numbers': existing_serial_numbers } response = make_response(render_template('vendorproduct/create_vendor_build_modal.html', **variables), 500) return response