source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/People2.py @ 3501

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/People2.py@3501
Revision 3501, 7.5 KB checked in by cbyrom, 12 years ago (diff)

Update ows_server code to use new ndgUtils module - and remove files that have
been migrated to this module.

Line 
1from Utilities import wrapGetText
2from ndgUtils.ETxmlView import nsdumb
3from htmlUtilities import *
4from xmlHandler import xmlHandler
5
6
7def tr(x):return '<tr>%s</tr>'%x
8def td(x,c=1):return '<td colspan="%s">%s</td>'%(c,x)
9tde='<td></td>'
10
11class Name:
12    ''' Handles standar formatting of a name, for MOLES! '''
13    def __init__(self,helper,elem):
14        self.name=''
15        if elem is None: return 
16        if helper.getText(elem,'nameOrder')=='L2R':
17            o=('title','initials','familyName')
18        else:
19            o=('title','familyName','personalName')
20        for i in o: self.name+=' '+helper.getText(elem,i)
21    def __str__(self):
22        return self.name
23       
24class holder:
25    def __init__(self,model):
26        self.data=model
27        self.empty=1
28    def __getitem__(self,item):
29        return self.data[item]
30    def __setitem__(self,item,val):
31        self.data[item]=val
32        self.empty=0
33    def __str__(self):
34        return str(self.data)
35       
36       
37class Contact(holder):
38    ''' A simple information model and html representation of a contact '''
39    def __init__(self,helper,elem,keys,name=''):
40        model={'name':None,'email':'','phone':'','fax':'','address':[],'role':''}
41        holder.__init__(self,model)
42        if name=='':
43            for k in model:
44                if model[k]=='': 
45                    self[k]=helper.getText(elem,keys[k])
46        else:
47            self['name']=name
48    def toHTML(self):
49        print self
50        if self.empty: return 'Unknown'
51        h=''
52        r=self['role']
53        if r<>'':r+=': '
54        if self['name'] is not None: h+='%s%s<br/>'%(r,self['name'])
55        if self['email']<>'': h+='email:%s<br/>'%self['email']
56        if self['phone']<>'' or self['fax']<>'':
57            h+='Phone:%s, Fax:%s<br/>'%(self['phone'],self['fax'])
58        for i in self['address']: h+=i+'<br/>'
59        return h
60    def url(self):
61        return 'url to a person?'
62
63class Organisation(holder):
64    ''' A simple information modle and html representation of an organisation '''
65    def __init__(self,helper,elem,keys):
66        model={'sname':'','lname':'','address':[],'person':None,'url':''}
67        holder.__init__(self,model)
68        for k in model:
69            if model[k]=='': 
70                self[k]=helper.getText(elem,keys[k])
71                print k,keys[k],self[k],elem.tag
72    def toHTML(self):
73        print self
74        if self.empty: return 'Unknown'
75        h=''
76        if self['lname']=='' and self['sname']=='':
77            return 'Unknown'
78        if self['sname']=='':
79            if self['url']<>'':
80                h+=hyperlink(self['lname'],self['url'])+'<br/>'
81            else: h+=self['lname']+'<br/>'
82        else:
83            u=self.url()
84            if self['lname']<>'':
85                h+='%s (%s)<br/>'%(self['lname'],u)
86            else: h+=u+'<br/>'
87        for i in self['address']: h+=i+'<br/'
88        if self['person'] is not None:
89            h+=self['person'].toHTML()
90        return h
91    def url(self):
92        ''' Return hyperlink to centre ''' 
93        if self['url']<>'':
94                if self['url'][0:4]<>'http': self['url']='http://%s'%self['url']
95                u=hyperlink(self['sname'],self['url'])
96        else: u=self['sname']
97        return u
98
99class dgContact(Contact):
100    '''A simple summary of contact information for an organisation or a person '''
101    def __init__(self,elem,helper=None):
102        ''' Instantiate a MOLES contact element '''
103        self.elem=element
104        if helper is None: helper=nsdumb()
105        keys={'email':'contactDetails/eMail','phone':'contactDetails/telephone'}
106        contact.__init__(self,helper,elem,keys)
107               
108class DIFcontact:
109    def __init__(self,helper,elem,name=''):
110        okeys={'sname':'Data_Center_Name/Short_Name','lname':'Data_Center_Name/Long_Name',
111                'url':'Data_Center_URL'}
112        pkeys={'role':'Role','name':None,'phone':'Phone','fax':'Fax','address':None,'email':'Email'}
113        if name=='':
114            if elem is None:
115                self.core=None
116                return
117        else:
118            self.core=Contact(0,0,0,name=name)
119            return
120        if helper.strip(elem.tag)=='Data_Center':
121            self.core=Organisation(helper,elem,okeys)
122            p=helper.find(elem,'Personnel')
123            self.core['person']=DIFcontact(helper,p)
124        elif helper.strip(elem.tag)=='Personnel':
125            self.core=Contact(helper,elem,pkeys)
126            a=[]
127            # we don't need the helper because we're getting the tags from the element.
128            # should we worry about empty elements here? yes!
129            for t in (helper.find(elem,'Contact_Address') or []):
130                if t.text is not None: a.append(t.text)
131            self.core['address']=a
132            self.core['name']=self.__handleName(
133                    helper.getText(elem,'First_Name'),helper.getText(elem,'Last_Name'))
134        else:
135            raise ValueError('DIF contact type not understood [%s]'%elem.tag)
136    def toHTML(self):
137        if self.core is None: 
138            return 'Not Found'
139        else: return self.core.toHTML()
140    def url(self):
141        if self.core is None:
142            return 'Not Found'
143        else: return self.core.url()
144    def __handleName(self,f,l):
145        ''' Sort out first name last name handling '''
146        if f=='': return l
147        return '%s, %s'%(l,f)
148       
149def getAuthors(dclist):
150    ''' Parse a list of DIFcontacts and get any "author" types out '''
151    authorTypes=['Investigator','Dataset_Creator','Dataset_Author']
152    authors=[]
153    for a in dclist:
154        if a.core['role'] in authorTypes:authors.append(a)
155    return authors
156       
157import unittest
158Example='''
159<Data_Center> 
160<Data_Center_Name> 
161<Short_Name>BODC</Short_Name>
162<Long_Name>British Oceanographic Data Centre</Long_Name>
163</Data_Center_Name>
164<Data_Center_URL>http://www.bodc.ac.uk</Data_Center_URL>
165<Personnel> 
166<Role>Data Center Contact</Role>
167<Last_Name>British Oceanographic Data Centre</Last_Name>
168<Phone>+44 (0)151 795 4884</Phone>
169<Fax>+44 (0)151 795 4912</Fax>
170<Contact_Address> 
171<Address>Joseph Proudman Building</Address>
172<Address>6 Brownlow Street</Address>
173<City>UKPT0811</City>
174<Postal_Code>L3 5DA</Postal_Code>
175<Country>74</Country>
176</Contact_Address>
177</Personnel>
178</Data_Center>
179'''
180Example2='''
181<Data_Center>
182<Data_Center_Name>
183<Short_Name>BODC</Short_Name>
184<Long_Name>British Oceanographic Data Centre</Long_Name>
185</Data_Center_Name>
186<Data_Center_URL>http://www.bodc.ac.uk/</Data_Center_URL>
187<Personnel>
188<Role>BODC Enquiries Officer</Role>
189<First_Name></First_Name>
190<Last_Name></Last_Name>
191<Email>enquiries@bodc.ac.uk</Email>
192<Phone>+44 (0) 151 795 4884</Phone>
193<Fax>+44 (0) 151 795 4912</Fax>
194<Contact_Address>
195<Address>6 Brownlow Street</Address>
196<City>Liverpool</City>
197<Province_or_State>Merseyside</Province_or_State>
198<Postal_Code>L3 5DA</Postal_Code>
199<Country>United Kingdom</Country>
200</Contact_Address>
201</Personnel>
202</Data_Center>'''
203
204
205class TestCase(unittest.TestCase):
206   
207    def testDIFContact(self):
208        '''Test instantiating a DIFContact'''
209        w='<DIF xmlns="http://gcmd.gsfc.nasa.gov/Aboutus/xml/dif/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">%s</DIF>'%Example2
210        #w='<wrap>%s</wrap>'%Example2
211        y=xmlHandler(w,string=1)
212        h=nsdumb(y.tree)
213        e=h.find(y.tree,'Data_Center')
214        d=DIFcontact(h,e)
215        print d.toHTML()
216   
217    def testDIFname(self):
218        '''Make sure we can create a contact, with just a name '''
219        x=DIFcontact(0,0,name='Bryan Lawrence')
220        print x.toHTML()
221   
222if __name__=="__main__":
223    unittest.main()         
Note: See TracBrowser for help on using the repository browser.