Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: [email protected] in admin

Tags:

python

django

I'm having a seemingly simple problem, but I haven't found a way to debug it.

In the admin on our production website, when editing an object that has a ForeignKey to User, all the users show up as [email protected]. That makes the admin unusable in these areas!

I tried googling the problem, but because the term "email protected" appears in many mailing lists in an unrelated context, I can't find a solution. Also, I searched for "email protected" in the Django codebase but I didn't find it.

Any idea what to do?

like image 561
Ram Rachum Avatar asked Aug 31 '12 20:08

Ram Rachum


People also ask

How to send emails with Django?

Sending Emails with the Django ShellWe import the Django send_mail function. Then we import the settings object which contains all the global settings and the per-site settings (those inside the settings.py file). Finally, we pass all the needed arguments to the send_mail function.

How to change user password in Django admin?

manage.py changepassword *username* offers a method of changing a user's password from the command line. It prompts you to change the password of a given user which you must enter twice. If they both match, the new password will be changed immediately.


2 Answers

I don't really know the answer but whenever I see [email protected] showing up on Google, if I navigate to the link then the email shows up and if I inspect the element it has near it this piece of javascript:

/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l=document.getElementById("__cf_email__");a=l.className;if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */

This may help you further. (Inspect your element to see if this applies to you too.)

If you see it in your code too then This and this may help you.

EDIT: It seems that it is caused by Cloudflare's email obfuscation.

like image 112
thikonom Avatar answered Oct 23 '22 01:10

thikonom


Email obfuscation is good thing for public site, and I would like to disable it for admin. So I write this middleware to disable email obfuscation in admin.

def _insert_email_off(html):
    origin = html
    try:
        pos1 = html.index('>', html.index('<body')) + 1
        html = html[:pos1] + '<!--email_off-->' + html[pos1:]
        pos2 = html.index('</body>')
        html = html[:pos2] +'<!--/email_off-->' + html[pos2:]
    except ValueError:
        return origin
    return html


class CloudflareEmailProtect(MiddlewareMixin):

    def process_response(self, request, response):
        if request.path.startswith('/admin/'):
            response.content = smart_bytes(_insert_email_off(smart_text(response.content)))
        return response


class TestCloudflareEmailProtect:

    def test_admin(self, rf):
        request = rf.get('/admin/aaa')
        html = '<html><body>content</body>'
        response = CloudflareEmailProtect().process_response(request, HttpResponse(html))
        assert b'<!--email_off--' in response.content

    def test_not_admin(self, rf):
        request = rf.get('/public')
        html = '<html><body>content</body>'
        response = CloudflareEmailProtect().process_response(request, HttpResponse(html))
        assert b'<!--email_off--' not in response.content


def test_insert_email_off():
    html = 'aa <body zzz>bb cc</body>dd'
    result = _insert_email_off(html)
    assert result == 'aa <body zzz><!--email_off-->bb cc<!--/email_off--></body>dd'

    assert _insert_email_off('aaa') == 'aaa'
like image 23
un1t Avatar answered Oct 22 '22 23:10

un1t