Store time codes as timestamps in database
This commit is contained in:
13
db.py
13
db.py
@@ -20,7 +20,7 @@ def connect_db():
|
|||||||
|
|
||||||
def query_db(query, args=(), one=False, cls=None):
|
def query_db(query, args=(), one=False, cls=None):
|
||||||
"""Runs an SQL query on an new database connection, returning the fetched rv"""
|
"""Runs an SQL query on an new database connection, returning the fetched rv"""
|
||||||
log.info(f"Running query ({query}) with arguments ({args})")
|
log.debug(f"Running query ({query}) with arguments ({args})")
|
||||||
cur = connect_db().execute(query, args)
|
cur = connect_db().execute(query, args)
|
||||||
rv = cur.fetchall()
|
rv = cur.fetchall()
|
||||||
cur.close()
|
cur.close()
|
||||||
@@ -34,6 +34,7 @@ def update_db(query, args=()):
|
|||||||
Runs an changing query on the database
|
Runs an changing query on the database
|
||||||
Returns either False if no error has occurred, or an sqlite3 Exception
|
Returns either False if no error has occurred, or an sqlite3 Exception
|
||||||
"""
|
"""
|
||||||
|
log.debug(f"Running query ({query}) with arguments ({args})")
|
||||||
with connect_db() as con:
|
with connect_db() as con:
|
||||||
try:
|
try:
|
||||||
con.cursor().execute(query, args)
|
con.cursor().execute(query, args)
|
||||||
@@ -195,9 +196,9 @@ def save_episode(episode: models.Episode):
|
|||||||
episode.season_id,
|
episode.season_id,
|
||||||
episode.title,
|
episode.title,
|
||||||
episode.date,
|
episode.date,
|
||||||
time_to_str(episode.start),
|
episode.start.timestamp(),
|
||||||
time_to_str(episode.end),
|
episode.end.timestamp(),
|
||||||
episode.code
|
episode.code,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
sql = (
|
sql = (
|
||||||
@@ -209,8 +210,8 @@ def save_episode(episode: models.Episode):
|
|||||||
episode.season_id,
|
episode.season_id,
|
||||||
episode.title,
|
episode.title,
|
||||||
episode.date,
|
episode.date,
|
||||||
time_to_str(episode.start),
|
episode.start.timestamp(),
|
||||||
time_to_str(episode.end),
|
episode.end.timestamp(),
|
||||||
episode.code,
|
episode.code,
|
||||||
episode.id,
|
episode.id,
|
||||||
)
|
)
|
||||||
|
|||||||
37
models.py
37
models.py
@@ -1,12 +1,10 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import logging
|
from numbers import Rational
|
||||||
from typing import Dict, List
|
from typing import Dict, List, Union
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
import forms
|
import forms
|
||||||
from util import str_to_time
|
import util
|
||||||
|
|
||||||
INVALID_STR = 'Form entry "{}" is invalid'
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -110,27 +108,30 @@ class Episode:
|
|||||||
id: int
|
id: int
|
||||||
season_id: int
|
season_id: int
|
||||||
title: str
|
title: str
|
||||||
date: datetime.date
|
date: Union[datetime.date, Rational]
|
||||||
start: datetime.time
|
start: Union[datetime.datetime, Rational]
|
||||||
end: datetime.time
|
end: Union[datetime.datetime, Rational]
|
||||||
code: str
|
code: str
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
try:
|
if isinstance(self.date, Rational):
|
||||||
self.date = datetime.datetime.strptime(self.date, "%Y-%m-%d").date()
|
self.date = datetime.date.fromtimestamp(self.date)
|
||||||
except TypeError as err:
|
if isinstance(self.start, Rational):
|
||||||
logging.warning(err)
|
self.start = datetime.datetime.fromtimestamp(self.start)
|
||||||
self.start = str_to_time(str(self.start))
|
if isinstance(self.end, Rational):
|
||||||
self.end = str_to_time(str(self.end))
|
self.end = datetime.datetime.fromtimestamp(self.end)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_form(cls, form: forms.EpisodeForm):
|
def from_form(cls, form: forms.EpisodeForm):
|
||||||
episode_id = int(form.episode_id.data) if form.episode_id.data else None
|
episode_id = int(form.episode_id.data) if form.episode_id.data else None
|
||||||
season_id = int(form.season_id.data)
|
season_id = int(form.season_id.data)
|
||||||
title = str(form.title.data)
|
|
||||||
date = form.date.data
|
|
||||||
start = form.start.data
|
|
||||||
end = form.end.data
|
|
||||||
code = str(form.code.data)
|
code = str(form.code.data)
|
||||||
|
title = str(form.title.data)
|
||||||
|
|
||||||
|
date = form.date.data
|
||||||
|
start = util.combine_datetime(date, form.start.data)
|
||||||
|
end = util.combine_datetime(date, form.end.data)
|
||||||
|
if end < start:
|
||||||
|
end = end + datetime.timedelta(days=1)
|
||||||
|
|
||||||
return cls(episode_id, season_id, title, date, start, end, code)
|
return cls(episode_id, season_id, title, date, start, end, code)
|
||||||
|
|||||||
24
schema.sql
24
schema.sql
@@ -78,3 +78,27 @@ create unique index if not exists episode_id_uindex
|
|||||||
|
|
||||||
alter table episode
|
alter table episode
|
||||||
add code text not null default 'EXX';
|
add code text not null default 'EXX';
|
||||||
|
|
||||||
|
|
||||||
|
create table episode_dg_tmp
|
||||||
|
(
|
||||||
|
id integer not null
|
||||||
|
constraint episode_pk
|
||||||
|
primary key autoincrement,
|
||||||
|
season_id integer not null
|
||||||
|
references season,
|
||||||
|
title text not null,
|
||||||
|
date text not null,
|
||||||
|
start timestamp not null,
|
||||||
|
end timestamp not null,
|
||||||
|
code text default 'EXX' not null
|
||||||
|
);
|
||||||
|
|
||||||
|
insert into episode_dg_tmp(id, season_id, title, date, start, end, code) select id, season_id, title, date, start, end, code from episode;
|
||||||
|
|
||||||
|
drop table episode;
|
||||||
|
|
||||||
|
alter table episode_dg_tmp rename to episode;
|
||||||
|
|
||||||
|
create unique index episode_id_uindex
|
||||||
|
on episode (id);
|
||||||
|
|||||||
22
util.py
22
util.py
@@ -4,9 +4,12 @@ TIME_FMT = "%H:%M"
|
|||||||
DATE_FMT = "%Y-%m-%d"
|
DATE_FMT = "%Y-%m-%d"
|
||||||
|
|
||||||
|
|
||||||
def str_to_time(data: str) -> datetime.time:
|
def str_to_datetime(data: str) -> datetime.datetime:
|
||||||
|
"""
|
||||||
|
Convert %H:%M formatted string into a python datetime object
|
||||||
|
"""
|
||||||
data = ":".join(data.split(":")[:2])
|
data = ":".join(data.split(":")[:2])
|
||||||
return datetime.datetime.strptime(data, TIME_FMT).time()
|
return datetime.datetime.strptime(data, TIME_FMT)
|
||||||
|
|
||||||
|
|
||||||
def time_to_str(data: datetime.time) -> str:
|
def time_to_str(data: datetime.time) -> str:
|
||||||
@@ -16,3 +19,18 @@ def time_to_str(data: datetime.time) -> str:
|
|||||||
:return: str
|
:return: str
|
||||||
"""
|
"""
|
||||||
return data.strftime(TIME_FMT)
|
return data.strftime(TIME_FMT)
|
||||||
|
|
||||||
|
|
||||||
|
def combine_datetime(date: datetime.date, time: datetime.time):
|
||||||
|
"""
|
||||||
|
Combine a date and time object into a datetime object
|
||||||
|
"""
|
||||||
|
return datetime.datetime(
|
||||||
|
date.year,
|
||||||
|
date.month,
|
||||||
|
date.day,
|
||||||
|
time.hour,
|
||||||
|
time.minute,
|
||||||
|
time.second,
|
||||||
|
time.microsecond
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user