datatracker/ietf/sync/views.py
2012-09-21 09:01:00 +00:00

103 lines
3.9 KiB
Python

import subprocess, os
from django.http import HttpResponse, HttpResponseForbidden
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.template.loader import render_to_string
from django.conf import settings
from django import forms
from django.db.models import Q
from django.contrib.auth.models import User
from ietf.ietfauth.decorators import role_required, has_role
from ietf.doc.models import *
from ietf.sync import iana, rfceditor
from ietf.sync.discrepancies import find_discrepancies
SYNC_BIN_PATH = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), "../bin"))
#@role_required('Secretariat', 'IANA', 'RFC Editor')
def discrepancies(request):
sections = find_discrepancies()
return render_to_response("sync/discrepancies.html",
dict(sections=sections),
context_instance=RequestContext(request))
def notify(request, org, notification):
"""Notify that something has changed at another site to trigger a
run of one of the sync scripts."""
known_orgs = {
"iana": "IANA",
"rfceditor": "RFC Editor",
}
if org not in known_orgs:
raise Http404
# handle auth, to make it easier for the other end, you can send
# the username/password as POST parameters instead of having to
# visit the login page
user = request.user
username = request.POST.get("username") or request.GET.get("username")
password = request.POST.get("password") or request.GET.get("password")
if username != None and password != None:
if settings.SERVER_MODE == "production" and not request.is_secure():
return HttpResponseForbidden("You must use HTTPS when sending username/password")
if not user.is_authenticated():
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
return HttpResponse("Invalid username/password")
if not user.check_password(password):
return HttpResponse("Invalid username/password")
if not has_role(user, ("Secretariat", known_orgs[org])):
return HttpResponseForbidden("You do not have the necessary permissions to view this page")
known_notifications = {
"protocols": "an added reference to an RFC at <a href=\"%s\">the IANA protocols page</a>" % iana.PROTOCOLS_URL,
"changes": "new changes at <a href=\"%s\">the changes JSON dump</a>" % iana.CHANGES_URL,
"queue": "new changes to <a href=\"%s\">queue2.xml</a>" % rfceditor.QUEUE_URL,
"index": "new changes to <a href=\"%s\">rfc-index.xml</a>" % rfceditor.INDEX_URL,
}
if notification not in known_notifications:
raise Http404
if request.method == "POST":
def runscript(name):
p = subprocess.Popen(["python", os.path.join(SYNC_BIN_PATH, name)],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
out, _ = p.communicate()
return (p.returncode, out)
if notification == "protocols":
failed, out = runscript("iana-protocols-updates")
if notification == "changes":
failed, out = runscript("iana-changes-updates")
if notification == "queue":
failed, out = runscript("rfc-editor-queue-updates")
if notification == "index":
failed, out = runscript("rfc-editor-index-updates")
if failed:
return HttpResponse("FAIL\n\n" + out, content_type="text/plain")
else:
return HttpResponse("OK", content_type="text/plain")
return render_to_response('sync/notify.html',
dict(org=known_orgs[org],
notification=notification,
help_text=known_notifications[notification],
),
context_instance=RequestContext(request))