utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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
  16. range(1, len(calendar.month_name))]
  17. def get_renewals_by_user(user):
  18. """ Returns subscriptions (OrderItems) by user
  19. Each subscription
  20. """
  21. from accounts.subscriptions import Subscription
  22. if not user:
  23. return None
  24. ordered_items = get_subscriptions().filter(
  25. order__user=user, gift_detail__exact=None)
  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(Subscription(
  31. order_item.product.title,
  32. order_item.order.date,
  33. order_item.product.child().duration))
  34. return subscriptions
  35. def get_subscriptions_by_user(user):
  36. """ Returns subscriptions (OrderItems) by user
  37. Each subscription
  38. """
  39. from accounts.subscriptions import Subscription
  40. if not user:
  41. return None
  42. ordered_items = get_subscriptions().filter(
  43. order__user=user, gift_detail__exact=None)
  44. gifted_items = get_subscriptions().filter(
  45. gift_detail__email__iexact=user.email)
  46. order_items = list(chain(ordered_items, gifted_items))
  47. subscriptions = []
  48. for order_item in order_items:
  49. if not order_item.product.child().renewal:
  50. subscriptions.append(Subscription(
  51. order_item.product.title,
  52. order_item.order.date,
  53. order_item.product.child().duration))
  54. return subscriptions
  55. def get_subscriptions(order=None):
  56. """ Subscriptions are really OrderItems with a subscription product
  57. attached.
  58. """
  59. from catalog.models import Subscription
  60. order_items = OrderItem.objects.filter(
  61. product__subclass_type__iexact=Subscription.__name__.lower())
  62. if order:
  63. order_items = order_items.filter(order=order)
  64. return order_items
  65. def send_change_of_address_email(
  66. title, account_number, date_effective, date_from, date_to,
  67. old_address, new_address):
  68. template = 'storefront/change_of_address_email.html'
  69. DEFAULT_FROM_EMAIL = getattr(settings, 'DEFAULT_FROM_EMAIL')
  70. NOTIFICATIONS_EMAIL = getattr(settings, 'NOTIFICATIONS_EMAIL', [])
  71. subject, from_email, to = ('Change of Address Update', DEFAULT_FROM_EMAIL,
  72. NOTIFICATIONS_EMAIL)
  73. data = {'old_address': old_address, 'new_address': new_address}
  74. data.update({'title': title, 'account_number': account_number})
  75. data.update({'date_effective': date_effective, 'date_from': date_from,
  76. 'date_to': date_to})
  77. text_content = render_to_string(template, dictionary=data)
  78. html_content = render_to_string(template, dictionary=data)
  79. msg = EmailMultiAlternatives(subject, text_content, from_email, to)
  80. msg.attach_alternative(html_content, "text/html")
  81. msg.send()
  82. def send_new_account_email(email, password, user, profile, site):
  83. template = 'storefront/new_account_email.html'
  84. DEFAULT_FROM_EMAIL = getattr(settings, 'DEFAULT_FROM_EMAIL')
  85. subject, from_email, to = ('New Account', DEFAULT_FROM_EMAIL, email)
  86. data = {'email': email, 'password': password, 'site': site}
  87. data.update({'first_name': user.first_name, 'last_name': user.last_name})
  88. data.update({'company': profile.company, 'address1': profile.address1,
  89. 'address2': profile.address2, 'city': profile.city,
  90. 'region': profile.region, 'phone': profile.phone,
  91. 'country': profile.country})
  92. text_content = render_to_string(template, dictionary=data)
  93. html_content = render_to_string(template, dictionary=data)
  94. msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
  95. msg.attach_alternative(html_content, "text/html")
  96. msg.send()