def account_details(self, request): if request.method == 'POST': form = ChangeDetailsForm(request.POST) else: form = ChangeDetailsForm() form.configure(request) if form.is_valid(): data = form.cleaned_data u = request.user p = get_customer_model().objects.get(user=u) if request.settings.mailchimp_enabled: # subscription details ms = MailSnake(request.settings.mailchimp_api) person_name = [data.get('first_name'), data.get('last_name')] merge_vars = {'FNAME': ' '.join(person_name)} # unsubscribe if email changed or we no longer want to be subscribed if 'email_address' in form.changed_data or ('newsletter' in form.changed_data and not data.get('newsletter')): try: ms.listUnsubscribe(id=request.settings.mailchimp_list_id, email_address=u.email) except: pass # subscribe if newsletter subscription changed or email was changed if ('email_address' in form.changed_data or 'newsletter' in form.changed_data) and data.get('newsletter'): try: ms.listSubscribe(id=request.settings.mailchimp_list_id, email_address=data.get('email_address'), merge_vars=merge_vars) except: pass # update newletter state in profile p.newsletter = data.get('newsletter') # update personal details on user record u.first_name = data.get('first_name') u.last_name = data.get('last_name') u.email = data.get('email_address') u.save() # update personal details on profile record p.title = data.get('title') p.first_name = data.get('first_name') p.last_name = data.get('last_name') p.telephone = data.get('phone') p.email = data.get('email_address') p.save() # success message and redirect to dashboard messages.success(request, 'Your details have been changed.') return HttpResponseRedirect(reverse('shop.account.index')) return { 'account_section': True, 'form': form }
def pre_save_user_profile(sender, instance, raw, **kwargs): # Don't update MailChimp if this is a raw save (to prevent cycles when updating in response to the MailChimp webhook) if not raw: try: original = UserProfile.objects.get(pk=instance.pk) except UserProfile.DoesNotExist: pass else: if not original.newsletter == instance.newsletter: user = instance.user # Update MailChimp whenever the newsletter preference changes for an active user if user.is_active: email = user.email.encode('utf-8') profile = instance mailsnake = MailSnake(settings.MAILCHIMP_API_KEY) if profile.newsletter: logger.debug('Subscribing ' + email + ' to MailChimp list...') try: mailsnake.listSubscribe( id=settings.MAILCHIMP_LIST_ID, email_address=email, double_optin=False, update_existing=True, send_welcome=True) except: logger.exception('Failed to subscribe ' + email + ' to MailChimp list') else: logger.debug('Unsubscribing ' + email + ' from MailChimp list...') try: mailsnake.listUnsubscribe( id=settings.MAILCHIMP_LIST_ID, email_address=email) except: logger.exception('Failed to unsubscribe ' + email + ' from MailChimp list')
class Invitation: class List: def __init__(self, name, list_id): self.__name = name self.__list_id = list_id def subscribe(self, ms, email_address): try: ms.listSubscribe(id=self.__list_id, email_address=email_address, double_optin=False) elle.log.trace("%s: subscribed to %s (%s)" % (email_address, self.__name, self.__list_id)) return True except: elle.log.warn("couldn't subscribe %s to %s (%s)" % (email_address, self.__name, self.__list_id)) return False def unsubscribe(self, ms, email_address): try: if isinstance(email_address, (set, list)): ms.listBatchUnsubscribe(id=self.__list_id, emails=email_address) else: ms.listUnsubscribe(id=self.__list_id, email_address=email_address) elle.log.trace("%s: unsubscribed from %s (%s)" % (email_address, self.__name, self.__list_id)) return True except: elle.log.warn("couldn't unsubscribe %s from %s (%s)" % (email_address, self.__name, self.__list_id)) return False def contains(self, ms, email_address): res = ms.listMemberInfo(id=self.__list_id, email_address=email_address) if not res['success']: return False if len(res['data']) == 0: return False return res['data'][0]['status'] == 'subscribed' def members(self, ms): res = ms.listMembers(id=self.__list_id) return res['data'] def __init__(self, active=True): self.__active = active if self.__active: from mailsnake import MailSnake self.ms = MailSnake(conf.MAILCHIMP_APIKEY) self.lists = {} for name, list_id in chain(os_lists.items(), general_lists.items()): self.add_list(name=name, list_id=list_id) def add_list(self, name, list_id): with elle.log.trace("add list: %s (%s)" % (name, list_id)): self.lists.update( {name: Invitation.List(list_id=list_id, name=name)}) def is_active(method): def wrapper(wrapped, self, *a, **ka): if not self.__active: elle.log.warn( "invitation was ignored because inviter is inactive") return # Return an empty func. return wrapped(self, *a, **ka) return decorator.decorator(wrapper, method) @is_active def move_from_invited_to_userbase(self, ghost_mail, new_mail): try: self.ms.listUnsubscribe(id=INVITED_LIST, email_address=ghost_mail) elle.log.trace("%s: unsubscribed from INVITED: %s" % (ghost_mail, INVITED_LIST)) except: elle.log.warn("Couldn't unsubscribe %s from INVITED: %s" % (ghost_mail, INVITED_LIST)) self.subscribe(new_mail) @is_active def subscribe(self, email, list_name='userbase'): mailing_list = self.lists.get(list_name) if mailing_list is not None: return mailing_list.subscribe(ms=self.ms, email_address=email) else: elle.log.warn("unknown list %s" % list_name) # XXX should be moved to another class. @is_active def unsubscribe(self, email, list_name='userbase'): mailing_list = self.lists.get(list_name) if mailing_list is not None: return mailing_list.unsubscribe(ms=self.ms, email_address=email) else: elle.log.warn("unknown list %s" % list_name) @is_active def subscribed(self, email, list_name='userbase'): mailing_list = self.lists.get(list_name) if mailing_list is not None: return mailing_list.contains(ms=self.ms, email_address=email) else: elle.log.warn("unknown list %s" % list_name) @is_active def members(self, list_name='userbase'): mailing_list = self.lists.get(list_name) if mailing_list is not None: return mailing_list.members(ms=self.ms) else: elle.log.warn("unknown list %s" % list_name)
def subscribe_update_cancel(request): user = getUser(request) if request.POST: if "cancel_subscribe" in request.POST: user_profile = user.get_profile() # remove subscription stripe.api_key = settings.STRIPE_API_KEY cu = stripe.Customer.retrieve(user_profile.stripeprofile) cu.cancel_subscription() # remove from email list LIST_IDS = {"sflaunch_group": "eaa0a378ba"} l_id = LIST_IDS["sflaunch_group"] ms = MailSnake(settings.MAILCHIMP_API_KEY) success = ms.listUnsubscribe(id=l_id, email_address=user.email, send_notify=False) # email admin / email user subscription_cancel_email(user.email) # update userprofile sub = Subscription.objects.get(userprofile=user_profile) sub.subscription = False sub.subscription_type = "canceled" sub.save() messages.add_message(request, messages.SUCCESS, "Subscription canceled!") return redirect("/profile/manage") elif "update_stripe_cc" in request.POST: updateStripecc_Subscription(request) messages.add_message(request, messages.SUCCESS, "Credit card updated!") template = "profile/subscribe_update_cancel.html" init = {"country": "US"} payForm = PaymentForm(initial=init) customer = None if user: try: user_profile = user.get_profile() customer_id = user_profile.stripeprofile stripe.api_key = settings.STRIPE_API_KEY customer = stripe.Customer.retrieve(customer_id) except UserProfile.DoesNotExist: pass except stripe.InvalidRequestError: pass except AttributeError: pass change_card = False if "change_card" in request.GET: if request.GET.get("change_card") == "True": change_card = True data = {"customer": customer, "change_card": change_card, "payment": payForm, "user": user} return render_to_response(template, data, context_instance=RequestContext(request))