Beispiel #1
0
 def _view(request, backend, *args, **kwargs):
     try:
         if not get_backend(backend).supports(functionnality):
             raise Http404
     except:
         raise Http404
     else:
         return view_func(request, backend, *args, **kwargs)
Beispiel #2
0
 def _view(request, backend, *args, **kwargs):
     try:
         if not get_backend(backend).supports(functionnality):
             raise Http404
     except:
         raise Http404
     else:
         return view_func(request, backend, *args, **kwargs)
Beispiel #3
0
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:
Beispiel #4
0
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:
Beispiel #5
0
def supports(backend, functionnality):
    """
    Return True if the given backend supports the given functionnality.
    `backend` can be a backend name, or a backend object
    """

    if isinstance(backend, basestring):
        backend = get_backend(backend)
        if not backend:
            return False

    if isinstance(backend, BaseBackend):
        return backend.supports(functionnality)

    return False
Beispiel #6
0
    def get_dialer(self, dev_opath, opath, plain=False):
        """
        Returns an instance of the dialer that will be used to connect

        :param dev_opath: DBus object path of the device to use
        :param opath: DBus object path of the dialer
        """
        from core.backends import get_backend, plain_backend

        device = self.ctrl.hm.clients[dev_opath]

        if plain:
            dialer_klass = plain_backend.get_dialer_klass(device)
        else:
            dialer_klass = get_backend().get_dialer_klass(device)

        return dialer_klass(device, opath, ctrl=self.ctrl)
Beispiel #7
0
    def get_or_new(self, backend, project=None, **defaults):
        """
        Try to get a existing accout, else create one (without saving it in
        database)
        If the project is given, get params from it
        This way we can manage projects with user+slug or without user
        """
        backend = get_backend(backend)

        defaults = copy(defaults)

        # get params from the project name
        if project:
            identifiers = backend.parse_project(project)
            for identifier in backend.needed_repository_identifiers:
                if identifiers.get(identifier, False):
                    defaults[identifier] = identifiers[identifier]

        # test that we have all needed defaults
        backend.assert_valid_repository_identifiers(**defaults)

        try:
            identifiers = dict(
                (key, defaults[key].lower())
                for key in backend.needed_repository_identifiers)
            if 'slug' in identifiers and 'slug_lower' not in identifiers:
                identifiers['slug_lower'] = identifiers['slug']
                del identifiers['slug']
            if 'official_owner' in identifiers and 'official_owner_lower' not in identifiers:
                identifiers['official_owner_lower'] = identifiers[
                    'official_owner']
                del identifiers['official_owner']
            repository = self.get(backend=backend.name, **identifiers)
        except self.model.DoesNotExist:
            # remove empty defaults
            allowed_fields = self.model._meta.get_all_field_names()
            defaults = dict((key, value) for key, value in defaults.items()
                            if key in allowed_fields)
            defaults['backend'] = backend.name

            repository = self.model(**defaults)
        else:
            if defaults:
                repository.update_many_fields(**defaults)

        return repository
Beispiel #8
0
def fetch(request):
    """
    Trigger an asyncrhronous fetch_full of an object
    """

    try:
        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

        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:
        obj.fetch_full(async=True,
                       async_priority=4,
                       notify_user=request.user,
                       allowed_interval=obj.MIN_FETCH_DELTA)

        message = 'Fetch of %s is in the queue and will be done soon' % obj.str_for_user(
            request.user)

        if request.is_ajax():
            result = dict(message=message)

            return JSONResponse(result)

        offline_messages.success(request.user, message, content_object=obj)

        return redirect(obj)
Beispiel #9
0
    def get_or_new(self, backend, project=None, **defaults):
        """
        Try to get a existing accout, else create one (without saving it in
        database)
        If the project is given, get params from it
        This way we can manage projects with user+slug or without user
        """
        backend = get_backend(backend)

        defaults = copy(defaults)

        # get params from the project name
        if project:
            identifiers = backend.parse_project(project)
            for identifier in backend.needed_repository_identifiers:
                if identifiers.get(identifier, False):
                    defaults[identifier] = identifiers[identifier]

        # test that we have all needed defaults
        backend.assert_valid_repository_identifiers(**defaults)

        try:
            identifiers = dict((key, defaults[key].lower())
                for key in backend.needed_repository_identifiers)
            if 'slug' in identifiers and 'slug_lower' not in identifiers:
                identifiers['slug_lower'] = identifiers['slug']
                del identifiers['slug']
            if 'official_owner' in identifiers and 'official_owner_lower' not in identifiers:
                identifiers['official_owner_lower'] = identifiers['official_owner']
                del identifiers['official_owner']
            repository = self.get(backend=backend.name, **identifiers)
        except self.model.DoesNotExist:
            # remove empty defaults
            allowed_fields = self.model._meta.get_all_field_names()
            defaults = dict((key, value) for key, value in defaults.items()
                if key in allowed_fields)
            defaults['backend'] = backend.name

            repository = self.model(**defaults)
        else:
            if defaults:
                repository.update_many_fields(**defaults)

        return repository
Beispiel #10
0
def fetch(request):
    """
    Trigger an asyncrhronous fetch_full of an object
    """

    try:
        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

        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:
        obj.fetch_full(async=True, async_priority=4, notify_user=request.user, allowed_interval=obj.MIN_FETCH_DELTA)

        message = 'Fetch of %s is in the queue and will be done soon' % obj.str_for_user(request.user)

        if request.is_ajax():
            result = dict(
                message = message
            )

            return JSONResponse(result)

        offline_messages.success(request.user, message, content_object=obj)

        return redirect(obj)
Beispiel #11
0
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:
Beispiel #12
0
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: