utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """ Catalog utils """
  2. import logging
  3. from catalog.constants import (
  4. OUTSTATE, INSTATE, FOREIGN, MAINE, HANCOCK, UNITED_STATES, INCOUNTY)
  5. from catalog.models import ZipCode
  6. from django.contrib.localflavor.us.us_states import STATE_CHOICES
  7. LOG = logging.getLogger(__name__)
  8. def subscription_type(the_zip, state, country):
  9. """ For a zip code, looks up the state and country to determine the
  10. subscription type
  11. :returns: INCOUNTY, INSTATE, OUTSTATE, FOREIGN
  12. """
  13. LOG.debug("Subscription type: {0}, {1}, {2}".format(
  14. the_zip, state, country))
  15. if country == UNITED_STATES:
  16. try:
  17. zipcode = ZipCode.objects.get(zipcode=the_zip)
  18. if zipcode.county == HANCOCK:
  19. return INCOUNTY
  20. return _sub_by_state(zipcode.state)
  21. except ZipCode.DoesNotExist:
  22. return _sub_by_state(state)
  23. else:
  24. return FOREIGN
  25. def get_type(postal_code, region, country_code, postal_code2=None,
  26. region2=None, country_code2=None):
  27. """ get the type, only use the second address if there's values """
  28. if country_code2 and region2:
  29. shipping_type1 = subscription_type(postal_code, region, country_code)
  30. shipping_type2 = subscription_type(postal_code2, region2,
  31. country_code2)
  32. if _type_index(shipping_type1) > _type_index(shipping_type2):
  33. return shipping_type1
  34. else:
  35. return shipping_type2
  36. else:
  37. return subscription_type(postal_code, region, country_code)
  38. def get_type_by_profile(profile, shipping_type=None):
  39. """ For a profile, return the subscription type and first class option.
  40. type is In-State, Out of State, or Foreign
  41. first class is a boolean value, true if its an option
  42. """
  43. def _address_not_empty(address):
  44. """ is address not empty """
  45. return (address and address.postal_code and address.region and
  46. address.postal_code != '')
  47. if profile:
  48. shipping_type = subscription_type(profile.postal_code, profile.region,
  49. profile.country)
  50. address = profile.secondary_address
  51. if _address_not_empty(address):
  52. secondary_type = subscription_type(
  53. address.postal_code, address.region, address.country)
  54. if _type_index(secondary_type) > _type_index(shipping_type):
  55. shipping_type = secondary_type
  56. return shipping_type
  57. def _type_index(value):
  58. """ type index """
  59. return [INCOUNTY, INSTATE, OUTSTATE, FOREIGN].index(value)
  60. def _is_maine(state):
  61. """ Is the state maine? """
  62. return state.lower() == MAINE.lower()
  63. def _is_us_state(my_state):
  64. """ is a us state? """
  65. states = [state.lower() for state, state_string in STATE_CHOICES]
  66. return my_state.lower() in states
  67. def _sub_by_state(state):
  68. """ For a state, determines the subscription type
  69. :returns: INSTATE, OUTSTATE
  70. """
  71. if _is_maine(state):
  72. return INSTATE
  73. if _is_us_state(state):
  74. return OUTSTATE