def token_manager(self): """ Return the token manager for this backend """ if not self._token_manager: self._token_manager = AccessTokenManager(self.name) return self._token_manager
def fetch(request): """ Try to fetch an object or its related objects """ try: related = 'related' in request.POST otype = request.POST['type'] if otype not in ('account', 'repository'): raise id = int(request.POST['id']) backend = get_backend(request.POST['backend'] or None) if not backend: raise # check if you can manage related for this type for this backend if related and not backend.supports( '%s_related' % ('user' if otype == 'account' else otype)): raise if otype == 'account': slug = request.POST['slug'] obj = Account.objects.get(id=id, backend=backend.name, slug=slug) else: project = request.POST['project'] obj = Repository.objects.get(id=id, backend=backend.name, project=project) except: return HttpResponseBadRequest('Vilain :)') else: # find a access token token = AccessTokenManager.get_for_backend( obj.backend).get_one(wait=False) if not token: messages.error( request, 'Fetch is not possible right now, all the workers are working hard...' ) elif related: if obj.fetch_related_allowed(): try: obj.fetch_related(token=token) except MultipleBackendError, e: for message in e.messages: messages.error(request, message) except BackendError, e: messages.error(request, e.message) else: messages.success(request, 'Fetch of related is successfull !') else:
def fetch(request): """ Try to fetch an object or its related objects """ try: related = 'related' in request.POST otype = request.POST['type'] if otype not in ('account', 'repository'): raise id = int(request.POST['id']) backend = get_backend(request.POST['backend'] or None) if not backend: raise # check if you can manage related for this type for this backend if related and not backend.supports( '%s_related' % ('user' if otype == 'account' else otype)): raise if otype == 'account': slug = request.POST['slug'] obj = Account.objects.get(id=id, backend=backend.name, slug=slug) else: project = request.POST['project'] obj = Repository.objects.get(id=id, backend=backend.name, project=project) except: return HttpResponseBadRequest('Vilain :)') else: # find a access token token = AccessTokenManager.get_for_backend(obj.backend).get_one(wait=False) if not token: messages.error(request, 'Fetch is not possible right now, all the workers are working hard...') elif related: if obj.fetch_related_allowed(): try: obj.fetch_related(token=token) except MultipleBackendError, e: for message in e.messages: messages.error(request, message) except BackendError, e: messages.error(request, e.message) else: messages.success(request, 'Fetch of related is successfull !') else:
def parse_json(json, priority): """ Parse the data got from redis lists """ result = {} # unserialize data = simplejson.loads(json) # parse object string model_name, id = data['object'].split(':') if model_name == 'core.account': model = Account elif model_name == 'core.repository': model = Repository else: raise Exception('Invalid object string') # check if object not already done or to be done in another list try: wanted_priority = int( Hash(settings.WORKER_FETCH_FULL_HASH_KEY)[data['object']]) except: wanted_priority = None if wanted_priority is None or wanted_priority != priority: return {'ignore': True} # load object result['object'] = model.objects.get(pk=id) # find a good token token_manager = AccessTokenManager.get_for_backend( result['object'].backend) result['token'] = token_manager.get_by_uid(data.get('token', None)) # which depth... result['depth'] = data.get('depth', 0) or 0 # maybe a user to notity result['notify_user'] = data.get('notify_user', None) return result
def parse_json(json, priority): """ Parse the data got from redis lists """ result = {} # unserialize data = simplejson.loads(json) # parse object string model_name, id = data['object'].split(':') if model_name == 'core.account': model = Account elif model_name == 'core.repository': model = Repository else: raise Exception('Invalid object string') # check if object not already done or to be done in another list try: wanted_priority = int(Hash(settings.WORKER_FETCH_FULL_HASH_KEY)[data['object']]) except: wanted_priority = None if wanted_priority is None or wanted_priority != priority: return { 'ignore': True } # load object result['object'] = model.objects.get(pk=id) # find a good token token_manager = AccessTokenManager.get_for_backend(result['object'].backend) result['token'] = token_manager.get_by_uid(data.get('token', None)) # which depth... result['depth'] = data.get('depth', 0) or 0 # maybe a user to notity result['notify_user'] = data.get('notify_user', None) return result
def fetch(request): """ Try to fetch an object or its related objects """ try: related = 'related' in request.POST otype = request.POST['type'] if otype not in ('account', 'repository'): raise id = int(request.POST['id']) backend = get_backend(request.POST['backend'] or None) if not backend: raise # check if you can manage related for this type for this backend if related and not backend.supports( '%s_related' % ('user' if otype == 'account' else otype)): raise if otype == 'account': slug = request.POST['slug'] obj = Account.objects.get(id=id, backend=backend.name, slug=slug) else: project = request.POST['project'] obj = Repository.objects.get(id=id, backend=backend.name, project=project) except: return HttpResponseBadRequest('Vilain :)') else: # find a access token token = AccessTokenManager.get_for_backend(obj.backend).get_one(wait=False) if not token: messages.error(request, 'Fetch is not possible right now, all the workers are working hard...') elif related: if obj.fetch_related_allowed(): try: obj.fetch_related(token=token) except BackendError, e: messages.error(request, 'Fetch of related failed :(') exceptions = [e] if isinstance(e, MultipleBackendError): exceptions = e.exceptions for ex in exceptions: if isinstance(ex, BackendSuspendedTokenError): token.suspend(ex.extra.get('suspended_until'), str(ex)) elif isinstance(ex, BackendError) and ex.code: if ex.code == 401: token.set_status(ex.code, str(ex)) elif ex.code in (403, 404): obj.set_backend_status(ex.code, str(ex)) else: messages.success(request, 'Fetch of related is successful!') else: messages.error(request, 'Fetch of related is not allowed (maybe the last one is too recent)') else:
def fetch(request): """ Try to fetch an object or its related objects """ try: related = 'related' in request.POST otype = request.POST['type'] if otype not in ('account', 'repository'): raise id = int(request.POST['id']) backend = get_backend(request.POST['backend'] or None) if not backend: raise # check if you can manage related for this type for this backend if related and not backend.supports( '%s_related' % ('user' if otype == 'account' else otype)): raise if otype == 'account': slug = request.POST['slug'] obj = Account.objects.get(id=id, backend=backend.name, slug=slug) else: project = request.POST['project'] obj = Repository.objects.get(id=id, backend=backend.name, project=project) except: return HttpResponseBadRequest('Vilain :)') else: # find a access token token = AccessTokenManager.get_for_backend( obj.backend).get_one(wait=False) if not token: messages.error( request, 'Fetch is not possible right now, all the workers are working hard...' ) elif related: if obj.fetch_related_allowed(): try: obj.fetch_related(token=token) except BackendError, e: messages.error(request, 'Fetch of related failed :(') exceptions = [e] if isinstance(e, MultipleBackendError): exceptions = e.exceptions for ex in exceptions: if isinstance(ex, BackendSuspendedTokenError): token.suspend(ex.extra.get('suspended_until'), str(ex)) elif isinstance(ex, BackendError) and ex.code: if ex.code == 401: token.set_status(ex.code, str(ex)) elif ex.code in (403, 404): obj.set_backend_status(ex.code, str(ex)) else: messages.success(request, 'Fetch of related is successful!') else: messages.error( request, 'Fetch of related is not allowed (maybe the last one is too recent)' ) else: