assert isinstance(other.id_, int)
return self.id_ < other.id_
+ @property
+ def as_dict(self) -> dict[str, object]:
+ """Return self as (json.dumps-coompatible) dict."""
+ d: dict[str, object] = {'id': self.id_}
+ for k in self.to_save:
+ attr = getattr(self, k)
+ if hasattr(attr, 'as_dict'):
+ d[k] = attr.as_dict
+ d[k] = attr
+ for k in self.to_save_versioned:
+ attr = getattr(self, k)
+ d[k] = attr.as_dict
+ for r in self.to_save_relations:
+ attr_name = r[2]
+ d[attr_name] = [x.as_dict for x in getattr(self, attr_name)]
+ return d
+
# cache management
# (we primarily use the cache to ensure we work on the same object in
# memory no matter where and how we retrieve it, e.g. we don't want
"""Web server stuff."""
from __future__ import annotations
from dataclasses import dataclass
-from typing import Any, Callable, Mapping
+from typing import Any, Callable
from base64 import b64encode, b64decode
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
from urllib.parse import urlparse, parse_qs
+from json import dumps as json_dumps
from os.path import split as path_split
from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
from plomtask.dating import date_in_n_days
from plomtask.processes import Process, ProcessStep, ProcessStepsNode
from plomtask.conditions import Condition
from plomtask.todos import Todo
+from plomtask.db import BaseModel
TEMPLATES_DIR = 'templates'
*args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self.db = db_file
- self.jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
+ self.headers: list[tuple[str, str]] = []
+ self._render_mode = 'html'
+ self._jinja = JinjaEnv(loader=JinjaFSLoader(TEMPLATES_DIR))
+
+ def set_json_mode(self) -> None:
+ """Make server send JSON instead of HTML responses."""
+ self._render_mode = 'json'
+ self.headers += [('Content-Type', 'application/json')]
+
+ @staticmethod
+ def ctx_to_json(ctx: dict[str, object]) -> str:
+ """Render ctx into JSON string."""
+ def walk_ctx(node: object) -> Any:
+ if isinstance(node, BaseModel):
+ return node.as_dict
+ if isinstance(node, (list, tuple)):
+ return [walk_ctx(x) for x in node]
+ if isinstance(node, HandledException):
+ return str(node)
+ return node
+ for k, v in ctx.items():
+ ctx[k] = walk_ctx(v)
+ return json_dumps(ctx)
+
+ def render(self, ctx: dict[str, object], tmpl_name: str = '') -> str:
+ """Render ctx according to self._render_mode.."""
+ tmpl_name = f'{tmpl_name}.{self._render_mode}'
+ if 'html' == self._render_mode:
+ template = self._jinja.get_template(tmpl_name)
+ return template.render(ctx)
+ return self.__class__.ctx_to_json(ctx)
class InputsParser:
_form_data: InputsParser
_params: InputsParser
- def _send_html(self,
+ def _send_page(self,
+ ctx: dict[str, Any],
tmpl_name: str,
- ctx: Mapping[str, object],
- code: int = 200) -> None:
+ code: int = 200
+ ) -> None:
"""Send HTML as proper HTTP response."""
- tmpl = self.server.jinja.get_template(tmpl_name)
- html = tmpl.render(ctx)
+ body = self.server.render(ctx, tmpl_name)
self.send_response(code)
+ for header_tuple in self.server.headers:
+ self.send_header(*header_tuple)
self.end_headers()
- self.wfile.write(bytes(html, 'utf-8'))
+ self.wfile.write(bytes(body, 'utf-8'))
@staticmethod
def _request_wrapper(http_method: str, not_found_msg: str
assert hasattr(cls, 'empty_cache')
cls.empty_cache()
ctx = {'msg': error}
- self._send_html('msg.html', ctx, error.http_code)
+ self._send_page(ctx, 'msg', error.http_code)
finally:
self.conn.close()
return wrapper
def do_GET(self, handler: Callable[[], str | dict[str, object]]
) -> str | None:
"""Render page with result of handler, or redirect if result is str."""
- tmpl_name = f'{self._site}.html'
- ctx_or_redir = handler()
- if isinstance(ctx_or_redir, str):
- return ctx_or_redir
- self._send_html(tmpl_name, ctx_or_redir)
+ tmpl_name = f'{self._site}'
+ ctx_or_redir_target = handler()
+ if isinstance(ctx_or_redir_target, str):
+ return ctx_or_redir_target
+ self._send_page(ctx_or_redir_target, tmpl_name)
return None
@_request_wrapper('POST', 'Unknown POST target')
self.calendarize = calendarize
self.n_owners: int | None = None # only set by from_table_row
+ @property
+ def as_dict(self) -> dict[str, object]:
+ """Return self as (json.dumps-coompatible) dict."""
+ d = super().as_dict
+ d['explicit_steps'] = [s.as_dict for s in self.explicit_steps]
+ d['suppressed_steps'] = [s.as_dict for s in self.suppressed_steps]
+ return d
+
@classmethod
def from_table_row(cls, db_conn: DatabaseConnection,
row: Row | list[Any]) -> Process:
self.parent_step_id = parent_step_id
def save(self, db_conn: DatabaseConnection) -> None:
- """Remove from DB, and owner's .explicit_steps."""
+ """Update into DB/cache, and owner's .explicit_steps."""
super().save(db_conn)
owner = Process.by_id(db_conn, self.owner_id)
if self not in owner.explicit_steps:
"""Test Days module."""
from unittest import TestCase
from datetime import datetime
+from json import loads as json_loads
from tests.utils import TestCaseWithDB, TestCaseWithServer
from plomtask.dating import date_in_n_days
from plomtask.days import Day
class TestsWithServer(TestCaseWithServer):
"""Tests against our HTTP server/handler (and database)."""
+ def test_get_json(self) -> None:
+ """Test /day for JSON response."""
+ self.conn.request('GET', '/day?date=2024-01-01')
+ response = self.conn.getresponse()
+ self.assertEqual(response.status, 200)
+ expected = {'day': {'id': '2024-01-01',
+ 'comment': '',
+ 'todos': []},
+ 'top_nodes': [],
+ 'make_type': '',
+ 'enablers_for': {},
+ 'disablers_for': {},
+ 'conditions_present': [],
+ 'processes': []}
+ retrieved = json_loads(response.read().decode())
+ self.assertEqual(expected, retrieved)
+
def test_do_GET(self) -> None:
"""Test /day and /calendar response codes, and / redirect."""
self.check_get('/day', 200)