Example #1
0
    def test_single_contact_life_cycle(self):
        contact_info = {
        'first_name':'test_first_name',
        'last_name':'test_last_name',
        'email':'test_email',
        'phone_number':'0123456789'
        }
        # Create the contact and assert it's existence and confirm the database created it
        contact_insertion = database.insert_contact(contact_info, self.user_id)
        self.assertTrue(contact_insertion['created'])
        self.assertIsNotNone(contact_insertion['contact_id'])
        contact_id = contact_insertion['contact_id']

        # Read the contact just created
        contact = database.get_contact_with_id(contact_id)
        self.assertIsNotNone(contact)

        # Edit the contact just read
        update_info = {
        'first_name':'update_first_name',
        'last_name':'update_last_name',
        'email':'update_email',
        'phone_number':'0123456789876543210',
        'contact_id':contact_id
        }
        contact_update = database.update_contact(update_info)
        self.assertTrue(contact_update['updated'])

        # Delete the contact just updated
        contact_deletion = database.delete_contact(contact_id)
        self.assertTrue(contact_deletion['deleted'])
        # To confirm, try to get the contact with the same id and make sure we get None back
        contact = database.get_contact_with_id(contact_id)
        self.assertIsNone(contact)
Example #2
0
def update_contact():
	if not 'session_username' in session:
		return redirect('/')
	contact_form = forms.ContactForm(request.form)
	print(contact_form)
	if contact_form.validate():
		contact_update = database.update_contact(request.form)
		response = make_response((jsonpickle.encode(contact_update), 200))
		response.mimetype = 'application/json'
		return response
	else:
		print(contact_form.errors)
Example #3
0
 def update(self):
     """Update contact details to database"""
     with create_connection() as connection:
         update_contact(connection, self.name, self.phone_no, self.email,
                        self.id, self.user_id)
Example #4
0
def enrich_contact(first_name, last_name, organization, age_str):
    timestamp = time.time()
    contact_id, database_contact_data, contact_last_sync, contact_sync_interval = \
     database.get_contact_by_name(first_name, last_name, organization)
    ''' Check if data exists in database '''
    if database_contact_data:
        if age_str:
            age = float(age_str) * 60 * 60
        else:
            age = 604800.0  # = sec in a week
        ''' Check if data in database is up-to-date '''
        if timestamp - float(contact_last_sync) > age:
            ''' Update contact '''
            data = datacollector.collect_data(first_name, last_name,
                                              organization)
            min_sync_interval = float("Inf")
            ''' Update source contacts'''
            for source_name, source_contact_data in data.items():
                database_source_contact_data, source_contact_sync_interval = database.get_source_contact_by_id(
                    source_name, contact_id)
                sync_interval = float(source_contact_sync_interval)
                if database_source_contact_data == source_contact_data:
                    sync_interval = sync_interval * 1.05
                else:
                    sync_interval = sync_interval * 0.95
                ''' Sync interval of merged contact is the minimum of the sync intervals of the sources '''
                if min_sync_interval > sync_interval:
                    min_sync_interval = sync_interval
                database.update_source_contact(source_name, contact_id,
                                               source_contact_data,
                                               sync_interval)

            notnull.update_nn_score_matrix(contact_id)
            enriched_contact = merge(contact_id, database_contact_data, data)
            database.update_contact(contact_id, enriched_contact, timestamp,
                                    min_sync_interval)
            return enriched_contact
        else:
            return database_contact_data
    else:
        ''' Enrich contact '''
        data = datacollector.collect_data(first_name, last_name, organization)
        ''' Check if at least one source could get some data '''
        if not data:
            return "{}"

        contact_id = (uuid.uuid1().int >> 64) % 100000000
        request_data = dict()
        request_data["first_name"] = first_name
        request_data["last_name"] = last_name
        request_data["orga_name"] = organization

        notnull.add_contact_to_matrices(contact_id)
        contact_sync_interval = 0.0

        for source_name, source_data in data.items():
            sync_interval = database.get_avg_source_sync_interval(source_name)
            ''' Sync interval of merged contact is the minimum of the sync intervals of the sources '''
            if contact_sync_interval < sync_interval:
                contact_sync_interval = sync_interval

            database.write_source_contact(source_name, contact_id, source_data,
                                          sync_interval)

        notnull.update_nn_score_matrix(contact_id)
        enriched_contact = merge(contact_id, request_data, data)
        database.write_contact(contact_id, enriched_contact, timestamp,
                               contact_sync_interval)
        return enriched_contact