Automatically exported from code.google.com/p/planningalerts
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PlanningUtils.py 4.0 KiB

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