def register_user(self, username):
        from findit.models import Request
        new_requests = Request.objects.filter(illiad_tn='new')
        for request in new_requests:
            user = request.user
            username = request.user.username
            print username
#            username += '@brown.edu'
#            try:
#                user = User.objects.get(username=username)
#            except:
#                print>>sys.stderr, "%s was not found in the easyArticle database." % username
#                return
            profile = user.libraryprofile
            illiad_profile = profile.illiad()
            ill_username = illiad_profile['username']

            #Get an illiad instance
            sess = IlliadSession(ILLIAD_REMOTE_AUTH_URL,
                                   ILLIAD_REMOTE_AUTH_HEADER,
                                   ill_username)
            illiad_session = sess.login()

            #if not sess.registered:
            print>>sys.stderr, "Registering %s with Illliad as %s." % (username, illiad_profile)
            reg = sess.register_user(illiad_profile)
            print reg
            #else:
            #    print>>sys.stderr, "%s is already registered with Illiad." % username
            sess.logout()
            break
Exemplo n.º 2
0
def illiad_client(request, make_request=False):
    """
    Register the given user with Illiad if not registered already.
    Move this to the user profile at some point.
    """
    from illiad.account import IlliadSession
    from utils import make_illiad_url
    user = request.user
    profile = user.libraryprofile
    illiad_profile = profile.illiad()
    ill_username = illiad_profile['username']
    #dict to store output
    out = {}
    #Get an illiad instance
    illiad = IlliadSession(settings.FINDIT_ILLIAD_REMOTE_AUTH_URL,
                           settings.FINDIT_ILLIAD_REMOTE_AUTH_HEADER,
                           ill_username)
    illiad_session = illiad.login()
    ilog.info('User %s established Illiad session: %s.' % (ill_username,
                                                          illiad_session['session_id']))
    out['session'] = illiad_session

    #Exit if we can't authenticate the user.
    if not illiad_session['authenticated']:
        out['session_error'] = 'Failed login.'
        ilog.error("Illiad login failed for %s" % ill_username)
        illiad.logout()
        return out

    #Register users if neccessary.
    if not illiad.registered:
        ilog.info('Registering %s with Illiad as %s.' % (ill_username,
                                                         illiad_profile['status'])
                  )
        reg = illiad.register_user(illiad_profile)
        ilog.info('%s registration response: %s' % (ill_username, reg))
        out['was_registered'] = False
    else:
        ilog.debug('%s is already registered.' % (ill_username))
        out['was_registered'] = True
    #Handle the request if the kwargs exist.
    if make_request is True:
        query = make_illiad_url(request.bib)
        ilog.debug("Illiad url to submit is: %s" % query)
        illiad_post_key = illiad.get_request_key(query)
        ilog.info(illiad_post_key)
        #If blocked comes back in the post key, stop here with appropriate status.
        blocked = illiad_post_key.get('blocked', None)
        errors = illiad_post_key.get('errors', None)
        #for mocking errors
        #errors = True
        if blocked:
            out['blocked'] = blocked
            ilog.info("%s is blocked in Illiad." % ill_username)
            _send_message('blocked', request)
        elif errors:
            out['errors'] = True
            msg = illiad_post_key.get('message')
            ilog.info("Request errors during Illiad submission: %s %s" %\
                        (ill_username,
                         msg))
            out['message'] = msg
        else:
            submit_status = illiad.make_request(illiad_post_key)
            #Mock a request for testing.
#            submit_status = {
#                           'transaction_number': '1234',
#                           'submitted': True,
#                           'error': False,
#                           'message': None
#                           }
            out['submit_status'] = submit_status
            #Write the request to the requests table.
            if submit_status['submitted']:
                illiad_tn = submit_status['transaction_number']
                request.transaction_number = illiad_tn
                out['submitted'] = True
                _send_message('confirmation', request)
                ilog.info("%s request submitted for %s with transaction %s." %\
                        (ill_username,
                         request.id,
                         illiad_tn))
            else:
                ilog.error("%s request failed with message %s." %\
                           (ill_username,
                           submit_status['message']))
    illiad.logout()
    return out
    def handle(self, **options):
        from findit.models import Request
        count = 0
        new_requests = Request.objects.filter(illiad_tn='new')
        for request in new_requests:
            user = request.user
            profile = user.libraryprofile
            illiad_profile = profile.illiad()
            tries = 0
            while tries < 3:
                try:
                    sersol = get_sersol_data(request.item.query, key=sersol_key)
                    break
                except urllib2.URLError:
                    print>>sys.stderr, "360Link timeout.  Trying again."
                    tries += 1
            resolved = Resolved(sersol)
            illiad_request_url = "%s&sid=%s" % (make_illiad_url(resolved.openurl), request.item.referrer)
            #print bib
            #print illiad_request_url
            #print illiad_request_url
            ill_username = illiad_profile['username']
            #Get the OpenURL we will submit.
            ill_url = illiad_request_url
            ilog.info('User %s posted %s for request.' % (ill_username,
                                                           ill_url))
            out = {}
            #Get an illiad instance
            illiad = IlliadSession(ILLIAD_REMOTE_AUTH_URL,
                                   ILLIAD_REMOTE_AUTH_HEADER,
                                   ill_username)
            illiad_session = illiad.login()
            ilog.info('User %s established Illiad session: %s.' % (ill_username,
                                                                  illiad_session['session_id']))
            out['session'] = illiad_session

            if not illiad_session['authenticated']:
                out['session_error'] = 'Failed login.'
                ilog.error("Illiad login failed for %s" % ill_username)
            else:
                #Register users if neccessary.
                if not illiad.registered:
                    ilog.info('Will register %s with illiad.' % (ill_username))
                    ilog.info('Registering %s with Illiad as %s.' % (ill_username,
                                                                     illiad_profile['status'])
                              )
                    reg = illiad.register_user(illiad_profile)
                    ilog.info('%s registration response: %s' % (ill_username, reg))

                illiad_post_key = illiad.get_request_key(ill_url)
                #If blocked comes back in the post key, stop here with appropriate status.
                blocked = illiad_post_key.get('blocked', None)
                errors = illiad_post_key.get('errors', None)
                if blocked:
                    out['blocked'] = blocked
                    ilog.info("%s is blocked in Illiad." % ill_username)
                    self.send_message('blocked', resource=resource)
                elif errors:
                    out['errors'] = True
                    msg = illiad_post_key['message']
                    ilog.info("Request errors during Illiad submission: %s %s" %\
                                (ill_username,
                                 self.msg))
                    out['message'] = msg
                else:
                    #Submit this
                    submit_status = illiad.make_request(illiad_post_key)
                    out['submit_status'] = submit_status
                    #Write the request to the requests table.
                    if submit_status['submitted']:
                        illiad_tn = submit_status['transaction_number']
                        request.illiad_tn = illiad_tn
                        print request.user, request.id, request.item.id, illiad_tn
                        request.save()
                        count += 1
                    else:
                        ilog.error("%s request failed with message %s." %\
                                   (ill_username,
                                   submit_status['message']))

            illiad.logout()