Esempio n. 1
0
    def _log_mailchimp_error(e):

        # Log a warning, an InfluxDB metric, and a Sentry alert.

        log.warning("Failed to contact MailChimp API: %s" % e)
        influx_metric("mailchimp_request_failures", {"count": 1})
        error_handler(e)
Esempio n. 2
0
    def post(self, request):
        marketing_prefs = request.POST.get("marketing", "") == "on"

        request.user.settings["email"] = {
            "marketing": marketing_prefs,
            "updated": timezone.now().isoformat(),
        }
        request.user.save()

        try:
            client = get_mailchimp_client()
            email = find_best_email_for_user(request.user)
            client.lists.members.create_or_update(
                settings.MAILCHIM_LIST_KEY_ID,
                get_subscriber_hash(email.email), {
                    "email_address":
                    email.email,
                    "status_if_new":
                    get_mailchimp_subscription_status(request.user)
                })
        except Exception as e:
            log.warning("Failed to contact MailChimp API: %s" % e)
            influx_metric("mailchimp_request_failures", {"count": 1})

        messages.info(request, _("Your email preferences have been saved."))
        return redirect(self.success_url)
Esempio n. 3
0
 def get(self, request, id):
     claim = get_uuid_object_or_404(AccountClaim, id=id)
     log.info("Claim %r: Token=%r, User=%r", claim, claim.token,
              claim.token.user)
     if claim.token.user:
         if claim.token.user.is_fake:
             count = GameReplay.objects.filter(
                 user=claim.token.user).update(user=request.user)
             log.info("Updated %r replays. Deleting %r.", count,
                      claim.token.user)
             # For now we just delete the fake user, because we are not using it.
             claim.token.user.delete()
         else:
             log.warning("%r is a real user. Deleting %r.",
                         claim.token.user, claim)
             # Something's wrong. Get rid of the claim and reject the request.
             claim.delete()
             influx_metric("hsreplaynet_account_claim", {"count": 1},
                           error=1)
             return HttpResponseForbidden(
                 "This token has already been claimed.")
     claim.token.user = request.user
     claim.token.save()
     # Replays are claimed in AuthToken post_save signal (games.models)
     claim.delete()
     messages.info(request, "You have claimed your account. Nice!")
     influx_metric("hsreplaynet_account_claim", {"count": 1})
     return redirect(self.get_redirect_url(request))
Esempio n. 4
0
 def get(self, request, id):
     claim = get_uuid_object_or_404(AccountClaim, id=id)
     log.info("Claim %r: Token=%r, User=%r", claim, claim.token,
              claim.token.user)
     if claim.token.user:
         if claim.token.user.is_fake:
             count = GameReplay.objects.filter(
                 user=claim.token.user).update(user=request.user)
             log.info("Updated %r replays. Deleting %r.", count,
                      claim.token.user)
             # For now we just delete the fake user, because we are not using it.
             claim.token.user.delete()
         else:
             log.warning("%r is a real user. Deleting %r.",
                         claim.token.user, claim)
             # Something's wrong. Get rid of the claim and reject the request.
             claim.delete()
             return HttpResponseForbidden(
                 "This token has already been claimed.")
     claim.token.user = request.user
     claim.token.save()
     # Replays are claimed in AuthToken post_save signal (games.models)
     claim.delete()
     msg = "You have claimed your account. Yay!"
     # XXX: using WARNING as a hack to ignore login/logout messages for now
     messages.add_message(request, messages.WARNING, msg)
     return redirect(settings.LOGIN_REDIRECT_URL)
Esempio n. 5
0
def add_address_to_mailchimp(request, user, from_email_address,
                             to_email_address, **_):
    """Signal handler for the email_changed event to add or update emails in MailChimp"""

    client = get_mailchimp_client()

    try:
        if from_email_address:

            # If there was a previous email address, we may (or may not) have added it to
            # our list in MailChimp already, along with zero or more tags. So let's try to
            # update it via the subscriber hash of the _previous_ address and update the
            # email address to the _new_ address.

            client.lists.members.create_or_update(
                settings.MAILCHIMP_LIST_KEY_ID,
                get_subscriber_hash(from_email_address.email), {
                    "email_address": to_email_address.email,
                    "status_if_new": get_mailchimp_subscription_status(user)
                })

            influx_metric("mailchimp_requests", {"count": 1},
                          method="create_or_update")

        else:

            # But if there was no previous primary address, just add a new list member.

            client.lists.members.create(
                settings.MAILCHIMP_LIST_KEY_ID, {
                    "email_address": to_email_address.email,
                    "status": get_mailchimp_subscription_status(user)
                })

            influx_metric("mailchimp_requests", {"count": 1}, method="create")

    except Exception as e:
        log.warning("Failed to contact MailChimp API: %s" % e)
        influx_metric("mailchimp_request_failures", {"count": 1})
Esempio n. 6
0
	def get(self, request, id):
		claim = get_uuid_object_or_404(AccountClaim, id=id)
		log.info("Claim %r: Token=%r, User=%r", claim, claim.token, claim.token.user)
		if claim.token.user:
			if claim.token.user.is_fake:
				count = GameReplay.objects.filter(user=claim.token.user).update(user=request.user)
				log.info("Updated %r replays. Deleting %r.", count, claim.token.user)
				# For now we just delete the fake user, because we are not using it.
				claim.token.user.delete()
			else:
				log.warning("%r is a real user. Deleting %r.", claim.token.user, claim)
				# Something's wrong. Get rid of the claim and reject the request.
				claim.delete()
				return HttpResponseForbidden("This token has already been claimed.")
		claim.token.user = request.user
		claim.token.save()
		# Replays are claimed in AuthToken post_save signal (games.models)
		claim.delete()
		msg = "You have claimed your account. Yay!"
		# XXX: using WARNING as a hack to ignore login/logout messages for now
		messages.add_message(request, messages.WARNING, msg)
		return redirect(settings.LOGIN_REDIRECT_URL)
Esempio n. 7
0
def _update_mailchimp_tags_for_premium_subscriber(user):
    email = find_best_email_for_user(user)

    if email:
        tag = PremiumSubscriberTag()
        if tag.should_apply_to(user) and tag.add_user_to_tag_group(user):

            client = get_mailchimp_client()
            list_key_id = settings.MAILCHIMP_LIST_KEY_ID
            email_hash = get_subscriber_hash(email.email)

            # At this point we have no idea whether the user's email address is already
            # registered with MailChimp, so do a defensive create-or-update to make sure
            # it's on the list before we give them the premium tag.

            try:
                client.lists.members.create_or_update(
                    list_key_id, email_hash, {
                        "email_address": email.email,
                        "status_if_new":
                        get_mailchimp_subscription_status(user),
                    })

                influx_metric("mailchimp_requests", {"count": 1},
                              method="create_or_update")

                # ...then assign the premium tag.

                client.lists.members.tags.add(list_key_id, email_hash,
                                              tag.name)
                influx_metric("mailchimp_requests", {"count": 1},
                              method="add_tags")

            except Exception as e:
                log.warning("Failed to contact MailChimp API: %s" % e)
                influx_metric("mailchimp_request_failures", {"count": 1})
Esempio n. 8
0
 def error(self, id):
     log.warning("%r got error %r when making account primary" %
                 (self.request.user, id))
     influx_metric("hsreplaynet_make_primary", {"count": 1}, error=id)
     messages.error(self.request, _("Could not make account primary."))
     return redirect(self.success_url)