]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/python/oils/utils/utils.py
Bring Evergreen Python code in sync with OpenSRF python.
[Evergreen.git] / Open-ILS / src / python / oils / utils / utils.py
1 # -----------------------------------------------------------------------
2 # Copyright (C) 2007  Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # -----------------------------------------------------------------------
15
16 import re, md5
17 from osrf.ses import AtomicRequest
18 from osrf.log import *
19
20
21
22 # -----------------------------------------------------------------------
23 # Grab-bag of general utility functions
24 # -----------------------------------------------------------------------
25
26 def isEvent(evt):
27     return (evt and isinstance(evt, dict) and evt.get('ilsevent') != None)
28
29 def eventCode(evt):
30     if isEvent(evt):
31         return evt['ilsevent']
32     return None
33
34 def eventText(evt):
35     if isEvent(evt):
36         return evt['textcode']
37     return None
38
39       
40 def md5sum(str):
41     m = md5.new()
42     m.update(str)
43     return m.hexdigest()
44
45 def unique(arr):
46     ''' Unique-ify a list.  only works if list items are hashable '''
47     o = {}
48     for x in arr:
49         o[x] = 1
50     return o.keys()
51
52
53 def login(username, password, type=None, workstation=None):
54     ''' Login to the server and get back an authtoken'''
55
56     log_info("attempting login with user " + username)
57
58     seed = AtomicRequest(
59         'open-ils.auth', 
60         'open-ils.auth.authenticate.init', username)
61
62     # generate the hashed password
63     password = md5sum(seed + md5sum(password))
64
65     return AtomicRequest(
66         'open-ils.auth',
67         'open-ils.auth.authenticate.complete',
68         {   'workstation' : workstation,
69             'username' : username,
70             'password' : password,
71             'type' : type
72         }
73     )
74