How to use the shortuuid.get_alphabet function in shortuuid

To help you get started, we’ve selected a few shortuuid examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ImperialCollegeLondon / django-drf-filepond / tests / test_utils.py View on Github external
def test_get_file_id(self):
        fid = _get_file_id()
        self.assertTrue(isinstance(fid, text_type),
                        'The file ID must be a string.')
        id_format = re.compile('^([%s]){22}$' % (shortuuid.get_alphabet()))
        self.assertRegex(fid, id_format, ('The generated ID does not match '
                                          'the defined ID format.'))
github ImperialCollegeLondon / django-drf-filepond / django_drf_filepond / api.py View on Github external
upload_id: This function takes a 22-character unique ID assigned to the
    original upload of the requested file.
    """
    # If the parameter matched the upload ID format, we assume that it
    # must be an upload ID and proceed accordingly. If the lookup of the
    # record fails, then we have another go assuming a filename was
    # instead provided.

    # NOTE: The API doesn't officially provide support for requesting stored
    # uploads by filename. This is retained here for backward compatibility
    # but it is DEPRECATED and will be removed in a future release.
    param_filename = False

    upload_id_fmt = re.compile('^([%s]){22}$'
                               % (shortuuid.get_alphabet()))

    if not upload_id_fmt.match(upload_id):
        param_filename = True
        LOG.debug('The provided string doesn\'t seem to be an '
                  'upload ID. Assuming it is a filename/path.')

    if not param_filename:
        try:
            su = StoredUpload.objects.get(upload_id=upload_id)
        except StoredUpload.DoesNotExist:
            LOG.debug('A StoredUpload with the provided ID doesn\'t '
                      'exist. Assuming this could be a filename.')
            param_filename = True

    if param_filename:
        # Try and lookup a StoredUpload record with the specified id
github ImperialCollegeLondon / django-drf-filepond / django_drf_filepond / views.py View on Github external
def get(self, request):
        LOG.debug('Filepond API: Restore view GET called...')
        if LOAD_RESTORE_PARAM_NAME not in request.GET:
            return Response('A required parameter is missing.',
                            status=status.HTTP_400_BAD_REQUEST)

        upload_id = request.GET[LOAD_RESTORE_PARAM_NAME]

        upload_id_fmt = re.compile('^([%s]){22}$' %
                                   (shortuuid.get_alphabet()))

        if not upload_id_fmt.match(upload_id):
            return Response('An invalid ID has been provided.',
                            status=status.HTTP_400_BAD_REQUEST)

        LOG.debug('Carrying out restore for file ID <%s>' % upload_id)

        try:
            tu = TemporaryUpload.objects.get(upload_id=upload_id)
        except TemporaryUpload.DoesNotExist:
            return Response('Not found', status=status.HTTP_404_NOT_FOUND)

        upload_file_name = tu.upload_name
        try:
            with open(tu.file.path, 'rb') as f:
                data = f.read()
github ImperialCollegeLondon / django-drf-filepond / django_drf_filepond / api.py View on Github external
destination_file_path + destination_file_name
    """
    # TODO: If the storage backend is not initialised, init now - this will
    # be removed when this module is refactored into a class.
    if not storage_backend_initialised:
        _init_storage_backend()

    # If there's no storage backend set then we're using local file storage
    # and FILE_STORE_PATH must be set.
    if not storage_backend:
        if ((not hasattr(local_settings, 'FILE_STORE_PATH')) or
                (not local_settings.FILE_STORE_PATH)):
            raise ImproperlyConfigured('A required setting is missing in your '
                                       'application configuration.')

    id_fmt = re.compile('^([%s]){22}$' % (shortuuid.get_alphabet()))
    if not id_fmt.match(upload_id):
        LOG.error('The provided upload ID <%s> is of an invalid format.'
                  % upload_id)
        raise ValueError('The provided upload ID is of an invalid format.')

    if not destination_file_path or destination_file_path == '':
        raise ValueError('No destination file path provided.')

    try:
        tu = TemporaryUpload.objects.get(upload_id=upload_id)
    except TemporaryUpload.DoesNotExist:
        raise ValueError('Record for the specified upload_id doesn\'t exist')

    # Before this was updated, passing a path ending in os.sep, i.e. a
    # directory name, would ensure that the file was stored in the specified
    # directory using the name that the file had when it was originally