utils.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import calendar
  2. from itertools import chain
  3. from django.conf import settings
  4. from django.core.mail import EmailMultiAlternatives
  5. from django.template.loader import render_to_string
  6. from store_order.models import OrderItem, Order
  7. def year_list(current_year, num_years=5):
  8. """ Return a list of years, starting at current year ending with
  9. (current year + num_years), the default is 5
  10. """
  11. return [(i, i) for i in range(current_year, current_year + num_years)]
  12. def month_list():
  13. """ Return iterated list of months
  14. """
  15. return [(i, calendar.month_name[i]) for i in range(1, len(calendar.month_name))]
  16. def get_renewals_by_user(user):
  17. """ Returns subscriptions (OrderItems) by user
  18. Each subscription
  19. """
  20. from accounts.subscriptions import Subscription
  21. if not user:
  22. return None
  23. ordered_items = get_subscriptions().filter(
  24. order__user=user, gift_detail__exact=None
  25. )
  26. order_items = list(chain(ordered_items))
  27. subscriptions = []
  28. for order_item in order_items:
  29. if order_item.product.child().renewal:
  30. subscriptions.append(
  31. Subscription(
  32. order_item.product.title,
  33. order_item.order.date,
  34. order_item.product.child().duration,
  35. )
  36. )
  37. return subscriptions
  38. def get_subscriptions_by_user(user):
  39. """ Returns subscriptions (OrderItems) by user
  40. Each subscription
  41. """
  42. from accounts.subscriptions import Subscription
  43. if not user:
  44. return None
  45. ordered_items = get_subscriptions().filter(
  46. order__user=user, gift_detail__exact=None
  47. )
  48. gifted_items = get_subscriptions().filter(gift_detail__email__iexact=user.email)
  49. order_items = list(chain(ordered_items, gifted_items))
  50. subscriptions = []
  51. for order_item in order_items:
  52. if not order_item.product.child().renewal:
  53. subscriptions.append(
  54. Subscription(
  55. order_item.product.title,
  56. order_item.order.date,
  57. order_item.product.child().duration,
  58. )
  59. )
  60. return subscriptions
  61. def get_subscriptions(order=None):
  62. """ Subscriptions are really OrderItems with a subscription product
  63. attached.
  64. """
  65. from catalog.models import Subscription
  66. order_items = OrderItem.objects.filter(
  67. product__subclass_type__iexact=Subscription.__name__.lower()
  68. )
  69. if order:
  70. order_items = order_items.filter(order=order)
  71. return order_items
  72. def send_change_of_address_email(
  73. title, account_number, date_effective, date_from, date_to, old_address, new_address
  74. ):
  75. template = "storefront/change_of_address_email.html"
  76. DEFAULT_FROM_EMAIL = getattr(settings, "DEFAULT_FROM_EMAIL")
  77. NOTIFICATIONS_EMAIL = getattr(settings, "NOTIFICATIONS_EMAIL", [])
  78. subject, from_email, to = (
  79. "Change of Address Update",
  80. DEFAULT_FROM_EMAIL,
  81. NOTIFICATIONS_EMAIL,
  82. )
  83. data = {"old_address": old_address, "new_address": new_address}
  84. data.update({"title": title, "account_number": account_number})
  85. data.update(
  86. {"date_effective": date_effective, "date_from": date_from, "date_to": date_to}
  87. )
  88. text_content = render_to_string(template, dictionary=data)
  89. html_content = render_to_string(template, dictionary=data)
  90. msg = EmailMultiAlternatives(subject, text_content, from_email, to)
  91. msg.attach_alternative(html_content, "text/html")
  92. msg.send()
  93. def send_new_account_email(email, password, user, profile, site):
  94. template = "storefront/new_account_email.html"
  95. DEFAULT_FROM_EMAIL = getattr(settings, "DEFAULT_FROM_EMAIL")
  96. subject, from_email, to = ("New Account", DEFAULT_FROM_EMAIL, email)
  97. data = {"email": email, "password": password, "site": site}
  98. data.update({"first_name": user.first_name, "last_name": user.last_name})
  99. data.update(
  100. {
  101. "company": profile.company,
  102. "address1": profile.address1,
  103. "address2": profile.address2,
  104. "city": profile.city,
  105. "region": profile.region,
  106. "phone": profile.phone,
  107. "country": profile.country,
  108. }
  109. )
  110. text_content = render_to_string(template, dictionary=data)
  111. html_content = render_to_string(template, dictionary=data)
  112. msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
  113. msg.attach_alternative(html_content, "text/html")
  114. msg.send()