Esempio n. 1
0
    def get_token(cls, user):
        token = super().get_token(user)
        data = get_object_or_404(Cooperative, pk=user.cooperative_id)
        coop_data = CooperativeSerializer(data).data

        user_data = PartnerSerializer(user).data
        token['user'] = camelize(user_data)
        token['cooperative'] = camelize(coop_data)
        token['SDGEnabled'] = settings.SDG_ENABLED or False

        return token
Esempio n. 2
0
 def test_camel_to_under_input_untouched_for_sequence(self):
     data = [
         {
             'firstInput': 1
         },
         {
             'secondInput': 2
         },
     ]
     reference_input = deepcopy(data)
     camelize(data)
     self.assertEqual(data, reference_input)
Esempio n. 3
0
 def test_camelize_iterates_over_generator(self):
     data = self._underscore_generator()
     output = [
         {
             'simpleIsBetter': 'than complex'
         },
         {
             'thatIs': 'correct'
         },
     ]
     self.assertEqual(camelize(data), output)
 def render_errors(self,
                   data,
                   accepted_media_type=None,
                   renderer_context=None):
     render_data = utils.format_errors(data, accepted_media_type,
                                       renderer_context)
     if api_settings.CAMELIZE:
         render_data = camelize(render_data)
     return super(JSONRenderer,
                  self).render(render_data, accepted_media_type,
                               renderer_context)
    def render(self, data, accepted_media_type=None, renderer_context=None):
        renderer_context = renderer_context or {}

        # If this is an error response, skip the rest.
        if utils.is_error_response(renderer_context):
            return self.render_errors(data, accepted_media_type,
                                      renderer_context)

        google_style_params = renderer_context.get('kwargs')
        google_style_params.update(
            renderer_context.get('request').query_params.items())

        serialize_data = OrderedDict()
        if data is not None:
            if 'meta' in data:
                serialize_data.update(data['meta'])
                data.pop('meta', None)

            if 'results' in data:
                data['items'] = data['results']
                data.pop('results', None)
                serialize_data.update(data)
            else:
                if issubclass(type(data), list):
                    serialize_data['items'] = data
                else:
                    serialize_data = data
        else:
            serialize_data = {}

        render_data = OrderedDict()
        render_data['method'] = getattr(renderer_context.get('view'), 'action',
                                        'null')
        render_data['params'] = google_style_params
        render_data['data'] = serialize_data

        if api_settings.CAMELIZE:
            render_data = camelize(render_data)
        return super(JSONRenderer,
                     self).render(render_data, accepted_media_type,
                                  renderer_context)
Esempio n. 6
0
 def test_under_to_camel_keys(self):
     data = {
         "two_word": 1,
         "long_key_with_many_underscores": 2,
         "only_1_key": 3,
         "only_one_letter_a": 4,
         "b_only_one_letter": 5,
         "only_c_letter": 6,
         "mix_123123a_and_letters": 7,
         "no_underscore_before123": 8
     }
     output = {
         "twoWord": 1,
         "longKeyWithManyUnderscores": 2,
         "only1Key": 3,
         "onlyOneLetterA": 4,
         "bOnlyOneLetter": 5,
         "onlyCLetter": 6,
         "mix123123aAndLetters": 7,
         "noUnderscoreBefore123": 8
     }
     self.assertEqual(camelize(data), output)
 def render(self, data, *args, **kwargs):
     return super(CamelCaseJSONRenderer,
                  self).render(camelize(data), *args, **kwargs)
Esempio n. 8
0
 def test_non_string_key(self):
     data = {1: "test"}
     self.assertEqual(underscoreize(camelize(data)), data)
Esempio n. 9
0
 def test_tuples(self):
     data = {"multiple_values": (1, 2), "data": [1, 3, 4]}
     output = {"multipleValues": [1, 2], "data": [1, 3, 4]}
     self.assertEqual(camelize(data), output)