Automatically exported from code.google.com/p/planningalerts
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

129 рядки
4.1 KiB

  1. __auth__ = None
  2. import re
  3. date_format = "%d/%m/%Y"
  4. def xmlQuote(text):
  5. # Change &s to &s
  6. # I suspect there is probably some standard python
  7. # function I should be using for this...
  8. return text.replace('&', '&')
  9. def fixNewlines(text):
  10. # This can be used to sort out windows newlines
  11. return text.replace("\r\n","\n")
  12. # So what can a postcode look like then?
  13. # This list of formats comes from http://www.mailsorttechnical.com/frequentlyaskedquestions.cfm
  14. #AN NAA M1 1AA
  15. #ANN NAA M60 1NW
  16. #AAN NAA CR2 6XH
  17. #AANN NAA DN55 1PT
  18. #ANA NAA W1A 1HP
  19. #AANA NAA EC1A 1BB
  20. postcode_regex = re.compile("[A-Z][A-Z]?\d(\d|[A-Z])? ?\d[A-Z][A-Z]")
  21. def getPostcodeFromText(text):
  22. """This function takes a piece of text and returns the first
  23. bit of it that looks like a postcode."""
  24. postcode_match = postcode_regex.search(text)
  25. if postcode_match is not None:
  26. return postcode_match.group()
  27. class PlanningAuthorityResults:
  28. """This class represents a set of results of a planning search.
  29. This should probably be separated out so that it can be used for
  30. authorities other than Cherwell.
  31. """
  32. def __init__(self, authority_name, authority_short_name):
  33. self.authority_name = authority_name
  34. self.authority_short_name = authority_short_name
  35. # this will be a list of PlanningApplication objects
  36. self.planning_applications = []
  37. def addApplication(self, application):
  38. self.planning_applications.append(application)
  39. def __repr__(self):
  40. return self.displayXML()
  41. def displayXML(self):
  42. """This should display the contents of this object in the planningalerts format.
  43. i.e. in the same format as this one:
  44. http://www.planningalerts.com/lambeth.xml
  45. """
  46. applications_bit = "".join([x.displayXML() for x in self.planning_applications])
  47. return u"""<?xml version="1.0" encoding="UTF-8"?>\n""" + \
  48. u"<planning>\n" +\
  49. u"<authority_name>%s</authority_name>\n" %self.authority_name +\
  50. u"<authority_short_name>%s</authority_short_name>\n" %self.authority_short_name +\
  51. u"<applications>\n" + applications_bit +\
  52. u"</applications>\n" +\
  53. u"</planning>\n"
  54. class PlanningApplication:
  55. def __init__(self, no_postcode_default='No postcode'):
  56. self.council_reference = None
  57. self.address = None
  58. self.postcode = no_postcode_default
  59. self.description = None
  60. self.info_url = None
  61. self.comment_url = None
  62. # expecting this as a datetime.date object
  63. self.date_received = None
  64. # If we can get them, we may as well include OSGB.
  65. # These will be the entirely numeric version.
  66. self.osgb_x = None
  67. self.osgb_y = None
  68. def __repr__(self):
  69. return self.displayXML()
  70. def is_ready(self):
  71. # This method tells us if the application is complete
  72. # Because of the postcode default, we can't really
  73. # check the postcode - make sure it is filled in when
  74. # you do the address.
  75. return self.council_reference \
  76. and self.address \
  77. and self.description \
  78. and self.info_url \
  79. and self.comment_url \
  80. and self.date_received
  81. def displayXML(self):
  82. #print self.council_reference, self.address, self.postcode, self.description, self.info_url, self.comment_url, self.date_received
  83. contents = [
  84. u"<council_reference>%s</council_reference>" %xmlQuote(self.council_reference),
  85. u"<address>%s</address>" %xmlQuote(self.address),
  86. u"<postcode>%s</postcode>" %self.postcode,
  87. u"<description>%s</description>" %xmlQuote(self.description),
  88. u"<info_url>%s</info_url>" %xmlQuote(self.info_url),
  89. u"<comment_url>%s</comment_url>" %xmlQuote(self.comment_url),
  90. u"<date_received>%s</date_received>" %self.date_received.strftime(date_format),
  91. ]
  92. if self.osgb_x:
  93. contents.append(u"<osgb_x>%s</osgb_x>" %(self.osgb_x))
  94. if self.osgb_y:
  95. contents.append(u"<osgb_y>%s</osgb_y>" %(self.osgb_y))
  96. return u"<application>\n%s\n</application>" %('\n'.join(contents))