mirror of
https://github.com/postgres/pgweb.git
synced 2025-08-13 13:12:42 +00:00
Turn the XML generation helper into a class for better flexibility
This commit is contained in:
@ -3,7 +3,6 @@ from django.http import HttpResponse, Http404, HttpResponseRedirect
|
||||
from django.template import TemplateDoesNotExist, loader, Context
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db import connection, transaction
|
||||
from django.utils.xmlutils import SimplerXMLGenerator
|
||||
from django.conf import settings
|
||||
|
||||
import os
|
||||
@ -12,7 +11,7 @@ import urlparse
|
||||
|
||||
from pgweb.util.decorators import ssl_required, nocache
|
||||
from pgweb.util.contexts import NavContext
|
||||
from pgweb.util.helpers import simple_form, add_xml_element
|
||||
from pgweb.util.helpers import simple_form, PgXmlHelper
|
||||
|
||||
from models import *
|
||||
from forms import *
|
||||
@ -186,16 +185,16 @@ def mirrors_xml(request):
|
||||
all_mirrors = Mirror.objects.filter(mirror_active=True, mirror_private=False, mirror_dns=True).extra(where=["mirror_last_rsync>(now() - '48 hours'::interval)"]).order_by('country_name', 'mirror_index')
|
||||
|
||||
resp = HttpResponse(mimetype='text/xml')
|
||||
x = SimplerXMLGenerator(resp, 'utf8')
|
||||
x = PgXmlHelper(resp)
|
||||
x.startDocument()
|
||||
x.startElement('mirrors', {})
|
||||
for m in all_mirrors:
|
||||
for protocol in m.get_all_protocols():
|
||||
x.startElement('mirror', {})
|
||||
add_xml_element(x, 'country', m.country_name)
|
||||
add_xml_element(x, 'path', m.host_path)
|
||||
add_xml_element(x, 'protocol', protocol)
|
||||
add_xml_element(x, 'hostname', m.get_host_name())
|
||||
x.add_xml_element('country', m.country_name)
|
||||
x.add_xml_element('path', m.host_path)
|
||||
x.add_xml_element('protocol', protocol)
|
||||
x.add_xml_element('hostname', m.get_host_name())
|
||||
x.endElement('mirror')
|
||||
x.endElement('mirrors')
|
||||
x.endDocument()
|
||||
|
@ -3,6 +3,7 @@ from pgweb.util.contexts import NavContext
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.template import Context
|
||||
from django.template.loader import get_template
|
||||
import django.utils.xmlutils
|
||||
|
||||
def simple_form(instancetype, itemid, request, formclass, formtemplate='base/form.html', redirect='/account/', navsection='account'):
|
||||
if itemid == 'new':
|
||||
@ -46,7 +47,15 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for
|
||||
def template_to_string(templatename, attrs = {}):
|
||||
return get_template(templatename).render(Context(attrs))
|
||||
|
||||
def add_xml_element(xml, name, value):
|
||||
xml.startElement(name, {})
|
||||
xml.characters(value)
|
||||
xml.endElement(name)
|
||||
|
||||
class PgXmlHelper(django.utils.xmlutils.SimplerXMLGenerator):
|
||||
def __init__(self, outstream, skipempty=False):
|
||||
django.utils.xmlutils.SimplerXMLGenerator.__init__(self, outstream, 'utf-8')
|
||||
self.skipempty = skipempty
|
||||
|
||||
def add_xml_element(self, name, value):
|
||||
if self.skipempty and value=='': return
|
||||
self.startElement(name, {})
|
||||
self.characters(value)
|
||||
self.endElement(name)
|
||||
|
||||
|
Reference in New Issue
Block a user