예제 #1
0
def one_solution_asset_types(solution_id):
    # This needs fixed. This should really call a function that just returns a dictionary of all asset types without writing json again
    all_asset_types = write_asset_types_to_json()
    solution_asset_types = {}
    associated_solutions = {}
    solution = db_connect.query_one_db(model=models.Solutions,
                                       column=models.Solutions.id,
                                       v=solution_id)
    for t in solution.associated_asset_types:
        a_type = db_connect.query_one_db(model=models.AssetTypes,
                                         column=models.AssetTypes.id,
                                         v=t)
        solution_asset_types[t] = a_type.asset_type
    if solution.associated_solutions:
        for a in solution.associated_solutions:
            asid = db_connect.query_one_db(model=models.Solutions,
                                           column=models.Solutions.id,
                                           v=a)
            associated_solutions[a] = asid.solution_title

    all_solution_data = {
        'all_asset_types': all_asset_types,
        'solution_asset_types': solution_asset_types,
        'associated_solutions': associated_solutions,
    }
    data_folder = Path("static/data/one_solution_data.json")
    with open(data_folder, 'w') as fp:
        json.dump(all_solution_data, fp, indent=4)
    return solution_asset_types
예제 #2
0
def get_associated_solutions(solution_id):
    s = db_connect.query_one_db(model=models.Solutions,
                                column=models.Solutions.id,
                                v=solution_id)
    assoc_solutions = {}
    if s.associated_solutions:
        for r in s.associated_solutions:
            asid = db_connect.query_one_db(model=models.Solutions,
                                           column=models.Solutions.id,
                                           v=r)
            assoc_solutions[asid.id] = asid.solution_title
    else:
        assoc_solutions = []
    return assoc_solutions
예제 #3
0
def public_solution():
    if request.args.get('solution_id'):
        sid = request.args.get('solution_id')
    else:
        redirect(url_for('main'))
    solution = db_connect.query_one_db(model=models.Solutions, column=models.Solutions.id, v=sid)
    if not solution.public:
        return redirect(url_for('main'))
    primary_asset_type = db_connect.query_one_db(model=models.AssetTypes,
                                                 column=models.AssetTypes.id,
                                                 v=solution.primary_asset_type)
    return render_template('public_solution.html',
                           solution_info=solution,
                           steps=solution.steps,
                           primary_asset_type=primary_asset_type.asset_type,
                           title=solution.solution_title)
예제 #4
0
def view_one_software():
    soft_id = request.args.get('soft_id')
    add_software_license_form = forms.SoftwareLicensingForm()
    all_licenses = db_connect.query_filtered(model=models.SoftwareLicensing,
                                             model_column=models.SoftwareLicensing.software_name,
                                             v=int(soft_id))
    software_info = db_connect.query_one_db(model=models.Software,
                                            column=models.Software.id,
                                            v=int(soft_id))
    # soft_co = db_connect.query_one_db(model=models.SoftwareCompanies,
    #                                   column=models.SoftwareCompanies.software_company,
    #                                   v=software_info.software_company)
    if add_software_license_form.add_license_submit.data and add_software_license_form.validate():
        db_connect.insert_db(models.SoftwareLicensing(software_name=add_software_license_form.software_name.data,
                                                      version=add_software_license_form.version.data,
                                                      license_no=add_software_license_form.license_no.data,
                                                      licensed_to=add_software_license_form.licensed_to.data,
                                                      seats=add_software_license_form.seats.data,
                                                      expiration=add_software_license_form.expiration.data,
                                                      assets_installed_on=add_software_license_form.assets_installed_on.data
                                                      )
                             )
        return redirect(url_for('view_one_software', soft_id=add_software_license_form.software_name.data))
    add_software_license_form.software_name.default = str(soft_id)
    add_software_license_form.process()
    return render_template('view_one_software.html',
                           # soft_co=soft_co,
                           software_info=software_info,
                           all_licenses=all_licenses,
                           software_names=data_functions.software_names_as_dict(),
                           add_software_license_form=add_software_license_form)
예제 #5
0
def edit_solution_remove_assoc_solution():
    data = request.form
    print("solution to remove: " + data['sol_to_remove'] + " | Solution ID: " + data['solution_id'])
    solution_to_update = db_connect.query_one_db(model=models.Solutions,
                                                 column=models.Solutions.id,
                                                 v=data['solution_id'])
    assoc_solutions = solution_to_update.associated_solutions
    assoc_solutions.remove(int(data['sol_to_remove']))
    db_connect.update_column(model=models.Solutions,
                             column=models.Solutions.associated_solutions,
                             id=int(data['solution_id']),
                             v=assoc_solutions)
    return data
예제 #6
0
def add_solution_post_old():
    data = request.form
    combined_steps = {}
    temp_dict = {}
    count = 1

    asset_type = db_connect.query_one_db(model=models.AssetTypes,
                                         column=models.AssetTypes.id,
                                         v=data['asset_type'])

    solution = data['solution'].split('&')
    for s in solution:
        print(s)
        if s[:4] == 'step':
            step = s.replace("%20", " ")
            instructions = step.split('=')[1]
            images = []  # This should be a list of strings that will be file paths to the solution step's images
            # temp_dict[str(count)] = step.split('=')[1]
            print(s.split('=')[0] + ' - ' + s.split('=')[1])
            # Code to test adding images
            step_info = {
                "Instruction": instructions,
                "Images": images
            }
            temp_dict[str(count)] = step_info
            count += 1
        if 'solution_title' in s:
            title = s.split('=')[1]
            title = title.replace("%20", " ")
        if 'public_solution' in s:
            public = True
        else:
            public = False
    for i in temp_dict:
        print(i, temp_dict[i])
    print(asset_type)
    print(type(asset_type))
    db_connect.insert_db(models.Solutions(solution_title=title,
                                          steps=temp_dict,
                                          date_added=datetime.datetime.now(),
                                          date_revised=datetime.datetime.now(),
                                          primary_asset_type=asset_type.id,
                                          associated_asset_types=[asset_type.id],
                                          user=1,
                                          public=public))

    # if request.method == 'GET':
    #     return jsonify(combined_steps)
    data_functions.write_all_solution_data()
    return combined_steps
예제 #7
0
def edit_solution_remove_rel_asset_type():
    data = request.form
    print("Type to remove: " + data["type_to_remove"] + " | Solution ID: " + data['solution_id'])
    solution_to_update = db_connect.query_one_db(model=models.Solutions,
                                                 column=models.Solutions.id,
                                                 v=data['solution_id'])
    assoc_asset_types = solution_to_update.associated_asset_types
    # Double checks it's not the same as the primary asset type before removing
    if solution_to_update.primary_asset_type != int(data['type_to_remove']):
        assoc_asset_types.remove(int(data['type_to_remove']))
    db_connect.update_column(model=models.Solutions,
                             column=models.Solutions.associated_asset_types,
                             id=int(data['solution_id']),
                             v=assoc_asset_types)
    return data
예제 #8
0
def view_solutions():
    if request.args.get('asset_type'):
        asset_type = request.args.get('asset_type')
    this_asset_type = db_connect.query_one_db(models.AssetTypes, models.AssetTypes.id, asset_type)
    return render_template('view_solutions.html',
                           all_departments=db_connect.query_all(models.Departments),
                           asset_type=this_asset_type.asset_type,
                           asset_type_id=this_asset_type.id,
                           manufacturers=data_functions.manufacturers_as_dict(),
                           asset_types=db_connect.query_all(models.AssetTypes),
                           latest_five=db_connect.query_latest_five_by_asset_type(this_asset_type.id),
                           latest_five_assets=db_connect.query_latest_five_better(model=models.Assets,
                                                                                  column=models.Assets.asset_type,
                                                                                  v=this_asset_type.id)
                           )
예제 #9
0
def write_all_solution_data():
    all_solutions = db_connect.query_all(models.Solutions)
    solution_data = []
    for s in all_solutions:
        main_asset_type = db_connect.query_one_db(model=models.AssetTypes,
                                                  column=models.AssetTypes.id,
                                                  v=s.primary_asset_type)
        solution_data.append({
            'id': s.id,
            'title': s.solution_title,
            'primary_asset_type': main_asset_type.asset_type
        })
    data_folder = Path("static/data/all_solution_data.json")
    with open(data_folder, 'w') as fp:
        json.dump(solution_data, fp, indent=4)
    return None
예제 #10
0
def add_solution():
    data_folder = Path("static/data/one_solution.json")
    with open(data_folder, 'w') as fp:
        json.dump('', fp, indent=4)
    if request.args.get('message'):
        message = request.args.get('message')
    else:
        message = 0
    if request.args.get('asset_type'):
        asset_type = request.args.get('asset_type')
        this_asset_type = db_connect.query_one_db(model=models.AssetTypes, column=models.AssetTypes.id, v=asset_type)
    else:
        asset_type = 0
    return render_template('add_solution.html',
                           message=message,
                           asset_type_name=this_asset_type.asset_type,
                           asset_type_id=asset_type,
                           asset_types=db_connect.query_all(models.AssetTypes))
예제 #11
0
def write_one_asset_data_to_json(asset_id):
    asset_data = {}
    asset = db_connect.query_one_db(model=models.Assets,
                                    column=models.Assets.id,
                                    v=asset_id)
    asset_data["Asset_Id"] = asset.id
    asset_data["Asset_Type"] = asset.asset_type
    asset_data["Manufacturer"] = asset.manufacturer
    asset_data["Model"] = asset.model
    asset_data["Serial_No"] = asset.serial_no
    asset_data["Dia_Asset_Tag"] = asset.dia_asset_tag
    asset_data["Description"] = asset.description
    asset_data["Department"] = asset.department
    asset_data["Location"] = asset.location
    # data_folder = Path("static/data/one_asset.json")
    # with open(data_folder, 'w') as fp:
    #     json.dump(asset_data, fp, indent=4)
    return asset_data
예제 #12
0
def one_solution_data(solution_id):
    solution_data = {}
    solution = db_connect.query_one_db(model=models.Solutions,
                                       column=models.Solutions.id,
                                       v=solution_id)
    solution_data["Solution_id"] = solution.id
    solution_data["Title"] = solution.solution_title
    solution_data["Steps"] = solution.steps
    step_number = []
    for s in solution.steps:
        step_number.append(s)
    if len(step_number) > 0:
        solution_data["Highest_Step"] = max(step_number)
    else:
        solution_data["Highest_Step"] = '0'
    solution_data["Associated_Asset_Types"] = solution.associated_asset_types
    solution_data["Primary_Asset_Type"] = solution.primary_asset_type
    data_folder = Path("static/data/one_solution.json")
    with open(data_folder, 'w') as fp:
        json.dump(solution_data, fp, indent=4)
    return None
예제 #13
0
def view_one_asset():
    if request.args.get('asset_id'):
        asset_id = request.args.get('asset_id')
    else:
        asset_id = 0
    asset = db_connect.query_one_db(model=models.Assets,
                                    column=models.Assets.id,
                                    v=asset_id)
    data_functions.write_asset_data_to_json(asset_id)
    add_asset_type_form = forms.AddAssetTypeForm()
    add_manufacturer_form = forms.AddManufacturerForm()
    add_department_form = forms.AddDepartmentForm()
    all_asset_types = []
    all_departments = []
    all_manufacturers = []
    try:
        for t in db_connect.query_all(models.AssetTypes):
            all_asset_types.append((str(t.id), t.asset_type.title()))
    except:
        pass
    try:
        for d in db_connect.query_all(models.Departments):
            all_departments.append((str(d.id), d.department.title()))
    except:
        pass
    try:
        for m in db_connect.query_all(models.Manufacturers):
            all_manufacturers.append((str(m.id), m.manufacturer.title()))
    except:
        pass

    edit_asset_form = forms.EditAssetForm()
    edit_asset_form.asset_type.choices = all_asset_types
    edit_asset_form.department.choices = all_departments
    edit_asset_form.manufacturer.choices = all_manufacturers

    if add_asset_type_form.asset_type_submit.data and add_asset_type_form.validate():
        db_connect.insert_db(models.AssetTypes(asset_type=add_asset_type_form.asset_type.data))
        return redirect(url_for('view_one_asset', asset_id=asset_id))
    if add_department_form.department_submit.data and add_department_form.validate():
        db_connect.insert_db(models.Departments(department=add_department_form.department.data))
        return redirect(url_for('view_one_asset', asset_id=asset_id))
    if add_manufacturer_form.manufacturer_submit.data and add_manufacturer_form.validate():
        db_connect.insert_db(models.Manufacturers(manufacturer=add_manufacturer_form.manufacturer.data))
        return redirect(url_for('view_one_asset', asset_id=asset_id))

    if edit_asset_form.edit_asset_submit.data and edit_asset_form.validate():
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.asset_type, v=edit_asset_form.asset_type.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.manufacturer, v=edit_asset_form.manufacturer.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.model, v=edit_asset_form.model.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.serial_no, v=edit_asset_form.serial_no.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.dia_asset_tag, v=edit_asset_form.dia_asset_tag.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.name, v=edit_asset_form.name.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.description, v=edit_asset_form.description.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.ip_address, v=edit_asset_form.ip_address.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.department, v=edit_asset_form.department.data)
        db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.date_revised, v=datetime.datetime.now())
        if edit_asset_form.decommissioned.data:
            db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.decommissioned, v=datetime.datetime.now())
        else:
            db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.decommissioned, v=None)
        if edit_asset_form.deployed.data:
            db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.deployed, v=True)
        else:
            db_connect.update_column(model=models.Assets, id=asset_id, column=models.Assets.deployed, v=False)
        return redirect(url_for('view_one_asset', asset_id=asset_id))

    edit_asset_form.asset_type.default = str(asset.asset_type)
    edit_asset_form.process()
    edit_asset_form.department.default = str(asset.department)
    edit_asset_form.process()
    edit_asset_form.manufacturer.default = str(asset.manufacturer)
    edit_asset_form.process()
    edit_asset_form.model.default = asset.model
    edit_asset_form.process()
    if asset.serial_no:
        edit_asset_form.serial_no.default = asset.serial_no
        edit_asset_form.process()
    if asset.dia_asset_tag:
        edit_asset_form.dia_asset_tag.default = asset.dia_asset_tag
        edit_asset_form.process()
    if asset.name:
        edit_asset_form.name.default = asset.name
        edit_asset_form.process()
    if asset.description:
        edit_asset_form.description = asset.description
        edit_asset_form.process()
    if asset.ip_address:
        edit_asset_form.ip_address = asset.ip_address
        edit_asset_form.process()
    if asset.decommissioned:
        edit_asset_form.decommissioned.default = True
        edit_asset_form.process()

    return render_template('view_one_asset.html',
                           asset_types=db_connect.query_all(models.AssetTypes),
                           manufacturers=data_functions.manufacturers_as_dict(),
                           departments=data_functions.departments_as_dict(),
                           asset_id=asset_id,
                           add_asset_type_form=add_asset_type_form,
                           add_manufacturer_form=add_manufacturer_form,
                           add_department_form=add_department_form,
                           edit_asset_form=edit_asset_form,
                           asset=asset)
예제 #14
0
def view_one_solution():
    add_type_to_solution_form = forms.AddAssetTypeToSolutionForm()
    add_assoc_solution_form = forms.AddAssocSolutionForm()
    change_primary_asset_type_form = forms.ChangePrimaryAssetTypeForm()
    if request.args.get('solution_id'):
        solution_id = request.args.get('solution_id')
    else:
        solution_id = 0
    if request.args.get('asset_types'):
        for r in request.args.get('asset_types'):
            print(r)
    data_functions.one_solution_data(solution_id)
    solution = db_connect.query_one_db(model=models.Solutions, column=models.Solutions.id, v=solution_id)
    associated_asset_types = solution.associated_asset_types
    assoc_dict = {}
    all_types = db_connect.query_all(models.AssetTypes)
    # I believe the below comment is resolved. Leaving in just in case it is not.
    # this is causing an issue when reloading the page. It is returning too many things.
    for t in associated_asset_types:
        a_type = db_connect.query_one_db(model=models.AssetTypes, column=models.AssetTypes.id, v=t)
        assoc_dict[str(a_type.id)] = a_type.asset_type
    change_primary_choices = list(assoc_dict.items())
    change_primary_asset_type_form.all_asset_types.choices = change_primary_choices
    data_folder = Path("static/data/assoc_types_for_solution.json")
    with open(data_folder, 'w') as fp:
        json.dump(assoc_dict, fp, indent=4)
    data_functions.write_asset_types_to_json()

    data_functions.one_solution_asset_types(solution_id)

    add_type_to_solution_form.solution_id.data = solution_id
    non_assoc_types = []
    add_type_to_solution_form.asset_types.choices = non_assoc_types
    for t in all_types:
        if t.id not in associated_asset_types:
            non_assoc_types.append((str(t.id), t.asset_type))
    if change_primary_asset_type_form.change_primary_asset_type_submit.data and change_primary_asset_type_form.validate():
        db_connect.update_column(model=models.Solutions,
                                 column=models.Solutions.primary_asset_type,
                                 id=solution_id,
                                 v=change_primary_asset_type_form.all_asset_types.data)
        data_functions.one_solution_data(solution_id)
    # Adds asset type to solution
    if add_type_to_solution_form.add_submit.data and add_type_to_solution_form.validate():
        solution = db_connect.query_one_db(model=models.Solutions,
                                           column=models.Solutions.id,
                                           v=solution_id)
        updated_list = solution.associated_asset_types
        for t in add_type_to_solution_form.asset_types.data:
            if t not in solution.associated_asset_types:
                updated_list.append(int(t))
                print(t)
        db_connect.update_assoc_asset_types(sid=int(add_type_to_solution_form.solution_id.data),
                                            values=updated_list)
        db_connect.update_column(model=models.Solutions,
                                 id=solution_id,
                                 column=models.Solutions.date_revised,
                                 v=datetime.datetime.now())
        redirect(url_for('view_one_solution', solution_id=add_type_to_solution_form.solution_id.data))

    # Adds associated solution
    if add_assoc_solution_form.assoc_solution_submit.data and add_assoc_solution_form.validate():
        print(add_assoc_solution_form.assoc_solution_id.data + "will be added")
        update_column = db_connect.query_one_db(model=models.Solutions,
                                                column=models.Solutions.id,
                                                v=int(add_assoc_solution_form.main_solution_id.data))
        print(str(add_assoc_solution_form.assoc_solution_id.data) + " added to " + str(update_column.id))

        # This makes sure the associated solution is not the solution itself
        if int(add_assoc_solution_form.assoc_solution_id.data) == int(
                update_column.id):  # or update_column.associated_solutions is None or int(add_assoc_solution_form.assoc_solution_id.data) in update_column.associated_solutions:
            print("Caught to be the same solution!!!!!!!!!!!!!!!!!!!!!!!!!!")
            return redirect(url_for('view_one_solution', solution_id=add_assoc_solution_form.main_solution_id.data))
        else:
            # If the solution IDs are not the same, check if there is an associated solution yet.
            if update_column.associated_solutions is None:
                # If there are no associated solutions, make an empty list
                print("No associated solutions!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
                associated_solutions = []
                db_connect.update_column(model=models.Solutions,
                                         id=int(add_assoc_solution_form.main_solution_id.data),
                                         column=models.Solutions.associated_solutions,
                                         v=associated_solutions + [int(add_assoc_solution_form.assoc_solution_id.data)])
            # If the solution IDs are not the same, make sure it is not already an associated solution
            elif int(add_assoc_solution_form.assoc_solution_id.data) not in update_column.associated_solutions:
                print("Adding to solution!!!!!!!!!!!!!!!!!!!!!!!")
                # If it is already an associated solution it will just reload the page. Can make a message later
                # There might be a way to make the Array field have to be unique with sqlalchemy.
                # In which case I could just but this in a try/except statement.
                associated_solutions = []
                s = data_functions.get_associated_solutions(solution_id)
                for i in s:
                    associated_solutions.append(i)
                print(associated_solutions + [int(add_assoc_solution_form.assoc_solution_id.data)])
                db_connect.update_column(model=models.Solutions,
                                         id=int(add_assoc_solution_form.main_solution_id.data),
                                         column=models.Solutions.associated_solutions,
                                         v=associated_solutions + [int(add_assoc_solution_form.assoc_solution_id.data)])
                redirect(url_for('view_one_solution', solution_id=add_assoc_solution_form.main_solution_id.data))

    # Get image filenames for each step in the solution
    solution_path = os.getcwd() + '\\static\data\\solution_images\\sid' + solution_id
    # solution_path = '\\documentation_system\\static\data\\solution_images\\sid' + solution_id
    # solution_path = 'documentation_system/static/data/solution_images/sid' + solution_id
    step_folders = [f.path for f in os.scandir(solution_path) if f.is_dir()]
    step_count = 1
    step_images = {}
    f = open(os.getcwd() + '\\static\data\one_solution.json')
    solution_json = json.load(f)
    # for s in step_folders:
    #     # This doesn't work because listdir(s) is a list of all the filenames in the directory
    #     this_step = []
    #     # for i in os.listdir(s):
    #     # Getting the info from the JSON data instead of the folder because that is what will update when editing
    #     # a solution so that is what it should pull from when selecting what images to get from the directory.
    for s in solution_json['Steps']:
        this_step = []
        for i in solution_json['Steps'][str(step_count)]['Images']:
            this_step.append(["data/solution_images/sid" + str(solution_id) + "/" + i, i])
        step_images[str(step_count)] = this_step
        step_count += 1
    print(step_images)
    # Info on view_one_solution page that may be useful on the edit checkbox for images
    # style='background-image: url("{{ url_for('static', filename=i[0]) }}")'

    return render_template('view_one_solution.html',
                           asset_types=db_connect.query_all(models.AssetTypes),
                           associated_asset_types=data_functions.one_solution_asset_types(solution_id),
                           assoc_solutions=data_functions.get_associated_solutions(solution_id),
                           non_assoc_types=non_assoc_types,
                           all_asset_types=data_functions.write_asset_types_to_json(),
                           add_type_to_solution_form=add_type_to_solution_form,
                           add_assoc_solution_form=add_assoc_solution_form,
                           change_primary_asset_type_form=change_primary_asset_type_form,
                           solution_id=solution_id,
                           steps=solution.steps,
                           title=solution.solution_title,
                           step_images=step_images,
                           public=solution.public,
                           )