Files
postgres-web/pgweb/util/misc.py
Magnus Hagander 097a732031 cc bugreports to the original submitter
This gives the submitter a chance to respond to their own message even
if it's not delivered through the list (for example, because they are
not subscribed, or because it's caught in moderation for other reasons).

Per discussion at the developer meeting.
2017-07-04 16:57:51 +01: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, cc=None):
send_simple_mail(sender, receiver, subject,
template_to_string(templatename, templateattr),
usergenerated=usergenerated, cc=cc)
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['link']
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()