Exemplo n.º 1
0
    def to_python(self, value):
        if not value:
            return None
        if isinstance(value, _InstanceProxy):
            return value
        instance_url = from_slumber_scheme(
            super(TypedRemoteChoiceField, self).to_python(value),
            get_slumber_services())
        model_url = from_slumber_scheme(self.model_url, get_slumber_services())
        return get_instance(model_url, instance_url, None)


#     def to_python(self, value):
#         """
#         Validates that the value is in self.choices and can be coerced to the
#         right type.
#         """
#         value = super(TypedChoiceField, self).to_python(value)
#         if value == self.empty_value or value in self.empty_values:
#             return self.empty_value
#         try:
#             value = self.coerce(value)
#         except (ValueError, TypeError, ValidationError):
#             raise ValidationError(
#                 self.error_messages['invalid_choice'],
#                 code='invalid_choice',
#                 params={'value': value},
#             )
#         return value
Exemplo n.º 2
0
 def get_db_prep_value(self, value, *a, **kw):
     if value is None:
         url = None
     elif isinstance(value, basestring):
         url = to_slumber_scheme(value, get_slumber_services())
     else:
         url = to_slumber_scheme(value._url, get_slumber_services())
     return super(RemoteForeignKey, self).get_db_prep_value(url, *a, **kw)
Exemplo n.º 3
0
 def get_db_prep_value(self, value, *a, **kw):
     if value is None:
         url = None
     elif isinstance(value, basestring):
         url = to_slumber_scheme(value, get_slumber_services())
     else:
         url = to_slumber_scheme(value._url, get_slumber_services())
     return super(RemoteForeignKey, self).get_db_prep_value(url, *a, **kw)
Exemplo n.º 4
0
 def to_python(self, value):
     if not value:
         return None
     if isinstance(value, _InstanceProxy):
         return value
     instance_url = from_slumber_scheme(
         super(RemoteForeignKey, self).to_python(value),
         get_slumber_services())
     model_url = from_slumber_scheme(self.model_url, get_slumber_services())
     return get_instance(model_url, instance_url, None)
Exemplo n.º 5
0
 def to_python(self, value):
     if not value:
         return None
     if isinstance(value, _InstanceProxy):
         return value
     instance_url = from_slumber_scheme(
         super(RemoteForeignKey, self).to_python(value),
         get_slumber_services())
     model_url = from_slumber_scheme(
         self.model_url, get_slumber_services())
     return get_instance(model_url, instance_url, None)
Exemplo n.º 6
0
 def get_prep_value(self, value, *a, **kw):
     url = value
     if value is not None:
         if isinstance(value, basestring) or isinstance(value, DictObject):
             return value
         url = to_slumber_scheme(value._url, get_slumber_services())
     return super(RemoteForeignKey, self).get_prep_value(url, *a, **kw)
Exemplo n.º 7
0
 def test_slumber_services(self):
     with patch(
         "slumber.server.get_slumber_directory",
         lambda: {"pizzas": "/slumber/pizzas/", "takeaway": "http://localhost:8002:/slumber/"},
     ):
         directory = get_slumber_services()
         self.assertIsNotNone(directory)
Exemplo n.º 8
0
 def test_slumber_services(self):
     with patch(
             'slumber.server.get_slumber_directory', lambda: {
                 'pizzas': '/slumber/pizzas/',
                 'takeaway': 'http://localhost:8002:/slumber/'
             }):
         directory = get_slumber_services()
         self.assertIsNotNone(directory)
Exemplo n.º 9
0
 def render(self, name, value, attrs=None, choices=()):
     # we had to cast slumber field to string
     if value is not None:
         value = to_slumber_scheme(value._url, get_slumber_services())
     return super(RemoteSelect, self).render(name,
                                             value,
                                             attrs=attrs,
                                             choices=choices)
Exemplo n.º 10
0
 def __init__(self, directory=None):
     client_apps = getattr(settings, 'SLUMBER_CLIENT_APPS', [])
     for app in client_apps:
         __import__(app, globals(), locals(), ['slumber_client'])
     services = get_slumber_services(directory)
     if not services:
         if not directory:
             directory = get_slumber_directory()
         super(Client, self).__init__(directory)
     else:
         for k, v in services.items():
             setattr(self, k, ServiceConnector(v))
         super(Client, self).__init__(None)
Exemplo n.º 11
0
def to_slumber_scheme(url, services=None):
    """Look at the URL and convert it to a service based URL if applicable.
    """
    if services is None:
        services = get_slumber_services()
    ret = None
    if services:
        longest = None, ''
        for service, service_url in services.items():
            if url.startswith(service_url) and len(service_url) > len(longest):
                ret, longest =  \
                    'slumber://%s/%s' % (service, url[len(service_url):]), \
                        service_url
    return ret or url
Exemplo n.º 12
0
 def __init__(self, directory=None):
     self._instances = []
     client_apps = getattr(settings, 'SLUMBER_CLIENT_APPS', [])
     for app in client_apps:
         __import__(app, globals(), locals(), ['slumber_client'])
     services = get_slumber_services(directory)
     if not services:
         if not directory:
             directory = get_slumber_directory()
         super(Client, self).__init__(directory)
     else:
         for k, v in services.items():
             setattr(self, k, ServiceConnector(v))
         super(Client, self).__init__(None)
Exemplo n.º 13
0
 def clean(self, value):
     if not value:
         if self.required:
             raise forms.ValidationError('This field is required')
         return None
     elif isinstance(value, _InstanceProxy):
         return value
     else:
         try:
             model_url = from_slumber_scheme(
                 self.model_url, get_slumber_services())
             instance = get_instance(model_url, value, None)
             unicode(instance)
         except AssertionError:
             raise forms.ValidationError("The remote object doesn't exist")
         return instance
Exemplo n.º 14
0
 def clean(self, value):
     if not value:
         if self.required:
             raise forms.ValidationError('This field is required')
         return None
     elif isinstance(value, _InstanceProxy):
         return value
     else:
         try:
             model_url = from_slumber_scheme(self.model_url,
                                             get_slumber_services())
             instance = get_instance(model_url, value, None)
             unicode(instance)
         except AssertionError:
             raise forms.ValidationError("The remote object doesn't exist")
         return instance
Exemplo n.º 15
0
def from_slumber_scheme(url, services=None):
    """Turn a Slumber URL into a full URI as specified by the service
    configuration.
    """
    if services is None:
        services = get_slumber_services()
    if url.startswith('slumber://'):
        if not services:
            raise SlumberServiceURLError(
                "There are no services in the Slumber directory "
                "so the URL %s cannot be dereferenced" % url)
        for service in  services.keys():
            service_prefix = 'slumber://%s/' % service
            if url.startswith(service_prefix):
                service_url = services[service]
                final_url = service_url + url[len(service_prefix):]
                return final_url
        raise SlumberServiceURLError(
            "Service in URL %s not found in configured services %s"
                % (url, services.keys()))
    return url
Exemplo n.º 16
0
 def test_slumber_services_none(self):
     directory = get_slumber_services()
     self.assertIsNone(directory)
Exemplo n.º 17
0
 def test_slumber_services_none(self):
     directory = get_slumber_services()
     self.assertIsNone(directory)
Exemplo n.º 18
0
def get_service_directory(_request, response):
    """Return the services.
    """
    response['services'] = get_slumber_services()
Exemplo n.º 19
0
def get_service_directory(_request, response):
    """Return the services.
    """
    response['services'] = get_slumber_services()
Exemplo n.º 20
0
 def test_slumber_services(self):
     with patch('slumber.server.get_slumber_directory', lambda: {
             'pizzas': '/slumber/pizzas/',
             'takeaway': 'http://localhost:8002:/slumber/'}):
         directory = get_slumber_services()
         self.assertIsNotNone(directory)