analytics: Extract request.user for typecheck.

This commit is contained in:
PIG208 2021-07-26 20:23:13 +08:00 committed by Tim Abbott
parent 7d1c475f69
commit cd678232bd
1 changed files with 13 additions and 11 deletions

View File

@ -123,16 +123,18 @@ def support(request: HttpRequest) -> HttpResponse:
realm_id: str = assert_is_not_none(request.POST.get("realm_id")) realm_id: str = assert_is_not_none(request.POST.get("realm_id"))
realm = Realm.objects.get(id=realm_id) realm = Realm.objects.get(id=realm_id)
acting_user = request.user
assert isinstance(acting_user, UserProfile)
if request.POST.get("plan_type", None) is not None: if request.POST.get("plan_type", None) is not None:
new_plan_type = int(assert_is_not_none(request.POST.get("plan_type"))) new_plan_type = int(assert_is_not_none(request.POST.get("plan_type")))
current_plan_type = realm.plan_type current_plan_type = realm.plan_type
do_change_plan_type(realm, new_plan_type, acting_user=request.user) do_change_plan_type(realm, new_plan_type, acting_user=acting_user)
msg = f"Plan type of {realm.string_id} changed from {get_plan_name(current_plan_type)} to {get_plan_name(new_plan_type)} " msg = f"Plan type of {realm.string_id} changed from {get_plan_name(current_plan_type)} to {get_plan_name(new_plan_type)} "
context["success_message"] = msg context["success_message"] = msg
elif request.POST.get("discount", None) is not None: elif request.POST.get("discount", None) is not None:
new_discount = Decimal(assert_is_not_none(request.POST.get("discount"))) new_discount = Decimal(assert_is_not_none(request.POST.get("discount")))
current_discount = get_discount_for_realm(realm) or 0 current_discount = get_discount_for_realm(realm) or 0
attach_discount_to_realm(realm, new_discount, acting_user=request.user) attach_discount_to_realm(realm, new_discount, acting_user=acting_user)
context[ context[
"success_message" "success_message"
] = f"Discount of {realm.string_id} changed to {new_discount}% from {current_discount}%." ] = f"Discount of {realm.string_id} changed to {new_discount}% from {current_discount}%."
@ -144,7 +146,7 @@ def support(request: HttpRequest) -> HttpResponse:
except ValidationError as error: except ValidationError as error:
context["error_message"] = error.message context["error_message"] = error.message
else: else:
do_change_realm_subdomain(realm, new_subdomain, acting_user=request.user) do_change_realm_subdomain(realm, new_subdomain, acting_user=acting_user)
request.session[ request.session[
"success_message" "success_message"
] = f"Subdomain changed from {old_subdomain} to {new_subdomain}" ] = f"Subdomain changed from {old_subdomain} to {new_subdomain}"
@ -154,25 +156,25 @@ def support(request: HttpRequest) -> HttpResponse:
elif request.POST.get("status", None) is not None: elif request.POST.get("status", None) is not None:
status = request.POST.get("status") status = request.POST.get("status")
if status == "active": if status == "active":
do_send_realm_reactivation_email(realm, acting_user=request.user) do_send_realm_reactivation_email(realm, acting_user=acting_user)
context[ context[
"success_message" "success_message"
] = f"Realm reactivation email sent to admins of {realm.string_id}." ] = f"Realm reactivation email sent to admins of {realm.string_id}."
elif status == "deactivated": elif status == "deactivated":
do_deactivate_realm(realm, acting_user=request.user) do_deactivate_realm(realm, acting_user=acting_user)
context["success_message"] = f"{realm.string_id} deactivated." context["success_message"] = f"{realm.string_id} deactivated."
elif request.POST.get("billing_method", None) is not None: elif request.POST.get("billing_method", None) is not None:
billing_method = request.POST.get("billing_method") billing_method = request.POST.get("billing_method")
if billing_method == "send_invoice": if billing_method == "send_invoice":
update_billing_method_of_current_plan( update_billing_method_of_current_plan(
realm, charge_automatically=False, acting_user=request.user realm, charge_automatically=False, acting_user=acting_user
) )
context[ context[
"success_message" "success_message"
] = f"Billing method of {realm.string_id} updated to pay by invoice." ] = f"Billing method of {realm.string_id} updated to pay by invoice."
elif billing_method == "charge_automatically": elif billing_method == "charge_automatically":
update_billing_method_of_current_plan( update_billing_method_of_current_plan(
realm, charge_automatically=True, acting_user=request.user realm, charge_automatically=True, acting_user=acting_user
) )
context[ context[
"success_message" "success_message"
@ -180,14 +182,14 @@ def support(request: HttpRequest) -> HttpResponse:
elif request.POST.get("sponsorship_pending", None) is not None: elif request.POST.get("sponsorship_pending", None) is not None:
sponsorship_pending = request.POST.get("sponsorship_pending") sponsorship_pending = request.POST.get("sponsorship_pending")
if sponsorship_pending == "true": if sponsorship_pending == "true":
update_sponsorship_status(realm, True, acting_user=request.user) update_sponsorship_status(realm, True, acting_user=acting_user)
context["success_message"] = f"{realm.string_id} marked as pending sponsorship." context["success_message"] = f"{realm.string_id} marked as pending sponsorship."
elif sponsorship_pending == "false": elif sponsorship_pending == "false":
update_sponsorship_status(realm, False, acting_user=request.user) update_sponsorship_status(realm, False, acting_user=acting_user)
context["success_message"] = f"{realm.string_id} is no longer pending sponsorship." context["success_message"] = f"{realm.string_id} is no longer pending sponsorship."
elif request.POST.get("approve_sponsorship") is not None: elif request.POST.get("approve_sponsorship") is not None:
if request.POST.get("approve_sponsorship") == "approve_sponsorship": if request.POST.get("approve_sponsorship") == "approve_sponsorship":
approve_sponsorship(realm, acting_user=request.user) approve_sponsorship(realm, acting_user=acting_user)
context["success_message"] = f"Sponsorship approved for {realm.string_id}" context["success_message"] = f"Sponsorship approved for {realm.string_id}"
elif request.POST.get("downgrade_method", None) is not None: elif request.POST.get("downgrade_method", None) is not None:
downgrade_method = request.POST.get("downgrade_method") downgrade_method = request.POST.get("downgrade_method")
@ -209,7 +211,7 @@ def support(request: HttpRequest) -> HttpResponse:
] = f"{realm.string_id} downgraded and voided {voided_invoices_count} open invoices" ] = f"{realm.string_id} downgraded and voided {voided_invoices_count} open invoices"
elif request.POST.get("scrub_realm", None) is not None: elif request.POST.get("scrub_realm", None) is not None:
if request.POST.get("scrub_realm") == "scrub_realm": if request.POST.get("scrub_realm") == "scrub_realm":
do_scrub_realm(realm, acting_user=request.user) do_scrub_realm(realm, acting_user=acting_user)
context["success_message"] = f"{realm.string_id} scrubbed." context["success_message"] = f"{realm.string_id} scrubbed."
query = request.GET.get("q", None) query = request.GET.get("q", None)