Exemplo n.º 1
0
 def get_redirect_url(self, **kwargs):
     guid = Guid.load(kwargs['guids'])
     if guid:
         referent = guid.referent
         if getattr(referent, 'absolute_api_v2_url', None):
             return referent.absolute_api_v2_url
         else:
             raise EndpointNotImplementedError()
     return None
Exemplo n.º 2
0
    def update(self, instance, validated_data):
        addon_name = instance.config.short_name
        if addon_name not in ADDONS_FOLDER_CONFIGURABLE:
            raise EndpointNotImplementedError(
                'Requested addon not currently configurable via API.')

        auth = get_user_auth(self.context['request'])

        set_account, external_account_id = self.get_account_info(
            validated_data)
        set_folder, folder_info = self.get_folder_info(validated_data,
                                                       addon_name)

        # Maybe raise errors
        self.check_for_update_errors(instance, folder_info,
                                     external_account_id)

        if instance and instance.configured and set_folder and not folder_info:
            # Enabled and configured, user requesting folder unset
            instance.clear_settings()
            instance.save()

        if instance and instance.has_auth and set_account and not external_account_id:
            # Settings authorized, User requesting deauthorization
            instance.deauthorize(auth=auth)  # clear_auth performs save
            return instance
        elif external_account_id:
            # Settings may or may not be authorized, user requesting to set instance.external_account
            account = self.get_account_or_error(addon_name,
                                                external_account_id, auth)
            if instance.external_account and external_account_id != instance.external_account._id:
                # Ensure node settings are deauthorized first, logs
                instance.deauthorize(auth=auth)
            instance.set_auth(account, auth.user)

        if set_folder and self.should_call_set_folder(folder_info, instance,
                                                      auth, instance):
            # Enabled, user requesting to set folder
            try:
                instance.set_folder(folder_info, auth)
                instance.save()
            except InvalidFolderError:
                raise exceptions.NotFound('Unable to find requested folder.')
            except InvalidAuthError:
                raise exceptions.PermissionDenied(
                    'Addon credentials are invalid.')

        return instance