def test_creating_new_contact_and_saving_it_to_the_database(self):
     # Test creating a new event object.
     user = User.objects.create(username="******")
     country = Country.objects.create(name ='Nowhere')
     region = Region.objects.create(region_name="How cares", country=country)
     city = City.objects.create(city_name='WonderLand', country=country, region_name=region)
     contact = Contact()
     contact.user = user
     contact.fr_name = "John"
     contact.ls_name = "Doe"
     contact.m_name = ""
     contact.email = ""
     contact.overview = ""
     contact.photo_profile = ""
     contact.phone = ""
     contact.country = country
     contact.city = city
     contact.degrees = ""
     contact.lt_contact = ""
     contact.tags = ""
     contact.company = None
     contact.latech_contact = ""
     contact.financial_organization = ""
     contact.government_organization = ""
     contact.title = ""
     contact.industry = ""
     contact.technology = ""
     contact.application = ""
     # Testing __unicode__ method
     self.assertEquals(unicode(contact.fr_name +" "+contact.ls_name), 'John Doe')
 def readData(self, filePath):
     data = csv.DictReader(open(filePath , encoding='utf-8'))
     count = 0
     print('reading: %s' % filePath)
     for row in data:
         count += 1
         sys.stdout.write('.')
         sys.stdout.flush()
         contact = Contact()
         contact.given_name = row['GivenName']
         contact.surname = row['Surname']
         contact.street_address = row['StreetAddress']
         contact.city = row['City']
         contact.zip_code = row['ZipCode']
         contact.country = row['Country']
         contact.email_address = row['EmailAddress']
         contact.telephone_number = row['TelephoneNumber']
         contact.save()
     print('\nready: %d contacts read' % count)
Exemple #3
0
def view_contact_import(request):
    form = ImportContactsForm()
    message = ''
    errorList = []
    if request.method == 'POST':
        form = ImportContactsForm(request.POST,request.FILES)
        my_file = request.FILES['file']
        if form.is_valid():
            with open('/var/www/yellowpage/media/xls/'+my_file.name, 'wb+') as destination:
                for chunk in my_file.chunks():
                    destination.write(chunk)
            xls = xlrd.open_workbook('/var/www/yellowpage/media/xls/'+my_file.name)
            sheet = xls.sheet_by_index(0)
            completed = 0
            for row_index in range(1, sheet.nrows):
                try:
                    code = sheet.cell_value(row_index,0).capitalize()
                    #if str(code) == "":
                    #    
                    contact = Contact(code=code)
                    contact.surname = sheet.cell_value(row_index,1).capitalize()
                    contact.name = sheet.cell_value(row_index,2).capitalize()
                    contact.street = sheet.cell_value(row_index,3).capitalize()
                    contact.zip = str(sheet.cell_value(row_index,4))
                    contact.city = sheet.cell_value(row_index,5).capitalize()
                    contact.province = Province.objects.get(code=sheet.cell_value(row_index,6))
                    contact.email = sheet.cell_value(row_index,8)
                    contact.phone_number = sheet.cell_value(row_index, 7)
                    contact.birthdate = str(sheet.cell_value(row_index,12))
                    #Settore
                    sectorString = str(sheet.cell_value(row_index, 10))
                    professionString = str(sheet.cell_value(row_index, 9))
                    profession = Work.objects.filter(name=professionString)
                    if not profession:
                        sector = Sector.objects.filter(name=sectorString)
                        if not sector:
                            sector = Sector(name=sectorString)
                            sector.save()
                        #Professione
                        profession = Work(name=professionString, sector=Sector.objects.get(pk=sector.pk))
                        profession.save()
                    else:
                        profession = profession[0]
                    contact.work = Work.objects.get(name=profession.name)
                    contact.save()
                    completed += 1
                    #Certificato
                    cabinet = Cabinet.objects.get(pk=ContactCertFile.CABINET_ID)
                    current_user = request.user
                    file = ContactCertFile(contact=Contact.objects.get(pk=contact.pk))
                    uploadedFile = UploadedFile(title="Certificato Vuoto", cabinet=cabinet, file_ref="/cabinet/cert_empty.pdf", owner=current_user)
                    uploadedFile.save()
                    file.file = uploadedFile
                    file.expiry = str(sheet.cell_value(row_index,11))
                    file.save()
                except Exception as e:
                    print '%s (%s)' % (e.message, type(e))
                    errorList.append(sheet.cell_value(row_index,0).capitalize() + " " + e.message)
            message = 'Report Import: %d contatti importati correttamente. ' % completed

    return render_to_response('admin/contacts/view_contact_import.html',{'form':form, 'message':message, 'errorList': errorList}, context_instance=RequestContext(request))