Automatically exported from code.google.com/p/planningalerts
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

119 lignes
3.7 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. def __repr__(self):
  65. return self.displayXML()
  66. def is_ready(self):
  67. # This method tells us if the application is complete
  68. # Because of the postcode default, we can't really
  69. # check the postcode - make sure it is filled in when
  70. # you do the address.
  71. return self.council_reference \
  72. and self.address \
  73. and self.description \
  74. and self.info_url \
  75. and self.comment_url \
  76. and self.date_received
  77. def displayXML(self):
  78. #print self.council_reference, self.address, self.postcode, self.description, self.info_url, self.comment_url, self.date_received
  79. return u"<application>\n" +\
  80. u"<council_reference>%s</council_reference>\n" %xmlQuote(self.council_reference) +\
  81. u"<address>%s</address>\n" %xmlQuote(self.address) +\
  82. u"<postcode>%s</postcode>\n" %self.postcode +\
  83. u"<description>%s</description>\n" %xmlQuote(self.description) +\
  84. u"<info_url>%s</info_url>\n" %xmlQuote(self.info_url) +\
  85. u"<comment_url>%s</comment_url>\n" %xmlQuote(self.comment_url) +\
  86. u"<date_received>%s</date_received>\n" %self.date_received.strftime(date_format) +\
  87. u"</application>\n"