Commit 169da657 authored by Ondřej Borýsek's avatar Ondřej Borýsek
Browse files

Extract upload_scan2report_templates

parent 961eb41f
Loading
Loading
Loading
Loading
+10 −5
Original line number Original line Diff line number Diff line
@@ -90,13 +90,19 @@ def import_scan2report_templates_to_pwndoc():
        FlashLog.error(error_msg)
        FlashLog.error(error_msg)
        return redirect(url_for('api_templates.import_scan2report_templates_to_pwndoc'))
        return redirect(url_for('api_templates.import_scan2report_templates_to_pwndoc'))


    PwndocTemplateManager.update_template_db_from_pwndoc()  # necessary for mapping new uploads to existing templates
    saved_filepaths = [os.path.join(output_folder, x) for x in saved_filenames]
    upload_scan2report_templates(locale_to_import, saved_filepaths)
    return redirect(url_for('api_templates.import_scan2report_templates_to_pwndoc'))



def upload_scan2report_templates(locale_to_import: str, saved_filepaths: List[str]):
    PwndocTemplateManager.update_template_db_from_pwndoc()  # necessary for mapping new uploads to existing templates
    bundled_templates: Dict[str, Dict[str, TNSTemplatePwndoc]] = defaultdict(dict)
    bundled_templates: Dict[str, Dict[str, TNSTemplatePwndoc]] = defaultdict(dict)


    for single_filename in tqdm(saved_filenames, desc="Converting templates"):
    for single_filepath in tqdm(saved_filepaths, desc="Converting templates"):
        single_filename = os.path.basename(single_filepath)
        fid = extract_fid_from_filename(single_filename)
        fid = extract_fid_from_filename(single_filename)
        with open(os.path.join(output_folder, single_filename), encoding="utf8") as f:
        with open(single_filepath, encoding="utf8") as f:
            template_json = json.load(f)
            template_json = json.load(f)
        lang_and_templates: Dict[str, TemplateScan2Report] = TemplateScan2Report.parse_dict_for_locale_and_og(fid, locale_to_import, template_json)
        lang_and_templates: Dict[str, TemplateScan2Report] = TemplateScan2Report.parse_dict_for_locale_and_og(fid, locale_to_import, template_json)
        # I don't need to do DB refresh (for pwndoc ID) after every addition because I'm processing only single locale (or locale and og at the same time).
        # I don't need to do DB refresh (for pwndoc ID) after every addition because I'm processing only single locale (or locale and og at the same time).
@@ -105,7 +111,7 @@ def import_scan2report_templates_to_pwndoc():
            assert fid == single_tns_template.fid
            assert fid == single_tns_template.fid
            assert locale == single_tns_template.locale
            assert locale == single_tns_template.locale
            bundled_templates[fid][locale] = single_tns_template
            bundled_templates[fid][locale] = single_tns_template
    del single_filename, fid, single_tns_template, locale, template_json, lang_and_templates, f, output_folder
    del single_filename, fid, single_tns_template, locale, template_json, lang_and_templates, f


    for fid in tqdm(bundled_templates, desc="Uploading templates"):
    for fid in tqdm(bundled_templates, desc="Uploading templates"):
        dict_of_locales: Dict[str, TNSTemplatePwndoc] = bundled_templates[fid]
        dict_of_locales: Dict[str, TNSTemplatePwndoc] = bundled_templates[fid]
@@ -114,7 +120,6 @@ def import_scan2report_templates_to_pwndoc():
    PwndocTemplateManager.update_template_db_from_pwndoc()  # todo: this is workaround for not using PwndocTemplateManager.add_single_locale_template
    PwndocTemplateManager.update_template_db_from_pwndoc()  # todo: this is workaround for not using PwndocTemplateManager.add_single_locale_template


    FlashLog.info(f"OK; {len(bundled_templates)} templates uploaded")
    FlashLog.info(f"OK; {len(bundled_templates)} templates uploaded")
    return redirect(url_for('api_templates.import_scan2report_templates_to_pwndoc'))




@bp.route("/export_as_scan2report_native", methods=["GET"])
@bp.route("/export_as_scan2report_native", methods=["GET"])