Python django.utils.text.get_valid_filename() Examples

The following are 13 code examples of django.utils.text.get_valid_filename(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.utils.text , or try the search function .
Example #1
Source File: views.py    From civet with Apache License 2.0 6 votes vote down vote up
def get_job_results(request, job_id):
    """
    Just download all the output of the job into a tarball.
    """
    job = get_object_or_404(models.Job.objects.select_related('recipe',).prefetch_related('step_results'), pk=job_id)
    perms = Permissions.job_permissions(request.session, job)
    if not perms['can_see_results']:
        return HttpResponseForbidden('Not allowed to see results')

    response = HttpResponse(content_type='application/x-gzip')
    base_name = 'results_{}_{}'.format(job.pk, get_valid_filename(job.recipe.name))
    response['Content-Disposition'] = 'attachment; filename="{}.tar.gz"'.format(base_name)
    tar = tarfile.open(fileobj=response, mode='w:gz')
    for result in job.step_results.all():
        info = tarfile.TarInfo(name='{}/{:02}_{}'.format(base_name, result.position, get_valid_filename(result.name)))
        s = BytesIO(result.plain_output().replace('\u2018', "'").replace("\u2019", "'").encode("utf-8", "replace"))
        buf = s.getvalue()
        info.size = len(buf)
        info.mtime = time.time()
        tar.addfile(tarinfo=info, fileobj=s)
    tar.close()
    return response 
Example #2
Source File: scale_pre_steps.py    From scale with Apache License 2.0 6 votes vote down vote up
def _calculate_remote_path(self, job_exe):
        """Returns the remote path for storing the manifest

        :param job_exe: The job execution model (with related job and job_type fields) that is storing the files
        :type job_exe: :class:`job.models.JobExecution`
        :returns: The remote path for storing the manifest
        :rtype: str
        """

        remote_path = ''
        if job_exe.job.recipe:
            recipe = job_exe.job.recipe
            recipe_type_path = get_valid_filename(recipe.recipe_type.name)
            recipe_version_path = get_valid_filename(recipe.recipe_type.revision_num)
            remote_path = os.path.join(remote_path, 'recipes', recipe_type_path, recipe_version_path)
        job_type_path = get_valid_filename(job_exe.job.job_type.name)
        job_version_path = get_valid_filename(job_exe.job.job_type.version)
        remote_path = os.path.join(remote_path, 'jobs', job_type_path, job_version_path)

        the_date = now()
        year_dir = str(the_date.year)
        month_dir = '%02d' % the_date.month
        day_dir = '%02d' % the_date.day
        return os.path.join(remote_path, year_dir, month_dir, day_dir, 'job_exe_%i' % job_exe.id) 
Example #3
Source File: test_models.py    From scale with Apache License 2.0 6 votes vote down vote up
def test_fails(self, mock_makedirs, mock_getsize):
        """Tests calling ScaleFileManager.upload_files() when Workspace.upload_files() fails"""
        def new_getsize(path):
            return 100
        mock_getsize.side_effect = new_getsize

        upload_dir = os.path.join('upload', 'dir')
        work_dir = os.path.join('work', 'dir')

        workspace = storage_test_utils.create_workspace()
        file_1 = ScaleFile()
        file_1.media_type = None  # Scale should auto-detect text/plain
        remote_path_1 = 'my/remote/path/file.txt'
        local_path_1 = 'my/local/path/file.txt'
        file_2 = ScaleFile()
        file_2.media_type = 'application/json'
        remote_path_2 = 'my/remote/path/2/file.json'
        local_path_2 = 'my/local/path/2/file.json'
        workspace.upload_files = MagicMock()
        workspace.upload_files.side_effect = Exception
        workspace.delete_files = MagicMock()
        delete_work_dir = os.path.join(work_dir, 'delete', get_valid_filename(workspace.name))

        files = [(file_1, local_path_1, remote_path_1), (file_2, local_path_2, remote_path_2)]
        self.assertRaises(Exception, ScaleFile.objects.upload_files, upload_dir, work_dir, workspace, files) 
Example #4
Source File: storage.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def get_valid_name(self, name):
        """
        Returns a filename, based on the provided filename, that's suitable for
        use in the target storage system.
        """
        return get_valid_filename(name) 
Example #5
Source File: views.py    From telemetry-analysis-service with Mozilla Public License 2.0 5 votes vote down vote up
def download_spark_job(request, id):
    """
    Download the notebook file for the scheduled Spark job with the given ID.
    """
    spark_job = SparkJob.objects.get(pk=id)
    response = StreamingHttpResponse(
        spark_job.notebook_s3_object["Body"].read().decode("utf-8"),
        content_type="application/x-ipynb+json",
    )
    response["Content-Disposition"] = "attachment; filename=%s" % get_valid_filename(
        spark_job.notebook_name
    )
    response["Content-Length"] = spark_job.notebook_s3_object["ContentLength"]
    return response 
Example #6
Source File: test_product_data_file.py    From scale with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        django.setup()

        self.workspace_1 = Workspace.objects.create(name='Test workspace 1')
        self.workspace_2 = Workspace.objects.create(name='Test workspace 2', is_active=False)

        manifest = job_utils.create_seed_manifest(name='Type-1')
        job_type = job_utils.create_seed_job_type(manifest=manifest)

        event = TriggerEvent.objects.create_trigger_event('TEST', None, {}, now())
        self.job = job_utils.create_job(job_type=job_type, event=event, status='RUNNING', last_status_change=now())
        self.job_exe = job_utils.create_job_exe(job=self.job, status='RUNNING', timeout=1, queued=now())
        self.remote_base_path = os.path.join('jobs', get_valid_filename(self.job.job_type.name),
                                             get_valid_filename(self.job.job_type.version)) 
Example #7
Source File: core.py    From django-djangui with GNU General Public License v3.0 5 votes vote down vote up
def output_path(self):
        return os.path.join(djangui_settings.DJANGUI_FILE_DIR, get_valid_filename(self.user.username if self.user is not None else ''),
                            get_valid_filename(self.script.slug if not self.script.save_path else self.script.save_path), str(self.pk)) 
Example #8
Source File: utils.py    From django-leonardo with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_valid_filename(s):
    """
    like the regular get_valid_filename, but also slugifies away
    umlauts and stuff.
    """
    s = get_valid_filename_django(s)
    filename, ext = os.path.splitext(s)
    filename = slugify(filename)
    ext = slugify(ext)
    if ext:
        return "%s.%s" % (filename, ext)
    else:
        return "%s" % (filename,) 
Example #9
Source File: json.py    From zentral with Apache License 2.0 5 votes vote down vote up
def save_dead_letter(data, file_suffix, directory="/tmp/zentral_dead_letters"):
    now = timezone.now()
    filename = "{}_{}.json".format(
        now.strftime("%Y-%m-%d_%H.%M.%S.%f"),
        file_suffix
    )
    dirpath = os.path.join(directory, now.strftime("%Y/%m/%d"))
    try:
        os.makedirs(dirpath, exist_ok=True)
        with open(os.path.join(dirpath, get_valid_filename(filename)), "w", encoding="utf-8") as f:
            json.dump(data, f, indent="  ")
    except Exception:
        logger.error("Could not save dead letter %s", file_suffix) 
Example #10
Source File: storage.py    From luscan-devel with GNU General Public License v2.0 5 votes vote down vote up
def get_valid_name(self, name):
        """
        Returns a filename, based on the provided filename, that's suitable for
        use in the target storage system.
        """
        return get_valid_filename(name) 
Example #11
Source File: test_text.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_valid_filename(self):
        filename = "^&'@{}[],$=!-#()%+~_123.txt"
        self.assertEqual(text.get_valid_filename(filename), "-_123.txt")
        self.assertEqual(text.get_valid_filename(lazystr(filename)), "-_123.txt") 
Example #12
Source File: test_text.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_get_valid_filename(self):
        filename = "^&'@{}[],$=!-#()%+~_123.txt"
        self.assertEqual(text.get_valid_filename(filename), "-_123.txt")
        self.assertEqual(text.get_valid_filename(lazystr(filename)), "-_123.txt") 
Example #13
Source File: product_data_file.py    From scale with Apache License 2.0 4 votes vote down vote up
def _calculate_remote_path(self, job_exe, input_file_ids):
        """Returns the remote path for storing the products

        :param job_exe: The job execution model (with related job and job_type fields) that is storing the files
        :type job_exe: :class:`job.models.JobExecution`
        :param input_file_ids: Set of input file IDs
        :type input_file_ids: set of int
        :returns: The remote path for storing the products
        :rtype: str
        """

        remote_path = ''
        job_recipe = Recipe.objects.get_recipe_for_job(job_exe.job_id)
        if job_recipe:
            recipe = job_recipe.recipe
            recipe_type_path = get_valid_filename(recipe.recipe_type.name)
            recipe_revision = RecipeTypeRevision.objects.get_revision(recipe.recipe_type.name, recipe.recipe_type.revision_num).revision_num
            recipe_version_path = get_valid_filename('revision_%i' % recipe.recipe_type.revision_num)
            remote_path = os.path.join(remote_path, 'recipes', recipe_type_path, recipe_version_path)
        job_type_path = get_valid_filename(job_exe.job.job_type.name)
        job_version_path = get_valid_filename(job_exe.job.job_type.version)
        remote_path = os.path.join(remote_path, 'jobs', job_type_path, job_version_path)

        # Try to use source start time from the job
        the_date = job_exe.job.source_started

        if not the_date:
            # Try to grab source started the old way through the source ancestor file
            for source_file in FileAncestryLink.objects.get_source_ancestors(list(input_file_ids)):
                if source_file.data_started:
                    if not the_date or source_file.data_started < the_date:
                        the_date = source_file.data_started

        # No data start time populated, use current time
        if not the_date:
            remote_path = os.path.join(remote_path, 'unknown_source_data_time')
            the_date = now()

        year_dir = str(the_date.year)
        month_dir = '%02d' % the_date.month
        day_dir = '%02d' % the_date.day
        return os.path.join(remote_path, year_dir, month_dir, day_dir, 'job_exe_%i' % job_exe.id)