fab_ui_case_study_images.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:directory-tests 作者: uktrade 项目源码 文件源码
def prepare_form_data(token: str, case_study: CaseStudy) -> (dict, dict):
    """Prepare form data based on the flags and custom values provided.

    :param token: CSRF middleware token required to submit the form
    :param case_study: a CaseStudy namedtuple with random data
    :return: a tuple consisting of form data and files to upload
    """
    data = {
        "csrfmiddlewaretoken": token,
        "supplier_case_study_wizard_view-current_step": "rich-media",
        "rich-media-image_one_caption": case_study.caption_1,
        "rich-media-image_two_caption": case_study.caption_2,
        "rich-media-image_three_caption": case_study.caption_3,
        "rich-media-testimonial": case_study.testimonial,
        "rich-media-testimonial_name": case_study.source_name,
        "rich-media-testimonial_job_title": case_study.source_job,
        "rich-media-testimonial_company": case_study.source_company
    }

    paths = [case_study.image_1, case_study.image_2, case_study.image_3]

    def get_basename(path):
        return basename(path) if path is not None else ""

    def get_mimetype(path):
        return MimeTypes().guess_type(path)[0] if path is not None else ""

    def read_image(path: str):
        res = ""
        if path is not None:
            with open(path, "rb") as f:
                res = f.read()
        return res

    name_1, name_2, name_3 = (get_basename(path) for path in paths)
    mime_1, mime_2, mime_3 = (get_mimetype(path) for path in paths)
    file_1, file_2, file_3 = (read_image(path) for path in paths)
    files = {
        "rich-media-image_one": (name_1, file_1, mime_1),
        "rich-media-image_two": (name_2, file_2, mime_2),
        "rich-media-image_three": (name_3, file_3, mime_3),
    }

    return data, files
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号