From 70a996c52889541927990800e7d4564e432a3c8b Mon Sep 17 00:00:00 2001 From: chrisjsimpson Date: Sun, 18 Feb 2024 21:35:06 +0000 Subject: [PATCH] #1281 add get_plan_revisions to Plan model --- subscribie/models.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/subscribie/models.py b/subscribie/models.py index 64fa0dbd..72a49298 100644 --- a/subscribie/models.py +++ b/subscribie/models.py @@ -6,6 +6,7 @@ from sqlalchemy import event from sqlalchemy import Column from sqlalchemy import Boolean +from sqlalchemy import text from typing import Optional from datetime import datetime @@ -744,6 +745,23 @@ def getPrice(self, currency): ) return sell_price, interval_amount + def get_plan_revisions(self): + textual_sql = text(f""" + WITH RECURSIVE RevisionChain AS + (SELECT id, created_at, uuid, title, parent_plan_revision_uuid + FROM plan WHERE uuid = '{self.uuid}' + UNION ALL + SELECT p.id, p.created_at, p.uuid, p.title, p.parent_plan_revision_uuid + FROM plan p + INNER JOIN RevisionChain rc + ON p.uuid = rc.parent_plan_revision_uuid + ) + SELECT * FROM RevisionChain""") + textual_sql = textual_sql.columns(Plan.id, Plan.created_at, Plan.uuid, Plan.title, Plan.parent_plan_revision_uuid) + orm_sql = database.select(Plan).from_statement(textual_sql) + plan_decendants = database.session.execute(orm_sql).scalars().all() + return plan_decendants + def applyRules(self, rules=[], context={}): """Apply pricelist rules to a given plan