コード例 #1
0
def edit_laboratory_requirements(current_laboratory_requirements):
    requirements = db.session.query(ormLaboratoryRequirements).filter(
        ormLaboratoryRequirements.ukey ==
        current_laboratory_requirements).one()
    laboratory_list = db.session.query(ormLaboratory).all()
    requirement_list = db.session.query(ormRequirement).all()
    form = laboratoryRequirementsForm(request.form)
    form.requirement_name.choices = [(requirement.requirement_name,
                                      requirement.requirement_name)
                                     for requirement in requirement_list]
    form.laboratory_name.choices = [(laboratory.laboratory_name,
                                     laboratory.laboratory_name)
                                    for laboratory in laboratory_list]

    if request.method == 'GET':

        form.requirement_name.data = requirements.requirement_name
        form.laboratory_name.data = requirements.laboratory_name

        return render_template('requirement/edit_laboratory_requirements.html',
                               form=form,
                               form_name="Edit requirement")

    else:

        if form.validate():
            requirements.requirement_name = form.requirement_name.data
            requirements.laboratory_name = form.laboratory_name.data

            try:
                db.session.commit()
                return redirect('/laboratory_requirement')
            except:
                form.requirement_name.errors = [
                    'Requirement with this name already exists!'
                ]
                return render_template(
                    'requirement/edit_laboratory_requirements.html',
                    form=form,
                    form_name="Edit requirement")

        else:
            return render_template(
                'requirement/edit_laboratory_requirements.html',
                form=form,
                form_name="Edit requirement")
コード例 #2
0
def laboratoryRequirements():
    def select_data(ormRequirement, ormLaboratoryRequirements, ormLaboratory):
        res = db.session.query(ormLaboratoryRequirements.ukey, ormRequirement.requirement_name,
                            ormRequirement.requirement_point, ormLaboratory.laboratory_name) \
            .select_from(ormRequirement) \
            .join(ormLaboratoryRequirements) \
            .join(ormLaboratory) \
            .all()

        return res

    def insert_data(data):
        db.session.add(data)

    def update_data(obj, class_name):
        mapped_values = {}
        for item in class_name.__dict__.items():
            field_name = item[0]
            field_type = item[1]
            is_column = isinstance(field_type, InstrumentedAttribute)
            if is_column:
                mapped_values[field_name] = getattr(obj, field_name)

        db.session.query(class_name).filter_by(
            ukey=obj.ukey).update(mapped_values)

    def save():
        db.session.commit()

    laboratory_list = db.session.query(ormLaboratory).all()
    requirement_list = db.session.query(ormRequirement).all()
    data = select_data(ormRequirement, ormLaboratoryRequirements,
                       ormLaboratory)
    laboratory_requirement = [
        LaboratoryRequirementDTO(i[0], i[1], i[3], i[2]) for i in data
    ]
    form = laboratoryRequirementsForm(request.form)
    form.requirement_name.choices = [(requirement.requirement_name,
                                      requirement.requirement_name)
                                     for requirement in requirement_list]
    form.laboratory_name.choices = [(laboratory.laboratory_name,
                                     laboratory.laboratory_name)
                                    for laboratory in laboratory_list]

    if request.method == 'POST':
        print(form.ukey.data)
        if form.ukey.data == '':
            laboratory_requirement = ormLaboratoryRequirements(
                randint(1, 1000), form.laboratory_name.data,
                form.requirement_name.data, form.requirement_point.data)
            insert_data(laboratory_requirement)
        else:
            laboratory_requirement = ormLaboratoryRequirements(
                randint(1, 1000), form.laboratory_name.data,
                form.requirement_name.data, form.requirement_point.data)
            update_data(laboratory_requirement, ormLaboratoryRequirements)
        save()
        return redirect('/laboratory_requirement')

    return render_template('laboratory_requirement.html',
                           laboratory_requirement=laboratory_requirement,
                           form=form)