Exemple #1
0
def deltas_id(environ, **kwargs):
    """
    API Call associated with specific delta
    Method: GET
    Output: application/json
    Examples: https://server-host/sitefe/v1/deltas/([-_A-Za-z0-9]+) # Will return info about specific delta
    """
    # METHOD DELETE!!!!! TODO
    if environ['REQUEST_METHOD'].upper() == 'DELETE':
        kwargs['http_respond'].ret_405('application/json',
                                       kwargs['start_response'],
                                       ('Location', '/'))
        print('DELETE Method is not supported yet. Return 405')
        return [
            getCustomOutMsg(errMsg="Method %s is not supported in %s" %
                            environ['REQUEST_METHOD'].upper(),
                            errCode=405)
        ]
    modTime = getModTime(kwargs['headers'])
    print('Delta Status query for %s' % kwargs['mReg'][0])
    delta = DELTABACKEND.getdelta(kwargs['mReg'][0], **kwargs)
    if not delta:
        kwargs['http_respond'].ret_204(
            'application/json', kwargs['start_response'],
            [('Last-Modified', httpdate(getUTCnow()))])
        print('Return empty list. There are no deltas on the system')
        return []
    if modTime > delta['updatedate']:
        print(
            'Delta with ID %s was not updated so far. Time request comparison requested'
            % kwargs['mReg'][0])
        kwargs['http_respond'].ret_304(
            'application/json', kwargs['start_response'],
            ('Last-Modified', httpdate(delta['updatedate'])))
        return []
    if kwargs['urlParams']['oldview']:
        kwargs['http_respond'].ret_200(
            'application/json', kwargs['start_response'],
            [('Last-Modified', httpdate(delta['updatedate']))])
        return [delta]
    current = {}
    current = {
        "id": delta['uid'],
        "lastModified": convertTSToDatetime(delta['updatedate']),
        "state": delta['state'],
        "href": "%s" % environ['SCRIPT_URI'],
        "modelId": delta['modelid']
    }
    if not kwargs['urlParams']['summary']:
        current['addition'] = encodebase64(delta['addition'],
                                           kwargs['urlParams']['encode'])
        current['reduction'] = encodebase64(delta['reduction'],
                                            kwargs['urlParams']['encode'])
    print('Returning delta %s information. Few details: ModelID: %s, State: %s, LastModified: %s' % \
          (current["id"], current["modelId"], current["state"], current["lastModified"]))
    kwargs['http_respond'].ret_200(
        'application/json', kwargs['start_response'],
        [('Last-Modified', httpdate(delta['updatedate']))])
    return [current]
Exemple #2
0
def models_id(environ, **kwargs):
    """
    API Call for getting specific model and associated deltas;
    Method: GET
    Output: application/json
    Examples: https://server-host/sitefe/v1/models/([-_A-Za-z0-9]+)/ # Returns list of all models;
    """
    modTime = getModTime(kwargs['headers'])
    modelID = kwargs['mReg'].groups()[0]
    outmodels = DELTABACKEND.getmodel(modelID, **kwargs)
    model = outmodels if isinstance(outmodels, dict) else outmodels[0]
    if modTime > model['insertdate']:
        print 'Model with ID %s was not updated so far. Time request comparison requested' % modelID
        kwargs['http_respond'].ret_304(
            'application/json', kwargs['start_response'],
            ('Last-Modified', httpdate(model['insertdate'])))
        return []
    current = {
        "id": model['uid'],
        "creationTime": convertTSToDatetime(model['insertdate']),
        "href": "%s/%s" % (environ['SCRIPT_URI'], model['uid'])
    }
    if not kwargs['urlParams']['summary']:
        current['model'] = encodebase64(
            DELTABACKEND.getmodel(model['uid'], content=True, **kwargs),
            kwargs['urlParams']['encode'])
    print 'Requested a specific model with id %s' % modelID
    kwargs['http_respond'].ret_200(
        'application/json', kwargs['start_response'],
        [('Last-Modified', httpdate(model['insertdate']))])
    return current
Exemple #3
0
def models(environ, **kwargs):
    """
    Returns a collection of available model resources within the Resource Manager
    Method: GET
    Output: application/json
    Examples: https://server-host/sitefe/v1/models/ # Returns list of all models;
    """
    # Get IF_MODIFIED_SINCE modification time in timestamp
    modTime = getModTime(kwargs['headers'])
    outmodels = DELTABACKEND.getmodel(**kwargs)
    if not outmodels:
        kwargs['http_respond'].ret_500('application/json',
                                       kwargs['start_response'], None)
        print 'LastModel does not exist in dictionary. First time run? See documentation'
        return getCustomOutMsg(errMsg="No models are available...",
                               errCode=500)
    outmodels = [outmodels] if isinstance(outmodels, dict) else outmodels
    outM = {"models": []}
    current = {
        "id": outmodels[0]['uid'],
        "creationTime": convertTSToDatetime(outmodels[0]['insertdate']),
        "href": "%s/%s" % (environ['SCRIPT_URI'], outmodels[0]['uid'])
    }
    print outmodels[0]['insertdate'], modTime, getUTCnow()
    if outmodels[0]['insertdate'] < modTime:
        print '%s and %s' % (outmodels[0]['insertdate'], modTime)
        kwargs['http_respond'].ret_304(
            'application/json', kwargs['start_response'],
            ('Last-Modified', httpdate(outmodels[0]['insertdate'])))
        return []
    kwargs['http_respond'].ret_200(
        'application/json', kwargs['start_response'],
        [('Last-Modified', httpdate(outmodels[0]['insertdate']))])
    if kwargs['urlParams']['oldview']:
        print 'Requested oldview model output. Return 200'
        return outmodels
    elif kwargs['urlParams']['current']:
        if not kwargs['urlParams']['summary']:
            current['model'] = encodebase64(
                DELTABACKEND.getmodel(outmodels[0]['uid'],
                                      content=True,
                                      **kwargs), kwargs['urlParams']['encode'])
        outM['models'].append(current)
        print 'Requested only current model. Return 200. Last Model %s' % outmodels[
            0]['uid']
        return [current]
    elif not kwargs['urlParams']['current']:
        for model in outmodels:
            tmpDict = {
                "id": model['uid'],
                "creationTime": convertTSToDatetime(model['insertdate']),
                "href": "%s/%s" % (environ['SCRIPT_URI'], model['uid'])
            }
            if not kwargs['urlParams']['summary']:
                tmpDict['model'] = encodebase64(
                    DELTABACKEND.getmodel(model['uid'], content=True,
                                          **kwargs),
                    kwargs['urlParams']['encode'])
            outM['models'].append(tmpDict)
        print 'Returning all models known to the system. Return 200'
        return outM['models']