Files
postgres-web/pgweb/util/misc.py
Magnus Hagander 9bcaa6ab33 Remove the "is behind cache" functionality
We're always behind cache now (we may or may not be cached, but that's
not what this check was about)
2016-05-24 21:14:33 +02:00

63 lines
2.0 KiB
Python

from django.db import connection
from django.conf import settings
from Crypto.Hash import SHA256
from Crypto import Random
from pgweb.mailqueue.util import send_simple_mail
from pgweb.util.helpers import template_to_string
import re
def send_template_mail(sender, receiver, subject, templatename, templateattr={}, usergenerated=False):
send_simple_mail(sender, receiver, subject,
template_to_string(templatename, templateattr),
usergenerated=usergenerated)
def get_client_ip(request):
"""
Get the IP of the client. If the client is served through our Varnish caches,
or behind one of our SSL proxies, make sure to get the *actual* client IP,
and not the IP of the cache/proxy.
"""
if request.META.has_key('HTTP_X_FORWARDED_FOR'):
# There is a x-forwarded-for header, so trust it but only if the actual connection
# is coming in from one of our frontends.
if request.META['REMOTE_ADDR'] in settings.FRONTEND_SERVERS:
return request.META['HTTP_X_FORWARDED_FOR']
# Else fall back and return the actual IP of the connection
return request.META['REMOTE_ADDR']
def varnish_purge(url):
"""
Purge the specified URL from Varnish. Will add initial anchor to the URL,
but no trailing one, so by default a wildcard match is done.
"""
url = '^%s' % url
connection.cursor().execute("SELECT varnish_purge(%s)", (url, ))
def version_sort(l):
"""
map a directory name to a format that will show up sensibly in an ascii sort
"""
mkey = l['url']
m = re.match('v([0-9]+)\.([0-9]+)\.([0-9]+)$',l['url'])
if m:
mkey = m.group(1) + '%02d' % int(m.group(2)) + '%02d' % int(m.group(3));
m = re.match('v([0-9]+)\.([0-9]+)$',l['url'])
if m:
mkey = m.group(1) + '%02d' % int(m.group(2));
return mkey
def generate_random_token():
"""
Generate a random token of 64 characters. This token will be
generated using a strong random number, and then hex encoded to make
sure all characters are safe to put in emails and URLs.
"""
s = SHA256.new()
r = Random.new()
s.update(r.read(250))
return s.hexdigest()