def test_wrong_type_bool_in_str(self): custom_field = CustomField(id='str', label='String', required=True, type='str') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_str=True) upcf.save() self.assertEqual(True, upcf.get_value()) upcf.value_str = False upcf.save() self.assertEqual(False, upcf.get_value())
def handle(self, *args, **options): custom_fields = CustomField.objects.all() with open(options['filepath'], newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: # check the user exists in db try: user = User.objects.get(username=row.get('username')) except User.DoesNotExist: print("user not found: ", row.get('username')) continue # update phone no user_profile = UserProfile.objects.get(user=user) if user_profile.phone_number == "" or user_profile.phone_number is None: user_profile.phone_number = row.get('phone_number') user_profile.save() print("%s: phone_number updated to %s" % (row.get("username"), row.get('phone_number'))) for cf in custom_fields: try: upcf = UserProfileCustomField.objects.get( user=user, key_name=cf.id) except UserProfileCustomField.DoesNotExist: upcf = UserProfileCustomField(user=user, key_name=cf) print("%s: adding customfield record for %s" % (row.get("username"), cf.id)) if cf.type == 'str' and (upcf.value_str is None or upcf.value_str == ""): upcf.value_str = row.get(cf.id) upcf.save() print("%s: %s updated to %s" % (row.get("username"), cf.id, row.get(cf.id))) if cf.type == 'int' and (upcf.value_int is None or upcf.value_int == ""): upcf.value_int = row.get(cf.id) upcf.save() print("%s: %s updated to %s" % (row.get("username"), cf.id, row.get(cf.id)))