diff --git a/webapp/helper.py b/webapp/helper.py index 8544dc17..c5160228 100644 --- a/webapp/helper.py +++ b/webapp/helper.py @@ -85,6 +85,17 @@ def get_project_id(project_name): return project.id if project else None +def get_or_create_project_id(project_name): + project = Project.query.filter_by(name=project_name).first() + if not project: + project, _ = get_or_create( + db.session, + Project, + name=project_name, + ) + return project.id if project else None + + def get_webpage_id(name, project_id): webpage = Webpage.query.filter_by(name=name, project_id=project_id).first() return webpage.id if webpage else None diff --git a/webapp/site_repository.py b/webapp/site_repository.py index bb93f106..7b9ff60a 100644 --- a/webapp/site_repository.py +++ b/webapp/site_repository.py @@ -8,11 +8,11 @@ from flask import Flask from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import delete, select +from sqlalchemy import select from webapp.helper import ( convert_webpage_to_dict, - get_project_id, + get_or_create_project_id, get_tree_struct, ) from webapp.models import ( @@ -245,31 +245,39 @@ def get_tree_from_db(self): """Get the tree from the database. If the tree is incomplete, reload from the repository. """ - webpages = ( - self.db.session.execute( - select(Webpage).where( - Webpage.project_id == get_project_id(self.repository_uri), - ), - ) - .scalars() - .all() - ) - # build tree from repository in case DB table is empty - # TODO: Revert this line to `if not webpages ...` - # before merging this PR - # This is only for QA - if True or not webpages or self._has_incomplete_pages(webpages): - tree = self.get_new_tree() - # otherwise, build tree from DB - else: - tree = get_tree_struct(db.session, webpages) - # If the tree is empty, load from the repository - if not tree.get("children") and not tree.get("parent_id"): - self.logger.info( - f"Reloading incomplete tree root {self.repository_uri}.", + if project_id := get_or_create_project_id(self.repository_uri): + webpages = ( + self.db.session.execute( + select(Webpage) + .where( + Webpage.project_id == project_id, + ) + .where(Webpage.status != WebpageStatus.TO_DELETE), ) + .scalars() + .all() + ) + # build tree from repository in case DB table is empty + if not webpages or self._has_incomplete_pages(webpages): tree = self.get_new_tree() - return tree + # otherwise, build tree from DB + else: + tree = get_tree_struct(db.session, webpages) + # If the tree is empty, load from the repository + if not tree or ( + tree.get("children") and not tree.get("parent_id") + ): + msg = ( + "Reloading incomplete tree root " + f"{self.repository_uri}." + ) + self.logger.info( + msg, + ) + tree = self.get_new_tree() + return tree + # Raise an error if the project does not exist. + raise SiteRepositoryError("Invalid project_id. Unable to load tree.") def get_tree(self, no_cache: bool = False): """Get the tree from the cache or load a new tree to cache and db.""" @@ -351,22 +359,6 @@ def __create_webpages_for_children__( webpage_dict["id"], ) - def __remove_webpages_to_delete__(self, db, tree): - # convert tree of pages from repository to list - webpages = [] - self.add_pages_to_list(tree, webpages) - - webpages_to_delete = db.session.execute( - select(Webpage).where(Webpage.status == WebpageStatus.TO_DELETE), - ) - - for row in webpages_to_delete: - page_to_delete = row[0] - if page_to_delete.name not in webpages: - db.session.execute( - delete(Webpage).where(Webpage.id == page_to_delete.id), - ) - def create_webpages_for_tree(self, db: SQLAlchemy, tree: Tree): """Create webpages for each node in the tree.""" self.logger.info(f"Existing tree {tree}") @@ -396,9 +388,6 @@ def create_webpages_for_tree(self, db: SQLAlchemy, tree: Tree): webpage_dict["id"], ) - # Remove pages that don't exist in the repository anymore - self.__remove_webpages_to_delete__(db, tree) - self.logger.info(f"Existing dict {webpage_dict}") db.session.commit()