def to_representation(self, value): # get guild_id guild_id = value[0] # Check if an emblem already exists for the current guild if os.path.isfile(os.path.join(BASE_DIR, self.GUILD_EMBLEM_PATH.format(id=guild_id))): return serializers.Hyperlink('{url}/{path}'.format(url=CONFIG['server']['conf']['server_domain'], path=self.GUILD_EMBLEM_PATH.format(id=guild_id)), None) else: # if not - create # get blob data [emblem_data] emblem_data = value[1] if emblem_data: # generate .bmp guild emblem based on blob in our db binary_string = binascii.unhexlify(emblem_data) binary_string = codecs.decode(binary_string, "zlib") # write new emblem into emblem's path with open(self.GUILD_EMBLEM_PATH.format(id=guild_id), 'wb') as f: f.write(binary_string) f.close() return serializers.Hyperlink('{url}/{path}'.format(url=CONFIG['server']['conf']['server_domain'], path=self.GUILD_EMBLEM_PATH.format( id=guild_id)), None) # No emblem found for this guild return None
def get_narrow_url(self, instance): """ Return a link suitable for narrowing on the current item. Since we don't have any means of getting the ``view name`` from here, we can only return relative paths. """ text = instance[0] request = self.context["request"] query_params = request.GET.copy() # Never keep the page query parameter in narrowing urls. # It will raise a NotFound exception when trying to paginate a narrowed queryset. page_query_param = self.get_paginate_by_param() if page_query_param in query_params: del query_params[page_query_param] selected_facets = set( query_params.pop(self.root.facet_query_params_text, [])) selected_facets.add("%(field)s_exact:%(text)s" % { "field": self.parent_field, "text": text }) query_params.setlist(self.root.facet_query_params_text, sorted(selected_facets)) path = "%(path)s?%(query)s" % { "path": request.path_info, "query": query_params.urlencode() } url = request.build_absolute_uri(path) return serializers.Hyperlink(url, "narrow-url")
def get_narrow_url(self, instance): """ Return a link suitable for narrowing on the current item. Since we don't have any means of getting the ``view name`` from here, we can only return relative paths. """ field = instance['field'] request = self.context['request'] query_params = request.GET.copy() # Never keep the page query parameter in narrowing urls. # It will raise a NotFound exception when trying to paginate a narrowed queryset. page_query_param = self.get_paginate_by_param() if page_query_param in query_params: del query_params[page_query_param] selected_facets = set(query_params.pop('selected_query_facets', [])) selected_facets.add(field) query_params.setlist('selected_query_facets', sorted(selected_facets)) path = '{path}?{query}'.format(path=request.path_info, query=query_params.urlencode()) url = request.build_absolute_uri(path) return serializers.Hyperlink(url, name='narrow-url')
def get_narrow_url(self, instance): """ Return a link suitable for narrowing on the current item. """ text = instance[0] request = self.context['request'] query_params = request.GET.copy() # Never keep the page query parameter in narrowing urls. # It will raise a NotFound exception when trying to paginate a narrowed queryset. page_query_param = self.get_paginate_by_param() if page_query_param and page_query_param in query_params: del query_params[page_query_param] selected_facets = set( query_params.pop(self.root.facet_query_params_text, [])) selected_facets.add('%(field)s_exact:%(text)s' % { 'field': self.parent_field, 'text': text }) query_params.setlist(self.root.facet_query_params_text, sorted(selected_facets)) path = '%(path)s?%(query)s' % { 'path': request.path_info, 'query': query_params.urlencode() } url = request.build_absolute_uri(path) return serializers.Hyperlink(url, 'narrow-url')
def to_representation(self, value): # get guild_id guild_id = value[0] # Check if an emblem already exists for the current guild if os.path.isfile( os.path.join(BASE_DIR, self.GUILD_EMBLEM_PATH.format(id=guild_id))): return serializers.Hyperlink( '{url}/{path}'.format( url=CONFIG['server']['conf']['server_domain'], path=self.GUILD_EMBLEM_PATH.format(id=guild_id)), None) else: # if not - create # get blob data [emblem_data] emblem_data = value[1] if emblem_data: # generate guild emblem string based on blob in our db binary_string = binascii.unhexlify(emblem_data) binary_string = codecs.decode(binary_string, "zlib") # make emblem transparent img = Image.open(BytesIO(binary_string)) img = img.convert("RGBA") items = img.getdata() png_transparent = [] for item in items: if item[0] == 255 and item[1] == 0 and item[2] == 255: png_transparent.append((255, 255, 255, 0)) else: png_transparent.append(item) img.putdata(png_transparent) # save new emblem into emblem's path img.save(self.GUILD_EMBLEM_PATH.format(id=guild_id), "PNG") return serializers.Hyperlink( '{url}/{path}'.format( url=CONFIG['server']['conf']['server_domain'], path=self.GUILD_EMBLEM_PATH.format(id=guild_id)), None) # No emblem found for this guild return None
def to_representation(self, value): map_name, x, y = value[0], value[1], value[2] # check if map image already exists if not os.path.isfile( os.path.join( BASE_DIR, self.MINIMAP_PATH.format(map_name=map_name, x=x, y=y))): gat = Gat(self.GAT_PATH.format(map_name=map_name), scale=2) gat(x=x, y=y, map_name=map_name, path=self.MINIMAP_PATH) # return path to it return serializers.Hyperlink( '{url}/{path}'.format( url=CONFIG['server']['conf']['server_domain'], path=self.MINIMAP_PATH.format(map_name=map_name, x=x, y=y)), None)
def to_representation(self, value): request = self.context.get('request', None) format = self.context.get('format', None) assert request is not None, ( "`%s` requires the request in the serializer" " context. Add `context={'request': request}` when instantiating " "the serializer." % self.__class__.__name__) # By default use whatever format is given for the current context # unless the target is a different type to the source. # # Eg. Consider a HyperlinkedIdentityField pointing from a json # representation to an html property of that representation... # # '/snippets/1/' should link to '/snippets/1/highlight/' # ...but... # '/snippets/1/.json' should link to '/snippets/1/highlight/.html' if format and self.format and self.format != format: format = self.format # Return the hyperlink, or error if incorrectly configured. try: url = self.get_url(value, request, format) except NoReverseMatch: msg = ('Could not resolve URL for hyperlinked identity using ' 'get_complete_url method. ') if value in ('', None): value_string = {'': 'the empty string', None: 'None'}[value] msg += ( " WARNING: The value of the field on the model instance " "was %s, which may be why it didn't match any " "entries in your URL conf." % value_string) raise ImproperlyConfigured(msg) if url is None: return None return serializers.Hyperlink(url, six.text_type(value))
def setup(self): self.default_hyperlink = serializers.Hyperlink('http://example.com', 'test')