def to_representation(self, obj: Project): rep = super().to_representation(obj) if self.context.get("request"): user = self.context["request"].user else: user = None rep["has_write_access"] = obj.has_write_access(user) if not obj.has_write_access(user): rep.pop("sim_count") rep.pop("user_count") return rep
def test_project(self): markdown = "[hello](www.world.com)" p = Project(title="test project", server_cost=36, description=markdown) assert p.server_cost_in_secs == 0.01 assert p.n_secs_per_penny == 1.0 assert p.run_cost(1) == 0.01 assert p.run_cost(0.5, adjust=True) == 0.01 assert p.run_cost(0.5, adjust=False) < 0.01 assert p.run_cost(2) == 0.02 assert Project.dollar_to_penny(0.01) == 1 assert p.safe_description
def test_project(self, monkeypatch): monkeypatch.setattr("webapp.apps.users.models.COMPUTE_PRICING", { "cpu": 12, "memory": 2 }) markdown = "[hello](www.world.com)" p = Project(title="test project", description=markdown) assert p.server_cost_in_secs == 0.01 assert p.n_secs_per_penny == 1.0 assert p.run_cost(1) == 0.01 assert p.run_cost(0.5, adjust=True) == 0.01 assert p.run_cost(0.5, adjust=False) < 0.01 assert p.run_cost(2) == 0.02 assert Project.dollar_to_penny(0.01) == 1 assert p.safe_description
def charge_run(self, sim, meta_dict, use_stripe=True): sim.run_time = sum(meta_dict["task_times"]) sim.run_cost = sim.project.run_cost(sim.run_time) if use_stripe and sim.project.pay_per_sim: quantity = sim.project.run_cost(sim.run_time, adjust=True) plan = sim.project.product.plans.get(usage_type="metered") # The sponsor is also stored on the Simulation object. However, the # Project object should be considered the single source of truth # for sending usage records. sponsor = sim.project.sponsor if sponsor is not None: customer = sponsor.user.customer else: customer = sim.owner.user.customer si = SubscriptionItem.objects.get(subscription__customer=customer, plan=plan) stripe_ur = UsageRecord.create_stripe_object( quantity=Project.dollar_to_penny(quantity), timestamp=None, subscription_item=si, ) UsageRecord.construct(stripe_ur, si)