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 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 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_slumber_service_url_with_a_different_service(self): translated = from_slumber_scheme( 'slumber://another_service/Model/', dict(testservice='http://example.com/slumber/testservice/', another_service='http://example.com/slumber/another_service/')) self.assertEquals(translated, 'http://example.com/slumber/another_service/Model/')
def test_call_monitor_model(self, mock_callback_test): self.client.login(username='******', password='******') monitor_model(update_log_model_url=self.update_log_model_url, model_url=self.model_url, table=self.table, callback=self.callback, callback_kwargs=self.callback_kwargs) # it should create a new ChangeSubscription object ss = ChangeSubscription.objects.all()[0] self.assertEqual(ss.update_log_model_url, self.update_log_model_url) self.assertEqual(ss.table, self.table) self.assertEqual(ss.callback, self.callback) self.assertDictEqual(json.loads(ss.callback_kwargs), self.callback_kwargs) # it should create a job in django-async job = Job.objects.all()[0] self.assertEqual(job.name, 'pubsubpull.async.pull_monitor') self.assertEqual(json.loads(job.args), [self.model_url, self.callback]) self.assertDictEqual(json.loads(job.kwargs), dict(delay=0, callback_kwargs=dict(param1="Title"))) # callback_test should be called because there is a pizza pizza = Pizza.objects.create(name="P1") pizza_instance_url = from_slumber_scheme('slumber://pizza/slumber_examples/Pizza/data/{}/'.format(pizza.pk)) management.call_command('flush_queue') mock_callback_test.assert_called_with(pizza_instance_url, **self.callback_kwargs)
def _fetch_instance(self): """Fetch the underlying instance. """ from slumber import _client for candidate in getattr(_client, '_instances', []): candidate_url = from_slumber_scheme(candidate._url) self_url = from_slumber_scheme(self._url) if candidate_url == self_url: return candidate instance = CLIENT_INSTANCE_CACHE.get(self._url, None) if not instance: # We now have a cache miss so construct a new connector instance = _InstanceConnector(self._url, **self._fields) if CLIENT_INSTANCE_CACHE.enabled: CLIENT_INSTANCE_CACHE[self._url] = instance return instance
def test_call_monitor_model(self, mock_callback_test): self.client.login(username='******', password='******') monitor_model(update_log_model_url=self.update_log_model_url, model_url=self.model_url, table=self.table, callback=self.callback, callback_kwargs=self.callback_kwargs) # it should create a new ChangeSubscription object ss = ChangeSubscription.objects.all()[0] self.assertEqual(ss.update_log_model_url, self.update_log_model_url) self.assertEqual(ss.table, self.table) self.assertEqual(ss.callback, self.callback) self.assertDictEqual(json.loads(ss.callback_kwargs), self.callback_kwargs) # it should create a job in django-async job = Job.objects.all()[0] self.assertEqual(job.name, 'pubsubpull.async.pull_monitor') self.assertEqual(json.loads(job.args), [self.model_url, self.callback]) self.assertDictEqual( json.loads(job.kwargs), dict(delay=0, callback_kwargs=dict(param1="Title"))) # callback_test should be called because there is a pizza pizza = Pizza.objects.create(name="P1") pizza_instance_url = from_slumber_scheme( 'slumber://pizza/slumber_examples/Pizza/data/{}/'.format(pizza.pk)) management.call_command('flush_queue') mock_callback_test.assert_called_with(pizza_instance_url, **self.callback_kwargs)
def test_slumber_service_url_with_a_different_service(self): translated = from_slumber_scheme( 'slumber://another_service/Model/', dict( testservice='http://example.com/slumber/testservice/', another_service='http://example.com/slumber/another_service/')) self.assertEquals(translated, 'http://example.com/slumber/another_service/Model/')
def _fetch_instance(self): """Fetch the underlying instance. """ from slumber import _client for candidate in getattr(_client, '_instances', []): candidate_url = from_slumber_scheme(candidate._url) self_url = from_slumber_scheme(self._url) if candidate_url == self_url: return candidate instance = getattr(PER_THREAD, 'cache', {}).get(self._url, None) if not instance: # We now have a cache miss so construct a new connector instance = _InstanceConnector(self._url, **self._fields) if hasattr(PER_THREAD, 'cache'): PER_THREAD.cache[self._url] = instance else: logging.info("No cache to write instance %s into", self._url) return instance
def get_model(url): """Return the client model connector for a given URL. """ url = from_slumber_scheme(url) if not MODEL_URL_TO_SLUMBER_MODEL.has_key(url): bases = [ModelConnector] model_type = get_model_type(url, bases) return model_type(url) else: return MODEL_URL_TO_SLUMBER_MODEL[url]
def get_instance(model, instance_url, display_name, fields=None): """Return an instance of the specified model etc. """ if isinstance(model, basestring): model = get_model(model) # Assume we've been given a URL fields = fields or {} bases = [_InstanceProxy] for type_url, proxy in INSTANCE_PROXIES.items(): if model._url.endswith(type_url): bases.append(proxy) type_name = str(instance_url) instance_type = type(type_name, tuple(bases), {}) return instance_type(from_slumber_scheme(instance_url), display_name, fields)
def test_call_async_monitor(self, mock_callback_test, mock_pull_down): self.client.login(username='******', password='******') monitor_model(update_log_model_url=self.update_log_model_url, model_url=self.model_url, table=self.table, callback=self.callback, callback_kwargs=self.callback_kwargs) pizza = Pizza.objects.create(name="P1") pizza_instance_url = from_slumber_scheme('slumber://pizza/slumber_examples/Pizza/data/{}/'.format(pizza.pk)) async_monitor(update_log_url='slumber://pizza/pubsubpull/UpdateLog/data/1', update_log_model_url=self.update_log_model_url) management.call_command('flush_queue') # callback_test must be called mock_callback_test.assert_called_with(pizza_instance_url, **self.callback_kwargs)
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 async_monitor(update_log_url, update_log_model_url): """ schedule jobs in ChangeSubscription model corresponding to update_log_model_url """ json_data = _get_data_from_slumber(update_log_url) relative_instance_url = json_data['fields']['instance_url']['data'] table = json_data['fields']['table']['data'] base_url = _get_base_url(from_slumber_scheme(update_log_url)) instance_url = urljoin(base_url, relative_instance_url) subscriptions = ChangeSubscription.objects.filter( update_log_model_url=update_log_model_url, table=table) for subscription in subscriptions: schedule(subscription.callback, args=[instance_url], kwargs=json.loads(subscription.callback_kwargs))
def test_call_async_monitor(self, mock_callback_test, mock_pull_down): self.client.login(username='******', password='******') monitor_model(update_log_model_url=self.update_log_model_url, model_url=self.model_url, table=self.table, callback=self.callback, callback_kwargs=self.callback_kwargs) pizza = Pizza.objects.create(name="P1") pizza_instance_url = from_slumber_scheme( 'slumber://pizza/slumber_examples/Pizza/data/{}/'.format(pizza.pk)) async_monitor( update_log_url='slumber://pizza/pubsubpull/UpdateLog/data/1', update_log_model_url=self.update_log_model_url) management.call_command('flush_queue') # callback_test must be called mock_callback_test.assert_called_with(pizza_instance_url, **self.callback_kwargs)
def async_monitor(update_log_url, update_log_model_url): """ schedule jobs in ChangeSubscription model corresponding to update_log_model_url """ json_data = _get_data_from_slumber(update_log_url) relative_instance_url = json_data['fields']['instance_url']['data'] table = json_data['fields']['table']['data'] base_url = _get_base_url(from_slumber_scheme(update_log_url)) instance_url = urljoin(base_url, relative_instance_url) subscriptions = ChangeSubscription.objects.filter( update_log_model_url=update_log_model_url, table=table ) for subscription in subscriptions: schedule(subscription.callback, args=[instance_url], kwargs=json.loads(subscription.callback_kwargs))
def test_get_data_from_slumber(self, mock_get): source_url = 'slumber://pizza/slumber_examples/Pizza/' mock_get.return_value = None, 'response' actual = _get_data_from_slumber(source_url) mock_get.assert_called_with(from_slumber_scheme(source_url)) self.assertEqual(actual, 'response')
def check_pizzas(self, pizzas): base = from_slumber_scheme('slumber://pizza/slumber_examples/Pizza/') urls = [urljoin(base, data_link(p)) for p in pizzas] self.assertEquals(set(urls), self.URLS)
def test_slumber_service_url_with_invalid_service(self): with self.assertRaises(SlumberServiceURLError): from_slumber_scheme( 'slumber://not-a-service/Model/', dict(testservice='http://example.com/slumber/testservice/'))
def _get_data_from_slumber(update_log_url): """ get data from given url, accept both http and slumber scheme """ _, json_data = get(from_slumber_scheme(update_log_url)) return json_data
def test_not_a_slumber_url(self): translated = from_slumber_scheme( 'http://example.org/slumber/', dict(testservice='http://example.com/slumber/testservice/')) self.assertEquals(translated, 'http://example.org/slumber/')
def test_no_services_with_normal_url(self): translated = from_slumber_scheme( 'http://example.com/slumber/', None) self.assertEquals(translated, 'http://example.com/slumber/')
def test_no_services_with_normal_url(self): translated = from_slumber_scheme('http://example.com/slumber/', None) self.assertEquals(translated, 'http://example.com/slumber/')
def test_no_services_with_slumber_url(self): with self.assertRaises(SlumberServiceURLError): translated = from_slumber_scheme('slumber://service/Model/', None)
def test_no_services_with_slumber_url(self): with self.assertRaises(SlumberServiceURLError): translated = from_slumber_scheme( 'slumber://service/Model/', None)
def test_slumber_service_url(self): translated = from_slumber_scheme( 'slumber://testservice/Model/', dict(testservice='http://example.com/slumber/testservice/')) self.assertEquals(translated, 'http://example.com/slumber/testservice/Model/')