示例#1
0
def save_or_store_media(file: models.FileField, filename: str):
    """
    Saves FileField filename as relative path if it's under MEDIA_ROOT.
    Otherwise writes file under media root.
    """
    if is_media_full_path(filename):
        file.name = strip_media_root(filename)  # type: ignore
    else:
        with open(filename, "rb") as fp:
            plain_filename = os.path.basename(filename)
            file.save(plain_filename, File(fp))  # type: ignore  # noqa
示例#2
0
 def _set_default_paths(self, options: dict):
     default_path = os.path.abspath(options["path"])
     qs = PayoutStatus.objects.all().filter(file_path="")
     if options["ws"]:
         qs = qs.filter(payout__connection_id=options["ws"])
     objs = list(qs)
     print("Setting default path of {} status updates to {}".format(len(objs), strip_media_root(default_path)))
     for obj in objs:
         assert isinstance(obj, PayoutStatus)
         full_path = os.path.join(default_path, obj.file_name)
         if not os.path.isfile(full_path):
             msg = "Error while updating file path of PayoutStatus id={}: File {} not found".format(obj.id, full_path)
             if not options["ignore_errors"]:
                 raise Exception(msg)
             logger.error(msg)
             continue
         file_path = strip_media_root(full_path)
         logger.info('PayoutStatus.objects.filter(id=%s).update(file_path="%s")', obj.id, file_path)
         if not options["test"]:
             PayoutStatus.objects.filter(id=obj.id).update(file_path=file_path)
             admin_log([obj], 'File path set as "{}" from terminal (parse_xp)'.format(full_path))
     print("Done")
示例#3
0
def list_files(dir_name: str,
               suffix: str = "",
               ignore_case: bool = True,
               use_media_root: bool = False,
               recurse: bool = False) -> List[str]:
    """
    Lists all files under specified directory.
    Optionally filter files by suffix and recurse to subdirectories.
    :param dir_name: Directory path
    :param suffix: Case-sensitive suffix (optional)
    :param ignore_case: Case-insensitive suffix. Default is True.
    :param use_media_root: Instead of full path return files relative to media root.
    :param recurse: Recurse subdirectories (optional)
    :return: List of file names found
    """
    if not os.path.isdir(dir_name):
        raise ValueError(_("{} is not a directory").format(dir_name))
    dir_full_path = os.path.abspath(dir_name)
    if use_media_root and not is_media_full_path(dir_full_path):
        raise ValueError(_("{} is not under MEDIA_ROOT"))

    if suffix:
        if not suffix.startswith("."):
            suffix = "." + suffix
        if ignore_case:
            suffix = suffix.lower()

    out: List[str] = []
    for ent in os.scandir(dir_full_path):
        assert isinstance(ent, os.DirEntry)
        if ent.is_file():
            name = ent.name
            if suffix and ignore_case:
                name = name.lower()
            if not suffix or name.endswith(suffix):
                file_path = strip_media_root(
                    ent.path) if use_media_root else os.path.abspath(ent.path)
                out.append(file_path)
        elif recurse and ent.is_dir() and ent.name != "." and ent.name != "..":
            out.extend(
                list_files(ent.path,
                           suffix=suffix,
                           ignore_case=ignore_case,
                           use_media_root=use_media_root,
                           recurse=recurse))
    return out
示例#4
0
def process_pain002_file_content(bcontent: bytes,
                                 filename: str,
                                 created: Optional[datetime] = None):
    if not created:
        created = now()
    s = Pain002(bcontent)
    p = Payout.objects.filter(msg_id=s.original_msg_id).first()

    ps = PayoutStatus(
        payout=p,
        file_name=basename(filename),
        file_path=strip_media_root(filename),
        msg_id=s.msg_id,
        original_msg_id=s.original_msg_id,
        group_status=s.group_status,
        status_reason=s.status_reason[:255],
        created=created,
        timestamp=s.credit_datetime,
    )
    ps.full_clean()
    fields = (
        "payout",
        "file_name",
        "response_code",
        "response_text",
        "msg_id",
        "original_msg_id",
        "group_status",
        "status_reason",
    )
    params = {}
    for k in fields:
        params[k] = getattr(ps, k)
    ps_old = PayoutStatus.objects.filter(**params).first()
    if ps_old:
        ps = ps_old
    else:
        ps.save()
        logger.info("%s status updated %s", p, ps)
    if p:
        if ps.is_accepted:
            p.state = PAYOUT_PAID
            p.paid_date = s.credit_datetime
            p.save(update_fields=["state", "paid_date"])
            logger.info("%s marked as paid %s", p, ps)
    return ps
示例#5
0
def find_file(filename: str,
              dir_name: str = ".",
              use_media_root: bool = False,
              recurse: bool = False) -> List[str]:
    """
    Finds file under specified directory.
    Optionally filter files by suffix and recurse to subdirectories.
    :param filename: File name to find. You can also specify relative paths e.g. "en/LC_MESSAGES/django.po"
    :param dir_name: Directory path. Default '.'
    :param use_media_root: Instead of full path return files relative to media root.
    :return: List of file names found
    :param recurse: Recurse subdirectories (optional)
    """
    if not os.path.isdir(dir_name):
        raise ValueError(_("{} is not a directory").format(dir_name))
    dir_full_path = os.path.abspath(dir_name)
    if use_media_root and not is_media_full_path(dir_full_path):
        raise ValueError(_("{} is not under MEDIA_ROOT"))
    out: List[str] = []
    if "/" not in filename:
        filename = "/" + filename
    for ent in os.scandir(dir_full_path):
        assert isinstance(ent, os.DirEntry)
        if ent.is_file():
            full_path = str(os.path.abspath(ent.path))
            if full_path.endswith(filename):
                file_path = strip_media_root(
                    full_path) if use_media_root else full_path
                out.append(file_path)
        elif recurse and ent.is_dir() and ent.name != "." and ent.name != "..":
            out.extend(
                find_file(filename,
                          dir_name=ent.path,
                          use_media_root=use_media_root,
                          recurse=recurse))
    return out