Django Filter ignoring characters/symbols

Asked

Viewed 56 times

0

Problem

Users who register with the email in gmail can create multiple accounts just by changing the . (dot) place. For example: [email protected] and [email protected] direct to the same email but are two accounts on my website. So the user can spam accounts considering this.

Doubt

I wonder if I can make a filter ignoring points. That way, if you have @gmail.com in the email, I search for the email ignoring both the search string and the Django filter. Something to that effect:

User.objects.filter(email = useremail.replace('.').('')).exclude(email__ignore_char = '.')

That way I’d be searching [email protected] across my base and ignore the . in the records I have also.

  • What would be the reason for downvote? Thank you

  • It seems that someone programmed a bot to negate all questions. I just asked a question and was instantly negative, without explanation. The same thing happened to other people. See another case here: https://answall.com/questions/438365/como-realizar-uma-coleta-informa%C3%A7%C3%b5es-de-um-file-json-via-ajax-no-use-de

  • Oh yes, it really was just after creating the question..

1 answer

1


A colleague of mine found a gist with a solution that solved the problem:

from django.db.models import F, Func, Value
if '@gmail.com' in email:
    email_gmail_spam = email.replace('.', '')
    user = User.objects.annotate(
        fixed_email=Func(
            F('email'),
            Value('.'), Value(''),
            function='replace',
        )
    ).filter(fixed_email=email_gmail_spam)

    if user.exists():
        message.error("Ja existe um usuário com este email cadastrado.")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.