def merge_users(base_user, user): """ Merge user into base_user """ def do_queryset(qs, **kwargs): if not kwargs: kwargs = {"user": base_user} ret = qs.update(**kwargs) log.info("Merging %r -> %r: %r", user, base_user, ret) return ret # Auth tokens do_queryset(user.auth_tokens) # Replays do_queryset(user.replays) # Comments do_queryset(user.comment_comments) do_queryset(user.comment_flags) # Pegasus Accounts do_queryset(user.pegasusaccount_set) # Emails do_queryset(user.emailaddress_set) # Social accounts do_queryset(user.socialaccount_set) # Webhooks do_queryset(user.webhooks) # Stripe customers # Always delete livemode customers Customer.objects.filter(livemode=False, subscriber=user).delete() customers = Customer.objects.filter(livemode=True) user_has_customer = customers.filter(subscriber=user).exists() base_user_has_customer = customers.filter(subscriber=base_user).exists() if user_has_customer and base_user_has_customer: cus1 = customers.get(subscriber=user) cus2 = customers.get(subscriber=base_user) log.warn("Found customers for both users: %r and %r. Merge manually!", cus1, cus2) elif user_has_customer: cus = customers.get(subscriber=user) log.info("Moving Customer %r from user %r to user %r", cus, user, base_user) cus.subscriber = base_user cus.metadata["django_account"] = str(base_user.pk) cus.save() elif base_user_has_customer: log.info("Found Stripe customer on %r, nothing to do.", base_user) else: log.info("No Stripe customers to merge.")
def merge_users(base_user, user): """ Merge user into base_user """ def do_queryset(qs, **kwargs): if not kwargs: kwargs = {"user": base_user} ret = qs.update(**kwargs) log.info("Merging %r -> %r: %r", user, base_user, ret) return ret # Auth tokens do_queryset(user.auth_tokens) # Replays do_queryset(user.replays) # Packs do_queryset(user.packs) # Blizzard Accounts do_queryset(user.blizzard_accounts) # Emails if base_user.emailaddress_set.filter(primary=True).count(): user.emailaddress_set.update(primary=False) do_queryset(user.emailaddress_set) # OAuth2 do_queryset(user.oauth2_application) do_queryset(user.oauth2_provider_accesstoken) do_queryset(user.oauth2_provider_refreshtoken) do_queryset(user.oauth2_provider_grant) # Social accounts do_queryset(user.socialaccount_set) # Webhooks do_queryset(user.webhook_endpoints) # Stripe customers # Always delete livemode customers Customer.objects.filter(livemode=False, subscriber=user).delete() customers = Customer.objects.filter(livemode=True) user_has_customer = customers.filter(subscriber=user).exists() base_user_has_customer = customers.filter(subscriber=base_user).exists() if user_has_customer and base_user_has_customer: cus1 = customers.get(subscriber=user) cus2 = customers.get(subscriber=base_user) log.warn("Found customers for both users: %r and %r. Merge manually!", cus1, cus2) elif user_has_customer: cus = customers.get(subscriber=user) log.info("Moving Customer %r from user %r to user %r", cus, user, base_user) cus.subscriber = base_user cus.metadata["django_account"] = str(base_user.pk) cus.save() elif base_user_has_customer: log.info("Found Stripe customer on %r, nothing to do.", base_user) else: log.info("No Stripe customers to merge.") # dj-paypal do_queryset(user.paypal_payers) do_queryset(user.preparedbillingagreement_set) do_queryset(user.billingagreement_set)
def _get_query_and_params(request, name): query = get_redshift_query(name) if not query: raise Http404("No query named: %s" % name) supplied_params = request.GET.dict() deck = None if "deck_id" in supplied_params and not supplied_params["deck_id"].isdigit( ): # We got sent a shortid, so we need to translate it into a deck_id int try: deck = Deck.objects.get_by_shortid(supplied_params["deck_id"]) supplied_params["deck_id"] = str(deck.id) except Deck.DoesNotExist: raise Http404("Deck does not exist") if query.is_personalized: if request.user and request.user.is_authenticated: if "Region" in supplied_params and "account_lo" in supplied_params: # The parameters region and account_lo were both supplied, use these supplied_region = supplied_params["Region"] supplied_account_lo = supplied_params["account_lo"] if not (supplied_region.isdigit() and supplied_account_lo.isdigit()): return JsonResponse( { "msg": "Both account_lo and Region should be numeric." }, status=400) user_owns_blizzard_account = request.user.blizzard_accounts.filter( region__exact=int(supplied_region), account_lo__exact=int(supplied_account_lo)).exists() if not user_owns_blizzard_account and not request.user.is_staff: return HttpResponseForbidden() elif "Region" not in supplied_params and "account_lo" not in supplied_params: # Neither region nor account_lo were supplied, default to first default_blizzard_account = request.user.blizzard_accounts.first( ) if default_blizzard_account: supplied_params[ "Region"] = default_blizzard_account.region.name supplied_params[ "account_lo"] = default_blizzard_account.account_lo else: raise Http404("User does not have any Blizzard Accounts.") else: # Supplying only either Region or account_lo is a bad request msg = "Personalized queries require both region and account_lo to be present." return JsonResponse({"msg": msg}, status=400) # Map numeric region to FilterEnum if supplied_params["Region"].isdigit(): supplied_region = supplied_params["Region"] region_member = Region.from_int(int(supplied_region)) supplied_params["Region"] = region_member.name try: personal_parameterized_query = query.build_full_params( supplied_params) except InvalidOrMissingQueryParameterError as e: # Return a 400 Bad Request response log.warn(str(e)) return JsonResponse({"msg": str(e)}, status=400) if not user_is_eligible_for_query(request.user, query, personal_parameterized_query): return HttpResponseForbidden() return personal_parameterized_query else: # Anonymous or Fake Users Can Never Request Personal Stats return HttpResponseForbidden() else: if deck and not deck.eligible_for_global_stats: return HttpResponseForbidden() try: parameterized_query = query.build_full_params(supplied_params) except InvalidOrMissingQueryParameterError as e: # Return a 400 Bad Request response log.warn(str(e)) return JsonResponse({"msg": str(e)}, status=400) if not user_is_eligible_for_query(request.user, query, parameterized_query): return HttpResponseForbidden() return parameterized_query
def snapshot(self, game_format=enums.FormatType.FT_STANDARD, num_clusters=20, merge_threshold=0.85, inherit_threshold=0.85, lookback=7, min_observations=100, min_pilots=10, experimental_threshold=.01, dry_run=False): from hsreplaynet.analytics.processing import get_cluster_set_data data = get_cluster_set_data(game_format=game_format, lookback=lookback, min_observations=min_observations, min_pilots=min_pilots) log.info("\nClustering Raw Data Volume:") total_data_points = 0 total_observations = 0 experimental_thresholds = {} for player_class_name, data_points in data.items(): data_points_for_class = len(data_points) observations_for_class = sum(d["observations"] for d in data_points) total_observations += observations_for_class threshold_for_class = int(observations_for_class * experimental_threshold) experimental_thresholds[player_class_name] = threshold_for_class total_data_points += data_points_for_class log.info( "\t%s: %i Data Points,\t%i Observations,\tExperimental Threshold: %i" % (player_class_name, data_points_for_class, observations_for_class, threshold_for_class)) log.info("\tTotal Data Points: %i, Observations: %i\n" % (total_data_points, total_observations)) inheritance_missed = [] with transaction.atomic(): cs_snapshot = create_cluster_set(data, factory=ClusterSetSnapshot, num_clusters=num_clusters, merge_similarity=merge_threshold, consolidate=False, create_experimental_cluster=False) previous_snapshot = ClusterSetSnapshot.objects.filter( live_in_production=True).first() cs_snapshot.consolidate_clusters(merge_threshold) ClusterSetSnapshot.objects.update(latest=False) cs_snapshot.game_format = game_format cs_snapshot.latest = True cs_snapshot.save() uninherited_id_set = cs_snapshot.inherit_from_previous( previous_snapshot, merge_threshold=inherit_threshold) for uninherited_id in uninherited_id_set: inheritance_missed.append(str(uninherited_id)) cs_snapshot.create_experimental_clusters( experimental_cluster_thresholds=experimental_thresholds) if not dry_run: for class_cluster in cs_snapshot.class_clusters: class_cluster.cluster_set = cs_snapshot # Don't use pcp_adjustments after consolidation is complete # In order to leave signatures for tooltips unaffected. class_cluster.update_cluster_signatures( use_pcp_adjustment=False) class_cluster.save() for cluster in class_cluster.clusters: cluster.class_cluster = class_cluster cluster.save() if inheritance_missed: log.warn("Inheritance missed on archetypes {}".format( ", ".join(inheritance_missed))) return cs_snapshot
def _get_query_and_params(request, name): query = get_redshift_catalogue().get_query(name) if not query: raise Http404("No query named: %s" % name) supplied_params = request.GET.dict() if "deck_id" in supplied_params and not supplied_params["deck_id"].isdigit( ): # We got sent a shortid, so we need to translate it into a deck_id int try: deck = Deck.objects.get_by_shortid(supplied_params["deck_id"]) supplied_params["deck_id"] = str(deck.id) except Deck.DoesNotExist: raise Http404("Deck does not exist") if query.is_personalized: if request.user and request.user.is_authenticated: if "Region" not in supplied_params: default_blizzard_account = request.user.blizzard_accounts.first( ) if default_blizzard_account: supplied_params[ "Region"] = default_blizzard_account.region.name supplied_params[ "account_lo"] = default_blizzard_account.account_lo else: raise Http404("User does not have any Blizzard Accounts.") else: user_owns_blizzard_account = request.user.blizzard_accounts.filter( region__exact=int(supplied_params["Region"]), account_lo__exact=int( supplied_params["account_lo"])).exists() if not user_owns_blizzard_account: return HttpResponseForbidden() if supplied_params["Region"].isdigit(): region_member = Region.from_int(int(supplied_params["Region"])) supplied_params["Region"] = region_member.name try: personal_parameterized_query = query.build_full_params( supplied_params) except InvalidOrMissingQueryParameterError as e: # Return a 400 Bad Request response log.warn(str(e)) return HttpResponseBadRequest() if not user_is_eligible_for_query(request.user, query, personal_parameterized_query): return HttpResponseForbidden() return personal_parameterized_query else: # Anonymous or Fake Users Can Never Request Personal Stats return HttpResponseForbidden() else: try: parameterized_query = query.build_full_params(supplied_params) except InvalidOrMissingQueryParameterError as e: # Return a 400 Bad Request response log.warn(str(e)) return HttpResponseBadRequest() if not user_is_eligible_for_query(request.user, query, parameterized_query): return HttpResponseForbidden() return parameterized_query