def submit_to_illiad(self, request_id):
        print "Submitting: ", request_id
        try:
            request = Request.objects.get(id=request_id)
        except ObjectDoesNotExist:
            print>>sys.stderr, "%s was not found in the easyArticle database." % request
            return
        profile = request.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()

        #query
        query = request.bib['_query']

        #get the request key
        rk = sess.get_request_key(query)
        #submit the request
        submit = sess.make_request(rk)

        pprint(submit)

        sess.logout()
    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
    def handle(self, **options):
        #Time delta for querying users that haven't been registerd in X
        #number of days.
        today = date.today()
        cutoff = today - timedelta(days=DAYS)
        #Exclude any users who have been registered or updated after
        #the cutoff date.
        profiles = LibraryProfile.objects.exclude(
                illiad_registration_date__gte=cutoff
                )
        for user_profile in profiles:
            user = user_profile.user
            #Skip local admin users.
            if user.username.find('@brown.edu') < 0:
                continue
            illiad_key = user_profile.illiad()
            ill_session = IlliadSession(ILLIAD_REMOTE_AUTH_URL,
                               ILLIAD_REMOTE_AUTH_HEADER,
                               illiad_key.get('username'))
            status = ill_session.login()
            if status.get('registered') == False:
                logging.info("Will register %s with ILLiad as %s." % (user.username, illiad_key))
                registration = ill_conn.register_user(illiad_key)
            else:
                #update registration
                logging.info("Updating ILLiad registration for %s with %s." % (user.username, illiad_key))

            #Update the illiad registration information.
            user_profile.illiad_registration_date = today
            user_profile.save()

            ill_session.logout()
Exemple #4
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
Exemple #5
0
def illiad_handler( request ):
    """ Processes the confirmation 'Submit' button behind-the-scenes by submitting the request to illiad and reading the result.
        Then redirects user (behind-the-scenes) to views.shib_logout() for the SP shib-logout ( which will then direct user to views.message() )
        """

    ## here check
    here_check = 'init'
    illiad_url = request.session.get( 'illiad_url', '' )
    log.debug( 'illiad_url, ``{}```'.format(illiad_url) )
    if len( illiad_url ) == 0:
        here_check = 'problem'
    if here_check == 'init':
        shib_dct = json.loads( request.session.get('user_json', '{}') )
        if 'eppn' not in shib_dct.keys():
            here_check = 'problem'
    if here_check == 'problem':
        log.warning( 'bad attempt from source-url, ```%s```; ip, `%s`' % (
            request.META.get('HTTP_REFERER', ''), request.META.get('REMOTE_ADDR', '') ) )
        request.session['message'] = new_ill_helper.problem_message
        request.session['last_path'] = request.path
        return HttpResponseRedirect( reverse('article_request:message_url') )

    ## get illiad_instance
    ill_username = shib_dct['eppn'].split('@')[0]
    log.debug( 'ill_username, `%s`' % ill_username )
    illiad_instance = IlliadSession( settings_app.ILLIAD_REMOTE_AUTH_URL, settings_app.ILLIAD_REMOTE_AUTH_HEADER, ill_username )
    log.debug( 'illiad_instance.__dict__, ```%s```' % pprint.pformat(illiad_instance.__dict__) )
    try:
        illiad_session = illiad_instance.login()
    except Exception as e:
        log.error( 'Exception on illiad login, ```%s```' % unicode(repr(e)) )
        if request.session.get( 'message', '' ) == '':
            request.session['message'] = new_ill_helper.problem_message
        request.session['last_path'] = request.path
        return HttpResponseRedirect( reverse('article_request:message_url') )

    ## submit to illiad
    illiad_url = request.session['illiad_url']
    illiad_post_key = illiad_instance.get_request_key( illiad_url )
    log.debug( 'illiad_post_key, ```%s```' % pprint.pformat(illiad_post_key) )
    errors = illiad_post_key.get( 'errors', None )
    if errors:
        log.warning( 'errors during illiad submission: username, `%s`; message, ```%s```' % (ill_username, illiad_post_key['message']) )
        if request.session.get( 'message', '' ) == '':
            request.session['message'] = new_ill_helper.problem_message
        request.session['last_path'] = request.path
        return HttpResponseRedirect( reverse('article_request:message_url') )
    else:
        submit_status = illiad_instance.make_request( illiad_post_key )
        log.debug( 'submit_status, ```%s```' % pprint.pformat(submit_status) )
        illiad_transaction_number = submit_status['transaction_number']

    ## illiad logout
    try:
        illiad_instance.logout()
        log.debug( 'illiad logout successful' )
    except Exception as e:
        log.debug( 'illiad logout exception, ```%s```' % unicode(repr(e)) )

    ## update db eventually

    ## send email
    citation_json = request.session.get( 'citation_json', '{}' )
    citation_dct = json.loads( citation_json )
    if citation_dct.get( 'title', '' ) != '':
        citation_title = citation_dct['title']
    else:
        citation_title = citation_dct.get('source', 'title_unavailable')
    #
    subject = 'easyAccess request confirmation'
    body = new_ill_helper.make_illiad_success_message(
        shib_dct['name_first'], shib_dct['name_last'], citation_title, illiad_transaction_number, shib_dct['email'] )
    ffrom = settings_app.EMAIL_FROM
    addr = shib_dct['email']
    try:
        log.debug( 'about to send mail' )
        send_mail(
            subject, body, ffrom, [addr], fail_silently=True )
        log.debug( 'mail sent' )
    except Exception as e:
        log.error( 'exception sending mail, ```{}```'.format(unicode(repr(e))) )

    ## store message
    request.session['message'] = '{}\n---'.format( body )
    log.debug( 'session updated' )

    ## prep redirect
    message_redirect_url = reverse('article_request:message_url')
    log.debug( 'message_redirect_url, `%s`' % message_redirect_url )

    ## cleanup
    request.session['citation_json'] = ''

    ## build shib_logout() redirect url
    redirect_url = '{main_url}?{querystring}'.format(
        main_url=reverse('article_request:shib_logout_url'), querystring=request.META.get('QUERY_STRING', '').decode('utf-8') )
    log.debug( 'redirect_url, ```{}```'.format(redirect_url) )

    ## redirect
    return HttpResponseRedirect( redirect_url )
    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()
## openurl
"""
Two examples:
- article: 'rft.jtitle=Facial plastic surgery : FPS&rft.atitle=Anatomy for blepharoplasty and brow-lift.&rft.pages=177-85&rft.date=2010&rft.volume=26&rft.end_page=85&ctx_ver=Z39.88-2004&rft.genre=article'
- book: 'sid=FirstSearch%3AWorldCat&genre=book&isbn=9780688002305&title=Zen+and+the+art+of+motorcycle+maintenance%3A+an+inquiry+into+values%2C&date=1974&aulast=Pirsig&aufirst=Robert&auinitm=M&id=doi%3A&pid=673595%3Cfssessid%3E0%3C%2Ffssessid%3E&url_ver=Z39.88-2004&rfr_id=info%3Asid%2Ffirstsearch.oclc.org%3AWorldCat&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&req_dat=%3Csessionid%3E0%3C%2Fsessionid%3E&rfe_dat=%3Caccessionnumber%3E673595%3C%2Faccessionnumber%3E&rft_id=info%3Aoclcnum%2F673595&rft_id=urn%3AISBN%3A9780688002305&rft.aulast=Pirsig&rft.aufirst=Robert&rft.auinitm=M&rft.btitle=Zen+and+the+art+of+motorcycle+maintenance%3A+an+inquiry+into+values%2C&rft.date=1974&rft.isbn=9780688002305&rft.place=New+York&rft.pub=Morrow&rft.genre=book&checksum=8bf1504d891b0a2551ab879c3a555a8c&title=Brown University&linktype=openurl&detail=RBN'
"""
openurl = os.environ['ILLIAD_SAMPLE_SCRIPT__TEST_OPENURL']

## establish an ILLiad session
"""
The remote-auth url assumes the user is one that illiad knows about and is legit.
"""
ill = IlliadSession( REMOTE_AUTH_URL, AUTH_KEY, USERNAME )

## log user in
ill.login()

## get submission data
"""
The openurl is submitted via a GET request, and the response parsed to prepare the data for a submission POST.
"""
request_key = ill.get_request_key(openurl)

## submit request
submission_response_dct = ill.make_request(request_key)
pprint.pprint( submission_response_dct )
print submission_response_dct.get( 'transaction_number' )  # transaction-number on success
print submission_response_dct.get( 'message' )  # error-message on failure

## logout
ill.logout()
class AccountTest(unittest.TestCase):

    def setUp(self):
        self.ILLIAD_REMOTE_AUTH_URL = os.environ['ILLIAD_MODULE__TEST_REMOTE_AUTH_URL']
        self.ILLIAD_REMOTE_AUTH_KEY = os.environ['ILLIAD_MODULE__TEST_REMOTE_AUTH_KEY']
        self.ILLIAD_USERNAME = os.environ['ILLIAD_MODULE__TEST_USERNAME']
        self.PROJECT_DIR_PATH = os.environ['ILLIAD_MODULE__TEST_PATH']
        self._setup_path()
        from illiad.account import IlliadSession
        self.ill = IlliadSession(
            self.ILLIAD_REMOTE_AUTH_URL, self.ILLIAD_REMOTE_AUTH_KEY, self.ILLIAD_USERNAME )

    def tearDown(self):
        self.ill.logout()

    def _setup_path(self):
        """ Adds project dir path to sys path if necessary.
            Allows `from illiad.account...` to work
            Called by setUp() """
        if self.PROJECT_DIR_PATH not in sys.path:
            sys.path.append( self.PROJECT_DIR_PATH )
        return

    def test_login(self):
        login = self.ill.login()
        self.assertTrue(login.has_key('session_id'))
        self.assertTrue(login.has_key('authenticated'))
        self.assertTrue(login.has_key('registered'))
        self.assertTrue(login['authenticated'])

    ## submit_key tests ##

    def test_submit_key(self):
        """ Tests submit_key on article openurl. """
        ill = self.ill
        ill.login()
        #Url encoded
        openurl = "rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.spage=538&rft.issue=5&rft.date=2010-02-11&rft.volume=16&url_ver=Z39.88-2004&rft.atitle=Targeting+%CE%B17+Nicotinic+Acetylcholine+Receptors+in+the+Treatment+of+Schizophrenia.&rft.jtitle=Current+pharmaceutical+design&rft.issn=1381-6128&rft.genre=article"
        submit_key = ill.get_request_key(openurl)
        self.assertEqual(submit_key['ILLiadForm'],
                        'ArticleRequest')
        self.assertEqual(submit_key['PhotoJournalTitle'],
                        'Current pharmaceutical design')

    def test_book(self):
        """ Tests submit_key on simple book openurl. """
        ill = self.ill
        ill.login()
        openurl = "sid=FirstSearch:WorldCat&genre=book&isbn=9780231122375&title=Mahatma%20Gandhi%20%3A%20nonviolent%20power%20in%20action&date=2000&rft.genre=book"
        submit_key = ill.get_request_key(openurl)
        # print 'in test_book()...'; pprint.pprint( submit_key )
        self.assertEqual( 'LoanRequest', submit_key['ILLiadForm'] )
        self.assertEqual( 'Mahatma Gandhi : nonviolent power in action', submit_key['LoanTitle'] )
        self.assertEqual( 'LoanRequest', submit_key['ILLiadForm'] )
        self.assertEqual(
            ['CitedIn', 'ILLiadForm', 'ISSN', 'LoanDate', 'LoanTitle', 'NotWantedAfter', 'SearchType', 'SessionID', 'SubmitButton', 'Username', 'blocked', 'errors'],
            sorted(submit_key.keys()) )

    def test_book_with_long_openurl(self):
        """ Tests submit_key on long book openurl. """
        ill = self.ill
        ill.login()
        openurl = 'sid=FirstSearch%3AWorldCat&genre=book&isbn=9784883195732&title=Shin+kanzen+masuta%CC%84.+Nihongo+no%CC%84ryoku+shiken&date=2011&aulast=Fukuoka&aufirst=Rieko&id=doi%3A&pid=858811926%3Cfssessid%3E0%3C%2Ffssessid%3E%3Cedition%3EShohan.%3C%2Fedition%3E&url_ver=Z39.88-2004&rfr_id=info%3Asid%2Ffirstsearch.oclc.org%3AWorldCat&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&req_dat=%3Csessionid%3E0%3C%2Fsessionid%3E&rfe_dat=%3Caccessionnumber%3E858811926%3C%2Faccessionnumber%3E&rft_id=info%3Aoclcnum%2F858811926&rft_id=urn%3AISBN%3A9784883195732&rft.aulast=Fukuoka&rft.aufirst=Rieko&rft.btitle=Shin+kanzen+masuta%CC%84.+Nihongo+no%CC%84ryoku+shiken&rft.date=2011&rft.isbn=9784883195732&rft.place=To%CC%84kyo%CC%84&rft.pub=Suri%CC%84e%CC%84+Nettowa%CC%84ku&rft.edition=Shohan.&rft.genre=book'
        submit_key = ill.get_request_key( openurl )
        # print 'in test_book_returning_no_ILLiadForm()...'; pprint.pprint( submit_key )
        self.assertEqual(
            'LoanRequest', submit_key['ILLiadForm'] )
        self.assertEqual(
            ['CitedIn', 'ESPNumber', 'ILLiadForm', 'ISSN', 'LoanAuthor', 'LoanDate', 'LoanEdition', 'LoanPlace', 'LoanPublisher', 'LoanTitle', 'NotWantedAfter', 'SearchType', 'SessionID', 'SubmitButton', 'Username', 'blocked', 'errors'],
            sorted(submit_key.keys()) )

    def test_bookitem(self):
        """ Tests submit_key on genre=bookitem openurl. """
        ill = self.ill
        ill.login()
        openurl = 'url_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=bookitem&rft.btitle=Current%20Protocols%20in%20Immunology&rft.atitle=Isolation%20and%20Functional%20Analysis%20of%20Neutrophils&rft.date=2001-05-01&rft.isbn=9780471142737&rfr_id=info%3Asid%2Fwiley.com%3AOnlineLibrary'
        submit_key = ill.get_request_key( openurl )
        # print 'in test...'; pprint.pprint( submit_key )
        self.assertEqual(
            'BookChapterRequest', submit_key['ILLiadForm'] )
        self.assertEqual(
            ['CitedIn', 'ILLiadForm', 'ISSN', 'NotWantedAfter', 'PhotoArticleTitle', 'PhotoJournalInclusivePages', 'PhotoJournalTitle', 'PhotoJournalYear', 'SearchType', 'SessionID', 'SubmitButton', 'Username', 'blocked', 'errors'],
            sorted(submit_key.keys()) )

    def test_tiny_openurl(self):
        """ Tests submit_key on painfully minimalist openurl. """
        ill = self.ill
        ill.login()
        openurl = 'sid=Entrez:PubMed&id=pmid:23671965'
        submit_key = ill.get_request_key( openurl )
        self.assertEqual(
            'LoanRequest', submit_key['ILLiadForm'] )
        self.assertEqual(
            ['CitedIn', 'ILLiadForm', 'LoanDate', 'LoanTitle', 'NotWantedAfter', 'Notes', 'SearchType', 'SessionID', 'SubmitButton', 'Username', 'blocked', 'errors'],
            sorted(submit_key.keys()) )
        self.assertEqual(
            'entire openurl: `sid=Entrez:PubMed&id=pmid:23671965`', submit_key['Notes'] )

    def test_logout(self):
        """ Tests logout. """
        logout = self.ill.logout()
        self.assertTrue(logout.has_key('authenticated'))
        self.assertFalse(logout['authenticated'])