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.
 
 
 
 
 
 

124 rader
4.1 KiB

  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, default_postcode="No Postcode"):
  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. return postcode_match.group() if postcode_match else default_postcode
  21. class PlanningAuthorityResults:
  22. """This class represents a set of results of a planning search.
  23. This should probably be separated out so that it can be used for
  24. authorities other than Cherwell.
  25. """
  26. def __init__(self, authority_name, authority_short_name):
  27. self.authority_name = authority_name
  28. self.authority_short_name = authority_short_name
  29. # this will be a list of PlanningApplication objects
  30. self.planning_applications = []
  31. def addApplication(self, application):
  32. self.planning_applications.append(application)
  33. def __repr__(self):
  34. return self.displayXML()
  35. def displayXML(self):
  36. """This should display the contents of this object in the planningalerts format.
  37. i.e. in the same format as this one:
  38. http://www.planningalerts.com/lambeth.xml
  39. """
  40. applications_bit = "".join([x.displayXML() for x in self.planning_applications])
  41. return u"""<?xml version="1.0" encoding="UTF-8"?>\n""" + \
  42. u"<planning>\n" +\
  43. u"<authority_name>%s</authority_name>\n" %self.authority_name +\
  44. u"<authority_short_name>%s</authority_short_name>\n" %self.authority_short_name +\
  45. u"<applications>\n" + applications_bit +\
  46. u"</applications>\n" +\
  47. u"</planning>\n"
  48. class PlanningApplication:
  49. def __init__(self):
  50. self.council_reference = None
  51. self.address = None
  52. self.postcode = None
  53. self.description = None
  54. self.info_url = None
  55. self.comment_url = None
  56. # expecting this as a datetime.date object
  57. self.date_received = None
  58. # If we can get them, we may as well include OSGB.
  59. # These will be the entirely numeric version.
  60. self.osgb_x = None
  61. self.osgb_y = None
  62. def __repr__(self):
  63. return self.displayXML()
  64. def is_ready(self):
  65. # This method tells us if the application is complete
  66. # Because of the postcode default, we can't really
  67. # check the postcode - make sure it is filled in when
  68. # you do the address.
  69. return self.council_reference \
  70. and self.address \
  71. and self.description \
  72. and self.info_url \
  73. and self.comment_url \
  74. and self.date_received
  75. def displayXML(self):
  76. #print self.council_reference, self.address, self.postcode, self.description, self.info_url, self.comment_url, self.date_received
  77. if not self.postcode:
  78. self.postcode = getPostcodeFromText(self.address)
  79. contents = [
  80. u"<council_reference><![CDATA[%s]]></council_reference>" %(self.council_reference),
  81. u"<address><![CDATA[%s]]></address>" %(self.address),
  82. u"<postcode><![CDATA[%s]]></postcode>" %self.postcode,
  83. u"<description><![CDATA[%s]]></description>" %(self.description),
  84. u"<info_url><![CDATA[%s]]></info_url>" %(self.info_url),
  85. u"<comment_url><![CDATA[%s]]></comment_url>" %(self.comment_url),
  86. u"<date_received><![CDATA[%s]]></date_received>" %self.date_received.strftime(date_format),
  87. ]
  88. if self.osgb_x:
  89. contents.append(u"<osgb_x>%s</osgb_x>" %(self.osgb_x))
  90. if self.osgb_y:
  91. contents.append(u"<osgb_y>%s</osgb_y>" %(self.osgb_y))
  92. return u"<application>\n%s\n</application>" %('\n'.join(contents))