Exemplo n.º 1
0
def server_validation(stmt_set, auth, payload_sha2s):
    auth_validated = False    
    if type(stmt_set) is list:
        for stmt in stmt_set:
            server_validation(stmt, auth, payload_sha2s)
    else:
        if 'id' in stmt_set:
            statement_id = stmt_set['id']
            if check_for_existing_statementId(statement_id):
                err_msg = "A statement with ID %s already exists" % statement_id
                raise ParamConflict(err_msg)

        server_validate_statement_object(stmt_set['object'], auth)

        if stmt_set['verb']['id'] == 'http://adlnet.gov/expapi/verbs/voided':
            validate_void_statement(stmt_set['object']['id'])

        if not 'objectType' in stmt_set['object'] or stmt_set['object']['objectType'] == 'Activity':
            get_act_def_data(stmt_set['object'])
            
            try:
                validator = StatementValidator.StatementValidator(None)
                validator.validate_activity(stmt_set['object'])
            except Exception, e:
                raise BadRequest(e.message)
            except ParamError, e:
                raise ParamError(e.message)
Exemplo n.º 2
0
def get_activity_metadata(act_id):
    act_url_data = {}
    # See if id resolves
    try:
        req = urllib2.Request(act_id)
        req.add_header('Accept', 'application/json, */*')
        act_resp = urllib2.urlopen(
            req, timeout=settings.ACTIVITY_ID_RESOLVE_TIMEOUT)
    except Exception:
        # Doesn't resolve-hopefully data is in payload
        pass
    else:
        # If it resolves then try parsing JSON from it
        try:
            act_url_data = json.loads(act_resp.read())
        except Exception:
            # Resolves but no data to retrieve - this is OK
            pass

        # If there was data from the URL
        if act_url_data:
            valid_url_data = True
            # Have to validate new data given from URL
            try:
                fake_activity = {"id": act_id, "definition": act_url_data}
                validator = SV.StatementValidator()
                validator.validate_activity(fake_activity)
            except Exception, e:
                valid_url_data = False
                logger.exception(e.message)

            if valid_url_data:
                update_activity_definition(fake_activity)
Exemplo n.º 3
0
def statements_post(r_dict):
    payload_sha2s = r_dict.get('payload_sha2s', None)

    try:
        validator = StatementValidator.StatementValidator(r_dict['body'])
        msg = validator.validate()
    except Exception, e:
        raise BadRequest(e.message)
Exemplo n.º 4
0
def statements_post(req_dict):
    if req_dict['params'].keys():
        raise ParamError("The post statements request contained unexpected parameters: %s" % ", ".join(req_dict['params'].keys()))

    payload_sha2s = req_dict.get('payload_sha2s', None)

    if isinstance(req_dict['body'], basestring):
        req_dict['body'] = convert_to_dict(req_dict['body'])

    try:
        validator = StatementValidator.StatementValidator(req_dict['body'])
        msg = validator.validate()
    except Exception, e:
        raise BadRequest(e.message)
Exemplo n.º 5
0
def stmt_validator(request):
    if request.method == 'GET':
        form = forms.ValidatorForm()
        return render_to_response('validator.html', {"form": form}, context_instance=RequestContext(request))
    elif request.method == 'POST':
        form = forms.ValidatorForm(request.POST)
        if form.is_valid():
            # Initialize validator (validates incoming data structure)
            try:
                validator = StatementValidator.StatementValidator(form.cleaned_data['jsondata'])
            except SyntaxError, se:
                return render_to_response('validator.html', {"form": form, "error_message": "Statement is not a properly formatted dictionary"},
                context_instance=RequestContext(request))
            except ValueError, ve:
                return render_to_response('validator.html', {"form": form, "error_message": "Statement is not a properly formatted dictionary"},
                context_instance=RequestContext(request))                
            except Exception, e:
                return render_to_response('validator.html', {"form": form, "error_message": e.message},
                context_instance=RequestContext(request))
Exemplo n.º 6
0
def statements_put(r_dict):
    # Must have statementId param-if not raise paramerror
    try:
        statement_id = r_dict['params']['statementId']
    except KeyError:
        err_msg = "Error -- statements - method = %s, but statementId paramater is missing" % r_dict[
            'method']
        raise ParamError(err_msg)

    # If statement with that ID already exists-raise conflict error
    if check_for_existing_statementId(statement_id):
        err_msg = "A statement with ID %s already exists" % statement_id
        raise ParamConflict(err_msg)

    # If there are no other params-raise param error since nothing else is supplied
    if not check_for_no_other_params_supplied(r_dict['body']):
        err_msg = "No other params are supplied with statementId."
        raise ParamError(err_msg)

    try:
        validator = StatementValidator.StatementValidator(r_dict['body'])
        msg = validator.validate()
    except Exception, e:
        raise BadRequest(e.message)
Exemplo n.º 7
0
    # If statement with that ID already exists-raise conflict error
    if check_for_existing_statementId(statement_id):
        raise ParamConflict("A statement with ID %s already exists" % statement_id)
    
    # Set id inside of statement with param id
    if not statement_body_id:
        req_dict['body']['id'] = statement_id

    # If there are no other params-raise param error since nothing else is supplied
    if not check_for_no_other_params_supplied(req_dict['body']):
        raise ParamError("No other params are supplied with statementId.")

    # Validate statement in body
    try:
        validator = StatementValidator.StatementValidator(req_dict['body'])
        msg = validator.validate()
    except Exception, e:
        raise BadRequest(e.message)
    except ParamError, e:
        raise ParamError(e.message)
    server_validation(req_dict['body'], req_dict.get('auth', None), req_dict.get('payload_sha2s', None))
    return req_dict

def validate_attachments(attachment_data, payload_sha2s):
    # For each attachment that is in the actual statement
    for attachment in attachment_data:
        # If the attachment data has a sha2 field, must validate it against the payload data
        if 'sha2' in attachment:
            sha2 = attachment['sha2']
            # Check if the sha2 field is a key in the payload dict