|
| 1 | +"""Generate Jinja template rendering context from domain models.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from collections import UserList |
| 6 | +from dataclasses import dataclass |
| 7 | +from datetime import datetime |
| 8 | +from typing import List, Optional, Sequence |
| 9 | + |
| 10 | +from keeper.models import Build, Edition, EditionKind, Product |
| 11 | + |
| 12 | + |
| 13 | +@dataclass |
| 14 | +class ProjectContext: |
| 15 | + """Template context model for a project.""" |
| 16 | + |
| 17 | + title: str |
| 18 | + """Project title.""" |
| 19 | + |
| 20 | + source_repo_url: str |
| 21 | + """Url of the associated GitHub repository.""" |
| 22 | + |
| 23 | + url: str |
| 24 | + """Root URL where this project is published.""" |
| 25 | + |
| 26 | + @classmethod |
| 27 | + def from_product(cls, product: Product) -> ProjectContext: |
| 28 | + return cls( |
| 29 | + title=product.title, |
| 30 | + source_repo_url=product.doc_repo, |
| 31 | + url=product.published_url, |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +@dataclass |
| 36 | +class EditionContext: |
| 37 | + """Template context model for an edition.""" |
| 38 | + |
| 39 | + title: str |
| 40 | + """Human-readable label for this edition.""" |
| 41 | + |
| 42 | + url: str |
| 43 | + """URL where this edition is published.""" |
| 44 | + |
| 45 | + date_updated: datetime |
| 46 | + """Date when this edition was last updated.""" |
| 47 | + |
| 48 | + kind: EditionKind |
| 49 | + """The edition's kind.""" |
| 50 | + |
| 51 | + slug: str |
| 52 | + """The edition's slug.""" |
| 53 | + |
| 54 | + git_ref: Optional[str] |
| 55 | + """The git ref that this edition tracks.""" |
| 56 | + |
| 57 | + github_url: Optional[str] |
| 58 | + """URL to this git ref on GitHub.""" |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def from_edition( |
| 62 | + cls, edition: Edition, product: Product |
| 63 | + ) -> EditionContext: |
| 64 | + if edition.tracked_ref and product.doc_repo: |
| 65 | + repo_url = product.doc_repo.rstrip("/") |
| 66 | + if repo_url[-4:] == ".git": |
| 67 | + repo_url = repo_url[:-4] |
| 68 | + github_url = f"{repo_url}/tree/{edition.tracked_ref}" |
| 69 | + else: |
| 70 | + github_url = None |
| 71 | + |
| 72 | + return cls( |
| 73 | + title=edition.title, |
| 74 | + url=edition.published_url, |
| 75 | + date_updated=edition.date_rebuilt, |
| 76 | + kind=edition.kind, |
| 77 | + slug=edition.slug, |
| 78 | + git_ref=edition.tracked_ref, |
| 79 | + github_url=github_url, |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +class EditionContextList(UserList): |
| 84 | + def __init__(self, contexts: Sequence[EditionContext]) -> None: |
| 85 | + self.data: List[EditionContext] = list(contexts) |
| 86 | + self.data.sort(key=lambda x: x.date_updated) |
| 87 | + |
| 88 | + @property |
| 89 | + def main_edition(self) -> EditionContext: |
| 90 | + """The main (current) edition.""" |
| 91 | + for edition in self.data: |
| 92 | + if edition.slug == "__main": |
| 93 | + return edition |
| 94 | + raise ValueError("No __main edition found") |
| 95 | + |
| 96 | + @property |
| 97 | + def has_releases(self) -> bool: |
| 98 | + return len(self.releases) > 0 |
| 99 | + |
| 100 | + @property |
| 101 | + def releases(self) -> List[EditionContext]: |
| 102 | + """All editions tagged as releases.""" |
| 103 | + release_kinds = ( |
| 104 | + EditionKind.release, |
| 105 | + EditionKind.major, |
| 106 | + EditionKind.minor, |
| 107 | + ) |
| 108 | + release_items = [ |
| 109 | + e |
| 110 | + for e in self.data |
| 111 | + if (e.kind in release_kinds and e.slug != "__main") |
| 112 | + ] |
| 113 | + sorted_items = sorted( |
| 114 | + release_items, key=lambda x: x.slug, reverse=True |
| 115 | + ) |
| 116 | + return sorted_items |
| 117 | + |
| 118 | + @property |
| 119 | + def has_drafts(self) -> bool: |
| 120 | + return len(self.drafts) > 0 |
| 121 | + |
| 122 | + @property |
| 123 | + def drafts(self) -> List[EditionContext]: |
| 124 | + """All editions tagged as drafts.""" |
| 125 | + draft_items = [ |
| 126 | + e |
| 127 | + for e in self.data |
| 128 | + if (e.kind == EditionKind.draft and e.slug != "__main") |
| 129 | + ] |
| 130 | + return sorted(draft_items, key=lambda x: x.date_updated, reverse=True) |
| 131 | + |
| 132 | + |
| 133 | +@dataclass |
| 134 | +class BuildContext: |
| 135 | + """Template context model for a build.""" |
| 136 | + |
| 137 | + slug: str |
| 138 | + """The URL slug for this build.""" |
| 139 | + |
| 140 | + url: str |
| 141 | + """The URL for this build.""" |
| 142 | + |
| 143 | + git_ref: Optional[str] |
| 144 | + """The git ref associated with this build (if appropriate.""" |
| 145 | + |
| 146 | + date: datetime |
| 147 | + """Date when the build was uploaded.""" |
| 148 | + |
| 149 | + @classmethod |
| 150 | + def from_build(cls, build: Build) -> BuildContext: |
| 151 | + return cls( |
| 152 | + slug=build.slug, |
| 153 | + url=build.published_url, |
| 154 | + git_ref=build.git_ref, |
| 155 | + date=build.date_created, |
| 156 | + ) |
| 157 | + |
| 158 | + |
| 159 | +class BuildContextList(UserList): |
| 160 | + def __init__(self, contexts: Sequence[BuildContext]) -> None: |
| 161 | + self.data: List[BuildContext] = list(contexts) |
| 162 | + self.data.sort(key=lambda x: x.date) |
| 163 | + |
| 164 | + |
| 165 | +@dataclass |
| 166 | +class Context: |
| 167 | + """A class that creates Jinja template rendering context from |
| 168 | + domain models. |
| 169 | + """ |
| 170 | + |
| 171 | + project_context: ProjectContext |
| 172 | + |
| 173 | + edition_contexts: EditionContextList |
| 174 | + |
| 175 | + build_contexts: BuildContextList |
| 176 | + |
| 177 | + @classmethod |
| 178 | + def create(cls, product: Product) -> Context: |
| 179 | + project_context = ProjectContext.from_product(product) |
| 180 | + |
| 181 | + edition_contexts: EditionContextList = EditionContextList( |
| 182 | + [ |
| 183 | + EditionContext.from_edition(edition=edition, product=product) |
| 184 | + for edition in product.editions |
| 185 | + ] |
| 186 | + ) |
| 187 | + |
| 188 | + build_contexts: BuildContextList = BuildContextList( |
| 189 | + [BuildContext.from_build(build) for build in product.builds] |
| 190 | + ) |
| 191 | + |
| 192 | + return cls( |
| 193 | + project_context=project_context, |
| 194 | + edition_contexts=edition_contexts, |
| 195 | + build_contexts=build_contexts, |
| 196 | + ) |
0 commit comments