Exemplo n.º 1
0
    def convertToContact(self, c):
        """ Convert a Contact object to a vCard object """
        con = Contact(name=c.fn.value)
        flagaddress = True
        for el in c.getSortedChildren():
            #if len(el.params.keys()) > 0:
            #print el.name, el.params[u'TYPE'], el.value
            #else:
            #print el.name, el.value

            if el.name == 'N':
                con.addValue(ContactField('last_name', 'none'),
                             unicode(c.n.value.family))
                con.addValue(ContactField('first_name', 'none'),
                             unicode(c.n.value.given))
            elif el.name == 'ROLE':
                con.addValue(ContactField('job_title', 'none'),
                             unicode(el.value))
            elif el.name == 'ORG':
                for val in el.value:
                    con.addValue(ContactField('company_name', 'none'),
                                 unicode(val))
            elif el.name == 'BDAY':
                con.addValue(ContactField('date', 'none'), unicode(el.value))
            elif el.name == 'URL':
                con.addValue(ContactField('url', 'none'), unicode(el.value))
            elif el.name == 'TEL':
                if len(el.params.keys()) == 0:
                    #print 'tel none'
                    con.addValue(ContactField('phone_number', 'none'),
                                 unicode(el.value))
                else:
                    params = el.params[u'TYPE']
                    if len(params) == 1:
                        if u'HOME' in params:
                            #print 'tel home'
                            con.addValue(ContactField('phone_number', 'home'),
                                         unicode(el.value))
                        elif u'WORK' in params:
                            #print 'tel work'
                            con.addValue(ContactField('phone_number', 'work'),
                                         unicode(el.value))
                        elif u'CELL' in params:
                            #print 'cell none'
                            con.addValue(ContactField('mobile_number', 'none'),
                                         unicode(el.value))
                        elif u'FAX' in params:
                            #print 'fax none'
                            con.addValue(ContactField('fax_number', 'none'),
                                         unicode(el.value))
                    elif len(params) == 2:
                        if u'HOME' in params and u'CELL' in params:
                            #print 'cell home'
                            con.addValue(ContactField('mobile_number', 'home'),
                                         unicode(el.value))
                        elif u'HOME' in params and u'FAX' in params:
                            #print 'fax home'
                            con.addValue(ContactField('fax_number', 'home'),
                                         unicode(el.value))
                        elif u'WORK' in params and u'CELL' in params:
                            #print 'cell work'
                            con.addValue(ContactField('mobile_number', 'work'),
                                         unicode(el.value))
                        elif u'WORK' in params and u'FAX' in params:
                            #print 'fax work'
                            con.addValue(ContactField('fax_number', 'work'),
                                         unicode(el.value))
            elif el.name == 'EMAIL':
                if len(el.params.keys()) == 0:
                    con.addValue(ContactField('email_address', 'none'),
                                 unicode(el.value))
                else:
                    param = el.params[u'TYPE'][0]
                    con.addValue(ContactField('email_address', param.lower()),
                                 unicode(el.value))
            elif el.name == 'ADR' and flagaddress:
                for adr in c.adr_list:
                    if len(adr.params.keys()) > 0:
                        location = unicode(adr.params[u'TYPE'][0].lower())
                    else:
                        location = u'none'
                    [po_box, extended_address, street_address,
                    city, state, postal_code, country] = \
                            adr.serialize().split(':')[1].split(';')
                    #print po_box, extended_address, street_address, \
                    #city, state, postal_code, country
                    if street_address.strip() != '':
                        con.addValue(ContactField('street_address', location),
                                     unicode(street_address.strip(), 'utf-8'))
                    if state.strip() != '':
                        con.addValue(ContactField('state', location),
                                     unicode(state.strip(), 'utf-8'))
                    if postal_code.strip() != '':
                        con.addValue(ContactField('postal_code', location),
                                     unicode(postal_code.strip(), 'utf-8'))
                    if po_box.strip() != '':
                        con.addValue(ContactField('po_box', location),
                                     unicode(po_box.strip(), 'utf-8'))
                    if extended_address.strip() != '':
                        con.addValue(
                            ContactField('extended_address', location),
                            unicode(extended_address.strip(), 'utf-8'))
                    if country.strip() != '':
                        con.addValue(ContactField('country', location),
                                     unicode(country.strip(), 'utf-8'))
                    if city.strip() != '':
                        con.addValue(ContactField('city', location),
                                     unicode(city.strip(), 'utf-8'))
                flagaddress = False
            #else:
            #if len(el.params.keys()) > 0:
            #print el.name, el.params[u'TYPE'], el.value
            #else:
            #print el.name, el.value
        return con
Exemplo n.º 2
0
    def convertToContact(self, entry):
        """ Convert a ldif object to a Contact object """
        con = Contact( name = unicode(entry['cn'][0], 'utf-8') )

        for el in entry.keys():
            if el == 'sn':
                for val in entry[el]:
                    con.addValue(ContactField('last_name', 'none'),
                        unicode(val, 'utf-8'))
            elif el == 'givenName':
                for val in entry[el]:
                    con.addValue(ContactField('first_name', 'none'),
                        unicode(val, 'utf-8'))
            elif el == 'title':
                for val in entry[el]:
                    con.addValue(ContactField('job_title', 'none'),
                        unicode(val, 'utf-8'))
            elif el == 'o':
                for val in entry[el]:
                    con.addValue(ContactField('company_name', 'none'),
                        unicode(val, 'utf-8'))
            elif el == 'telephoneNumber':
                for val in entry[el]:
                    con.addValue(ContactField('phone_number', 'none'),
                        unicode(val))
            elif el == 'homePhone':
                for val in entry[el]:
                    con.addValue(ContactField('phone_number', 'home'),
                        unicode(val))
            elif el == 'mobile':
                for val in entry[el]:
                    con.addValue(ContactField('mobile_number', 'none'),
                        unicode(val))
            elif el == 'mail':
                for val in entry[el]:
                    con.addValue(ContactField('email_address', 'none'),
                        unicode(val))
            elif el == 'mozillaSecondEmail':
                for val in entry[el]:
                    con.addValue(ContactField('email_address', 'none'),
                        unicode(val))
            elif el == 'mozillaSecondEmail':
                for val in entry[el]:
                    con.addValue(ContactField('email_address', 'none'),
                        unicode(val))
            elif el == 'mozillaHomeUrl':
                for val in entry[el]:
                    con.addValue(ContactField('url', 'home'),
                        unicode(val))
            elif el == 'mozillaWorkUrl':
                for val in entry[el]:
                    con.addValue(ContactField('url', 'work'),
                        unicode(val))
            ## Home address
            elif el == 'mozillaHomeStreet':
                for val in entry[el]:
                    con.addValue(ContactField('street_address', 'home'),
                        unicode(val, 'utf-8'))
            elif el == 'mozillaHomeLocalityName':
                for val in entry[el]:
                    con.addValue(ContactField('city', 'home'),
                        unicode(val, 'utf-8'))
            elif el == 'mozillaHomeCountryName':
                for val in entry[el]:
                    con.addValue(ContactField('country', 'home'),
                        unicode(val, 'utf-8'))
            elif el == 'mozillaHomePostalCode':
                for val in entry[el]:
                    con.addValue(ContactField('postal_code', 'home'),
                        unicode(val))
            elif el == 'mozillaHomeState':
                for val in entry[el]:
                    con.addValue(ContactField('state', 'home'),
                        unicode(val, 'utf-8'))
            ## Work Address
            elif el == 'postalCode':
                for val in entry[el]:
                    con.addValue(ContactField('postal_code', 'work'),
                        unicode(val))
            elif el == 'street':
                for val in entry[el]:
                    con.addValue(ContactField('street_address', 'work'),
                        unicode(val, 'utf-8'))
            elif el == 'l':
                for val in entry[el]:
                    con.addValue(ContactField('city', 'work'),
                        unicode(val, 'utf-8'))
            elif el == 'st':
                for val in entry[el]:
                    con.addValue(ContactField('state', 'work'),
                        unicode(val, 'utf-8'))
            elif el == 'c':
                for val in entry[el]:
                    con.addValue(ContactField('country', 'work'),
                        unicode(val, 'utf-8'))
            #else:
                #print el, entry[el]
        # Add birthday
        if 'birthyear' in entry.keys() and 'birthmonth' in entry.keys() \
            and 'birthday' in entry.keys():
            date = entry['birthyear'][0] + entry['birthmonth'][0] + entry['birthday'][0]
            con.addValue(ContactField('date', 'none'),
                        unicode(date))
        return con
Exemplo n.º 3
0
    def convertToContact(self, c):
        """ Convert a Contact object to a vCard object """
        con = Contact(name = c.fn.value )
        flagaddress = True
        for el in c.getSortedChildren():
            #if len(el.params.keys()) > 0:
                #print el.name, el.params[u'TYPE'], el.value
            #else:
                #print el.name, el.value

            if el.name == 'N':
                con.addValue(ContactField('last_name', 'none'),
                        unicode(c.n.value.family))
                con.addValue(ContactField('first_name', 'none'),
                        unicode(c.n.value.given))
            elif el.name == 'ROLE':
                con.addValue(ContactField('job_title', 'none'),
                        unicode(el.value))
            elif el.name == 'ORG':
                for val in el.value:
                    con.addValue(ContactField('company_name', 'none'),
                        unicode(val))
            elif el.name == 'BDAY':
                con.addValue(ContactField('date', 'none'),
                        unicode(el.value))
            elif el.name == 'URL':
                con.addValue(ContactField('url', 'none'),
                        unicode(el.value))
            elif el.name == 'TEL':
                if len(el.params.keys()) == 0:
                    #print 'tel none'
                    con.addValue(ContactField('phone_number', 'none'),
                        unicode(el.value))
                else:
                    params = el.params[u'TYPE']
                    if len(params) == 1:
                        if u'HOME' in params:
                            #print 'tel home'
                            con.addValue(ContactField('phone_number', 'home'),
                                unicode(el.value))
                        elif u'WORK' in params:
                            #print 'tel work'
                            con.addValue(ContactField('phone_number', 'work'),
                                unicode(el.value))
                        elif u'CELL' in params:
                            #print 'cell none'
                            con.addValue(ContactField('mobile_number', 'none'),
                                unicode(el.value))
                        elif u'FAX' in params:
                            #print 'fax none'
                            con.addValue(ContactField('fax_number', 'none'),
                                unicode(el.value))
                    elif len(params) == 2:
                        if u'HOME' in params and u'CELL' in params:
                            #print 'cell home'
                            con.addValue(
                                ContactField('mobile_number', 'home'),
                                unicode(el.value)
                            )
                        elif u'HOME' in params and u'FAX' in params:
                            #print 'fax home'
                            con.addValue(
                                ContactField('fax_number', 'home'),
                                unicode(el.value)
                            )
                        elif u'WORK' in params and u'CELL' in params:
                            #print 'cell work'
                            con.addValue(
                                ContactField('mobile_number', 'work'),
                                unicode(el.value)
                            )
                        elif u'WORK' in params and u'FAX' in params:
                            #print 'fax work'
                            con.addValue(
                                ContactField('fax_number', 'work'),
                                unicode(el.value)
                            )
            elif el.name == 'EMAIL':
                if len(el.params.keys()) == 0:
                    con.addValue(ContactField('email_address', 'none'),
                        unicode(el.value))
                else:
                    param = el.params[u'TYPE'][0]
                    con.addValue(ContactField('email_address', param.lower()),
                        unicode(el.value))
            elif el.name == 'ADR' and flagaddress:
                for adr in c.adr_list:
                    if len(adr.params.keys()) > 0:
                        location = unicode(adr.params[u'TYPE'][0].lower())
                    else:
                        location = u'none'
                    [po_box, extended_address, street_address,
                    city, state, postal_code, country] = \
                            adr.serialize().split(':')[1].split(';')
                    #print po_box, extended_address, street_address, \
                    #city, state, postal_code, country
                    if street_address.strip() != '':
                        con.addValue(ContactField('street_address', location),
                                    unicode(street_address.strip(), 'utf-8'))
                    if state.strip() != '':
                        con.addValue(ContactField('state', location),
                                    unicode(state.strip(), 'utf-8'))
                    if postal_code.strip() != '':
                        con.addValue(ContactField('postal_code', location),
                                    unicode(postal_code.strip(), 'utf-8'))
                    if po_box.strip() != '':
                        con.addValue(ContactField('po_box', location),
                                    unicode(po_box.strip(), 'utf-8'))
                    if extended_address.strip() != '':
                        con.addValue(ContactField('extended_address', location),
                                    unicode(extended_address.strip(), 'utf-8'))
                    if country.strip() != '':
                        con.addValue(ContactField('country', location),
                                    unicode(country.strip(), 'utf-8'))
                    if city.strip() != '':
                        con.addValue(ContactField('city', location),
                                    unicode(city.strip(), 'utf-8'))
                flagaddress = False
            #else:
                #if len(el.params.keys()) > 0:
                    #print el.name, el.params[u'TYPE'], el.value
                #else:
                    #print el.name, el.value
        return con