示例#1
0
 def process_add_phone_number(self):
     if self.phone_number_is_valid():
         user = self.request.couch_user
         is_new_phone_number = self.phone_number not in user.phone_numbers
         user.add_phone_number(self.phone_number)
         user.save()
         if is_new_phone_number:
             log_user_change(
                 by_domain=None,
                 for_domain=None,
                 couch_user=user,
                 changed_by_user=user,
                 changed_via=USER_CHANGE_VIA_WEB,
                 change_messages=UserChangeMessage.phone_numbers_added(
                     [self.phone_number]),
                 by_domain_required_for_log=False,
                 for_domain_required_for_log=False,
             )
         messages.success(self.request, _("Phone number added."))
     else:
         messages.error(
             self.request,
             _("Invalid phone number format entered. "
               "Please enter number, including country code, in digits only."
               ))
     return HttpResponseRedirect(reverse(MyAccountSettingsView.urlname))
示例#2
0
    def _log_phone_number_changes(self, old_phone_numbers, new_phone_numbers):
        (items_added,
         items_removed) = find_differences_in_list(target=new_phone_numbers,
                                                   source=old_phone_numbers)

        change_messages = {}
        if items_added:
            change_messages.update(
                UserChangeMessage.phone_numbers_added(
                    list(items_added))["phone_numbers"])

        if items_removed:
            change_messages.update(
                UserChangeMessage.phone_numbers_removed(
                    list(items_removed))["phone_numbers"])

        if change_messages:
            self.logger.add_change_message({'phone_numbers': change_messages})
示例#3
0
def _log_phone_number_change(new_phone_numbers, old_phone_numbers,
                             user_change_logger):
    numbers_added, numbers_removed = find_differences_in_list(
        target=list(new_phone_numbers), source=list(old_phone_numbers))

    change_messages = {}
    if numbers_removed:
        change_messages.update(
            UserChangeMessage.phone_numbers_removed(
                list(numbers_removed))["phone_numbers"])

    if numbers_added:
        change_messages.update(
            UserChangeMessage.phone_numbers_added(
                list(numbers_added))["phone_numbers"])

    if change_messages:
        user_change_logger.add_change_message(
            {'phone_numbers': change_messages})
    def test_update(self):
        restore_phone_numbers_to = self.commcare_user.to_json(
        )['phone_numbers']

        self.commcare_user.add_phone_number("9999999999")

        change_messages = UserChangeMessage.phone_numbers_added(["9999999999"])
        user_history = log_user_change(
            self.domain,
            self.domain,
            self.commcare_user,
            self.web_user,
            changed_via=USER_CHANGE_VIA_BULK_IMPORTER,
            change_messages=change_messages,
            fields_changed={
                'phone_numbers': self.commcare_user.phone_numbers,
                'password': '******'
            },
            action=UserModelAction.UPDATE)

        self.assertEqual(user_history.by_domain, self.domain)
        self.assertEqual(user_history.for_domain, self.domain)
        self.assertEqual(user_history.user_type, "CommCareUser")
        self.assertIsNotNone(user_history.user_id)
        self.assertEqual(user_history.user_id, self.commcare_user.get_id)
        self.assertIsNotNone(user_history.changed_by)
        self.assertEqual(user_history.changed_by, self.web_user.get_id)
        self.assertEqual(user_history.changes,
                         {'phone_numbers': ['9999999999']})
        self.assertEqual(user_history.changed_via,
                         USER_CHANGE_VIA_BULK_IMPORTER)
        self.assertEqual(user_history.change_messages, change_messages)
        self.assertEqual(user_history.action, UserModelAction.UPDATE.value)
        self.assertEqual(user_history.user_repr,
                         user_id_to_username(self.commcare_user.get_id))
        self.assertEqual(user_history.changed_by_repr,
                         user_id_to_username(self.web_user.get_id))

        self.commcare_user.phone_numbers = restore_phone_numbers_to