コード例 #1
0
 def obj_create(self, bundle, **kwargs):
     try:
         data = bundle.data
         user = bundle.request.user
         if user.is_anonymous():
             user = None
         if len(contact_is_valid(bundle)) > 0:
             raise BadRequest(contact_is_valid(bundle))
         print "user2 %s" % user
         data['dob'] = data['dob'] or None if 'dob' in data.keys(
         ) else None  # Empty string no good
         agencyId = data['agencyId']
         del data['agencyId']
         emails = [
             EmailAddress.get_or_create(email)[0]
             for email in data['emails'] if email
         ]
         map(lambda x: x.save(), emails)
         notes = [Note(content=data['notes'], user=user)]
         del data['notes']
         map(lambda x: x.save(), notes)
         titles = [
             Title(content=title) for title in data['titles'] if title
         ]
         del data['titles']
         map(lambda x: x.save(), titles)
         del data['emails']
         phone_numbers = [
             Phone(content=phone) for phone in data['phone_numbers']
             if phone
         ]
         del data['phone_numbers']
         map(lambda x: x.save(), phone_numbers)
         addresses = [
             Address(content=address) for address in data['addresses']
             if address
         ]
         map(lambda x: x.save(), addresses)
         del data['addresses']
         agency = Agency.objects.get(id=agencyId)
         thecontact = Contact(**data)
         thecontact.save()
         thecontact.titles = titles
         thecontact.emails = emails
         thecontact.phone_numbers = phone_numbers
         thecontact.addresses = addresses
         thecontact.notes = notes
         thecontact.agency_related_contacts = [agency]
         thecontact.creator = user
         thecontact.save()
         agency.save()
         bundle.obj = thecontact
         return bundle
     except Exception as e:
         logger.exception(e)
         raise BadRequest(str(e))
コード例 #2
0
def create_contact(sh, row, govt):
    '''
    put agency name and note together as its name
    '''
    try:
        agency_name = sh.cell(row, 0).value.strip()
        #note = sh.cell(row, 1).value.strip()
        #agency_name = agency_name + '/' + note
        name = sh.cell(row, 1).value.strip().split(' ')
        fname, middle, lname = ('', '', '')
        if len(name) == 1:
            fname = name[0]
        elif len(name) == 2:
            fname = name[0]
            lname = name[1]
        elif len(name) > 2:
            fname = name[0]
            middle = name[1]
            lname = name[2]
        title = sh.cell(row, 2).value.strip()
    except Exception as e:
        print e
        import pdb;pdb.set_trace()
    
    email = sh.cell(row, 10).value.strip().split('mailto:')

    agency, created = Agency.objects.get_or_create(name=agency_name, government=govt)
    #i wish each contact had an agency field because this is a good way to associate the wrong contact with an agency
    #contact, created = Contact.objects.get_or_create(first_name=fname, middle_name=middle,
    #    last_name=lname)
    contact = Contact(first_name=fname, middle_name=middle, last_name=lname)
    logger.info('created %s %s %s' % (fname, middle, lname))
    contact.save()
    agency.contacts.add(contact)
    #contact.add_note(note)
    contact.add_title(title)
    #contact.add_address(address)
    #contact.add_phone(phone)
    if len(email) > 1:
        mail = email[1]
    else:
        mail = email[0]
    contact.add_email(mail)
    try:
        tags = str(sh.cell(row, 13).value).split(',')
        for tag in tags:
            tag = tag.strip()
            if tag != '':
                print 'tag added %s' % tag
                agency.tags.add(tag)
    except Exception as e:
        print '%s' % (e)