from __future__ import annotations
from datetime import datetime, timedelta
from sqlite3 import Row
-from plomtask.misc import HandledException
+from plomtask.exceptions import BadFormatException, NotFoundException
from plomtask.db import DatabaseConnection
DATE_FORMAT = '%Y-%m-%d'
dt = datetime.strptime(date_str, DATE_FORMAT)
except (ValueError, TypeError) as e:
msg = f'Given date of wrong format: {date_str}'
- raise HandledException(msg) from e
+ raise BadFormatException(msg) from e
return dt.strftime(DATE_FORMAT)
for row in db_conn.exec('SELECT * FROM days WHERE date = ?', (date,)):
return cls.from_table_row(row)
if not create:
- raise HandledException(f'Day not found for date: {date}')
+ raise NotFoundException(f'Day not found for date: {date}')
return cls(date)
@property
from difflib import Differ
from sqlite3 import connect as sql_connect, Cursor
from typing import Any
-from plomtask.misc import HandledException
+from plomtask.exceptions import HandledException
PATH_DB_SCHEMA = 'scripts/init.sql'
--- /dev/null
+"""
+Whatever fits nowhere else, and/or is too small/trivial
+to merit its own module at this point.
+"""
+
+
+class HandledException(Exception):
+ """To identify Exceptions based on expected (if faulty) user behavior."""
+ http_code = 400
+
+
+class BadFormatException(HandledException):
+ """To identify Exceptions on malformed inputs."""
+ http_code = 401
+
+
+class NotFoundException(HandledException):
+ """To identify Exceptions on unsuccessful queries."""
+ http_code = 404
from os.path import split as path_split
from jinja2 import Environment as JinjaEnv, FileSystemLoader as JinjaFSLoader
from plomtask.days import Day, todays_date
-from plomtask.misc import HandledException
+from plomtask.exceptions import HandledException, BadFormatException
from plomtask.db import DatabaseConnection, DatabaseFile
from plomtask.processes import Process
try:
id__ = int(id_) if id_ else None
except ValueError as e:
- raise HandledException(f'Bad ?id= value: {id_}') from e
+ raise BadFormatException(f'Bad ?id= value: {id_}') from e
html = self.do_GET_process(conn, id__)
elif 'processes' == site:
html = self.do_GET_processes(conn)
conn.close()
self._send_html(html)
except HandledException as error:
- self._send_msg(error)
+ self._send_msg(error, code=error.http_code)
def do_GET_calendar(self, conn: DatabaseConnection,
start: str, end: str) -> str:
try:
id__ = int(id_) if id_ else None
except ValueError as e:
- raise HandledException(f'Bad ?id= value: {id_}') from e
+ raise BadFormatException(f'Bad ?id= value: {id_}') from e
self.do_POST_process(conn, id__, postvars)
conn.commit()
conn.close()
self._redirect('/')
except HandledException as error:
- self._send_msg(error)
+ self._send_msg(error, code=error.http_code)
def do_POST_day(self, conn: DatabaseConnection,
date: str, postvars: dict[str, list[str]]) -> None:
try:
process.effort.set(float(effort))
except ValueError as e:
- raise HandledException(f'Bad effort value: {effort}') from e
+ raise BadFormatException(f'Bad effort value: {effort}') from e
process.save(conn)
def _init_handling(self) -> \
+++ /dev/null
-"""
-Whatever fits nowhere else, and/or is too small/trivial
-to merit its own module at this point.
-"""
-
-
-class HandledException(Exception):
- """To identify Exceptions based on expected (if faulty) user behavior."""
from sqlite3 import Row
from datetime import datetime
from plomtask.db import DatabaseConnection
-from plomtask.misc import HandledException
+from plomtask.exceptions import NotFoundException
class Process:
break
if not process:
if not create:
- raise HandledException(f'Process not found of id: {id_}')
+ raise NotFoundException(f'Process not found of id: {id_}')
process = Process(id_)
if process:
for row in db_conn.exec('SELECT * FROM process_titles '
"""Call this to start the application."""
from sys import exit as sys_exit
from os import environ
-from plomtask.misc import HandledException
+from plomtask.exceptions import HandledException
from plomtask.http import TaskHandler, TaskServer
from plomtask.db import DatabaseFile
from datetime import datetime
from tests.utils import TestCaseWithDB, TestCaseWithServer
from plomtask.days import Day, todays_date
-from plomtask.misc import HandledException
+from plomtask.exceptions import BadFormatException, NotFoundException
class TestsSansDB(TestCase):
def test_Day_dates(self) -> None:
"""Test Day's date format."""
- with self.assertRaises(HandledException):
+ with self.assertRaises(BadFormatException):
Day('foo')
- with self.assertRaises(HandledException):
+ with self.assertRaises(BadFormatException):
Day('2024-02-30')
- with self.assertRaises(HandledException):
+ with self.assertRaises(BadFormatException):
Day('2024-02-01 23:00:00')
self.assertEqual(datetime(2024, 1, 1), Day('2024-01-01').datetime)
def test_Day_by_date(self) -> None:
"""Test Day.by_date()."""
- with self.assertRaises(HandledException):
+ with self.assertRaises(NotFoundException):
Day.by_date(self.db_conn, '2024-01-01')
Day('2024-01-01').save(self.db_conn)
self.assertEqual(Day('2024-01-01'),
Day.by_date(self.db_conn, '2024-01-01'))
- with self.assertRaises(HandledException):
+ with self.assertRaises(NotFoundException):
Day.by_date(self.db_conn, '2024-01-02')
self.assertEqual(Day('2024-01-02'),
Day.by_date(self.db_conn, '2024-01-02', create=True))
self.conn.request('GET', '/day?date=3000-01-01')
self.assertEqual(self.conn.getresponse().status, 200)
self.conn.request('GET', '/day?date=FOO')
- self.assertEqual(self.conn.getresponse().status, 400)
+ self.assertEqual(self.conn.getresponse().status, 401)
self.conn.request('GET', '/calendar')
self.assertEqual(self.conn.getresponse().status, 200)
self.conn.request('GET', '/calendar?start=&end=')
self.conn.request('GET', '/calendar?start=2024-01-01&end=2025-01-01')
self.assertEqual(self.conn.getresponse().status, 200)
self.conn.request('GET', '/calendar?start=foo')
- self.assertEqual(self.conn.getresponse().status, 400)
+ self.assertEqual(self.conn.getresponse().status, 401)
from urllib.parse import urlencode
from tests.utils import TestCaseWithDB, TestCaseWithServer
from plomtask.processes import Process
-from plomtask.misc import HandledException
+from plomtask.exceptions import NotFoundException
class TestsSansDB(TestCase):
def test_Process_by_id(self) -> None:
"""Test Process.by_id()."""
- with self.assertRaises(HandledException):
+ with self.assertRaises(NotFoundException):
Process.by_id(self.db_conn, None, create=False)
- with self.assertRaises(HandledException):
+ with self.assertRaises(NotFoundException):
Process.by_id(self.db_conn, 0, create=False)
- with self.assertRaises(HandledException):
+ with self.assertRaises(NotFoundException):
Process.by_id(self.db_conn, 1, create=False)
self.assertNotEqual(Process(1).id_,
Process.by_id(self.db_conn, None, create=True).id_)
body=encoded_form_data, headers=headers)
self.assertEqual(self.conn.getresponse().status, expect)
form_data = {'title': 'foo', 'description': 'foo', 'effort': 1.0}
- post_data_to_expect(form_data, '/process?id=FOO', 400)
+ post_data_to_expect(form_data, '/process?id=FOO', 401)
form_data['effort'] = 'foo'
- post_data_to_expect(form_data, '/process?id=', 400)
+ post_data_to_expect(form_data, '/process?id=', 401)
form_data['effort'] = None
- post_data_to_expect(form_data, '/process?id=', 400)
+ post_data_to_expect(form_data, '/process?id=', 401)
form_data = {'title': None, 'description': 1, 'effort': 1.0}
post_data_to_expect(form_data, '/process?id=', 302)
retrieved = Process.by_id(self.db_conn, 1)
self.conn.request('GET', '/process?id=0')
self.assertEqual(self.conn.getresponse().status, 200)
self.conn.request('GET', '/process?id=FOO')
- self.assertEqual(self.conn.getresponse().status, 400)
+ self.assertEqual(self.conn.getresponse().status, 401)
self.conn.request('GET', '/processes')
self.assertEqual(self.conn.getresponse().status, 200)