def test10_UserCertFile(self): ''' Adding a certificate ''' self.assertIsNotNone(self.certfile.date_created, "Expected UploadedFile.date_created to be populated.") self.assertEqual(self.certfile.cabinet_id, 2, "Expected cabinet to be %s but got %s" % (self.certfile.cabinet, self.cabinet_file.pk)) user_cert = ContactCertFile( contact=self.contact, file=self.certfile, expiry=datetime.date(2012,7,10) ) user_cert.save() self.assertEqual(user_cert.contact_id, self.contact.pk, "Expected user_id to be %s but got %s" % (self.contact.pk, user_cert.contact_id)) self.assertFalse(user_cert.is_valid, "Expected certificate not to be valid.")
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))
def test12_userCertFile_expiry(self): ''' Check certificate is valid logic and duration ''' today = datetime.date.today() # Set certificate to expire in 20 days, and should be valid expiry = today + datetime.timedelta(20) user_cert = ContactCertFile( contact=self.contact, file=self.certfile, expiry=expiry ) user_cert.save() self.assertTrue(user_cert.is_valid, "Expected certificate with '%s' expiry to be valid." % expiry) self.assertEqual(user_cert.duration, 0, "Expected expiry '%s' to result in duration of '0' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "1y-", "Expected expiry '%s' to result in duration of '1y-' but got '%s'" % (expiry,user_cert.duration_code)) # Set certificate to expire today, and should be valid user_cert.expiry = today user_cert.save() self.assertTrue(user_cert.is_valid, "Expected certificate to be valid on the expiry day.") self.assertEqual(user_cert.duration, 0, "Expected expiry '%s' to result in duration of '0' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "1y-", "Expected expiry '%s' to result in duration of '1y-' but got '%s'" % (expiry,user_cert.duration_code)) # Set the expiry to 10 days ago, and should be invalid expiry = today - datetime.timedelta(10) user_cert.expiry = expiry user_cert.save() self.assertFalse(user_cert.is_valid, "Expected certificate with '%s' expiry NOT to be valid." % expiry) self.assertEqual(user_cert.duration, -1, "Expected expiry '%s' to result in duration of '-1' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "expired", "Expected expiry '%s' to result in duration of 'expired' but got '%s'" % (expiry,user_cert.duration_code)) # Set the expiry to 1 year from now, and should be invalid expiry = today + datetime.timedelta(365 + 10) user_cert.expiry = expiry user_cert.save() self.assertTrue(user_cert.is_valid, "Expected certificate with '%s' expiry be valid." % expiry) self.assertEqual(user_cert.duration, 1, "Expected expiry '%s' to result in duration of '1' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "1y-", "Expected expiry '%s' to result in duration of '1y-' but got '%s'" % (expiry,user_cert.duration_code)) # Set the expiry to 2 years from ago, and should be invalid expiry = today + datetime.timedelta(365 * 2 + 10) user_cert.expiry = expiry user_cert.save() self.assertTrue(user_cert.is_valid, "Expected certificate with '%s' expiry be valid." % expiry) self.assertEqual(user_cert.duration, 2, "Expected expiry '%s' to result in duration of '2' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "2y", "Expected expiry '%s' to result in duration of '2y' but got '%s'" % (expiry,user_cert.duration_code)) # Set the expiry to 3 years from ago, and should be invalid expiry = today + datetime.timedelta(365 * 3 + 10) user_cert.expiry = expiry user_cert.save() self.assertTrue(user_cert.is_valid, "Expected certificate with '%s' expiry be valid." % expiry) self.assertEqual(user_cert.duration, 3, "Expected expiry '%s' to result in duration of '3' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "3y", "Expected expiry '%s' to result in duration of '3y' but got '%s'" % (expiry,user_cert.duration_code)) # Set the expiry to 4 years from ago, and should be invalid expiry = today + datetime.timedelta(365 * 4 + 10) user_cert.expiry = expiry user_cert.save() self.assertTrue(user_cert.is_valid, "Expected certificate with '%s' expiry be valid." % expiry) self.assertEqual(user_cert.duration, 4, "Expected expiry '%s' to result in duration of '4' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "4y+", "Expected expiry '%s' to result in duration of '4y+' but got '%s'" % (expiry,user_cert.duration_code)) # Set the expiry to 7 years from ago, and should be invalid expiry = today + datetime.timedelta(365 * 7 + 10) user_cert.expiry = expiry user_cert.save() self.assertTrue(user_cert.is_valid, "Expected certificate with '%s' expiry be valid." % expiry) self.assertEqual(user_cert.duration, 7, "Expected expiry '%s' to result in duration of '7' but got '%s'" % (expiry,user_cert.duration)) self.assertEqual(user_cert.duration_code, "4y+", "Expected expiry '%s' to result in duration of '4y+' but got '%s'" % (expiry,user_cert.duration_code))