source: exist/trunk/python/ndgUtils/models/People2.py @ 4487

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/ndgUtils/models/People2.py@4487
Revision 4487, 6.7 KB checked in by cbyrom, 12 years ago (diff)

Further restructure of codebase - moving datamodels into ndgUtils to allow reuse + create visualise and browse sub structures to better
organise related code. Separate out some inner classes into their own
modules to make more visible.

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