utils.py 3.1 KB

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