2010-02-05 02:14:16 +03:00
|
|
|
import socket
|
2010-03-15 23:37:20 +03:00
|
|
|
import urllib2
|
2010-02-05 02:14:16 +03:00
|
|
|
|
2010-03-15 23:37:20 +03:00
|
|
|
from django import http
|
2010-02-05 02:14:16 +03:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.cache import parse_backend_uri
|
|
|
|
from django.views.decorators.cache import never_cache
|
2010-03-15 23:37:20 +03:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2010-02-05 02:14:16 +03:00
|
|
|
|
2010-02-05 04:51:14 +03:00
|
|
|
import jingo
|
2010-03-15 23:37:20 +03:00
|
|
|
import phpserialize as php
|
|
|
|
|
|
|
|
from stats.models import Contribution, ContributionError
|
2010-03-24 21:50:50 +03:00
|
|
|
from . import log
|
2010-02-05 04:51:14 +03:00
|
|
|
|
2010-02-05 02:14:16 +03:00
|
|
|
|
|
|
|
@never_cache
|
|
|
|
def monitor(request):
|
|
|
|
|
2010-02-05 08:13:41 +03:00
|
|
|
# For each check, a boolean pass/fail status to show in the template
|
|
|
|
status_summary = {}
|
2010-02-05 02:14:16 +03:00
|
|
|
status = 200
|
2010-03-24 22:51:03 +03:00
|
|
|
log = logging.getLogger('z.amo')
|
2010-02-05 02:14:16 +03:00
|
|
|
|
2010-02-05 08:13:41 +03:00
|
|
|
# Check all memcached servers
|
|
|
|
scheme, servers, _ = parse_backend_uri(settings.CACHE_BACKEND)
|
|
|
|
memcache_results = []
|
|
|
|
status_summary['memcache'] = True
|
2010-02-05 02:14:16 +03:00
|
|
|
if 'memcached' in scheme:
|
|
|
|
hosts = servers.split(';')
|
|
|
|
for host in hosts:
|
|
|
|
ip, port = host.split(':')
|
|
|
|
try:
|
|
|
|
s = socket.socket()
|
|
|
|
s.connect((ip, int(port)))
|
|
|
|
except Exception, e:
|
2010-02-05 08:13:41 +03:00
|
|
|
result = False
|
|
|
|
status_summary['memcache'] = False
|
|
|
|
status = 500
|
2010-03-24 22:51:03 +03:00
|
|
|
log.critical('Failed to connect to memcached (%s): %s' %
|
|
|
|
(host, e))
|
2010-02-05 02:14:16 +03:00
|
|
|
else:
|
2010-02-05 08:13:41 +03:00
|
|
|
result = True
|
2010-02-05 02:14:16 +03:00
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
|
2010-02-05 08:13:41 +03:00
|
|
|
memcache_results.append((ip, port, result))
|
|
|
|
if len(memcache_results) < 2:
|
2010-02-05 11:14:43 +03:00
|
|
|
status = 500
|
2010-02-05 08:13:41 +03:00
|
|
|
status_summary['memcache'] = False
|
2010-03-24 22:51:03 +03:00
|
|
|
log.warning('You should have 2+ memcache servers. You have %s.' %
|
|
|
|
len(memcache_results))
|
|
|
|
if not memcache_results:
|
|
|
|
status = 500
|
|
|
|
status_summary['memcache'] = False
|
|
|
|
log.info('Memcache is not configured.')
|
2010-02-05 02:14:16 +03:00
|
|
|
|
|
|
|
return jingo.render(request, 'services/monitor.html',
|
2010-02-05 08:13:41 +03:00
|
|
|
{'memcache_results': memcache_results,
|
|
|
|
'status_summary': status_summary},
|
|
|
|
status=status)
|
2010-02-05 04:51:14 +03:00
|
|
|
|
|
|
|
|
2010-03-15 23:37:20 +03:00
|
|
|
@csrf_exempt
|
|
|
|
def paypal(request):
|
|
|
|
"""
|
|
|
|
Handle PayPal IPN post-back for contribution transactions.
|
|
|
|
|
|
|
|
IPN will retry periodically until it gets success (status=200). Any
|
|
|
|
db errors or replication lag will result in an exception and http
|
|
|
|
status of 500, which is good so PayPal will try again later.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Check that the request is valid and coming from PayPal.
|
|
|
|
data = request.POST.copy()
|
|
|
|
data['cmd'] = '_notify-validate'
|
|
|
|
if urllib2.urlopen(settings.PAYPAL_CGI_URL,
|
|
|
|
data.urlencode(), 20).readline() != 'VERIFIED':
|
|
|
|
return http.HttpResponseForbidden('Invalid confirmation')
|
|
|
|
|
|
|
|
# We only care about completed transactions.
|
|
|
|
if request.POST['payment_status'] != 'Completed':
|
|
|
|
return http.HttpResponse('Payment not completed')
|
|
|
|
|
|
|
|
# Make sure transaction has not yet been processed.
|
|
|
|
if len(Contribution.objects.filter(transaction_id=request.POST['txn_id'])) > 0:
|
|
|
|
return http.HttpResponse('Transaction already processed')
|
|
|
|
|
|
|
|
# Fetch and update the contribution - item_number is the uuid we created.
|
|
|
|
c = Contribution.objects.get(uuid=request.POST['item_number'])
|
|
|
|
c.transaction_id = request.POST['txn_id']
|
|
|
|
c.amount = request.POST['mc_gross']
|
|
|
|
c.uuid = None
|
|
|
|
c.post_data = php.serialize(request.POST)
|
|
|
|
c.save()
|
|
|
|
|
|
|
|
# Send thankyou email.
|
|
|
|
try:
|
|
|
|
c.mail_thankyou(request)
|
|
|
|
except ContributionError as e:
|
|
|
|
# A failed thankyou email is not a show stopper, but is good to know.
|
|
|
|
log.error('Thankyou note email failed with error: %s' % e)
|
|
|
|
|
|
|
|
return http.HttpResponse('Success!')
|
|
|
|
|
|
|
|
|
2010-02-05 04:51:14 +03:00
|
|
|
def handler404(request):
|
|
|
|
return jingo.render(request, 'amo/404.lhtml', status=404)
|
|
|
|
|
|
|
|
|
|
|
|
def handler500(request):
|
2010-02-17 02:55:22 +03:00
|
|
|
return jingo.render(request, 'amo/500.lhtml', status=500)
|