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.
 
 
 
 
 
 

116 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 "<planning>\n" +\
  48. "<authority_name>%s</authority_name>\n" %self.authority_name +\
  49. "<authority_short_name>%s</authority_short_name>\n" %self.authority_short_name +\
  50. "<applications>\n" + applications_bit +\
  51. "</applications>\n" +\
  52. "</planning>\n"
  53. class PlanningApplication:
  54. def __init__(self, no_postcode_default='No postcode'):
  55. self.council_reference = None
  56. self.address = None
  57. self.postcode = no_postcode_default
  58. self.description = None
  59. self.info_url = None
  60. self.comment_url = None
  61. # expecting this as a datetime.date object
  62. self.date_received = None
  63. def __repr__(self):
  64. return self.displayXML()
  65. def is_ready(self):
  66. # This method tells us if the application is complete
  67. # Because of the postcode default, we can't really
  68. # check the postcode - make sure it is filled in when
  69. # you do the address.
  70. return self.council_reference \
  71. and self.address \
  72. and self.description \
  73. and self.info_url \
  74. and self.comment_url \
  75. and self.date_received
  76. def displayXML(self):
  77. #print self.council_reference, self.address, self.postcode, self.description, self.info_url, self.comment_url, self.date_received
  78. return "<application>\n" +\
  79. "<council_reference>%s</council_reference>\n" %xmlQuote(self.council_reference) +\
  80. "<address>%s</address>\n" %xmlQuote(self.address) +\
  81. "<postcode>%s</postcode>\n" %self.postcode +\
  82. "<description>%s</description>\n" %xmlQuote(self.description) +\
  83. "<info_url>%s</info_url>\n" %xmlQuote(self.info_url) +\
  84. "<comment_url>%s</comment_url>\n" %xmlQuote(self.comment_url) +\
  85. "<date_received>%s</date_received>\n" %self.date_received.strftime(date_format) +\
  86. "</application>\n"