Ejemplo n.º 1
0
    def post(self):
        agent = self.__get_agent(create=True)
        post_state = self.state
        if self.registrationId:
            p, created = models.ActivityState.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity_id=self.activity_id,
                registration_id=self.registrationId)
        else:
            p, created = models.ActivityState.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity_id=self.activity_id)

        if created:
            p.json_state = ast.literal_eval(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 = ast.literal_eval(p.json_state)
            post_state = ast.literal_eval(post_state)
            merged = '%s' % dict(orig_state.items() + post_state.items())
            p.json_state = merged
            p.etag = etag.create_tag(merged)

        p.save()
Ejemplo n.º 2
0
    def post(self):
        agent = self.__get_agent()
        post_state = self.state

        if self.registration:
            p,created = models.ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id,registration_id=self.registration)
        else:
            p,created = models.ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id)
        
        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()
Ejemplo n.º 3
0
    def post_profile(self, request_dict):
        post_profile = request_dict['profile']
        
        profile_id = request_dict['params']['profileId']

        # get / create  profile
        p, created = models.ActivityProfile.objects.get_or_create(activityId=request_dict['params']['activityId'],  profileId=request_dict['params']['profileId'])
        
        if created:
            p.json_profile = post_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(post_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)
        else:
            orig_prof = ast.literal_eval(p.json_profile)
            post_profile = ast.literal_eval(post_profile)
            # json.dumps changes the format of the string rep of the dict
            merged = '%s' % 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()
Ejemplo n.º 4
0
    def post_profile(self, request_dict):
        post_profile = request_dict['profile']
        
        profile_id = request_dict['params']['profileId']

        # get / create  profile
        p, created = models.ActivityProfile.objects.get_or_create(activityId=request_dict['params']['activityId'],  profileId=request_dict['params']['profileId'])
        
        if created:
            p.json_profile = post_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(post_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)
        else:
            etag.check_preconditions(request_dict,p, required=True)
            orig_prof = json.loads(p.json_profile)
            post_profile = json.loads(post_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()
Ejemplo n.º 5
0
    def post_profile(self, request_dict):
        post_profile = request_dict['profile']
        profile_id = request_dict['params']['profileId']

        p, created = AgentProfile.objects.get_or_create(profileId=profile_id,
                                                        agent=self.Agent)

        if created:
            p.json_profile = post_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(post_profile)

            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)
        else:
            etag.check_preconditions(request_dict, p, required=True)
            orig_prof = json.loads(p.json_profile)
            post_profile = json.loads(post_profile)
            if not isinstance(post_profile, dict):
                raise ParamError(
                    "The document was not able to be parsed into a JSON object."
                )
            else:
                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()
Ejemplo n.º 6
0
    def post_profile(self, request_dict):
        post_profile = request_dict["profile"]

        profile_id = request_dict["params"]["profileId"]
        if not uri.validate_uri(profile_id):
            err_msg = "Profile ID %s is not a valid URI" % profile_id
            raise ParamError(err_msg)

        # get / create  profile
        p, created = models.ActivityProfile.objects.get_or_create(
            activityId=request_dict["params"]["activityId"], profileId=request_dict["params"]["profileId"]
        )

        if created:
            p.json_profile = post_profile
            p.content_type = request_dict["headers"]["CONTENT_TYPE"]
            p.etag = etag.create_tag(post_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:
            orig_prof = ast.literal_eval(p.json_profile)
            post_profile = ast.literal_eval(post_profile)
            # json.dumps changes the format of the string rep of the dict
            merged = "%s" % dict(orig_prof.items() + post_profile.items())
            p.json_profile = merged
            p.etag = etag.create_tag(merged)

        p.save()
Ejemplo n.º 7
0
    def post_profile(self, request_dict):
        post_profile = request_dict['profile']

        profile_id = request_dict['params']['profileId']
        if not uri.validate_uri(profile_id):
            err_msg = 'Profile ID %s is not a valid URI' % profile_id
            raise ParamError(err_msg)

        # get / create  profile
        p, created = models.ActivityProfile.objects.get_or_create(
            activityId=request_dict['params']['activityId'],
            profileId=request_dict['params']['profileId'])

        if created:
            p.json_profile = post_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(post_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:
            orig_prof = ast.literal_eval(p.json_profile)
            post_profile = ast.literal_eval(post_profile)
            # json.dumps changes the format of the string rep of the dict
            merged = '%s' % dict(orig_prof.items() + post_profile.items())
            p.json_profile = merged
            p.etag = etag.create_tag(merged)

        p.save()
Ejemplo n.º 8
0
    def post_profile(self, request_dict):
        post_profile = request_dict['profile']

        profile_id = request_dict['params']['profileId']
        if not uri.validate_uri(profile_id):
            err_msg = 'Profile ID %s is not a valid URI' % profile_id
            raise ParamError(err_msg)

        p, created = AgentProfile.objects.get_or_create(profileId=profile_id,agent=self.Agent)
        
        if created:
            p.json_profile = post_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(post_profile)

            if 'headers' in request_dict and ('updated' in request_dict['headers'] and request_dict['headers']['updated']):
                p.updated = request_dict['headers']['updated']
        else:
            orig_prof = ast.literal_eval(p.json_profile)
            post_profile = ast.literal_eval(post_profile)
            merged = '%s' % dict(orig_prof.items() + post_profile.items())
            p.json_profile = merged
            p.etag = etag.create_tag(merged)

        p.save()
Ejemplo n.º 9
0
    def post_profile(self, request_dict):
        post_profile = request_dict['profile']

        profile_id = request_dict['params']['profileId']
        if not uri.validate_uri(profile_id):
            err_msg = 'Profile ID %s is not a valid URI' % profile_id
            raise ParamError(err_msg)

        p, created = AgentProfile.objects.get_or_create(profileId=profile_id,
                                                        agent=self.Agent)

        if created:
            p.json_profile = ast.literal_eval(post_profile)
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(post_profile)

            if 'headers' in request_dict and (
                    'updated' in request_dict['headers']
                    and request_dict['headers']['updated']):
                p.updated = request_dict['headers']['updated']
        else:
            orig_prof = ast.literal_eval(p.json_profile)
            post_profile = ast.literal_eval(post_profile)
            merged = '%s' % dict(orig_prof.items() + post_profile.items())
            p.json_profile = merged
            p.etag = etag.create_tag(merged)

        p.save()
Ejemplo n.º 10
0
    def post_profile(self, request_dict):
        post_profile = request_dict['profile']

        profile_id = request_dict['params']['profileId']

        p, created = AgentProfile.objects.get_or_create(profileId=profile_id,agent=self.Agent)
        
        if created:
            p.json_profile = post_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(post_profile)

            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)
        else:
            etag.check_preconditions(request_dict,p, required=True)
            orig_prof = ast.literal_eval(p.json_profile)
            post_profile = ast.literal_eval(post_profile)
            if not isinstance(post_profile, dict):
                raise ParamError("The document was not able to be parsed into a JSON object.")
            else:
                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()
Ejemplo n.º 11
0
    def put_profile(self, request_dict):
        profile_id = request_dict['params']['profileId']
        if not uri.validate_uri(profile_id):
            err_msg = 'Profile ID %s is not a valid URI' % profile_id
            raise ParamError(err_msg)

        p, created = AgentProfile.objects.get_or_create(profileId=profile_id,
                                                        agent=self.Agent)

        if request_dict['headers']['CONTENT_TYPE'] != "application/json":
            try:
                profile = ContentFile(request_dict['profile'].read())
            except:
                try:
                    profile = ContentFile(request_dict['profile'])
                except:
                    profile = ContentFile(str(request_dict['profile']))

            if not created:
                etag.check_preconditions(request_dict, p, required=True)
                p.profile.delete()
            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict['profile']
            p.json_profile = ast.literal_eval(the_profile)
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(the_profile)

            if 'headers' in request_dict and (
                    'updated' in request_dict['headers']
                    and request_dict['headers']['updated']):
                p.updated = request_dict['headers']['updated']
            p.save()
Ejemplo n.º 12
0
    def put(self):
        agent = self.__get_agent(create=True)
        if self.registrationId:
            p,created = models.ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id,registration_id=self.registrationId)
        else:
            p,created = models.ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id)
        
        if self.content_type != "application/json":
            try:
                state = ContentFile(self.state.read())
            except:
                try:
                    state = ContentFile(self.state)
                except:
                    state = ContentFile(str(self.state))

            if not created:
                etag.check_preconditions(self.req_dict,p)
                p.state.delete() # remove old state file
            self.save_state(p, created, state)
        else:
            if not created:
                etag.check_preconditions(self.req_dict, p)
            the_state = self.state
            p.json_state = the_state
            p.content_type = self.content_type
            p.etag = etag.create_tag(the_state)
            if self.updated:
                p.updated = self.updated
            p.save()
Ejemplo n.º 13
0
    def put(self):
        agent = self.__get_agent(create=True)
        try:
            state = ContentFile(self.state.read())
        except:
            try:
                state = ContentFile(self.state)
            except:
                state = ContentFile(str(self.state))

        if self.registrationId:
            p, created = models.activity_state.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity=self.activity,
                registration_id=self.registrationId)
        else:
            p, created = models.activity_state.objects.get_or_create(
                state_id=self.stateId, agent=agent, activity=self.activity)
        if not created:
            etag.check_preconditions(self.req_dict, p)
            p.state.delete()  # remove old state file
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id, p.activity_id,
                           self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 14
0
def activity_state_get(req_dict):
    # add ETag for concurrency
    activityId = req_dict['activityId']
    actor = req_dict['actor']
    registrationId = req_dict.get('registrationId', None)
    stateId = req_dict.get('stateId', None)
    if stateId:
        if registrationId:
            resource = "Success -- activity_state - method = GET - activityId = %s - actor = %s - registrationId = %s - stateId = %s" % (activityId, actor, registrationId, stateId)
        else:
            resource = "Success -- activity_state - method = GET - activityId = %s - actor = %s - stateId = %s" % (activityId, actor, stateId)
    else:
        since = req_dict.get('since', None)
        if registrationId or since:
            if registrationId and since:
                resource = "Success -- activity_state - method = GET - activityId = %s - actor = %s - registrationId = %s - since = %s" % (activityId, actor, registrationId, since)
            elif registrationId:
                resource = "Success -- activity_state - method = GET - activityId = %s - actor = %s - registrationId = %s" % (activityId, actor, registrationId)
            else:
                resource = "Success -- activity_state - method = GET - activityId = %s - actor = %s - since = %s" % (activityId, actor, since)
        else:
            resource = "Success -- activity_state - method = GET - activityId = %s - actor = %s" % (activityId, actor)
    response = HttpResponse(resource)
    response['ETag'] = etag.create_tag(resource)
    return response
Ejemplo n.º 15
0
    def put(self):
        agent = self.__get_agent(create=True)
        try:
            state = ContentFile(self.state.read())
        except:
            try:
                state = ContentFile(self.state)
            except:
                state = ContentFile(str(self.state))

        if self.registrationId:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity,registration_id=self.registrationId)
        else:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity)
        if not created:
            etag.check_preconditions(self.req_dict,p)
            p.state.delete() # remove old state file
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id,p.activity_id, self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 16
0
    def put_profile(self, request_dict):
        profile_id = request_dict['params']['profileId']
        if not uri.validate_uri(profile_id):
            err_msg = 'Profile ID %s is not a valid URI' % profile_id
            raise ParamError(err_msg)

        p,created = AgentProfile.objects.get_or_create(profileId=profile_id,agent=self.Agent)

        if request_dict['headers']['CONTENT_TYPE'] != "application/json":
            try:
                profile = ContentFile(request_dict['profile'].read())
            except:
                try:
                    profile = ContentFile(request_dict['profile'])
                except:
                    profile = ContentFile(str(request_dict['profile']))
        

            if not created:
                etag.check_preconditions(request_dict,p, required=True)
                p.profile.delete()
            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict['profile']
            p.json_profile = the_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(the_profile)

            if 'headers' in request_dict and ('updated' in request_dict['headers'] and request_dict['headers']['updated']):
                p.updated = request_dict['headers']['updated']
            p.save()
Ejemplo n.º 17
0
    def put_profile(self, request_dict):
        profile_id = request_dict['params']['profileId']

        p,created = AgentProfile.objects.get_or_create(profileId=profile_id,agent=self.Agent)

        if "application/json" not in request_dict['headers']['CONTENT_TYPE']:
            try:
                profile = ContentFile(request_dict['profile'].read())
            except:
                try:
                    profile = ContentFile(request_dict['profile'])
                except:
                    profile = ContentFile(str(request_dict['profile']))
        

            if not created:
                etag.check_preconditions(request_dict,p, required=True)
                p.profile.delete()
            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict['profile']
            p.json_profile = the_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(the_profile)

            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)
            p.save()
Ejemplo n.º 18
0
    def put(self):
        agent = self.__get_agent()
        if self.registration:
            p,created = models.ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id,registration_id=self.registration)
        else:
            p,created = models.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:
                state = ContentFile(self.state.read())
            except:
                try:
                    state = ContentFile(self.state)
                except:
                    state = ContentFile(str(self.state))

            if not created:
                etag.check_preconditions(self.req_dict,p)
                p.state.delete() # remove old state file
            p.content_type = self.content_type
            self.save_state(p, created, state)
        else:
            if not created:
                etag.check_preconditions(self.req_dict, p)
            the_state = self.state
            p.json_state = the_state
            p.content_type = self.content_type
            p.etag = etag.create_tag(the_state)
            if self.updated:
                p.updated = self.updated
            else:
                p.updated = datetime.datetime.utcnow().replace(tzinfo=utc)
            p.save()
Ejemplo n.º 19
0
    def save_state(self, p, created, state):
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id,p.activity_id, self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 20
0
    def save_profile(self, p, created, profile, request_dict):
        p.content_type = request_dict['headers']['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        if 'headers' in request_dict and ('updated' in request_dict['headers'] and request_dict['headers']['updated']):
            p.updated = request_dict['headers']['updated']
        profile.seek(0)
        if created:
            p.save()

        fn = "%s_%s" % (p.agent_id,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 21
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        #Check if activity exists
        try:
            # Always want global version
            activity = models.activity.objects.get(activity_id=request_dict['activityId'],
                global_representation=True)
        except models.activity.DoesNotExist:
            err_msg = 'There is no activity associated with the id: %s' % request_dict['activityId']
            log_message(self.log_dict, err_msg, __name__, self.put_profile.__name__, True)
            update_parent_log_status(self.log_dict, 404)
            raise IDNotFoundError(err_msg)

        user = get_user_from_auth(request_dict.get('auth', None))
        #Get the profile, or if not already created, create one
        p,created = models.activity_profile.objects.get_or_create(profileId=request_dict['profileId'],activity=activity, user=user)
        
        if created:
            log_message(self.log_dict, "Created Activity Profile", __name__, self.put_profile.__name__)
        else:
            #If it already exists delete it
            etag.check_preconditions(request_dict,p, required=True)
            p.profile.delete()
            log_message(self.log_dict, "Retrieved Activity Profile", __name__, self.put_profile.__name__)
        
        #Save profile content type based on incoming content type header and create etag
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        
        #Set updated
        if request_dict['updated']:
            p.updated = request_dict['updated']
        
        #Go to beginning of file
        profile.seek(0)
        
        #If it didn't exist, save it
        if created:
            p.save()

        #Set filename with the activityID and profileID and save
        fn = "%s_%s" % (p.activity_id,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)

        log_message(self.log_dict, "Saved Activity Profile", __name__, self.put_profile.__name__)
Ejemplo n.º 22
0
    def save_state(self, p, created, state):
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id, p.activity_id,
                           self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 23
0
    def save_state(self, p, created, state):
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        else:
            p.updated = datetime.datetime.utcnow().replace(tzinfo=utc)
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id,p.activity_id, self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 24
0
    def save_profile(self, p, created, profile, request_dict):
        p.content_type = request_dict['headers']['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        if 'headers' in request_dict and ('updated' in request_dict['headers']
                                          and
                                          request_dict['headers']['updated']):
            p.updated = request_dict['headers']['updated']
        profile.seek(0)
        if created:
            p.save()

        fn = "%s_%s" % (p.agent_id, request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 25
0
    def post(self):
        agent = self.__get_agent()
        post_state = self.state

        if self.registration:
            p, created = models.ActivityState.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity_id=self.activity_id,
                registration_id=self.registration)
        else:
            p, created = models.ActivityState.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity_id=self.activity_id)

        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()
Ejemplo n.º 26
0
    def save_state(self, p, created, state):
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        else:
            p.updated = datetime.datetime.utcnow().replace(tzinfo=utc)
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id, p.activity_id,
                           self.req_dict.get('filename', p.id))
        p.state.save(fn, state)
Ejemplo n.º 27
0
    def post(self):
        agent = self.__get_agent(create=True)
        post_state = self.state
        if self.registrationId:
            p,created = models.ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id,registration_id=self.registrationId)
        else:
            p,created = models.ActivityState.objects.get_or_create(state_id=self.stateId,agent=agent,activity_id=self.activity_id)
        
        if created:
            p.json_state = ast.literal_eval(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 = ast.literal_eval(p.json_state)
            post_state = ast.literal_eval(post_state)
            merged = '%s' % dict(orig_state.items() + post_state.items())
            p.json_state = merged
            p.etag = etag.create_tag(merged)

        p.save()
Ejemplo n.º 28
0
def activity_profile_get(req_dict):
    # add ETag for concurrency
    activityId = req_dict['activityId']
    profileId = req_dict.get('profileId', None)
    if profileId:
        resource = "Success -- activity_profile - method = GET - activityId = %s - profileId = %s" % (activityId, profileId)
    else:
        since = req_dict.get('since', None)
        if since:
            resource = "Success -- activity_profile - method = GET - activityId = %s - since = %s" % (activityId, since)
        else:
            resource = "Success -- activity_profile - method = GET"
    response = HttpResponse(resource)
    response['ETag'] = etag.create_tag(resource)
    return response
Ejemplo n.º 29
0
def actor_profile_get(req_dict):
    # add ETag for concurrency
    actor = req_dict['actor']
    a = objects.Actor(actor)
    profileId = req_dict.get('profileId', None)
    if profileId:
        resource = "Success -- actor_profile - method = GET - actor = %s - profileId = %s" % (actor, profileId)
    else:
        since = req_dict.get('since', None)
        if since:
            resource = "Success -- actor_profile - method = GET - actor = %s - since = %s" % (actor, since)
        else:
            resource = "Success -- actor_profile - method = GET - actor = %s" % actor
    response = HttpResponse(resource)
    response['ETag'] = etag.create_tag(resource)
    return response
Ejemplo n.º 30
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        #Check if activity exists
        try:
            activity = models.activity.objects.get(
                activity_id=request_dict['activityId'])
        except models.activity.DoesNotExist:
            raise IDNotFoundError(
                'There is no activity associated with the id: %s' %
                request_dict['activityId'])

        #Get the profile, or if not already created, create one
        p, created = models.activity_profile.objects.get_or_create(
            profileId=request_dict['profileId'], activity=activity)

        #If it already exists delete it
        if not created:
            etag.check_preconditions(request_dict, p, required=True)
            p.profile.delete()

        #Save profile content type based on incoming content type header and create etag
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())

        #Set updated
        if request_dict['updated']:
            p.updated = request_dict['updated']

        #Go to beginning of file
        profile.seek(0)

        #If it didn't exist, save it
        if created:
            p.save()

        #Set filename with the activityID and profileID and save
        fn = "%s_%s" % (p.activity_id, request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 31
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        profile_id = request_dict['params']['profileId']

        #Get the profile, or if not already created, create one
        p, created = models.ActivityProfile.objects.get_or_create(
            profileId=profile_id,
            activityId=request_dict['params']['activityId'])

        if "application/json" not in request_dict['headers']['CONTENT_TYPE']:
            try:
                profile = ContentFile(request_dict['profile'].read())
            except:
                try:
                    profile = ContentFile(request_dict['profile'])
                except:
                    profile = ContentFile(str(request_dict['profile']))

            if not created:
                #If it already exists delete it
                etag.check_preconditions(request_dict, p, required=True)
                if p.profile:
                    try:
                        p.profile.delete()
                    except OSError:
                        # probably was json before
                        p.json_profile = {}

            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict['profile']
            p.json_profile = the_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(the_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)
            p.save()
Ejemplo n.º 32
0
    def save_profile(self, p, created, profile, request_dict):
        #Save profile content type based on incoming content type header and create etag
        p.content_type = request_dict['headers']['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        
        #Set updated
        if 'headers' in request_dict and ('updated' in request_dict['headers'] and request_dict['headers']['updated']):
            p.updated = request_dict['headers']['updated']
        
        #Go to beginning of file
        profile.seek(0)
        
        #If it didn't exist, save it
        if created:
            p.save()

        #Set filename with the activityID and profileID and save
        fn = "%s_%s" % (p.activityId,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 33
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        profile_id = request_dict['params']['profileId']
        if not uri.validate_uri(profile_id):
            err_msg = 'Profile ID %s is not a valid URI' % profile_id
            raise ParamError(err_msg)

        #Get the profile, or if not already created, create one
        p, created = models.ActivityProfile.objects.get_or_create(
            profileId=profile_id,
            activityId=request_dict['params']['activityId'])

        if request_dict['headers']['CONTENT_TYPE'] != "application/json":
            try:
                profile = ContentFile(request_dict['profile'].read())
            except:
                try:
                    profile = ContentFile(request_dict['profile'])
                except:
                    profile = ContentFile(str(request_dict['profile']))

            if not created:
                #If it already exists delete it
                etag.check_preconditions(request_dict, p, required=True)
                if p.profile:
                    p.profile.delete()

            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict['profile']
            p.json_profile = the_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(the_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']
            p.save()
Ejemplo n.º 34
0
    def put_profile(self, request_dict):
        # Parse out profile from request_dict
        profile_id = request_dict["params"]["profileId"]
        if not uri.validate_uri(profile_id):
            err_msg = "Profile ID %s is not a valid URI" % profile_id
            raise ParamError(err_msg)

        # Get the profile, or if not already created, create one
        p, created = models.ActivityProfile.objects.get_or_create(
            profileId=profile_id, activityId=request_dict["params"]["activityId"]
        )

        if request_dict["headers"]["CONTENT_TYPE"] != "application/json":
            try:
                profile = ContentFile(request_dict["profile"].read())
            except:
                try:
                    profile = ContentFile(request_dict["profile"])
                except:
                    profile = ContentFile(str(request_dict["profile"]))

            if not created:
                # If it already exists delete it
                etag.check_preconditions(request_dict, p, required=True)
                if p.profile:
                    p.profile.delete()

            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict["profile"]
            p.json_profile = the_profile
            p.content_type = request_dict["headers"]["CONTENT_TYPE"]
            p.etag = etag.create_tag(the_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"]
            p.save()
Ejemplo n.º 35
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        #Check if activity exists
        try:
            activity = models.activity.objects.get(activity_id=request_dict['activityId'])
        except models.activity.DoesNotExist:
            raise IDNotFoundError('There is no activity associated with the id: %s' % request_dict['activityId'])

        #Get the profile, or if not already created, create one
        p,created = models.activity_profile.objects.get_or_create(profileId=request_dict['profileId'],activity=activity)
        
        #If it already exists delete it
        if not created:
            etag.check_preconditions(request_dict,p, required=True)
            p.profile.delete()
        
        #Save profile content type based on incoming content type header and create etag
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        
        #Set updated
        if request_dict['updated']:
            p.updated = request_dict['updated']
        
        #Go to beginning of file
        profile.seek(0)
        
        #If it didn't exist, save it
        if created:
            p.save()

        #Set filename with the activityID and profileID and save
        fn = "%s_%s" % (p.activity_id,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 36
0
    def put_profile(self, request_dict):
        #Parse out profile from request_dict
        profile_id = request_dict['params']['profileId']

        #Get the profile, or if not already created, create one
        p,created = models.ActivityProfile.objects.get_or_create(profileId=profile_id,activityId=request_dict['params']['activityId'])
        
        if "application/json" not in request_dict['headers']['CONTENT_TYPE']:
            try:
                profile = ContentFile(request_dict['profile'].read())
            except:
                try:
                    profile = ContentFile(request_dict['profile'])
                except:
                    profile = ContentFile(str(request_dict['profile']))

            if not created:
                #If it already exists delete it
                etag.check_preconditions(request_dict,p, required=True)
                if p.profile:
                    try:
                        p.profile.delete()
                    except OSError:
                        # probably was json before
                        p.json_profile = {}
            
            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict['profile']
            p.json_profile = the_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(the_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)
            p.save()
Ejemplo n.º 37
0
    def save_profile(self, p, created, profile, request_dict):
        #Save profile content type based on incoming content type header and create etag
        p.content_type = request_dict['headers']['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())

        #Set updated
        if 'headers' in request_dict and ('updated' in request_dict['headers']
                                          and
                                          request_dict['headers']['updated']):
            p.updated = request_dict['headers']['updated']

        #Go to beginning of file
        profile.seek(0)

        #If it didn't exist, save it
        if created:
            p.save()

        #Set filename with the activityID and profileID and save
        fn = "%s_%s" % (p.activityId, request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 38
0
    def put(self):
        agent = self.__get_agent()
        if self.registration:
            p, created = models.ActivityState.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity_id=self.activity_id,
                registration_id=self.registration)
        else:
            p, created = models.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:
                state = ContentFile(self.state.read())
            except:
                try:
                    state = ContentFile(self.state)
                except:
                    state = ContentFile(str(self.state))

            if not created:
                etag.check_preconditions(self.req_dict, p)
                p.state.delete()  # remove old state file
            p.content_type = self.content_type
            self.save_state(p, created, state)
        else:
            if not created:
                etag.check_preconditions(self.req_dict, p)
            the_state = self.state
            p.json_state = the_state
            p.content_type = self.content_type
            p.etag = etag.create_tag(the_state)
            if self.updated:
                p.updated = self.updated
            else:
                p.updated = datetime.datetime.utcnow().replace(tzinfo=utc)
            p.save()
Ejemplo n.º 39
0
    def put(self):
        agent = self.__get_agent(create=True)
        if self.registrationId:
            p, created = models.ActivityState.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity_id=self.activity_id,
                registration_id=self.registrationId)
        else:
            p, created = models.ActivityState.objects.get_or_create(
                state_id=self.stateId,
                agent=agent,
                activity_id=self.activity_id)

        if self.content_type != "application/json":
            try:
                state = ContentFile(self.state.read())
            except:
                try:
                    state = ContentFile(self.state)
                except:
                    state = ContentFile(str(self.state))

            if not created:
                etag.check_preconditions(self.req_dict, p)
                p.state.delete()  # remove old state file
            self.save_state(p, created, state)
        else:
            if not created:
                etag.check_preconditions(self.req_dict, p)
            the_state = ast.literal_eval(
                self.state) if type(self.state) != dict else self.state

            p.json_state = the_state
            p.content_type = self.content_type
            p.etag = etag.create_tag(json.dumps(the_state))
            if self.updated:
                p.updated = self.updated
            p.save()
Ejemplo n.º 40
0
    def put_profile(self, request_dict):
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        p,created = agent_profile.objects.get_or_create(profileId=request_dict['profileId'],agent=self.agent)
        if not created:
            etag.check_preconditions(request_dict,p, required=True)
            p.profile.delete()
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        if request_dict['updated']:
            p.updated = request_dict['updated']
        profile.seek(0)
        if created:
            p.save()

        fn = "%s_%s" % (p.agent_id,request_dict.get('filename', p.id))
        p.profile.save(fn, profile)
Ejemplo n.º 41
0
    def put(self):
        agent = self.__get_agent(create=True)
        try:
            state = ContentFile(self.state.read())
        except:
            try:
                state = ContentFile(self.state)
            except:
                state = ContentFile(str(self.state))

        if self.registrationId:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity,registration_id=self.registrationId, user=self.user)
        else:
            p,created = models.activity_state.objects.get_or_create(state_id=self.stateId,agent=agent,activity=self.activity, user=self.user)
        
        if created:
            log_message(self.log_dict, "Created Activity State", __name__, self.put.__name__)
        elif not created:
            etag.check_preconditions(self.req_dict,p)
            p.state.delete() # remove old state file
            log_message(self.log_dict, "Retrieved Activity State", __name__, self.put.__name__)

        # if not created:
        #     etag.check_preconditions(self.req_dict,p)
        #     p.state.delete() # remove old state file
        p.content_type = self.content_type
        p.etag = etag.create_tag(state.read())
        if self.updated:
            p.updated = self.updated
        state.seek(0)
        if created:
            p.save()

        fn = "%s_%s_%s" % (p.agent_id,p.activity_id, self.req_dict.get('filename', p.id))
        p.state.save(fn, state)

        log_message(self.log_dict, "Saved Activity State", __name__, self.put.__name__)
Ejemplo n.º 42
0
    def put_profile(self, request_dict):
        profile_id = request_dict['params']['profileId']
        p, created = AgentProfile.objects.get_or_create(profileId=profile_id,
                                                        agent=self.Agent)

        if "application/json" not in request_dict['headers']['CONTENT_TYPE']:
            try:
                profile = ContentFile(request_dict['profile'].read())
            except:
                try:
                    profile = ContentFile(request_dict['profile'])
                except:
                    profile = ContentFile(str(request_dict['profile']))

            if not created:
                etag.check_preconditions(request_dict, p, required=True)
                try:
                    p.profile.delete()
                except OSError:
                    # p was probably json before.. gotta clear that field
                    p.json_profile = {}
            self.save_profile(p, created, profile, request_dict)
        else:
            if not created:
                etag.check_preconditions(request_dict, p, required=True)
            the_profile = request_dict['profile']
            p.json_profile = the_profile
            p.content_type = request_dict['headers']['CONTENT_TYPE']
            p.etag = etag.create_tag(the_profile)

            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)
            p.save()
Ejemplo n.º 43
0
    def put_profile(self, request_dict):
        try:
            profile = ContentFile(request_dict['profile'].read())
        except:
            try:
                profile = ContentFile(request_dict['profile'])
            except:
                profile = ContentFile(str(request_dict['profile']))

        p, created = agent_profile.objects.get_or_create(
            profileId=request_dict['profileId'], agent=self.agent)
        if not created:
            etag.check_preconditions(request_dict, p, required=True)
            p.profile.delete()
        p.content_type = request_dict['CONTENT_TYPE']
        p.etag = etag.create_tag(profile.read())
        if request_dict['updated']:
            p.updated = request_dict['updated']
        profile.seek(0)
        if created:
            p.save()

        fn = "%s_%s" % (p.agent_id, request_dict.get('filename', p.id))
        p.profile.save(fn, profile)