Esempio n. 1
0
    def set_hash(self):
        if not self.metadata.get('hash'):
            if self.is_remote_url:
                md5_hash = get_hash(self.metadata['redirect_url'])
            else:
                md5_hash = get_hash(self.content.file.read())

            self.metadata['hash'] = f'md5:{md5_hash}'
Esempio n. 2
0
    def hash(self, request):
        """
        Creates an hash of `version_id` of all accessible assets by the user.
        Useful to detect changes between each request.

        :param request:
        :return: JSON
        """
        user = self.request.user
        if user.is_anonymous:
            raise exceptions.NotAuthenticated()
        else:
            accessible_assets = (get_objects_for_user(
                user, 'view_asset',
                Asset).filter(asset_type=ASSET_TYPE_SURVEY).order_by("uid"))

            assets_version_ids = [
                asset.version_id for asset in accessible_assets
                if asset.version_id is not None
            ]
            # Sort alphabetically
            assets_version_ids.sort()

            if len(assets_version_ids) > 0:
                hash_ = get_hash(''.join(assets_version_ids))
            else:
                hash_ = ''

            return Response({'hash': hash_})
Esempio n. 3
0
 def test_version_content_hash(self):
     _content = {
         'survey': [{
             'type': 'note',
             'label': 'Read me',
             'name': 'n1'
         }],
     }
     new_asset = Asset.objects.create(asset_type='survey', content=_content)
     expected_hash = get_hash(json.dumps(new_asset.content, sort_keys=True),
                              'sha1')
     self.assertEqual(new_asset.latest_version.content_hash, expected_hash)
     return new_asset
Esempio n. 4
0
 def hash(self):
     return get_hash(self.xml)
Esempio n. 5
0
def gravatar_url(email, https=True):
    return "%s://www.gravatar.com/avatar/%s?%s" % (
        'https' if https else 'http',
        get_hash(email.lower()),
        urlencode({'s': '40'}),
        )
Esempio n. 6
0
def sluggify(_str, _opts):
    """
    this method is ported over from coffeescript:
    jsapp/xlform/src/model.utils.coffee
    """
    _initial = _str
    if _str == '':
        return ''
    opts = dict(DEFAULT_OPTS, **_opts)

    if opts['lrstrip']:
        _str = _str.strip()
    elif opts['lstrip']:
        _str = _str.lstrip()
    elif opts['rstrip']:
        _str = _str.rstrip()

    if opts['lowerCase']:
        _str = _str.lower()

    if opts['underscores']:
        _str = re.sub(r'\s', '_', _str)
        # .replace(/[_]+/g, "_") <- replaces duplicates?

    if opts['replaceNonWordCharacters']:
        if opts['nonWordCharsExceptions']:
            regex = r'[^a-zA-Z0-9_{}]'.format(opts['nonWordCharsExceptions'])
        else:
            regex = r'[^a-zA-Z0-9_]+'  # Cannot use `\W`. Different behaviour with Python 2 & 3

        _str = re.sub(regex, '_', _str)
        if _str != '_' and re.search('_$', _str):
            _str = re.sub('_$', '', _str)

    if opts['characterLimit']:
        _limit = opts['characterLimit']
        if opts['characterLimit_shorten_method'] == 'ends':
            _str = _shorten_long_name(_str, _limit, join_with='_')
        else:
            _str = _str[0:opts['characterLimit']]

    if opts['validXmlTag']:
        if re.search(r'^\d', _str):
            _str = '_' + _str

    if opts['preventDuplicateUnderscores']:
        while re.search('__', _str):
            _str = re.sub('__', '_', _str)

    names = opts.get('other_names', opts['preventDuplicates'])
    if isinstance(names, list):
        names_lc = [name.lower() for name in names]
        attempt_base = _str
        if len(attempt_base) == 0:
            # empty string because arabic / cyrillic characters
            _str = 'h{}'.format(get_hash(_initial[0:7])[0:7])
        attempt = attempt_base
        incremented = 0
        while attempt.lower() in names_lc:
            incremented += 1
            attempt = "{0}_{1:03d}".format(attempt_base, incremented)
        _str = attempt

    return _str
Esempio n. 7
0
 def content_hash(self):
     # used to determine changes in the content from version to version
     # not saved, only compared with other asset_versions
     _json_string = json.dumps(self.version_content, sort_keys=True)
     return get_hash(_json_string, 'sha1')