Beispiel #1
0
 def test_create(self):
     DataBag.objects.create(name='mybag')
     chef.DataBagItem.create('mybag', 'item1', attr=1)
     chef.DataBagItem.create('mybag', 'item2', attr=2)
     bag = DataBag.objects.get(name='mybag')
     self.assertEqual(bag.items.count(), 2)
     item = bag.items.get(name='item1')
     self.assertEqual(json.loads(item.data), {'id': 'item1', 'attr': 1})
     item = bag.items.get(name='item2')
     self.assertEqual(json.loads(item.data), {'id': 'item2', 'attr': 2})
Beispiel #2
0
def decode_json(request):
    request.json = None
    if request.META.get('CONTENT_TYPE') == 'application/json' and request.raw_post_data:
        try:
            request.json = json.loads(request.raw_post_data)
        except ValueError:
            pass
Beispiel #3
0
 def to_dict(self):
     return {
         'name': 'data_bag_item_%s_%s'%(self.bag.name, self.name),
         'data_bag': self.bag.name,
         'json_class': 'Chef::DataBagItem',
         'chef_type': 'data_bag_item',
         'raw_data': json.loads(self.data),
     }
Beispiel #4
0
 def test_update(self):
     bag = DataBag.objects.create(name='mybag')
     data = {'id': 'myitem', 'attr': 1, 'nested': {'nested_attr': 'foo'}}
     bag.items.create(name='myitem', data=json.dumps(data))
     chef_item = chef.DataBagItem('mybag', 'myitem')
     chef_item['attr'] = 2
     chef_item['nested']['nested_attr'] = 'bar'
     chef_item.save()
     data2 = {'id': 'myitem', 'attr': 2, 'nested': {'nested_attr': 'bar'}}
     item = DataBagItem.objects.get(bag__name='mybag', name='myitem')
     self.assertEqual(json.loads(item.data), data2)
Beispiel #5
0
 def process_response(self, request, response):
     # We're primarily interested in debuggin API output.
     # Non-API URLs are typically hit by a user who will see the debug info
     # in the browser. API can't do that!
     if request.path.startswith("/api"):
         content = response.content
         # JSON responses (caveat: this is probably all of them...) should
         # get pretty-printed
         if response['content-type'] == 'application/json':
             obj = json.loads(content)
             # And responses containing a top level traceback key should
             # also pretty-print that value.
             traceback = obj and obj.pop('traceback', None)
             if traceback is not None:
                 logger.debug(traceback)
             content = json.dumps(obj, indent=4)
         logger.debug(content)
     return response
Beispiel #6
0
 def object(self):
     if self.data:
         return json.loads(self.data)
     return {}
Beispiel #7
0
 def clean_data(self):
     try:
         json.loads(self.cleaned_data['data'])
     except ValueError, e:
         raise ValidationError(str(e))
Beispiel #8
0
 def default(self):
     if not self.default_data:
         return {}
     return json.loads(self.default_data)
Beispiel #9
0
 def normal(self):
     if not self.normal_data:
         return {}
     return json.loads(self.normal_data)
Beispiel #10
0
 def override(self):
     if not self.override_data:
         return {}
     return json.loads(self.override_data)
Beispiel #11
0
 def automatic(self):
     if not self.automatic_data:
         return {}
     return json.loads(self.automatic_data)