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.'))
Example #2
0
def store_upload(upload_id, destination_file_path):
    """
    Store the temporary upload with the specified upload ID to the
    destination_file_path. destination_file_path should be a directory only
    and not include the target name of the file.

    If destination_file_name is not provided, the file
    is stored using the name it was originally uploaded with. If
    destination_file_name is provided, this is the name used to store the
    file. i.e. the file will be stored at
        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
    # uploaded.  To ensure consistency with this previous approach to
    # handling files, we have to ensure here that if the original path ends
    # in os.sep and we're using local storage, this is maintained when it
    # is passed to _store_upload_local
    destination_name = ntpath.basename(destination_file_path)
    destination_path = ntpath.dirname(destination_file_path)

    if ((not storage_backend) and (destination_name == '')
            and (destination_file_path.endswith(os.sep))):
        # In some cases we'll enter this block but destination path will
        # already end in a '/' so check before updating
        if not destination_path.endswith('/'):
            destination_path += os.sep

    if storage_backend:
        return _store_upload_remote(destination_path, destination_name, tu)
    else:
        return _store_upload_local(destination_path, destination_name, tu)
Example #3
0
class ShortUUIDConverter:
    regex = '[{}]{{22}}'.format(shortuuid.get_alphabet())

    def to_python(self, value):
        return value

    def to_url(self, value):
        return value
Example #4
0
 def get_urls(self):
     urlpatterns = patterns(
         '',
         url(r'^$', self.ticket_list_view.as_view(), name='ticket-list'),
         url(r'^ticket/create/$',
             self.ticket_create_view.as_view(),
             name='ticket-create'),
         url(r'^ticket/update/(?P<pk>[{0}]+)/$'.format(get_alphabet()),
             self.ticket_update_view.as_view(),
             name='ticket-update'),
     )
     return self.post_process_urls(urlpatterns)
Example #5
0
 def get_urls(self):
     urlpatterns = patterns(
         '',
         url(r'^$', self.ticket_list_view.as_view(), name='ticket-list'),
         url(r'^ticket/create/$', self.ticket_create_view.as_view(),
             name='ticket-create'),
         url(
             r'^ticket/update/(?P<pk>[{0}]+)/$'.format(get_alphabet()),
             self.ticket_update_view.as_view(),
             name='ticket-update'
         ),
     )
     return self.post_process_urls(urlpatterns)
Example #6
0
 def get_urls(self):
     urls = [
         url(r'accounts/support/$',
             login_required(self.ticket_list_view.as_view()),
             name='customer-ticket-list'),
         url(r'accounts/support/ticket/create/$',
             login_required(self.ticket_create_view.as_view()),
             name='customer-ticket-create'),
         url(r'accounts/support/ticket/(?P<pk>[{0}]+)/update/$'.format(
             get_alphabet()),
             login_required(self.ticket_update_view.as_view()),
             name='customer-ticket-update'),
     ]
     return self.post_process_urls(urls)
Example #7
0
    def get_urls(self):
        urlpatterns = super(SupportApplication, self).get_urls()

        urlpatterns += patterns(
            '',
            url(r'accounts/support/$',
                self.ticket_list_view.as_view(),
                name='customer-ticket-list'),
            url(r'accounts/support/ticket/create/$',
                self.ticket_create_view.as_view(),
                name='customer-ticket-create'),
            url(r'accounts/support/ticket/(?P<pk>[{0}]+)/update/$'.format(
                get_alphabet()),
                self.ticket_update_view.as_view(),
                name='customer-ticket-update'),
        )
        return self.post_process_urls(urlpatterns)
Example #8
0
def get_stored_upload(upload_id):
    """
    Get an upload that has previously been stored using the store_upload
    function.

    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
        # as the file path
        try:
            su = StoredUpload.objects.get(file_path=upload_id)
        except StoredUpload.DoesNotExist as e:
            LOG.debug('A StoredUpload with the provided file path '
                      'doesn\'t exist. Re-raising error')
            raise e

    return su
Example #9
0
    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()
        except IOError as e:
            LOG.error('Error reading requested file: %s' % str(e))
            return Response('Error reading file data...',
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        ct = _get_content_type(upload_file_name)

        response = HttpResponse(data, content_type=ct)
        response['Content-Disposition'] = ('inline; filename=%s' %
                                           upload_file_name)

        return response
Example #10
0
    def get_urls(self):
        urlpatterns = super(SupportApplication, self).get_urls()

        urlpatterns += patterns(
            '',
            url(
                r'accounts/support/$',
                self.ticket_list_view.as_view(),
                name='customer-ticket-list'
            ),
            url(
                r'accounts/support/ticket/create/$',
                self.ticket_create_view.as_view(),
                name='customer-ticket-create'
            ),
            url(
                r'accounts/support/ticket/(?P<pk>[{0}]+)/update/$'.format(
                    get_alphabet()
                ),
                self.ticket_update_view.as_view(),
                name='customer-ticket-update'
            ),
        )
        return self.post_process_urls(urlpatterns)
Example #11
0
 def get_urls(self):
     urls = [
         url(r'^ticket/$', self.ticket_list_view.as_view(), name='ticket-list'),
         url(r'^ticket/create/$', self.ticket_create_view.as_view(),
             name='ticket-create'),
         url(
             r'^ticket/update/(?P<pk>[{0}]+)/$'.format(get_alphabet()),
             self.ticket_update_view.as_view(),
             name='ticket-update'
         ),
         url(
             r'tags/types/$', self.types_edit_view.as_view(),
             name='tag-type-list'
         ),
         url(
             r'tags/statuses/$', self.statuses_edit_view.as_view(),
             name='tag-status-list'
         ),
         url(
             r'tags/priorities/$', self.priorities_edit_view.as_view(),
             name='tag-priority-list'
         ),
     ]
     return self.post_process_urls(urls)
Example #12
0
import shortuuid

from django.conf.urls import patterns, url

from fancypages.api import views

SHORTUUID_ALPHA = shortuuid.get_alphabet()


urlpatterns = patterns(
    '',
    url(r'^blocks$', views.BlockListView.as_view(), name='block-list'),
    url(
        r'^block/new$',
        views.BlockNewView.as_view(),
        name='block-new'
    ),
    url(
        r'^block/(?P<uuid>[{0}]+)$'.format(SHORTUUID_ALPHA),
        views.BlockDetailView.as_view(),
        name='block-detail'
    ),
    url(
        r'^block/(?P<uuid>[{0}]+)/form$'.format(SHORTUUID_ALPHA),
        views.BlockFormView.as_view(),
        name='block-form'
    ),
    url(
        r'^block/(?P<uuid>[{0}]+)/move$'.format(SHORTUUID_ALPHA),
        views.BlockMoveView.as_view(),
        name='block-move'
Example #13
0
# 生成短UUID
print(shortuuid.uuid())

# 将URL生成UUID
print(shortuuid.uuid(name="baidu.com"))
print(shortuuid.uuid(name="http://www.baidu.com"))

print(shortuuid.uuid(name="测试用法"))

# 生成带密码的安全随机字符串( 内部使用 os.urandom(), )
# 默认生成22位数uuid。
print(shortuuid.ShortUUID().random(length=16))

# 查看用于生成新uuid的字母表
print(shortuuid.get_alphabet())

# 使用自己的字母表生成uuid
shortuuid.set_alphabet("012~`!@#$%^&*()_+{}|:<>?")
print(shortuuid.uuid())

# shortuuid 将自动排序和删除字母表中的重复项以确保一致性
print(shortuuid.get_alphabet())

# 序列化现有的uuid,请使用encode()和decode()
import uuid
u = uuid.uuid4()
print(u)
s = shortuuid.encode(u)
print(s)
print(shortuuid.decode(s) == u)
Example #14
0
    def get(self, request):
        LOG.debug('Filepond API: Load view GET called...')

        if ((not hasattr(local_settings, 'FILE_STORE_PATH'))
                or (not os.path.exists(local_settings.FILE_STORE_PATH))
                or (not os.path.isdir(local_settings.FILE_STORE_PATH))):
            return Response(
                'The file upload settings are not configured '
                'correctly.',
                status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        file_path_base = local_settings.FILE_STORE_PATH

        if LOAD_RESTORE_PARAM_NAME not in request.GET:
            return Response('A required parameter is missing.',
                            status=status.HTTP_400_BAD_REQUEST)

        param_filename = False
        upload_id = request.GET[LOAD_RESTORE_PARAM_NAME]

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

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

        su = None
        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.')
        else:
            # The provided id could be an upload_id so we can check here.
            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
            # as the file path
            try:
                su = StoredUpload.objects.get(file_path=upload_id)
            except StoredUpload.DoesNotExist:
                LOG.debug('A StoredUpload with the provided file path '
                          'doesn\'t exist.')
                return Response('Not found', status=status.HTTP_404_NOT_FOUND)

        # su is now the StoredUpload record for the requested file

        # See if the stored file with the path specified in su exists
        # in the file store location
        file_path = os.path.join(file_path_base, su.file_path)
        if ((not os.path.exists(file_path))
                or (not os.path.isfile(file_path))):
            return Response('Error reading file...',
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        # We now know that the file exists and is a file not a directory
        try:
            with open(file_path, 'rb') as f:
                data = f.read()
        except IOError as e:
            LOG.error('Error reading requested file: %s' % str(e))
            return Response('Error reading file...',
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

        filename = os.path.basename(su.file_path)
        ct = _get_content_type(filename)

        response = HttpResponse(data, content_type=ct)
        response['Content-Disposition'] = ('inline; filename=%s' % filename)

        return response
Example #15
0
def execute(data: list, **kwargs):
    for row in data:
        alphabet = row.get('alphabet', shortuuid.get_alphabet())
        length = int(row.get('length', 12))
        row['uuid'] = generate_uuid(alphabet, length)
    return data
Example #16
0
            time_range.append('timestamp<%d' % end_time)
        if time_range:
            params['ql'] = 'select * where %s' % ' and '.join(time_range)
        payload = {}
        try:
            body = json.dumps(payload, ensure_ascii=False).encode('utf8')
            r = requests.get(url, params=params, auth=self.app_client_auth, timeout=5)
            return easemob.http_result(r)
        except Exception, e:
            success = False
            result = unicode(e)
        return (success, result)


em = EaseMob()
_alphabet = shortuuid.get_alphabet()
shortuuid_lowercase = shortuuid.ShortUUID(alphabet=_alphabet.lower())

def prepare_msg_account():
    ''' 辅助函数:尝试注册环信用户,如果成功则返回环信用户名和密码。'''
    i = 0
    while i < 3:
        i += 1
        username = shortuuid_lowercase.uuid()   # 环信在很多接口其实会自动进行小写转换,所以统一使用小写字母更为安全
        password = shortuuid.uuid()
        success, result = em.register_new_user(username, password)
        if success:
            return (success, result, username, password)
    # ToDo: 创建失败应该写日志记录原因
    return (False, u'', u'', u'')
Example #17
0
import shortuuid

from django.conf.urls import patterns, url

from fancypages.api import views

SHORTUUID_ALPHA = shortuuid.get_alphabet()

urlpatterns = patterns(
    '',
    url(r'^blocks$', views.BlockListView.as_view(), name='block-list'),
    url(r'^block/new$', views.BlockNewView.as_view(), name='block-new'),
    url(r'^block/(?P<uuid>[{0}]+)$'.format(SHORTUUID_ALPHA),
        views.BlockDetailView.as_view(),
        name='block-detail'),
    url(r'^block/(?P<uuid>[{0}]+)/form$'.format(SHORTUUID_ALPHA),
        views.BlockFormView.as_view(),
        name='block-form'),
    url(r'^block/(?P<uuid>[{0}]+)/move$'.format(SHORTUUID_ALPHA),
        views.BlockMoveView.as_view(),
        name='block-move'),
    url(r'^ordered-containers$',
        views.OrderedContainerListView.as_view(),
        name='ordered-container-list'),
    url(r'^ordered-container/(?P<uuid>[{0}]+)$'.format(SHORTUUID_ALPHA),
        views.OrderedContainerDestroyView.as_view(),
        name='ordered-container-delete'),
    url(r'^block-types$',
        views.BlockTypesView.as_view(),
        name='block-type-list'),
    url(r'^pages$', views.PageList.as_view(), name='page-list'),
Example #18
0
        ProjectEmbedPopupView.as_view(),
        name='project_embed_popup'),
    url(r'create-project/$',
        CreateProjectView.as_view(),
        name='create_project'),
    url(r'users/(?P<profile_id>[0-9a-f]{32,32})/$',
        UserProfileDetailView.as_view(),
        name='userprofile_detail'),
    url(r'users/(?P<profile_id>[0-9a-f]{32,32})/share/$',
        UserProfileShareView.as_view(),
        name='userprofile_share'),
    url(r'users/(?P<profile_id>[0-9a-f]{32,32})/share/popup/$',
        UserProfileSharePopupView.as_view(),
        name='userprofile_share_popup'),
)

if shortuuid:
    alphabet = shortuuid.get_alphabet()
    urlpatterns = urlpatterns + patterns(
        '',
        url(r"users/(?P<short_profile_id>[%s]+)/$" % (alphabet),
            UserProfileDetailView.as_view(),
            name='userprofile_detail'),
        url(r"users/(?P<short_profile_id>[%s]+)/share/$" % (alphabet),
            UserProfileShareView.as_view(),
            name='userprofile_share'),
        url(r"users/(?P<short_profile_id>[%s]+)/share/popup/$" % (alphabet),
            UserProfileSharePopupView.as_view(),
            name='userprofile_share_popup'),
    )
Example #19
0
def store_upload(upload_id, destination_file_path):
    if ((not hasattr(local_settings, 'FILE_STORE_PATH'))
            or (not local_settings.FILE_STORE_PATH)
            or (not os.path.exists(local_settings.FILE_STORE_PATH))
            or (not os.path.isdir(local_settings.FILE_STORE_PATH))):

        raise ImproperlyConfigured('A required setting is missing in your '
                                   'application configuration.')

    file_path_base = local_settings.FILE_STORE_PATH

    if not file_path_base or file_path_base == '':
        raise ValueError('The FILE_STORE_PATH is not set to a directory.')

    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.')

    if destination_file_path.startswith(os.sep):
        destination_file_path = destination_file_path[1:]

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

    target_dir = os.path.join(file_path_base,
                              os.path.dirname(destination_file_path))
    if destination_file_path.endswith(os.sep):
        # Assume a directory was provided, get the file name from tu and
        # add this to the provided path.
        destination_file_path += tu.upload_name

    target_file_path = os.path.join(file_path_base, destination_file_path)

    # Check we're not about to overwrite anything
    if os.path.exists(target_file_path):
        LOG.error('File with specified name and path <%s> already exists' %
                  destination_file_path)
        raise FileExistsError('The specified temporary file cannot be stored'
                              ' to the specified location - file exists.')

    su = StoredUpload(upload_id=tu.upload_id,
                      file_path=destination_file_path,
                      uploaded=tu.uploaded)

    try:
        if not os.path.exists(target_dir):
            os.makedirs(target_dir)
        shutil.copy2(tu.get_file_path(), target_file_path)
        su.save()
        tu.delete()
    except IOError as e:
        LOG.error('Error moving temporary file to permanent storage location')
        raise e

    return su
from rest_framework import serializers

from .. import library
from ..utils import get_page_model, get_node_model


logger = logging.getLogger('fancypages.api')

FancyPage = get_page_model()
PageNode = get_node_model()
Container = get_model('fancypages', 'Container')
ContentBlock = get_model('fancypages', 'ContentBlock')
OrderedContainer = get_model('fancypages', 'OrderedContainer')

SHORTUUID_REGEX = re.compile(r'[{0}]+'.format(shortuuid.get_alphabet()))


class BlockSerializer(serializers.ModelSerializer):
    container = serializers.SlugRelatedField(slug_field='uuid')
    display_order = serializers.IntegerField(required=False, default=-1)
    code = serializers.CharField(required=True)

    def __init__(self, instance=None, data=None, files=None, context=None,
                 partial=False, many=None, allow_add_remove=False, **kwargs):
        if instance:
            self.Meta.model = instance.__class__
        elif data is not None:
            code = data.get('code')
            block_class = library.get_content_block(code)
            if block_class:
Example #21
0
from django.contrib.contenttypes.models import ContentType

from rest_framework import serializers

from .. import library
from ..utils import get_page_model, get_node_model

logger = logging.getLogger('fancypages.api')

# FancyPage = get_page_model()
# PageNode = get_node_model()
# Container = get_model('fancypages', 'Container')
# ContentBlock = get_model('fancypages', 'ContentBlock')
# OrderedContainer = get_model('fancypages', 'OrderedContainer')

SHORTUUID_REGEX = re.compile(r'[{0}]+'.format(shortuuid.get_alphabet()))


class BlockSerializer(serializers.ModelSerializer):
    container = serializers.SlugRelatedField(slug_field='uuid')
    display_order = serializers.IntegerField(required=False, default=-1)
    code = serializers.CharField(required=True)

    def __init__(self,
                 instance=None,
                 data=None,
                 files=None,
                 context=None,
                 partial=False,
                 many=None,
                 allow_add_remove=False,