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
def test_get_instance_with_slumber_urls(self): pizza = get_instance( 'slumber://pizzas/slumber_examples/Pizza/', 'slumber://pizzas/slumber_examples/Pizza/data/%s/' % self.pizza.id, None) self.assertEqual(pizza._url, 'http://localhost:8000/slumber/pizzas/slumber_examples/' 'Pizza/data/%s/' % self.pizza.id) self.assertEqual(pizza.id, self.pizza.id) self.assertEqual(pizza.name, self.pizza.name)
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)
def test_get_instance_with_slumber_urls(self): pizza = get_instance( "slumber://pizzas/slumber_examples/Pizza/", "slumber://pizzas/slumber_examples/Pizza/data/%s/" % self.pizza.id, None, ) self.assertEqual( pizza._url, "http://localhost:8000/slumber/pizzas/slumber_examples/" "Pizza/data/%s/" % self.pizza.id ) self.assertEqual(pizza.id, self.pizza.id) self.assertEqual(pizza.name, self.pizza.name)
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)
def get(self, **query): """Implements a mocked version of the get operator. """ for i in self.instances: found = True for k, v in query.items(): found = found and hasattr(i, k) and (getattr( i, k) == v or unicode(getattr(i, k)) == unicode(v)) if found: if hasattr(i, '_url'): return get_instance('slumber://' + self._url, i._url, None) else: return i assert False, "The instance was not found\n%s" % query
def get(self, **query): """Implements a mocked version of the get operator. """ for i in self.instances: found = True for k, v in query.items(): found = found and hasattr(i, k) and ( getattr(i, k) == v or unicode(getattr(i, k)) == unicode(v)) if found: if hasattr(i, '_url'): return get_instance( 'slumber://' + self._url, i._url, None) else: return i assert False, "The instance was not found\n%s" % query
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
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
def from_json_data(base_url, json): """Convert a JSON representation of some data to the right types within the client. """ if json['kind'] == 'object': if json['data'] is None: return None else: # It's a remote object from slumber.connector.api import get_instance, get_model model_url = urljoin(base_url, json['data']['type']) data_url = urljoin(base_url, json['data']['data']) display = json['data']['display'] return get_instance(get_model(model_url), data_url, display) else: return json['data']
def test_model_form_submission_with_instance_get_instance(self): shop = self.client.pizzas.slumber_examples.Shop.create( name='Shop', slug='shop') shop_from_url = get_instance( self.client.pizzas.slumber_examples.Shop, shop._url, unicode(shop)) form = WidgetTest.ModelForm(dict(shop=shop_from_url)) self.assertEquals(form.as_p(), '''<p><label for="id_shop">Shop:</label> ''' '''<input type="text" name="shop" ''' '''value="http://localhost:8000/slumber/pizzas/slumber_examples/Shop/data/1/" ''' '''id="id_shop" /></p>''') self.assertEquals(type(form.fields['shop']), RemoteForeignKeyField) self.assertTrue(form.is_valid()) self.assertEquals(unicode(form.cleaned_data['shop']), unicode(shop)) self.assertEquals(form.as_p(), '''<p><label for="id_shop">Shop:</label> ''' '''<input type="text" name="shop" ''' '''value="http://localhost:8000/slumber/pizzas/slumber_examples/Shop/data/1/" ''' '''id="id_shop" /></p>''')
def test_model_form_submission_with_instance_get_instance(self): shop = self.client.pizzas.slumber_examples.Shop.create(name='Shop', slug='shop') shop_from_url = get_instance(self.client.pizzas.slumber_examples.Shop, shop._url, unicode(shop)) form = WidgetTest.ModelForm(dict(shop=shop_from_url)) self.sameSoup( form, '''<p><label for="id_shop">Shop:</label> ''' '''<input type="text" name="shop" ''' '''value="http://localhost:8000/slumber/pizzas/slumber_examples/Shop/data/1/" ''' '''id="id_shop" /></p>''') self.assertEquals(type(form.fields['shop']), RemoteForeignKeyField) self.assertTrue(form.is_valid()) self.assertEquals(unicode(form.cleaned_data['shop']), unicode(shop)) self.sameSoup( form, '''<p><label for="id_shop">Shop:</label> ''' '''<input type="text" name="shop" ''' '''value="http://localhost:8000/slumber/pizzas/slumber_examples/Shop/data/1/" ''' '''id="id_shop" /></p>''')
def test_model_form_submission_with_instance_get_instance(self): shop = self.client.pizzas.slumber_examples.Shop.create(name="Shop", slug="shop") shop_from_url = get_instance(self.client.pizzas.slumber_examples.Shop, shop._url, unicode(shop)) form = WidgetTest.ModelForm(dict(shop=shop_from_url)) self.sameSoup( form, """<p><label for="id_shop">Shop:</label> """ """<input type="text" name="shop" """ """value="http://localhost:8000/slumber/pizzas/shop/1/" """ """id="id_shop" /></p>""", ) self.assertEquals(type(form.fields["shop"]), RemoteForeignKeyField) self.assertTrue(form.is_valid()) self.assertEquals(unicode(form.cleaned_data["shop"]), unicode(shop)) self.sameSoup( form, """<p><label for="id_shop">Shop:</label> """ """<input type="text" name="shop" """ """value="http://localhost:8000/slumber/pizzas/shop/1/" """ """id="id_shop" /></p>""", )
def __call__(self, url, display_name): return get_instance('slumber://' + self._url, url, display_name)