def test_get_included_serializers_against_class(): klass = SerializerWithIncludedSerializers included_serializers = get_included_serializers(klass) expected_included_serializers = { 'blog': BlogSerializer, 'authors': AuthorSerializer, 'comments': CommentSerializer, 'self': klass } assert (six.viewkeys(included_serializers) == six.viewkeys(klass.included_serializers), 'the keys must be preserved') assert included_serializers == expected_included_serializers
def test_get_included_serializers_against_class(): klass = SerializerWithIncludedSerializers included_serializers = get_included_serializers(klass) expected_included_serializers = { 'blog': BlogSerializer, 'authors': AuthorSerializer, 'comments': CommentSerializer, 'self': klass } assert (six.viewkeys(included_serializers) == six.viewkeys( klass.included_serializers), 'the keys must be preserved') assert included_serializers == expected_included_serializers
def update_vms(self, started_vm, stopped_vm): processed_vm = set() for vm in self.vm_set.all(): if vm in started_vm: vm.start() elif vm in stopped_vm: vm.stop() else: vm.disappear() processed_vm.add(vm.name) for vm in six.viewkeys(started_vm) - processed_vm: VM.objects.create(name=vm, hkvm=self, status='STARTED') for vm in six.viewkeys(stopped_vm) - processed_vm: VM.objects.create(name=vm, hkvm=self, status='STOPPED')
def geojson(self): if not self.is_serialized('geometry'): return self._dumps() geom = self['geometry'] or '{}' keys = six.viewkeys(self) - {'geometry'} props = json.dumps({k: self[k] for k in keys}, cls=JSONEncoder)[1:-1] return '{"geometry": %s, %s}' % (str(geom), props)
def geojson(self): if not self.is_serialized('geometry'): return self._dumps() geom = self['geometry'] or '{}' keys = six.viewkeys(self) - {'geometry'} props = json.dumps({k: self[k] for k in keys}, cls=JSONEncoder)[1:-1] return '{"geometry": %s, %s}' % (str(geom), props)
def geojson(self): if not self.has_serialized_geom: return self._dumps() features = ','.join(map(str, self['features'])) keys = six.viewkeys(self) - {'features'} collection = '%s, "features": [' % json.dumps( {k: self[k] for k in keys}, cls=JSONEncoder)[:-1] return ''.join([collection, features, ']}'])
def geojson(self): if not self.has_serialized_geom: return self._dumps() features = ','.join(map(str, self['features'])) keys = six.viewkeys(self) - {'features'} collection = '%s, "features": [' % json.dumps( {k: self[k] for k in keys}, cls=JSONEncoder)[:-1] return ''.join([collection, features, ']}'])
def auto_display(parser, token): """ This tag returns an HTML element. Arguments:: relation -- One object from the 'relations' template context object. prefix -- A keyword argument who's value is used as a prefix to the element id and name attributes. option -- A keyword argument who's value is in the 'dynamicColumns' context. The entire 'dynamicColumns' context can be supplied or just the object for this relation. display -- A keyword argument. If 'True' use <span> for all tags else 'False' use the default tag types. Assume data structures below for examples:: {'dynamicColumns': { 'book': [ [0, 'Choose a value'], [2, 'HTML5 Pocket Reference'], [1, 'SQL Pocket Guide'], [3, 'Raspberry Pi Hacks'] ] } } {'relations': { '1': { 'name': 'Project ID', 'required': false, 'relation': null, 'location': 'top-container', 'value_type': 0, 'order': 3, 'value': '12345' }, } Usage Examples:: {% auto_display relation %} {% auto_display relation prefix=test- %} {% auto_display relation options=books %} {% auto_display relation options=books display=True %} {% auto_display relation prefix=test- options=dynamicColumns %} """ tokens = token.split_contents() size = len(tokens) kwargs = {'prefix': '', 'options': None, 'display': 'False'} keywords = list(six.viewkeys(kwargs)) keywords.sort() if size == 2: tag_name, relation = tokens kwargs = {} elif size == 3: tag_name, relation, value1 = tokens kwargs.update( {k: v for k, d, v in [v.partition('=') for v in (value1, )]}) elif size == 4: tag_name, relation, value1, value2 = tokens kwargs.update( {k: v for k, d, v in [v.partition('=') for v in (value1, value2)]}) elif size == 5: tag_name, relation, value1, value2, value3 = tokens kwargs.update({ k: v for k, d, v in [v.partition('=') for v in (value1, value2, value3)] }) else: msg = ("Invalid number of arguments should be 1 - 4, " "found: {}").format(size - 1) raise template.TemplateSyntaxError(msg) if size > 2 and not all([key in keywords for key in kwargs]): msg = "Invalid keyword name, should be one of {}".format(keywords) raise template.TemplateSyntaxError(msg) return AutoDisplayNode(tag_name, relation, **kwargs)
def auto_display(parser, token): """ This tag returns an HTML element. Arguments:: relation -- One object from the 'relations' template context object. prefix -- A keyword argument who's value is used as a prefix to the element id and name attributes. option -- A keyword argument who's value is in the 'dynamicColumns' context. The entire 'dynamicColumns' context can be supplied or just the object for this relation. display -- A keyword argument. If 'True' use <span> for all tags else 'False' use the default tag types. Assume data structures below for examples:: {'dynamicColumns': { 'book': [ [0, 'Choose a value'], [2, 'HTML5 Pocket Reference'], [1, 'SQL Pocket Guide'], [3, 'Raspberry Pi Hacks'] ] } } {'relations': { '1': { 'name': 'Project ID', 'required': false, 'relation': null, 'location': 'top-container', 'value_type': 0, 'order': 3, 'value': '12345' }, } Usage Examples:: {% auto_display relation %} {% auto_display relation prefix=test- %} {% auto_display relation options=books %} {% auto_display relation options=books display=True %} {% auto_display relation prefix=test- options=dynamicColumns %} """ tokens = token.split_contents() size = len(tokens) kwargs = {'prefix': '', 'options': None, 'display': 'False'} keywords = list(six.viewkeys(kwargs)) keywords.sort() if size == 2: tag_name, relation = tokens kwargs = {} elif size == 3: tag_name, relation, value1 = tokens kwargs.update({k: v for k,d,v in [v.partition('=') for v in (value1,)]}) elif size == 4: tag_name, relation, value1, value2 = tokens kwargs.update({k: v for k,d,v in [v.partition('=') for v in (value1, value2)]}) elif size == 5: tag_name, relation, value1, value2, value3 = tokens kwargs.update({k: v for k,d,v in [v.partition('=') for v in (value1, value2, value3)]}) else: msg = ("Invalid number of arguments should be 1 - 4, " "found: {}").format(size-1) raise template.TemplateSyntaxError(msg) if size > 2 and not all([key in keywords for key in kwargs]): msg = "Invalid keyword name, should be one of {}".format(keywords) raise template.TemplateSyntaxError(msg) return AutoDisplayNode(tag_name, relation, **kwargs)