Пример #1
0
def _validateFormat(request):
    '''
        Returns the mimetype of the required format as mapped by rdflib
        return: String - an allowed rdflib mimetype 
    '''
    req_format = None    
    if isGET(request):
        req_format = FORMAT_MAP.get(request.GET.get('format', None))        
    if isPOST(request):
        req_format = request.environ.get('CONTENT_TYPE', None)        
    
    if req_format:
        if req_format not in FORMAT_MAP.values():
            raise SerializeError("Cannot generate the required format %s " % req_format)
    return req_format
Пример #2
0
def validateMimeFormat(request):
    req_format = http_accept(request)
    if req_format:
        for k,v in FORMAT_MAP.iteritems():
            if v in req_format:
                return k
    return None 
Пример #3
0
def _validateMimeFormat(request):
    req_format = request.META.get('HTTP_ACCEPT', None)
    if req_format:
        for k,v in FORMAT_MAP.iteritems():
            if req_format == v:
                return k
    return None
Пример #4
0
 def test_GET_default_2(self): 
     self.test_insert_anotation()
     for accept in FORMAT_MAP.values():
         request = self.factory.get('/endpoint', data = {}, 
                                HTTP_ACCEPT = accept)
         response = endpoint(request)
         self.assert_(response.status_code in [200, 204], "HTTPResponse has status: %s" 
                      % response.status_code)
         self.assert_('http://data.gov.uk//dataset/index-of-multiple-deprivation' 
                      in response.content, "Cannot serialize %s" % accept)        
Пример #5
0
def process_data(request, resource_id):
    if 'text/html' in http_accept(request):
        return process_resource(request, resource_id = None)
    
    req_format = validateMimeFormat(request)
    if req_format is None:
        return process_resource(request, resource_id)
            
    tmp_g = find_resource_by_id(resource_id)           
    return HttpResponse(tmp_g.serialize(format = req_format), 
                            mimetype = FORMAT_MAP.get(req_format))  
Пример #6
0
def processHEAD(request, return_content = False):
    '''
        Returns an httplib.HTTPRequest
    '''
    graph = get_graph_from_request(request)    
    accept = http_accept(request)
    
    if accept not in FORMAT_MAP.values():
        return HttpResponse(status = 406)        
    
    g = None
    if graph is None:
        g = ConjunctiveGraph(store=CharmeMiddleware.get_store())        
    else:
        g = generate_graph(CharmeMiddleware.get_store(), URIRef(graph))
    
    content = g.serialize(format = rdf_format_from_mime(accept))
    
    if return_content:
        return HttpResponse(content = content) 
    return HttpResponse()  
Пример #7
0
def insert(request):
    '''
        Inserts in the triplestore a new annotation under the "ANNO_SUBMITTED" graph
    '''
    try:
        req_format = _validateFormat(request)
        
    except SerializeError as e:
        messages.add_message(request, messages.ERROR, e)
        return mm_render_to_response_error(request, '400.html', 400)
    
    if isPOST(request) or isOPTIONS(request):
        triples = request.body
        tmp_g = insert_rdf(triples, req_format, graph=ANNO_SUBMITTED) 
        ret_format = validateMimeFormat(request)
        if ret_format is None:
            ret_format = req_format
        return HttpResponse(__serialize(tmp_g, req_format = ret_format), content_type=FORMAT_MAP.get(ret_format))