<commit_before>""" Filesystem backend for file upload. """


from pathlib import Path

from django.conf import settings
import django.core.cache
from django.urls import reverse
from django.utils.encoding import smart_text

from .. import exceptions
from .base import BaseBackend


class Backend(BaseBackend):
    """
    Upload openassessment student files to a local filesystem. Note
    that in order to use this file storage backend, you need to include the
    urls from openassessment.fileupload in your urls.py file:

    E.g:
        url(r'^openassessment/storage', include(openassessment.fileupload.urls)),

    The ORA2_FILEUPLOAD_CACHE_NAME setting will also have to be defined for the
    name of the django.core.cache instance which will maintain the list of
    active storage URLs.

    E.g:

        ORA2_FILEUPLOAD_CACHE_NAME = "ora2-storage"
        CACHES = {
            ...
            'ora2-storage': {
                'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
                ...
            },
            ...
        }
    """

    def get_upload_url(self, key, content_type):
        make_upload_url_available(self._get_key_name(key), self.UPLOAD_URL_TIMEOUT)
        return self._get_url(key)

    def get_download_url(self, key):
        key_name = self._get_key_name(key)
        if self._is_file_existing(key_name):
            make_download_url_available(key_name, self.DOWNLOAD_URL_TIMEOUT)
            return self._get_url(key)
        return None

    def remove_file(self, key):
        from openassessment.fileupload.views_filesystem import safe_remove, get_file_path
        return safe_remove(get_file_path(self._get_key_name(key)))

    def _get_url(self, key):
        key_name = self._get_key_name(key)
        url = reverse("openassessment-filesystem-storage", kwargs={'key': key_name})
        return url

    def _is_file_existing(self, key_name):
        from openassessment.fileupload.views_filesystem import get_file_path

        file_path = get_file_path(key_name)
        file_path = Path(file_path)
        return file_path.exists()


def get_cache():
    """
    Returns a django.core.cache instance in charge of maintaining the
    authorized upload and download URL.

    Raises:
        FileUploadInternalError if the cache name setting is not defined.
        InvalidCacheBackendError if the corresponding cache backend has not
        been configured.
    """
    cache_name = getattr(settings, "ORA2_FILEUPLOAD_CACHE_NAME", None)
    if cache_name is None:
        raise exceptions.FileUploadInternalError("Undefined cache backend for file upload")
    return django.core.cache.caches[cache_name]
<commit_msg>Rename file exists function<commit_after>""" Filesystem backend for file upload. """


from pathlib import Path

from django.conf import settings
import django.core.cache
from django.urls import reverse
from django.utils.encoding import smart_text

from .. import exceptions
from .base import BaseBackend


class Backend(BaseBackend):
    """
    Upload openassessment student files to a local filesystem. Note
    that in order to use this file storage backend, you need to include the
    urls from openassessment.fileupload in your urls.py file:

    E.g:
        url(r'^openassessment/storage', include(openassessment.fileupload.urls)),

    The ORA2_FILEUPLOAD_CACHE_NAME setting will also have to be defined for the
    name of the django.core.cache instance which will maintain the list of
    active storage URLs.

    E.g:

        ORA2_FILEUPLOAD_CACHE_NAME = "ora2-storage"
        CACHES = {
            ...
            'ora2-storage': {
                'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
                ...
            },
            ...
        }
    """

    def get_upload_url(self, key, content_type):
        make_upload_url_available(self._get_key_name(key), self.UPLOAD_URL_TIMEOUT)
        return self._get_url(key)

    def get_download_url(self, key):
        key_name = self._get_key_name(key)
        if self._file_exists(key_name):
            make_download_url_available(key_name, self.DOWNLOAD_URL_TIMEOUT)
            return self._get_url(key)
        return None

    def remove_file(self, key):
        from openassessment.fileupload.views_filesystem import safe_remove, get_file_path
        return safe_remove(get_file_path(self._get_key_name(key)))

    def _get_url(self, key):
        key_name = self._get_key_name(key)
        url = reverse("openassessment-filesystem-storage", kwargs={'key': key_name})
        return url

    def _file_exists(self, key_name):
        from openassessment.fileupload.views_filesystem import get_file_path

        file_path = get_file_path(key_name)
        file_path = Path(file_path)
        return file_path.exists()


def get_cache():
    """
    Returns a django.core.cache instance in charge of maintaining the
    authorized upload and download URL.

    Raises:
        FileUploadInternalError if the cache name setting is not defined.
        InvalidCacheBackendError if the corresponding cache backend has not
        been configured.
    """
    cache_name = getattr(settings, "ORA2_FILEUPLOAD_CACHE_NAME", None)
    if cache_name is None:
        raise exceptions.FileUploadInternalError("Undefined cache backend for file upload")
    return django.core.cache.caches[cache_name]

{'fraction_non_alphanumeric': 0.07578008915304606, 'fraction_numerical': 0.0018573551263001485, 'mean_word_length': 2.947947214076246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1555113', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1552, 'n_words': 415}