Only run the sqlite datetime hack on columns that are expecting a datetime object (#1270)

* Only run the SQLite datetime hack (#246) on columns that are expecting a datetime object
* Closes #1255
bulk-clear-sessions
Kevin Chung 2020-02-29 20:17:54 -05:00 committed by GitHub
parent e764bb16b8
commit 65a640184b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 15 deletions

View File

@ -14,9 +14,10 @@ from datafreeze.format.fjson import JSONEncoder, JSONSerializer
from flask import current_app as app from flask import current_app as app
from flask_migrate import upgrade from flask_migrate import upgrade
from sqlalchemy.exc import OperationalError, ProgrammingError from sqlalchemy.exc import OperationalError, ProgrammingError
from sqlalchemy.sql import sqltypes
from CTFd.cache import cache from CTFd.cache import cache
from CTFd.models import db from CTFd.models import db, get_class_by_tablename
from CTFd.utils import get_app_config, set_config from CTFd.utils import get_app_config, set_config
from CTFd.utils.migrations import ( from CTFd.utils.migrations import (
create_database, create_database,
@ -242,8 +243,20 @@ def import_ctf(backup, erase=True):
# This is a hack to get SQLite to properly accept datetime values from dataset # This is a hack to get SQLite to properly accept datetime values from dataset
# See Issue #246 # See Issue #246
if sqlite: if sqlite:
direct_table = get_class_by_tablename(table.name)
for k, v in entry.items(): for k, v in entry.items():
if isinstance(v, six.string_types): if isinstance(v, six.string_types):
# We only want to apply this hack to columns that are expecting a datetime object
try:
is_dt_column = (
type(getattr(direct_table, k).type)
== sqltypes.DateTime
)
except AttributeError:
is_dt_column = False
# If the table is expecting a datetime, we should check if the string is one and convert it
if is_dt_column:
match = re.match( match = re.match(
r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d", v r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d", v
) )