def post(self, request, *args, **kwargs): if self.valid: Profile.mark_valid_emails([self.profile.user.email]) else: Profile.mark_invalid_emails([self.profile.user.email]) if request.is_ajax(): success_value = 'valid' if self.valid else 'invalid' return JsonResponse({'success': success_value}) else: return TemplateResponse(request, self.template_name, context={'view': self})
def post(self, request, *args, **kwargs): if self.valid: Profile.mark_valid_emails([self.user.email]) else: Profile.mark_invalid_emails([self.user.email]) if request.is_ajax(): success_value = 'valid' if self.valid else 'invalid' return JsonResponse({'success': success_value}) else: return TemplateResponse(request, self.template_name)
def test_mark_identical_emails(self): # Having two profiles with the same public email address is # permitted. The `mark_invalid_emails` and `mark_valid_emails` # methods are expected to update both profile objects. profile_two = ProfileSansAccountFactory(with_email=True) Profile.all_objects.filter(pk=self.profile.pk).update( email=profile_two.email) email = profile_two.email invalid_email = f'{settings.INVALID_PREFIX}{email}' result = Profile.mark_invalid_emails([email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertEqual(self.profile.email, profile_two.email) self.assertTrue(self.profile.email.startswith(settings.INVALID_PREFIX)) self.assertEqual(result[Profile], 2) # When one of the profiles has its email already marked as # invalid, only the other one is expected to be updated. Profile.all_objects.filter(pk=self.profile.pk).update(email=email) result = Profile.mark_invalid_emails([email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertTrue(self.profile.email.startswith(settings.INVALID_PREFIX)) self.assertEqual(self.profile.email.count(settings.INVALID_PREFIX), 1) self.assertTrue(profile_two.email.startswith(settings.INVALID_PREFIX)) self.assertEqual(profile_two.email.count(settings.INVALID_PREFIX), 1) self.assertEqual(result[Profile], 1) # Repeating the operation is not expected to update anything. result = Profile.mark_invalid_emails([email]) self.assertEqual(result[Profile], 0) # Both profile objects' emails are expected to have the # marker prefix removed. result = Profile.mark_valid_emails([invalid_email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertEqual(self.profile.email, profile_two.email) self.assertEqual(self.profile.email.count(settings.INVALID_PREFIX), 0) self.assertEqual(result[Profile], 2) # When one of the profiles has its email already marked as # valid, only the other one is expected to be updated. Profile.all_objects.filter(pk=self.profile.pk).update( email=invalid_email) result = Profile.mark_valid_emails([invalid_email]) self.profile.refresh_from_db() profile_two.refresh_from_db() self.assertEqual(self.profile.email.count(settings.INVALID_PREFIX), 0) self.assertEqual(profile_two.email.count(settings.INVALID_PREFIX), 0) self.assertEqual(result[Profile], 1) # Repeating the operation is not expected to update anything. result = Profile.mark_valid_emails([email]) self.assertEqual(result[Profile], 0)
def post(self, request, *args, **kwargs): # Spare the extra trip to the database to fetch the User object associated # with the profile, just to retrieve the email address in that record. email = User.objects.filter( pk=self.profile.user_id).values_list('email') if self.valid: Profile.mark_valid_emails(email) else: Profile.mark_invalid_emails(email) if request.is_ajax(): success_value = 'valid' if self.valid else 'invalid' return JsonResponse({'success': success_value}) else: return TemplateResponse(request, self.template_name, context={'view': self})
def action(self, file_name, emails): results = Profile.mark_valid_emails(emails=emails) for model, updated in results.items(): print(updated, model.__name__, "emails marked as valid from", file_name)
def save(self, commit=True): super().save(commit) if commit: Profile.mark_valid_emails([self.user.email]) return self.user