本文整理汇总了Python中StatementValidator.StatementValidator.validate_iri方法的典型用法代码示例。如果您正苦于以下问题:Python StatementValidator.validate_iri方法的具体用法?Python StatementValidator.validate_iri怎么用?Python StatementValidator.validate_iri使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StatementValidator.StatementValidator
的用法示例。
在下文中一共展示了StatementValidator.validate_iri方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: activity_state_delete
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_state_delete(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId", "agent", "stateId", "registration"])
if rogueparams:
raise ParamError("The delete activity state request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity state")
else:
err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict['method']
raise ParamError(err_msg)
if 'registration' in req_dict['params']:
validator.validate_uuid(req_dict['params']['registration'], "registration param for activity state")
if 'agent' in req_dict['params']:
try:
agent = json.loads(req_dict['params']['agent'])
req_dict['params']['agent'] = agent
except Exception:
raise ParamError("agent param for activity state is not valid")
validator.validate_agent(agent, "Activity state agent param")
else:
err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict['method']
raise ParamError(err_msg)
# Extra validation if oauth
if req_dict['auth']['type'] == 'oauth':
validate_oauth_state_or_profile_agent(req_dict, "state")
return req_dict
示例2: activity_profile_put
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_profile_put(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId", "profileId"])
if rogueparams:
raise ParamError(
"The put activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(
req_dict['params']['activityId'], "activityId param for activity profile")
else:
err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'profileId' not in req_dict['params']:
err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'body' not in req_dict:
err_msg = "Could not find the profile document"
raise ParamError(err_msg)
# Set profile - req_parse converts all request bodies to dict, act profile needs it as string and need to replace single quotes with double quotes
# b/c of quotation issue when using javascript with activity profile
req_dict['profile'] = req_dict.pop('raw_body', req_dict.pop('body', None))
return req_dict
示例3: activities_get
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activities_get(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId"])
if rogueparams:
raise ParamError(
"The get activities request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
try:
activity_id = req_dict['params']['activityId']
except KeyError:
err_msg = "Error -- activities - method = %s, but activityId parameter is missing" % req_dict[
'method']
raise ParamError(err_msg)
else:
validator.validate_iri(
activity_id, "activityId param")
# Try to retrieve activity, if DNE then return empty else return activity
# info
try:
Activity.objects.get(activity_id=activity_id, authority__isnull=False)
except Activity.DoesNotExist:
err_msg = "No activity found with ID %s" % activity_id
raise IDNotFoundError(err_msg)
return req_dict
示例4: activity_profile_post
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_profile_post(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId", "profileId"])
if rogueparams:
raise ParamError(
"The post activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(
req_dict['params']['activityId'], "activityId param for activity profile")
else:
err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'profileId' not in req_dict['params']:
err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'body' not in req_dict:
err_msg = "Could not find the profile document"
raise ParamError(err_msg)
# Check json for incoming POSTed document
if "application/json" not in req_dict['headers']['CONTENT_TYPE']:
raise ParamError(
"Activity profile document to be posted does not have a Content-Type of 'application/json'")
# expected to be json
try:
raw_profile = req_dict.pop('raw_body', req_dict.pop('body', None))
convert_to_datatype(raw_profile)
except Exception:
raise ParamError("Activity profile document is not valid JSON")
else:
req_dict['profile'] = raw_profile
# Check the content type if the document already exists
exists = False
try:
p = ActivityProfile.objects.get(activity_id=req_dict['params']['activityId'],
profile_id=req_dict['params']['profileId'])
exists = True
except ActivityProfile.DoesNotExist:
pass
# Since document to be POSTed has to be json, so does the existing document
if exists and str(p.content_type) != "application/json":
raise ParamError("Activity profile already exists but is not JSON, cannot update it with new JSON document")
return req_dict
示例5: activity_state_put
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_state_put(req_dict):
rogueparams = set(req_dict['params']) - \
set(["activityId", "agent", "stateId", "registration"])
if rogueparams:
raise ParamError(
"The put activity state request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(
req_dict['params']['activityId'], "activityId param for activity state")
else:
err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'stateId' not in req_dict['params']:
err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'registration' in req_dict['params']:
validator.validate_uuid(
req_dict['params']['registration'], "registration param for activity state")
if 'agent' in req_dict['params']:
try:
agent = convert_to_datatype(req_dict['params']['agent'])
req_dict['params']['agent'] = agent
except Exception:
raise ParamError("agent param %s is not valid" % \
req_dict['params']['agent'])
validator.validate_agent(agent, "Agent param")
else:
err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
# Must have body included for state
if 'body' not in req_dict:
err_msg = "Could not find the state"
raise ParamError(err_msg)
# Extra validation if oauth
if req_dict['auth']['type'] == 'oauth':
validate_oauth_for_documents(req_dict, "state")
# Set state
req_dict['state'] = req_dict.pop('raw_body', req_dict.pop('body', None))
return req_dict
示例6: activity_profile_delete
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_profile_delete(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId", "profileId"])
if rogueparams:
raise ParamError("The delete activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity profile")
else:
err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict['method']
raise ParamError(err_msg)
if not 'profileId' in req_dict['params']:
err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict['method']
raise ParamError(err_msg)
return req_dict
示例7: activity_state_get
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_state_get(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId",
"agent", "stateId", "registration", "since"])
if rogueparams:
raise ParamError(
"The get activity state request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(
req_dict['params']['activityId'], "activityId param for activity state")
else:
err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'registration' in req_dict['params']:
validator.validate_uuid(
req_dict['params']['registration'], "registration param for activity state")
if 'agent' in req_dict['params']:
try:
agent = convert_to_datatype(req_dict['params']['agent'])
req_dict['params']['agent'] = agent
except Exception:
raise ParamError("agent param %s is not valid" % \
req_dict['params']['agent'])
validator.validate_agent(agent, "Agent param")
else:
err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'since' in req_dict['params']:
try:
parse_datetime(req_dict['params']['since'])
except (Exception, ISO8601Error):
raise ParamError(
"Since parameter was not a valid ISO8601 timestamp")
# Extra validation if oauth
if req_dict['auth']['type'] == 'oauth':
validate_oauth_for_documents(req_dict, "state")
return req_dict
示例8: activity_profile_get
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_profile_get(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId", "profileId", "since"])
if rogueparams:
raise ParamError("The get activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity profile")
else:
err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict['method']
raise ParamError(err_msg)
if 'since' in req_dict['params']:
try:
parse_datetime(req_dict['params']['since'])
except (Exception, ISO8601Error):
raise ParamError("Since parameter was not a valid ISO8601 timestamp")
return req_dict
示例9: activity_profile_post
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_profile_post(req_dict):
rogueparams = set(req_dict["params"]) - set(["activityId", "profileId"])
if rogueparams:
raise ParamError(
"The post activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams)
)
validator = StatementValidator()
if "activityId" in req_dict["params"]:
validator.validate_iri(req_dict["params"]["activityId"], "activityId param for activity profile")
else:
err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict["method"]
raise ParamError(err_msg)
if not "profileId" in req_dict["params"]:
err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict["method"]
raise ParamError(err_msg)
if "body" not in req_dict:
err_msg = "Could not find the profile document"
raise ParamError(err_msg)
# Check the content type if the document already exists
exists = False
try:
p = ActivityProfile.objects.get(
activityId=req_dict["params"]["activityId"], profileId=req_dict["params"]["profileId"]
)
exists = True
except ActivityProfile.DoesNotExist:
pass
if exists:
if str(p.content_type) != "application/json" or (
"application/json" not in req_dict["headers"]["CONTENT_TYPE"]
or req_dict["headers"]["CONTENT_TYPE"] != "application/json"
):
raise ParamError(
"Neither original document or document to be posted has a Content-Type of 'application/json'"
)
req_dict["profile"] = req_dict.pop("raw_body", req_dict.pop("body", None))
return req_dict
示例10: activity_state_put
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_state_put(req_dict):
rogueparams = set(req_dict["params"]) - set(["activityId", "agent", "stateId", "registration"])
if rogueparams:
raise ParamError("The put activity state request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if "activityId" in req_dict["params"]:
validator.validate_iri(req_dict["params"]["activityId"], "activityId param for activity state")
else:
err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
if not "stateId" in req_dict["params"]:
err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
if "registration" in req_dict["params"]:
validator.validate_uuid(req_dict["params"]["registration"], "registration param for activity state")
if "agent" in req_dict["params"]:
try:
agent = json.loads(req_dict["params"]["agent"])
req_dict["params"]["agent"] = agent
except Exception:
raise ParamError("agent param for activity state is not valid")
validator.validate_agent(agent, "Activity state agent param")
else:
err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
# Must have body included for state
if "body" not in req_dict:
err_msg = "Could not find the state"
raise ParamError(err_msg)
# Extra validation if oauth
if req_dict["auth"]["type"] == "oauth":
validate_oauth_state_or_profile_agent(req_dict, "state")
# Set state
req_dict["state"] = req_dict.pop("raw_body", req_dict.pop("body", None))
return req_dict
示例11: activity_profile_post
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_profile_post(req_dict):
rogueparams = set(req_dict['params']) - set(["activityId", "profileId"])
if rogueparams:
raise ParamError("The post activity profile request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(req_dict['params']['activityId'], "activityId param for activity profile")
else:
err_msg = "Error -- activity_profile - method = %s, but activityId parameter missing.." % req_dict['method']
raise ParamError(err_msg)
if not 'profileId' in req_dict['params']:
err_msg = "Error -- activity_profile - method = %s, but profileId parameter missing.." % req_dict['method']
raise ParamError(err_msg)
if 'body' not in req_dict:
err_msg = "Could not find the profile document"
raise ParamError(err_msg)
req_dict['profile'] = req_dict.pop('raw_body', req_dict.pop('body', None))
return req_dict
示例12: activity_state_get
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_state_get(req_dict):
rogueparams = set(req_dict["params"]) - set(["activityId", "agent", "stateId", "registration", "since"])
if rogueparams:
raise ParamError("The get activity state request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if "activityId" in req_dict["params"]:
validator.validate_iri(req_dict["params"]["activityId"], "activityId param for activity state")
else:
err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
if "registration" in req_dict["params"]:
validator.validate_uuid(req_dict["params"]["registration"], "registration param for activity state")
if "agent" in req_dict["params"]:
try:
agent = json.loads(req_dict["params"]["agent"])
req_dict["params"]["agent"] = agent
except Exception:
raise ParamError("agent param for activity state is not valid")
validator.validate_agent(agent, "Activity state agent param")
else:
err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
if "since" in req_dict["params"]:
try:
parse_datetime(req_dict["params"]["since"])
except (Exception, ISO8601Error):
raise ParamError("Since parameter was not a valid ISO8601 timestamp")
# Extra validation if oauth
if req_dict["auth"]["type"] == "oauth":
validate_oauth_state_or_profile_agent(req_dict, "state")
return req_dict
示例13: activity_state_post
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_state_post(req_dict):
rogueparams = set(req_dict['params']) - \
set(["activityId", "agent", "stateId", "registration"])
if rogueparams:
raise ParamError(
"The post activity state request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if 'activityId' in req_dict['params']:
validator.validate_iri(
req_dict['params']['activityId'], "activityId param for activity state")
else:
err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'stateId' not in req_dict['params']:
err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
if 'registration' in req_dict['params']:
validator.validate_uuid(
req_dict['params']['registration'], "registration param for activity state")
if 'agent' in req_dict['params']:
try:
agent = convert_to_datatype(req_dict['params']['agent'])
req_dict['params']['agent'] = agent
except Exception:
raise ParamError("agent param %s is not valid" % \
req_dict['params']['agent'])
validator.validate_agent(agent, "Agent param")
else:
err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict[
'method']
raise ParamError(err_msg)
# Must have body included for state
if 'body' not in req_dict:
err_msg = "Could not find the state"
raise ParamError(err_msg)
# Extra validation if oauth
if req_dict['auth']['type'] == 'oauth':
validate_oauth_for_documents(req_dict, "state")
# Check json for incoming POSTed document
if "application/json" not in req_dict['headers']['CONTENT_TYPE']:
raise ParamError(
"Activity state document to be posted does not have a Content-Type of 'application/json'")
# expected to be json
try:
raw_state = req_dict.pop('raw_body', req_dict.pop('body', None))
convert_to_datatype(raw_state)
except Exception:
raise ParamError("Activity state document is not valid JSON")
else:
req_dict['state'] = raw_state
# Check the content type if the document already exists
registration = req_dict['params'].get('registration', None)
agent = req_dict['params']['agent']
a = Agent.objects.retrieve_or_create(**agent)[0]
exists = False
if registration:
try:
s = ActivityState.objects.get(state_id=req_dict['params']['stateId'], agent=a,
activity_id=req_dict['params']['activityId'], registration_id=req_dict['params']['registration'])
exists = True
except ActivityState.DoesNotExist:
pass
else:
try:
s = ActivityState.objects.get(state_id=req_dict['params']['stateId'], agent=a,
activity_id=req_dict['params']['activityId'])
exists = True
except ActivityState.DoesNotExist:
pass
if exists and str(s.content_type) != "application/json":
raise ParamError("Activity state already exists but is not JSON, cannot update it with new JSON document")
return req_dict
示例14: statements_get
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
#.........这里部分代码省略.........
validator = StatementValidator()
if 'agent' in req_dict['params']:
try:
agent = convert_to_datatype(req_dict['params']['agent'])
req_dict['params']['agent'] = agent
except Exception:
raise ParamError("agent param %s is not valid" % \
req_dict['params']['agent'])
validator.validate_agent(agent, "Agent param")
formats = ['exact', 'canonical', 'ids']
if 'format' in req_dict['params']:
if req_dict['params']['format'] not in formats:
raise ParamError("The format filter value (%s) was not one of the known values: %s" % (
req_dict['params']['format'], ','.join(formats)))
else:
req_dict['params']['format'] = 'exact'
# StatementId could be for voided statement as well
if 'statementId' in req_dict['params'] or 'voidedStatementId' in req_dict['params']:
req_dict['statementId'] = validate_statementId(req_dict)
if 'since' in req_dict['params']:
try:
parse_datetime(req_dict['params']['since'])
except (Exception, ISO8601Error):
raise ParamError(
"since parameter was not a valid ISO8601 timestamp")
if 'until' in req_dict['params']:
try:
parse_datetime(req_dict['params']['until'])
except (Exception, ISO8601Error):
raise ParamError(
"until parameter was not a valid ISO8601 timestamp")
if 'ascending' in req_dict['params']:
if req_dict['params']['ascending'].lower() == 'true':
req_dict['params']['ascending'] = True
elif req_dict['params']['ascending'].lower() == 'false':
req_dict['params']['ascending'] = False
else:
raise ParamError(
"ascending parameter was not a boolean value")
else:
req_dict['params']['ascending'] = False
if 'related_agents' in req_dict['params']:
if req_dict['params']['related_agents'].lower() == 'true':
req_dict['params']['related_agents'] = True
elif req_dict['params']['related_agents'].lower() == 'false':
req_dict['params']['related_agents'] = False
else:
raise ParamError(
"related_agents parameter was not a boolean value")
else:
req_dict['params']['related_agents'] = False
if 'related_activities' in req_dict['params']:
if req_dict['params']['related_activities'].lower() == 'true':
req_dict['params']['related_activities'] = True
elif req_dict['params']['related_activities'].lower() == 'false':
req_dict['params']['related_activities'] = False
else:
raise ParamError(
"related_activities parameter was not a boolean value")
else:
req_dict['params']['related_activities'] = False
if 'attachments' in req_dict['params']:
if req_dict['params']['attachments'].lower() == 'true':
req_dict['params']['attachments'] = True
elif req_dict['params']['attachments'].lower() == 'false':
req_dict['params']['attachments'] = False
else:
raise ParamError(
"attachments parameter was not a boolean value")
else:
req_dict['params']['attachments'] = False
if 'limit' in req_dict['params']:
try:
req_dict['params']['limit'] = int(req_dict['params']['limit'])
except Exception:
raise ParamError(
"limit parameter was not a non-negative integer")
else:
if req_dict['params']['limit'] < 0:
raise ParamError(
"limit parameter was not a non-negative integer")
else:
req_dict['params']['limit'] = 0
if 'registration' in req_dict['params']:
validator.validate_uuid(req_dict['params']['registration'], "Registration param")
if 'verb' in req_dict['params']:
validator.validate_iri(
req_dict['params']['verb'], "verb param")
return req_dict
示例15: activity_state_post
# 需要导入模块: from StatementValidator import StatementValidator [as 别名]
# 或者: from StatementValidator.StatementValidator import validate_iri [as 别名]
def activity_state_post(req_dict):
rogueparams = set(req_dict["params"]) - set(["activityId", "agent", "stateId", "registration"])
if rogueparams:
raise ParamError("The post activity state request contained unexpected parameters: %s" % ", ".join(rogueparams))
validator = StatementValidator()
if "activityId" in req_dict["params"]:
validator.validate_iri(req_dict["params"]["activityId"], "activityId param for activity state")
else:
err_msg = "Error -- activity_state - method = %s, but activityId parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
if not "stateId" in req_dict["params"]:
err_msg = "Error -- activity_state - method = %s, but stateId parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
if "registration" in req_dict["params"]:
validator.validate_uuid(req_dict["params"]["registration"], "registration param for activity state")
if "agent" in req_dict["params"]:
try:
agent = json.loads(req_dict["params"]["agent"])
req_dict["params"]["agent"] = agent
except Exception:
raise ParamError("agent param for activity state is not valid")
validator.validate_agent(agent, "Activity state agent param")
else:
err_msg = "Error -- activity_state - method = %s, but agent parameter is missing.." % req_dict["method"]
raise ParamError(err_msg)
# Must have body included for state
if "body" not in req_dict:
err_msg = "Could not find the state"
raise ParamError(err_msg)
# Extra validation if oauth
if req_dict["auth"]["type"] == "oauth":
validate_oauth_state_or_profile_agent(req_dict, "state")
# Check the content type if the document already exists
registration = req_dict["params"].get("registration", None)
agent = req_dict["params"]["agent"]
a = Agent.objects.retrieve_or_create(**agent)[0]
exists = False
if registration:
try:
s = ActivityState.objects.get(
state_id=req_dict["params"]["stateId"],
agent=a,
activity_id=req_dict["params"]["activityId"],
registration_id=req_dict["params"]["registration"],
)
exists = True
except ActivityState.DoesNotExist:
pass
else:
try:
s = ActivityState.objects.get(
state_id=req_dict["params"]["stateId"], agent=a, activity_id=req_dict["params"]["activityId"]
)
exists = True
except ActivityState.DoesNotExist:
pass
if exists:
if str(s.content_type) != "application/json" or (
"application/json" not in req_dict["headers"]["CONTENT_TYPE"]
or req_dict["headers"]["CONTENT_TYPE"] != "application/json"
):
raise ParamError(
"Neither original document or document to be posted has a Content-Type of 'application/json'"
)
# Set state
req_dict["state"] = req_dict.pop("raw_body", req_dict.pop("body", None))
return req_dict