def activate(self, request, pk): card = get_user_cards(request).get(pk=pk) if card.next_plan == PlanKind.FREE_TRIAL.value: return self.activate_free_trial(request, card) else: return self.activate_plan(request, card)
def delete(self, request, pk): card = get_user_cards(request).get(pk=pk) if card.subscription_id: cancel_subscription(card) card.delete() return Response({"success": True})
def payment_method(self, request, pk): card = get_user_cards(request).get(pk=pk) serializer = CardPaymentMethodRequestSerializer(data=request.data) serializer.is_valid(raise_exception=True) user = User.objects.get(pk=request.user.id) update_default_payment_method(user, card, serializer.data["payment_method"]) return Response({"success": True})
def partial_update(self, request, pk): card = get_user_cards(request).get(pk=pk) data = {**request.data} def patch_address(name): address_serializer = CardAddressPatchSerializer( getattr(card, name, False), data=request.data[name], partial=True) address_serializer.is_valid(raise_exception=True) address_serializer.save() if "address2" in request.data: if request.data["address2"] is None: card.address2 = None card.save() else: if card.address2 is None: card.address2 = CardAddress.objects.create() card.save() patch_address("address2") if "address1" in request.data: patch_address("address1") if "need_virtual" in request.data: if card.status == CardStatus.ACTIVE: card.status = CardStatus.VIRTUAL elif card.status == CardStatus.VIRTUAL: card.status = CardStatus.ACTIVE card.save() if "saved_background" in request.data: saved_background = SavedBackground.objects.get( pk=request.data["saved_background"]["id"]) card.saved_background = saved_background card.save() if "background_url" in request.data: card.save() if "next_plan" in request.data: if card.plan == PlanKind.FREE.value: del data["next_plan"] serializer = CardPatchSerializer(card, data=data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() patchedCard = CardSerializer(card) return Response(patchedCard.data)
def partial_update(self, request, card_id): card = get_user_cards(request).get(pk=card_id) if card.appearance is None: card.appearance = CardAppearance.objects.create() card.save() if "background_color" in request.data: card.tab = "color" card.appearance.background_color = request.data["background_color"] card.save() card.appearance.save() if "border" in request.data: if not request.data["border"]: card.appearance.border = None card.appearance.save() else: if not card.appearance.border: serializer = CardAppearanceBorderSerializer( data=request.data["border"]) serializer.is_valid(raise_exception=True) border = serializer.save() card.appearance.border = border card.appearance.save() else: serializer = CardAppearanceBorderSerializer( card.appearance.border, data=request.data["border"], partial=True, ) serializer.is_valid(raise_exception=True) serializer.save() appearance_serializer = CardAppearancePatchSerializer(card.appearance, request.data, partial=True) appearance_serializer.is_valid(raise_exception=True) appearance_serializer.save() return Response(CardSerializer(Card.objects.get(pk=card_id)).data)
def list(self, request): serializer = CardsSerializer(get_user_cards(request), many=True) return Response(serializer.data)
def reactivate(self, request, pk): card = get_user_cards(request).get(pk=pk) reactivate_card(card) return Response({"success": True})
def cancel(self, request, pk): card = get_user_cards(request).get(pk=pk) cancel_card(card) return Response({"success": True})