Exemplo n.º 1
0
def gen_collabs(n, plan=None):
    for _ in range(n):
        key = secrets.token_hex(3)
        u = User.objects.create_user(
            f"collab-{key}", f"collab-{key}@example.com", "heyhey2222"
        )
        create_profile_from_user(u)
        profile = Profile.objects.get(user=u)
        if plan is not None and USE_STRIPE:
            stripe_customer = stripe.Customer.create(
                email=u.email, source="tok_bypassPending"
            )
            Customer.get_or_construct(stripe_customer.id, user=u)
            profile.refresh_from_db()
            product = Product.objects.get(name="Compute Studio Subscription")
            plan_instance = product.plans.get(nickname=f"Monthly {plan.title()} Plan")
            profile.user.customer.update_plan(plan_instance)
            profile.refresh_from_db()
            yield profile
        elif plan is not None:
            Customer.objects.create(
                stripe_id=f"collab-{u.pk}",
                livemode=False,
                user=u,
                default_source="123",
                metadata={},
            )
            profile.refresh_from_db()
            mock_cp = lambda: {"name": plan, "plan_duration": plan.lower()}
            profile.user.customer.current_plan = mock_cp
            yield profile
        else:
            yield profile
Exemplo n.º 2
0
def update_payment(user, stripe_token):
    if hasattr(user, "customer"):
        user.customer.update_source(stripe_token)
    else:  # create customer.
        stripe_customer = stripe.Customer.create(email=user.email,
                                                 source=stripe_token)
        Customer.construct(stripe_customer, user=user)
Exemplo n.º 3
0
    def test_construct_customer(self, stripe_customer, user):
        stripe_customer = Customer.get_stripe_object(stripe_customer.id)
        assert stripe_customer
        assert not stripe_customer.livemode

        customer, created = Customer.get_or_construct(stripe_customer.id, user)
        assert customer
        assert customer.user
        assert created
        assert not customer.livemode

        customer, created = Customer.get_or_construct(stripe_customer.id)
        assert customer
        assert customer.user
        assert not created
        assert not customer.livemode
Exemplo n.º 4
0
    def test_construct_customer(self, stripe_customer, user):
        stripe_customer = Customer.get_stripe_object(stripe_customer.id)
        assert stripe_customer
        assert not stripe_customer.livemode

        customer, created = Customer.get_or_construct(stripe_customer.id, user)
        assert customer
        assert customer.user
        assert created
        assert not customer.livemode
        assert customer.current_plan(as_dict=False).plan == Plan.objects.get(
            nickname="Free Plan"
        )

        customer, created = Customer.get_or_construct(stripe_customer.id)
        assert customer
        assert customer.user
        assert not created
        assert not customer.livemode
Exemplo n.º 5
0
 def save(self, commit=False):
     user = super().save()
     if USE_STRIPE:
         stripe_customer = stripe.Customer.create(email=user.email,
                                                  source=self.stripe_token)
         customer = Customer.construct(stripe_customer, user=user)
         if Project.objects.count() > 0:
             customer.subscribe_to_public_plans()
         else:
             print("No projects yet.")
     Profile.objects.create(user=user, is_active=True)
     return user
Exemplo n.º 6
0
def create_three_month_pro_subscription(user):
    """
    Gives user a free three month pro subscription that cancels
    at the end of the trial period, unless the user opts into
    the sub after the period ends.
    """
    if getattr(user, "customer", None) is None:
        stripe_customer = stripe.Customer.create(email=user.email)
        customer = Customer.construct(stripe_customer, user=user)
    else:
        customer: Customer = user.customer

    cs_product = Product.objects.get(name="Compute Studio Subscription")
    plan = cs_product.plans.get(nickname=f"Monthly Pro Plan")
    now = datetime.utcnow().replace(tzinfo=pytz.UTC)
    if now.month <= 9:
        three_months = now.replace(month=now.month + 3)
    else:
        three_months = now.replace(year=now.year + 1, month=now.month + 3 - 12)
    customer.update_plan(
        plan,
        cancel_at=three_months,
        trial_end=three_months,
    )
Exemplo n.º 7
0
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        User = get_user_model()
        modeler = User.objects.create_user(
            username="******", email="*****@*****.**", password="******"
        )

        sponsor = User.objects.create_user(
            username="******", email="*****@*****.**", password="******"
        )

        hdoupe = User.objects.create_user(
            username="******", email="*****@*****.**", password="******"
        )

        Profile.objects.create(user=modeler, is_active=True)
        Profile.objects.create(user=sponsor, is_active=True)
        Profile.objects.create(user=hdoupe, is_active=True)

        common = {
            "description": "[Matchups](https://github.com/hdoupe/Matchups) provides pitch data on pitcher and batter matchups.. Select a date range using the format YYYY-MM-DD. Keep in mind that Matchups only provides data on matchups going back to 2008. Two datasets are offered to run this model: one that only has the most recent season, 2018, and one that contains data on every single pitch going back to 2008. Next, select your favorite pitcher and some batters who he's faced in the past. Click submit to start analyzing the selected matchups!",
            "inputs_style": "paramtools",
            "meta_parameters": '{\n    "meta_parameters": {\n        "use_full_data": {\n            "type": "bool",\n            "title": "Use full data",\n            "default": true,\n            "validators": {}\n        }\n    }\n}',
            "package_defaults": 'import matchups\r\n\r\ndef package_defaults(**meta_parameters):\r\n    return matchups.get_inputs(use_full_data=meta_parameters["use_full_data"])',
            "parse_user_adjustments": 'import matchups\r\n\r\ndef parse_user_inputs(params, jsonparams, errors_warnings,\r\n                        **meta_parameters):\r\n    # parse the params, jsonparams, and errors_warnings further\r\n    use_full_data = meta_parameters["use_full_data"]\r\n    params, jsonparams, errors_warnings = matchups.parse_inputs(\r\n        params, jsonparams, errors_warnings, use_full_data==use_full_data)\r\n    return params, jsonparams, errors_warnings',
            "run_simulation": 'import matchups\r\n\r\ndef run(**kwargs):\r\n    result = matchups.get_matchup(kwargs["use_full_data"], kwargs["user_mods"])\r\n    return result',
            "server_size": ["8,2"],
            "exp_task_time": 10,
            "installation": "conda install pandas pyarrow bokeh paramtools -c pslmodels\r\npip install pybaseball matchups==0.3.5",
            "owner": modeler.profile,
            "server_cost": 0.1,
        }

        projects = [
            {"title": "Matchups", "owner": hdoupe.profile},
            {"title": "Used-for-testing"},
            {"title": "Used-for-testing-sponsored-apps", "sponsor": sponsor.profile},
        ]

        for project_config in projects:
            project = Project.objects.create(**dict(common, **project_config))
            if USE_STRIPE:
                if Product.objects.filter(name=project.title).count() == 0:
                    stripe_product = Product.create_stripe_object(project.title)
                    product = Product.construct(stripe_product, project)
                    stripe_plan_lic = Plan.create_stripe_object(
                        amount=0,
                        product=product,
                        usage_type="licensed",
                        interval="month",
                        currency="usd",
                    )
                    Plan.construct(stripe_plan_lic, product)
                    stripe_plan_met = Plan.create_stripe_object(
                        amount=1,
                        product=product,
                        usage_type="metered",
                        interval="month",
                        currency="usd",
                    )
                    Plan.construct(stripe_plan_met, product)

        if USE_STRIPE:
            for u in [modeler, sponsor, hdoupe]:
                stripe_customer = stripe.Customer.create(
                    email=u.email, source="tok_bypassPending"
                )
                # import pdb; pdb.set_trace()
                customer, _ = Customer.get_or_construct(stripe_customer.id, u)
                customer.subscribe_to_public_plans()
                customer_user = customer.user
Exemplo n.º 8
0
def basiccustomer(db, stripe_customer, user):
    customer, _ = Customer.get_or_construct(stripe_customer.id, user)
    return customer
Exemplo n.º 9
0
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        User = get_user_model()
        modeler = User.objects.create_user(username="******",
                                           email="*****@*****.**",
                                           password="******")

        sponsor = User.objects.create_user(username="******",
                                           email="*****@*****.**",
                                           password="******")

        hdoupe = User.objects.create_user(username="******",
                                          email="*****@*****.**",
                                          password="******")

        # User for pushing outputs from the workers to the webapp.
        comp_api_user = User.objects.create_user(
            username="******",
            email="*****@*****.**",
            password="******",
        )

        for u in [modeler, sponsor, hdoupe, comp_api_user]:
            Token.objects.create(user=u)
            Profile.objects.create(user=u, is_active=True)

        common = {
            "description":
            "[Matchups](https://github.com/hdoupe/Matchups) provides pitch data on pitcher and batter matchups.. Select a date range using the format YYYY-MM-DD. Keep in mind that Matchups only provides data on matchups going back to 2008. Two datasets are offered to run this model: one that only has the most recent season, 2018, and one that contains data on every single pitch going back to 2008. Next, select your favorite pitcher and some batters who he's faced in the past. Click submit to start analyzing the selected matchups!",
            "oneliner": "oneliner",
            "repo_url": "https://github.com/hdoupe/Matchups",
            "server_size": ["8,2"],
            "exp_task_time": 10,
            "owner": modeler.profile,
            "server_cost": 0.1,
            "listed": True,
        }

        projects = [
            {
                "title": "Matchups",
                "owner": hdoupe.profile
            },
            {
                "title": "Used-for-testing",
                "listed": False
            },
            {
                "title": "Tax-Brain"
            },
            {
                "title": "Used-for-testing-sponsored-apps",
                "sponsor": sponsor.profile
            },
        ]

        for project_config in projects:
            project = Project.objects.create(**dict(common, **project_config))

        if USE_STRIPE:
            Project.objects.sync_products()
            for u in [modeler, sponsor, hdoupe]:
                stripe_customer = stripe.Customer.create(
                    email=u.email, source="tok_bypassPending")
                customer, _ = Customer.get_or_construct(stripe_customer.id, u)
            Customer.objects.sync_subscriptions()
Exemplo n.º 10
0
def django_db_setup(django_db_setup, django_db_blocker):
    with django_db_blocker.unblock():
        User = get_user_model()
        modeler = User.objects.create_user(
            username="******", email="*****@*****.**", password="******"
        )

        sponsor = User.objects.create_user(
            username="******", email="*****@*****.**", password="******"
        )

        hdoupe = User.objects.create_user(
            username="******", email="*****@*****.**", password="******"
        )

        # User for pushing outputs from the workers to the webapp.
        comp_api_user = User.objects.create_user(
            username="******",
            email="*****@*****.**",
            password="******",
        )

        webapp.settings.DEFAULT_CLUSTER_USER = "******"
        webapp.apps.users.models.DEFAULT_CLUSTER_USER = "******"
        for u in [modeler, sponsor, hdoupe, comp_api_user]:
            Token.objects.create(user=u)
            Profile.objects.create(user=u, is_active=True)

        comp_api_user.refresh_from_db()

        cluster = Cluster.objects.create(
            service_account=comp_api_user.profile,
            url="http://scheduler",
            jwt_secret=cryptkeeper.encrypt(binascii.hexlify(os.urandom(32)).decode()),
            version="v0",
        )

        common = {
            "description": "[Matchups](https://github.com/hdoupe/Matchups) provides pitch data on pitcher and batter matchups.. Select a date range using the format YYYY-MM-DD. Keep in mind that Matchups only provides data on matchups going back to 2008. Two datasets are offered to run this model: one that only has the most recent season, 2018, and one that contains data on every single pitch going back to 2008. Next, select your favorite pitcher and some batters who he's faced in the past. Click submit to start analyzing the selected matchups!",
            "oneliner": "oneliner",
            "repo_url": "https://github.com/hdoupe/Matchups",
            "exp_task_time": 10,
            "owner": modeler.profile,
            "listed": True,
            "tech": "python-paramtools",
            "cluster": cluster,
        }

        projects = [
            {"title": "Matchups", "owner": hdoupe.profile},
            {"title": "Used-for-testing", "listed": False},
            {"title": "Tax-Brain"},
            {"title": "Used-for-testing-sponsored-apps", "sponsor": sponsor.profile},
            {"title": "Test-Viz", "tech": "dash", "callable_name": "serve"},
        ]

        for project_config in projects:
            project = Project.objects.create(**dict(common, **project_config))
            project.assign_role("admin", project.owner.user)
            assign_perm("write_project", project.cluster.service_account.user, project)
            project.latest_tag = Tag.objects.create(
                project=project, cpu=project.cpu, memory=project.memory, image_tag="v0"
            )
            project.save()

        if USE_STRIPE:
            create_pro_billing_objects()
            for u in [modeler, sponsor, hdoupe]:
                stripe_customer = stripe.Customer.create(
                    email=u.email, source="tok_bypassPending"
                )
                Customer.get_or_construct(stripe_customer.id, u)