Exemple #1
0
def remove_product():
    as_run_id = request.form.get('as_run_id')
    product_type = request.form.get('product_type')
    product_id = request.form.get('product_id')
    as_run = AsRun.get_by_id(as_run_id)
    if product_type == "design":
        product = Product.get_by_id(product_id)
        as_run.products.remove(product)
    elif product_type == "vendor":
        product = VendorProduct.get_by_id(product_id)
        as_run.vendor_products.remove(product)
    as_run.add_change_log_entry(action='Add',
                                field='Product',
                                original_value=product.get_descriptive_url())
    as_run.save()
    jsonData = {'success': True, 'productId': product_id}
    return jsonify(jsonData), 200, {'ContentType': 'application/json'}
Exemple #2
0
def create_as_run():
    """Create new As-Run."""
    form = CreateAsRunForm(request.form)
    validated = form.validate_on_submit()

    if validated:
        procedure_id = request.form['procedure_id']
        procedure = Procedure.get_by_id(procedure_id)
        as_run_number = AsRun.find_next_as_run_number(procedure)
        variables = {
            'owner': form.owner.data,
            'procedure': procedure,
            'project': procedure.project,
            'as_run_number': as_run_number
        }
        as_run = AsRun.create(**variables)

        product_ids = [] if request.form['products'] == '' else request.form[
            'products'].split(',')
        for product_id in product_ids:
            product = Product.get_by_id(product_id)
            as_run.products.append(product)

        vendor_product_ids = [] if request.form[
            'vendor_products'] == '' else request.form[
                'vendor_products'].split(',')
        for vendor_product_id in vendor_product_ids:
            vendor_product = VendorProduct.get_by_id(vendor_product_id)
            as_run.vendor_products.append(vendor_product)

        as_run.save()

        jsonData = {'success': True, 'url': as_run.get_url()}
        return jsonify(jsonData), 200, {'ContentType': 'application/json'}
    else:
        procedure_id = request.form['procedure_id']
        procedure = Procedure.get_by_id(procedure_id)
        as_run_instance = str(
            AsRun.find_next_as_run_number(procedure)).zfill(3)
        return make_response(
            render_template('asrun/create_as_run.html',
                            form=form,
                            proc=procedure,
                            as_run_instance=as_run_instance), 500)
Exemple #3
0
def get_record_by_id_and_class(record_id, record_class):
    # TODO: Try to find a better way that doesn't involve so much dynamic loading
    record = None
    record_class = record_class.lower()  # Just in case
    if record_class == 'anomaly':
        from pid.anomaly.models import Anomaly
        record = Anomaly.get_by_id(record_id)
    elif record_class == 'asrun':
        from pid.asrun.models import AsRun
        record = AsRun.get_by_id(record_id)
    elif record_class == 'build':
        from pid.product.models import Build
        record = Build.get_by_id(record_id)
    elif record_class == 'design':
        from pid.design.models import Design
        record = Design.get_by_id(record_id)
    elif record_class == 'eco':
        from pid.eco.models import ECO
        record = ECO.get_by_id(record_id)
    elif record_class == 'part':
        from pid.part.models import Part
        record = Part.get_by_id(record_id)
    elif record_class == 'procedure':
        from pid.procedure.models import Procedure
        record = Procedure.get_by_id(record_id)
    elif record_class == 'product':
        from pid.product.models import Product
        record = Product.get_by_id(record_id)
    elif record_class == 'specification':
        from pid.specification.models import Specification
        record = Specification.get_by_id(record_id)
    elif record_class == 'task':
        from pid.task.models import Task
        record = Task.get_by_id(record_id)
    elif record_class == 'vendorbuild':
        from pid.vendorproduct.models import VendorBuild
        record = VendorBuild.get_by_id(record_id)
    elif record_class == 'vendorpart':
        from pid.vendorpart.models import VendorPart
        record = VendorPart.get_by_id(record_id)
    elif record_class == 'vendorproduct':
        from pid.vendorproduct.models import VendorProduct
        record = VendorProduct.get_by_id(record_id)
    return record
Exemple #4
0
def dashboard():
    user = current_user
    # TODO: Add prefix option to forms so form field ids are unique

    anomalies_for_user = Anomaly.find_all_anomalies_for_user(user)
    as_runs_for_user = AsRun.find_all_as_runs_for_user(user)
    designs_for_user = Design.find_all_designs_for_user(user)
    ecos_for_user = ECO.find_all_ecos_for_user(user)
    products_for_user = Product.find_all_products_for_user(user)
    vendor_products_for_user = VendorProduct.find_all_vendor_products_for_user(
        user)
    vendor_parts_for_user = VendorPart.find_all_vendor_parts_for_user(user)
    procedures_for_user = Procedure.find_all_distinct_procedures_for_user(user)
    specifications = Specification.find_all_distinct_specifications()
    records_awaiting_approval = []
    for record_list in [
            anomalies_for_user, as_runs_for_user, designs_for_user,
            ecos_for_user, procedures_for_user, products_for_user,
            specifications, vendor_parts_for_user, vendor_products_for_user
    ]:
        for record in record_list:
            if record.state == record.workflow.get_approval_state():
                records_awaiting_approval.append(record)
    variables = {
        'anomalies_for_user': anomalies_for_user,
        'designs_for_user': designs_for_user,
        'ecos_for_user': ecos_for_user,
        'products_for_user': products_for_user,
        'vendor_products_for_user': vendor_products_for_user,
        'vendor_parts_for_user': vendor_parts_for_user,
        'procedures_for_user': procedures_for_user,
        'specifications': specifications,
        'users': User.query.all(),
        'bookmarks': process_bookmarks(user),
        'saved_searches': user.saved_searches,
        'settings': Settings.get_settings(),
        'approvals': Approver.get_open_approvals_for_user(user),
        'records_awaiting_approval': records_awaiting_approval
    }
    return render_template('backend/dashboard.html', **variables)
Exemple #5
0
def add_product():
    as_run_id = request.form.get('as_run_id')
    product_type = request.form.get('product_type')
    product_id = request.form.get('product_id')
    as_run = AsRun.get_by_id(as_run_id)
    if product_type == "design":
        product = Product.get_by_id(product_id)
        if product in as_run.products:
            jsonData = {'success': False, 'message': "Product already added."}
            return jsonify(jsonData), 500, {'ContentType': 'application/json'}
        as_run.products.append(product)
    elif product_type == "vendor":
        product = VendorProduct.get_by_id(product_id)
        if product in as_run.vendor_products:
            jsonData = {'success': False, 'message': "Product already added."}
            return jsonify(jsonData), 500, {'ContentType': 'application/json'}
        as_run.vendor_products.append(product)
    as_run.add_change_log_entry(action='Add',
                                field='Product',
                                new_value=product.get_descriptive_url())
    as_run.save()
    return render_template('asrun/as_run_product.html',
                           as_run=as_run,
                           product=product)
Exemple #6
0
    def validate(self):
        """Validate the form."""
        initial_validation = super(CreateVendorBuildForm, self).validate()
        errors = False
        data = {}
        if not initial_validation:
            errors = True

        vendor_part = VendorPart.get_by_id(self.vendor_part_id.data)
        existing_serial_numbers = VendorProduct.get_serial_numbers_for_vendor_part(vendor_part)

        if self.product_type.data == 's/n':
            # TODO: Try to move some of this to an external function, very messy now
            return_serial_numbers = []
            # TODO: Improve these replaces
            serial_numbers = self.serial_numbers.data.strip().replace(' , ', ',').replace(', ', ',').replace(' ,', ',').replace(' ', ',')  # In case user uses space instead of comma
            if len(serial_numbers) == 0:
                self.serial_numbers.errors.append('No S/N(s) entered')
                errors = True
            else:
                # First check for comma separated serial_numbers
                if ',' in serial_numbers:
                    serial_numbers_array = serial_numbers.split(',')
                    for sn in serial_numbers_array:
                        if sn in existing_serial_numbers:
                            self.serial_numbers.errors.append('S/N {0} already exists'.format(sn))
                            errors = True
                        else:
                            return_serial_numbers.append(sn)
                # Only one S/N present most likely, just append it
                else:
                    if serial_numbers in existing_serial_numbers:
                        self.serial_numbers.errors.append('S/N {0} already exists'.format(serial_numbers))
                        errors = True
                    else:
                        return_serial_numbers.append(serial_numbers)
            data['serial_numbers'] = return_serial_numbers
        elif self.product_type.data == 'lot':
            return_lot_record = None
            lot_record = self.lot_record.data.strip()
            if len(lot_record) == 0:
                self.lot_record.errors.append('No LOT record entered')
                errors = True
            else:
                return_lot_record = lot_record
            if lot_record in existing_serial_numbers:
                self.lot_record.errors.append('LOT record {0} already exists'.format(lot_record))
                errors = True
            else:
                data['lot_record'] = return_lot_record
        elif self.product_type.data == 'stock':
            # TODO: Check if STCK already exists
            if 'STCK' in existing_serial_numbers:
                self.product_type.errors.append('STCK already exists')
                errors = True
            else:
                data['is_stock'] = True

        if errors:
            return False, None

        return True, data
Exemple #7
0
def update_extra_product_component():
    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 product component exists
    product_component = ExtraProductComponent.get_by_id(id)
    parent = product_component.parent
    if field == 'product':
        product = Product.get_by_id(field_value)

        # Change log on this product and on product making up the component
        if product_component.product:
            p = product_component.product
            old_value = '{0} (1)'.format(p.get_descriptive_url())
            p.add_change_log_entry(action='Remove', field='Extra Product Component',
                                   original_value='Removed as Extra Product Component on {0}'.format(parent.get_descriptive_url()))
        else:
            old_value = '{0} - Empty (1)'.format(product_component.part.get_descriptive_url())
        if product:
            new_value = '{0} (1)'.format(product.get_descriptive_url())
            product.add_change_log_entry(action='Add', field='Extra Product Component',
                                         original_value='Added as Extra Product Component on {0}'.format(parent.get_descriptive_url()))
        else:
            p = product_component.part
            new_value = '{0} - Empty (1)'.format(p.get_descriptive_url())
        parent.add_change_log_entry(action='Edit', field='Extra Product Component', original_value=old_value, new_value=new_value)

        product_component.update(product=product)
        # Group installed components so can show them grouped on page
        components_array = arrange_product_components(parent)
        extra_components_array = arrange_extra_product_components(parent)

        variables = {
            'success': True,
            'product': parent,
            'components_array': components_array,
            'extra_components_array': extra_components_array
        }
        return render_template('product/as-built/component_list.html', **variables)
    elif field == 'product_all':
        product = Product.get_by_id(field_value)
        if product.product_type != 'SN':
            qty = product_component.update_all_unassigned_extra_product_components_for_part(product)
        else:
            product_component.update(product=product)
            qty = 1

        # Add change log, this method will only be used when adding new components
        product.add_change_log_entry(action='Add', field='Extra Product Component',
                                     original_value='Added as Extra Product Component on {0}'.format(parent.get_descriptive_url()))
        old_value = '{0} - Empty ({1})'.format(product.part.get_descriptive_url(), qty)
        new_value = '{0} ({1})'.format(product.get_descriptive_url(), qty)
        parent.add_change_log_entry(action='Edit', field='Extra Product Component', original_value=old_value, new_value=new_value)

        # Group installed components so can show them grouped on page
        components_array = arrange_product_components(parent)
        extra_components_array = arrange_extra_product_components(parent)

        variables = {
            'success': True,
            'product': parent,
            'components_array': components_array,
            'extra_components_array': extra_components_array
        }
        return render_template('product/as-built/component_list.html', **variables)
    if field == 'vendor_product':
        vendor_product = VendorProduct.get_by_id(field_value)

        # Change log on this product and on vendor_product making up the component
        if product_component.vendor_product:
            vp = product_component.vendor_product
            old_value = '{0} (1)'.format(vp.get_descriptive_url())
            vp.add_change_log_entry(action='Remove', field='Extra Product Component',
                                   original_value='Removed as Extra Product Component on {0}'.format(parent.get_descriptive_url()))
        else:
            old_value = '{0} - Empty (1)'.format(product_component.vendor_part.get_descriptive_url())
        if vendor_product:
            new_value = '{0} (1)'.format(vendor_product.get_descriptive_url())
            vendor_product.add_change_log_entry(action='Add', field='Extra Product Component',
                                                original_value='Added as Extra Product Component on {0}'.format(parent.get_descriptive_url()))
        else:
            new_value = '{0} - Empty (1)'.format(product_component.vendor_part.get_descriptive_url())
        parent.add_change_log_entry(action='Edit', field='Extra Product Component', original_value=old_value, new_value=new_value)

        product_component.update(vendor_product=vendor_product)
        # Group installed components so can show them grouped on page
        components_array = arrange_product_components(parent)
        extra_components_array = arrange_extra_product_components(parent)

        variables = {
            'success': True,
            'product': parent,
            'components_array': components_array,
            'extra_components_array': extra_components_array
        }
        return render_template('product/as-built/component_list.html', **variables)
    elif field == 'vendor_product_all':
        vendor_product = VendorProduct.get_by_id(field_value)
        if vendor_product.product_type != 'SN':
            qty = product_component.update_all_unassigned_extra_product_components_for_vendor_part(vendor_product)
        else:
            product_component.update(vendor_product=vendor_product)
            qty = 1

        # Add change log, this method will only be used when adding new components
        vendor_product.add_change_log_entry(action='Add', field='Extra Product Component',
                                            original_value='Added as Extra Product Component on {0}'.format(parent.get_descriptive_url()))
        old_value = '{0} - Empty ({1})'.format(vendor_product.vendor_part.get_descriptive_url(), qty)
        new_value = '{0} ({1})'.format(vendor_product.get_descriptive_url(), qty)
        parent.add_change_log_entry(action='Edit', field='Extra Product Component', original_value=old_value, new_value=new_value)

        # Group installed components so can show them grouped on page
        components_array = arrange_product_components(parent)
        extra_components_array = arrange_extra_product_components(parent)

        variables = {
            'success': True,
            'product': parent,
            'components_array': components_array,
            'extra_components_array': extra_components_array
        }
        return render_template('product/as-built/component_list.html', **variables)
    return jsonify({'success': True}), 200, {'ContentType': 'application/json'}