def create_user_key_link(self, user_id, one_time=False): success = False for _ in range(256): key_id = utils.rand_str(32) if one_time: short_id = utils.rand_str(settings.app.long_url_length) else: short_id = utils.rand_str_ne(settings.app.short_url_length) try: self.key_link_collection.update( { 'org_id': self.id, 'user_id': user_id, }, { '$set': { 'key_id': key_id, 'short_id': short_id, 'one_time': one_time, 'timestamp': utils.now(), } }, upsert=True) except pymongo.errors.DuplicateKeyError: continue success = True break if not success: raise KeyLinkError('Failed to generate random key short id') return { 'id': key_id, 'key_url': '/key/%s.tar' % key_id, 'key_zip_url': '/key/%s.zip' % key_id, 'key_onc_url': '/key_onc/%s.onc' % key_id, 'view_url': '/k/%s' % short_id, 'uri_url': '/ku/%s' % short_id, }
def create_user_key_link(self, user_id, one_time=False): success = False for _ in xrange(256): key_id = uuid.uuid4().hex if one_time: short_id = utils.rand_str(settings.app.long_url_length) else: short_id = utils.rand_str_ne(settings.app.short_url_length) try: self.key_link_collection.update({ 'org_id': self.id, 'user_id': user_id, }, {'$set': { 'key_id': key_id, 'short_id': short_id, 'one_time': one_time, 'timestamp': utils.now(), }}, upsert=True) except pymongo.errors.DuplicateKeyError: continue success = True break if not success: raise KeyLinkError('Failed to generate random key short id') return { 'id': key_id, 'key_url': '/key/%s.tar' % key_id, 'key_zip_url': '/key/%s.zip' % key_id, 'key_onc_url': '/key_onc/%s.zip' % key_id, 'view_url': '/k/%s' % short_id, 'uri_url': '/ku/%s' % short_id, }