def save_business_directory_info(self, user_input): attrs = { "custom_attributes": { "bio": user_input } } set_custom_attributes(attrs, self.user)
def save_gender_info(self, user_input): gender_selection = self.session.get_data('gender') gender = None if self.menu_one_selected(gender_selection): gender = 'male' if self.menu_two_selected(gender_selection): gender = 'female' attrs = {"custom_attributes": {"gender": gender}} set_custom_attributes(attrs, self.user)
def test_custom_attribute_cleansing(create_transfer_account_user): user = create_transfer_account_user # Test a base custom attribute attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['gender'] = 'female' ca = set_custom_attributes(attribute_dict, user)[0] assert user.custom_attributes[0].value == 'female' assert user.custom_attributes[0].key == 'gender' # Test each of the cleaning steps attribute_dict['custom_attributes']['gender'] = 'PIZZA' # PIZZA -> pizza -> aaba -> aaa -> AAA ca.custom_attribute.cleaning_steps = [("lower", ), ('replace', ['pizz', 'aa']), ('upper', )] ca = set_custom_attributes(attribute_dict, user)[0] assert user.custom_attributes[0].value == 'AAA' assert user.custom_attributes[0].key == 'gender' # Test that when options are set, you can't add things which aren't options ca.custom_attribute.cleaning_steps = None ca.custom_attribute.options = ['male', 'female', 'other'] attribute_dict['custom_attributes']['gender'] = 'PIZZA' with pytest.raises(Exception): set_custom_attributes(attribute_dict, user)[0] attribute_dict['custom_attributes']['gender'] = 'male' set_custom_attributes(attribute_dict, user)[0] assert user.custom_attributes[0].value == 'male'
def test_send_directory_listing(mocker, test_client, init_database, dl_processor): user = dl_processor.recipient transfer_account = default_transfer_account(user) transfer_account.token.name = "A Token" user1 = UserFactory(phone=phone()) attrs = {"custom_attributes": {"bio": "some bio"}} set_custom_attributes(attrs, user1) user2 = UserFactory(phone=phone()) dl_processor.get_directory_listing_users = mocker.MagicMock( return_value=[user1, user2]) dl_processor.get_business_category_translation = mocker.MagicMock( return_value="Test") dl_processor.send_sms = mocker.MagicMock() dl_processor.send_directory_listing() dl_processor.send_sms.assert_called_with( 'send_directory_listing_message', community_token_name="A Token", business_type="Test", directory_listing_users=f"{user1.phone} some bio\n{user2.phone}")
def generate_timeseries_metrics(create_organisation): # Generates metrics over timeline # User1 and User2 made today user1 = create_transfer_account_user(first_name='Ricky', phone="+19025551234", organisation=create_organisation, roles=[('BENEFICIARY', 'beneficiary') ]) user1.created = zero_time user1.default_transfer_account.is_approved = True user1.default_transfer_account._make_initial_disbursement(100, True) user1._location = 'Sunnyvale' attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['colour'] = 'red' attribute_dict['custom_attributes']['chips'] = 'Dressed All Over' set_custom_attributes(attribute_dict, user1) user1.lat = 44.675447 user1.lng = -63.594995 user2 = create_transfer_account_user(first_name='Bubbles', phone="+19025551235", organisation=create_organisation) user2.created = zero_time user2.default_transfer_account.is_approved = True user2.default_transfer_account._make_initial_disbursement(200, True) user2._location = 'Sunnyvale' attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['colour'] = 'red' attribute_dict['custom_attributes']['chips'] = 'Chicken' user2.lat = 44.675447 user2.lng = -63.594995 set_custom_attributes(attribute_dict, user2) # user3 made yesterday user3 = create_transfer_account_user(first_name='Julian', phone="+19025531230", organisation=create_organisation) user3.default_transfer_account.is_approved = True disburse = user3.default_transfer_account._make_initial_disbursement( 210, True) user3.created = zero_time user3.created = user3.created - timedelta(days=1) + timedelta(hours=6) disburse.created = user3.created - timedelta(days=1) + timedelta(hours=3) user3._location = 'Dartmouth' attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['colour'] = 'blue' attribute_dict['custom_attributes']['chips'] = 'Barbecue' set_custom_attributes(attribute_dict, user3) user3.lat = 44.668055 user3.lng = -63.580829 # user4 made 4 days ago user4 = create_transfer_account_user(first_name='Randy', phone="+19025511230", organisation=create_organisation) user4.created = zero_time user4.default_transfer_account.is_approved = True disburse = user4.default_transfer_account._make_initial_disbursement( 201, True) user4.created = user4.created - timedelta(days=4) + timedelta(hours=23) disburse.created = user4.created - timedelta(days=4) + timedelta(hours=1) user4._location = 'Lower Sackville' attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['colour'] = 'blue' attribute_dict['custom_attributes']['chips'] = 'Barbecue' set_custom_attributes(attribute_dict, user4) user4.lat = 44.770061 user4.lng = -63.692723 # user5/user6 made 10 days ago user5 = create_transfer_account_user(first_name='Cory', phone="+19011531230", organisation=create_organisation) user5.created = zero_time user5.default_transfer_account.is_approved = True disburse = user5.default_transfer_account._make_initial_disbursement( 202, True) user5.created = user5.created - timedelta(days=10) + timedelta(hours=2) disburse.created = user5.created - timedelta(days=10) + timedelta(hours=5) user5._location = 'Truro' attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['colour'] = 'green' attribute_dict['custom_attributes']['chips'] = 'Dressed All Over' set_custom_attributes(attribute_dict, user5) user5.lat = 45.368075 user5.lng = -63.256207 user6 = create_transfer_account_user(first_name='Trevor', phone="+19025111230", organisation=create_organisation) user6.created = zero_time user6.default_transfer_account.is_approved = True disburse = user6.default_transfer_account._make_initial_disbursement( 204, True) attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['colour'] = 'red' attribute_dict['custom_attributes']['chips'] = 'Jalapeno' set_custom_attributes(attribute_dict, user6) user6.lat = 44.368363 user6.lng = -64.526330 db.session.commit() tu1 = TransferUsage.find_or_create("Pepperoni") tu2 = TransferUsage.find_or_create("Jalepeno Chips") tu3 = TransferUsage.find_or_create("Shopping Carts") tu1.created = zero_time - timedelta(days=15) + timedelta(hours=22) tu2.created = zero_time - timedelta(days=15) + timedelta(hours=2) tu3.created = zero_time - timedelta(days=15) + timedelta(hours=1) p1 = make_payment_transfer( 100, create_organisation.token, send_user=user1, send_transfer_account=user1.default_transfer_account, receive_user=user2, receive_transfer_account=user2.default_transfer_account, transfer_use=str(int(tu1.id))) p1.created = zero_time + timedelta(hours=3) p2 = make_payment_transfer( 25, create_organisation.token, send_user=user3, send_transfer_account=user3.default_transfer_account, receive_user=user4, receive_transfer_account=user4.default_transfer_account, transfer_use=str(int(tu1.id))) p2.created = zero_time p2.created = p2.created - timedelta(days=1) + timedelta(hours=7) p3 = make_payment_transfer( 5, create_organisation.token, send_user=user4, send_transfer_account=user4.default_transfer_account, receive_user=user2, receive_transfer_account=user2.default_transfer_account, transfer_use=str(int(tu2.id))) p3.created = zero_time p3.created = p3.created - timedelta(days=1) + timedelta(hours=22) p4 = make_payment_transfer( 20, create_organisation.token, send_user=user5, send_transfer_account=user5.default_transfer_account, receive_user=user6, receive_transfer_account=user6.default_transfer_account, transfer_use=str(int(tu3.id))) p4.created = zero_time p4.created = p4.created - timedelta(days=4) + timedelta(hours=1) p5 = make_payment_transfer( 20, create_organisation.token, send_user=user6, send_transfer_account=user6.default_transfer_account, receive_user=user5, receive_transfer_account=user5.default_transfer_account, transfer_use=str(int(tu2.id))) p5.created = zero_time p5.created = p5.created - timedelta(days=6) + timedelta(hours=23) db.session.commit()
def create_transfer_user(email, transfer_usages, organisation, created_offset): first_name, middle_name = random.sample(ADJECTIVES, 2) last_name = f'{middle_name} {random.choice(ANIMALS)}' is_beneficiary = rand_bool(0.9) if is_beneficiary: roles = roles = [('BENEFICIARY', 'beneficiary')] else: roles = [('VENDOR', 'vendor')] phone = '+1' + ''.join([str(random.randint(0, 10)) for i in range(0, 10)]) user = create_transfer_account_user( first_name=first_name, last_name=last_name, email=email, phone=phone, organisation=organisation, roles=roles, ) # Set the created date to the first day user.created = datetime.utcnow() - timedelta(days=created_offset) location, lat, lng = random.choice(LOCATIONS) # Assign the user to a random location and jitter the latlong slightly user._location = location user.lat = lat + random.random() / 100 user.lng = lng + random.random() / 100 attribute_dict = {'custom_attributes': {}} attribute_dict['custom_attributes']['gender'] = random.choice( ['male', 'female']) set_custom_attributes(attribute_dict, user) if not is_beneficiary: bu = random.choice(transfer_usages) user.business_usage = bu # Create purchase behavior profile behavior = {} # 10% chance the user's purchase will always include a particular category single_item_purchaser = rand_bool(0.1) if single_item_purchaser: item = random.choice(transfer_usages) behavior['purchase_item'] = item else: behavior['purchase_item'] = None # A fixed likelihood that this user will actually make a purchase if selected. behavior['spend_probability'] = random.random() # The likelihood that the user's purchase is at a different location behavior['out_of_town_probability'] = random.random() / 3 return user, behavior
def save_business_directory_info(self, user_input): bio = self.session.get_data('bio') attrs = {"custom_attributes": {"bio": bio}} set_custom_attributes(attrs, self.user)