Example #1
0
def oauth2_helper(request):
    token = request['auth']['oauth_token']

    user = token.user
    user_name = user.username
    if user.email.startswith('mailto:'):
        user_email = user.email
    else:
        user_email = 'mailto:%s' % user.email
    consumer = token.client
    members = [
                {
                    "account":{
                                "name":consumer.client_id,
                                "homePage":"%s://%s/XAPI/OAuth2/access_token/" % (settings.SITE_SCHEME, str(Site.objects.get_current().domain))
                    },
                    "objectType": "Agent",
                    "oauth_identifier": "anonoauth:%s" % (consumer.client_id)
                },
                {
                    "name":user_name,
                    "mbox":user_email,
                    "objectType": "Agent"
                }
    ]
    kwargs = {"objectType":"Group", "member":members,"oauth_identifier": "anongroup:%s-%s" % (consumer.client_id, user_email)}
    # create/get oauth group and set in dictionary
    oauth_group, created = Agent.objects.oauth_group(**kwargs)
    request['auth']['authority'] = oauth_group
    request['auth']['user'] = get_user_from_auth(oauth_group)
    validate_oauth_scope(request)
Example #2
0
def oauth_helper(request):
    token = request['auth']['oauth_token']
    
    user = token.user
    user_name = user.username
    if user.email.startswith('mailto:'):
        user_email = user.email
    else:
        user_email = 'mailto:%s' % user.email
    consumer = token.consumer                
    members = [
                {
                    "account":{
                                "name":consumer.key,
                                "homePage":"lrs://XAPI/OAuth/token/"
                    },
                    "objectType": "Agent",
                    "oauth_identifier": "anonoauth:%s" % (consumer.key)
                },
                {
                    "name":user_name,
                    "mbox":user_email,
                    "objectType": "Agent"
                }
    ]
    kwargs = {"objectType":"Group", "member":members,"oauth_identifier": "anongroup:%s-%s" % (consumer.key, user_email)}
    # create/get oauth group and set in dictionary
    oauth_group, created = Agent.objects.oauth_group(**kwargs)
    request['auth']['authority'] = oauth_group
    request['auth']['user'] = get_user_from_auth(oauth_group)
    validate_oauth_scope(request)
Example #3
0
    def saveObjectToDB(self, args):
        # If it's a substatement, remove voided, authority, and id keys
        args['user'] = get_user_from_auth(self.auth)
        if self.__class__.__name__ == 'SubStatement':
            del args['voided']
            
            # If ID not given, created by models
            if 'statement_id' in args:
                del args['statement_id']
            
            if 'authority' in args:
                del args['authority']
            stmt = models.SubStatement(**args)
            stmt.save()
        else:
            stmt = models.statement(**args)
            stmt.save()

        if self.log_dict:
            self.log_dict['message'] = "Saved statement to database in %s.%s" % (__name__, self.saveObjectToDB.__name__)
            logger.info(msg=self.log_dict)
            self.log_dict['message'] = stmt.statement_id #stmt.object_return()
            logger.log(models.SystemAction.STMT_REF, msg=self.log_dict)            

        return stmt
    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__)
Example #5
0
    def saveObjectToDB(self, args):
        # If it's a substatement, remove voided, authority, and id keys
        args['user'] = get_user_from_auth(self.auth)
        if self.__class__.__name__ == 'SubStatementManager':
            del args['voided']
            
            # If ID not given, created by models
            if 'statement_id' in args:
                del args['statement_id']
            
            if 'authority' in args:
                del args['authority']

            stmt = models.SubStatement.objects.create(**args)
        else:
            stmt = models.Statement.objects.create(**args)
        return stmt
Example #6
0
 def __init__(self, request_dict, log_dict=None):
     self.req_dict = request_dict
     self.log_dict = log_dict
     self.agent = request_dict['agent']
     self.auth = request_dict.get('auth', None)
     self.user = get_user_from_auth(self.auth)
     try:
         self.activity = models.activity.objects.get(activity_id=request_dict['activityId'])
     except models.activity.DoesNotExist:
         err_msg = "Error with Activity State. The activity id (%s) did not match any activities on record" % (request_dict['activityId'])
         log_message(self.log_dict, err_msg, __name__, self.__init__.__name__, True)
         raise IDNotFoundError(err_msg)
     self.registrationId = request_dict.get('registrationId', None)
     self.stateId = request_dict.get('stateId', None)
     self.updated = request_dict.get('updated', None)
     self.content_type = request_dict.get('CONTENT_TYPE', None)
     self.state = request_dict.get('state', None)
     self.etag = request_dict.get('ETAG', None)
     self.since = request_dict.get('since', None)
Example #7
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']))

        user = get_user_from_auth(request_dict.get('auth', None))
        p,created = agent_profile.objects.get_or_create(profileId=request_dict['profileId'],agent=self.agent, user=user)
        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)