def post_state(self, request_dict): registration = request_dict['params'].get('registration', None) if registration: s, created = ActivityState.objects.get_or_create( state_id=request_dict['params']['stateId'], agent=self.Agent, activity_id=request_dict['params']['activityId'], registration_id=request_dict['params']['registration']) else: s, created = ActivityState.objects.get_or_create( state_id=request_dict['params']['stateId'], agent=self.Agent, activity_id=request_dict['params']['activityId']) if "application/json" not in request_dict['headers']['CONTENT_TYPE']: try: post_state = ContentFile(request_dict['state'].read()) except: try: post_state = ContentFile(request_dict['state']) except: post_state = ContentFile(str(request_dict['state'])) self.save_non_json_state(s, post_state, request_dict) else: post_state = request_dict['state'] # If incoming state is application/json and if a state didn't # already exist with the same agent, stateId, actId, and/or # registration if created: s.json_state = post_state s.content_type = request_dict['headers']['CONTENT_TYPE'] s.etag = etag.create_tag(post_state) # If incoming state is application/json and if a state already # existed with the same agent, stateId, actId, and/or registration else: orig_state = json.loads(s.json_state) post_state = json.loads(post_state) if not isinstance(post_state, dict): raise ParamError( "The document was not able to be parsed into a JSON object." ) else: merged = json.dumps( dict(orig_state.items() + post_state.items())) s.json_state = merged s.etag = etag.create_tag(merged) # Set updated if 'updated' in request_dict['headers'] and request_dict[ 'headers']['updated']: s.updated = request_dict['headers']['updated'] else: s.updated = datetime.datetime.utcnow().replace(tzinfo=utc) s.save()
def post_state(self, request_dict): registration = request_dict['params'].get('registration', None) if registration: s, created = ActivityState.objects.get_or_create(state_id=request_dict['params']['stateId'], agent=self.Agent, activity_id=request_dict['params']['activityId'], registration_id=request_dict['params']['registration']) else: s, created = ActivityState.objects.get_or_create(state_id=request_dict['params']['stateId'], agent=self.Agent, activity_id=request_dict['params']['activityId']) if "application/json" not in request_dict['headers']['CONTENT_TYPE']: try: post_state = ContentFile(request_dict['state'].read()) except: try: post_state = ContentFile(request_dict['state']) except: post_state = ContentFile(str(request_dict['state'])) self.save_non_json_state(s, post_state, request_dict) else: post_state = request_dict['state'] # If incoming state is application/json and if a state didn't # already exist with the same agent, stateId, actId, and/or # registration if created: s.json_state = post_state s.content_type = request_dict['headers']['CONTENT_TYPE'] s.etag = etag.create_tag(post_state) # If incoming state is application/json and if a state already # existed with the same agent, stateId, actId, and/or registration else: orig_state = json.loads(s.json_state) post_state = json.loads(post_state) if not isinstance(post_state, dict): raise ParamError( "The document was not able to be parsed into a JSON object.") else: merged = json.dumps( dict(orig_state.items() + post_state.items())) s.json_state = merged s.etag = etag.create_tag(merged) # Set updated if 'updated' in request_dict['headers'] and request_dict['headers']['updated']: s.updated = request_dict['headers']['updated'] else: s.updated = datetime.datetime.utcnow().replace(tzinfo=utc) s.save()
def post(self): agent = self.__get_agent() if self.registration: p,created = ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id,registration_id=self.registration) else: p,created = ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id) if "application/json" not in self.content_type: try: post_state = ContentFile(self.state.read()) except: try: post_state = ContentFile(self.state) except: post_state = ContentFile(str(self.state)) # Doesn't matter if it's created or not, content_type and state get overridden with new state p.content_type = self.content_type if self.updated: p.updated = self.updated else: p.updated = datetime.datetime.utcnow().replace(tzinfo=utc) p.save() fn = "%s_%s_%s" % (p.agent_id,p.activity_id, self.req_dict.get('filename', p.id)) p.state.save(fn, post_state) else: post_state = self.state if created: p.json_state = post_state p.content_type = self.content_type p.etag = etag.create_tag(post_state) if self.updated: p.updated = self.updated else: orig_state = json.loads(p.json_state) post_state = json.loads(post_state) if not isinstance(post_state, dict): raise ParamError("The document was not able to be parsed into a JSON object.") else: merged = json.dumps(dict(orig_state.items() + post_state.items())) p.json_state = merged p.etag = etag.create_tag(merged) p.updated = datetime.datetime.utcnow().replace(tzinfo=utc) p.save()
def post_profile(self, request_dict): # get/create profile p, created = ActivityProfile.objects.get_or_create( activity_id=request_dict['params']['activityId'], profile_id=request_dict['params']['profileId']) if "application/json" not in request_dict['headers']['CONTENT_TYPE']: try: post_profile = ContentFile(request_dict['profile'].read()) except: try: post_profile = ContentFile(request_dict['profile']) except: post_profile = ContentFile(str(request_dict['profile'])) self.save_non_json_profile(p, created, post_profile, request_dict) else: post_profile = request_dict['profile'] # If incoming profile is application/json and if a profile didn't # already exist with the same activityId and profileId if created: p.json_profile = post_profile p.content_type = request_dict['headers']['CONTENT_TYPE'] p.etag = etag.create_tag(post_profile) # If incoming profile is application/json and if a profile already # existed with the same activityId and profileId else: orig_prof = json.loads(p.json_profile) post_profile = json.loads(request_dict['profile']) if not isinstance(post_profile, dict): raise ParamError( "The document was not able to be parsed into a JSON object." ) else: # json.dumps changes the format of the string rep of the # dict merged = json.dumps( dict(orig_prof.items() + post_profile.items())) p.json_profile = merged p.etag = etag.create_tag(merged) # Set updated if 'updated' in request_dict['headers'] and request_dict[ 'headers']['updated']: p.updated = request_dict['headers']['updated'] else: p.updated = datetime.datetime.utcnow().replace(tzinfo=utc) p.save()
def post_profile(self, request_dict): # get/create profile p, created = ActivityProfile.objects.get_or_create(activity_id=request_dict['params']['activityId'], profile_id=request_dict['params']['profileId']) if "application/json" not in request_dict['headers']['CONTENT_TYPE']: try: post_profile = ContentFile(request_dict['profile'].read()) except: try: post_profile = ContentFile(request_dict['profile']) except: post_profile = ContentFile(str(request_dict['profile'])) self.save_non_json_profile(p, created, post_profile, request_dict) else: post_profile = request_dict['profile'] # If incoming profile is application/json and if a profile didn't # already exist with the same activityId and profileId if created: p.json_profile = post_profile p.content_type = request_dict['headers']['CONTENT_TYPE'] p.etag = etag.create_tag(post_profile) # If incoming profile is application/json and if a profile already # existed with the same activityId and profileId else: orig_prof = json.loads(p.json_profile) post_profile = json.loads(request_dict['profile']) if not isinstance(post_profile, dict): raise ParamError( "The document was not able to be parsed into a JSON object.") else: # json.dumps changes the format of the string rep of the # dict merged = json.dumps( dict(orig_prof.items() + post_profile.items())) p.json_profile = merged p.etag = etag.create_tag(merged) # Set updated if 'updated' in request_dict['headers'] and request_dict['headers']['updated']: p.updated = request_dict['headers']['updated'] else: p.updated = datetime.datetime.utcnow().replace(tzinfo=utc) p.save()
def post_profile(self, request_dict): # post_profile = request_dict['profile'] # get / create profile p, created = ActivityProfile.objects.get_or_create(activityId=request_dict['params']['activityId'], profileId=request_dict['params']['profileId']) if "application/json" not in request_dict['headers']['CONTENT_TYPE']: try: post_profile = ContentFile(request_dict['profile'].read()) except: try: post_profile = ContentFile(request_dict['profile']) except: post_profile = ContentFile(str(request_dict['profile'])) # #Set updated # if 'headers' in request_dict and ('updated' in request_dict['headers'] and request_dict['headers']['updated']): # p.updated = request_dict['headers']['updated'] # else: # p.updated = datetime.datetime.utcnow().replace(tzinfo=utc) self.save_profile(p, created, profile, request_dict) else: post_profile = request_dict['profile'] if created: p.json_profile = post_profile p.content_type = request_dict['headers']['CONTENT_TYPE'] p.etag = etag.create_tag(post_profile) p.updated = datetime.datetime.utcnow().replace(tzinfo=utc) else: etag.check_preconditions(request_dict,p, required=True) orig_prof = json.loads(p.json_profile) post_profile = json.loads(request_dict['profile']) if not isinstance(post_profile, dict): raise ParamError("The document was not able to be parsed into a JSON object.") else: # json.dumps changes the format of the string rep of the dict merged = json.dumps(dict(orig_prof.items() + post_profile.items())) p.json_profile = merged p.etag = etag.create_tag(merged) p.updated = datetime.datetime.utcnow().replace(tzinfo=utc) p.save()