diff options
Diffstat (limited to 'src/db/scanner.py')
-rw-r--r-- | src/db/scanner.py | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/src/db/scanner.py b/src/db/scanner.py index e9ac8c3..625fd8e 100644 --- a/src/db/scanner.py +++ b/src/db/scanner.py @@ -1,7 +1,7 @@ import enum from datetime import datetime -from sqlalchemy import (Boolean, Column, DateTime, Integer, Unicode, +from sqlalchemy import (JSON, Boolean, Column, DateTime, Integer, Unicode, UniqueConstraint, create_engine) from sqlalchemy.exc import IntegrityError from sqlalchemy.ext.declarative import declarative_base @@ -22,13 +22,12 @@ class Scanner(Base): id = Column(Integer, autoincrement=True, primary_key=True) uuid = Column(Unicode(37), nullable=False) - enabled = Column(Boolean, nullable=False) + enabled = Column(Boolean, default=False, nullable=False) first_seen = Column(DateTime, default=datetime.utcnow, nullable=False) last_seen = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) - comment = Column(Unicode(255), nullable=True) - scanners = Column(Unicode(2048), nullable=False) - targets = Column(Unicode(255), nullable=True) + comment = Column(Unicode(255), default="", nullable=True) + scanners = Column(JSON, nullable=True) def as_dict(self): """Return JSON serializable dict.""" @@ -47,7 +46,8 @@ class Scanner(Base): @classmethod def comment(cls, uuid, comment): with SqlDB.sql_session() as session: - scanner: Scanner = session.query(Scanner).filter(Scanner.uuid == uuid).one_or_none() + scanner: Scanner = session.query(Scanner).filter( + Scanner.uuid == uuid).one_or_none() if scanner: scanner.comment = comment @@ -58,7 +58,8 @@ class Scanner(Base): @classmethod def enable(cls, uuid): with SqlDB.sql_session() as session: - scanner: Scanner = session.query(Scanner).filter(Scanner.uuid == uuid).one_or_none() + scanner: Scanner = session.query(Scanner).filter( + Scanner.uuid == uuid).one_or_none() if scanner: scanner.enabled = True @@ -69,7 +70,8 @@ class Scanner(Base): @classmethod def disable(cls, uuid): with SqlDB.sql_session() as session: - scanner: Scanner = session.query(Scanner).filter(Scanner.uuid == uuid).one_or_none() + scanner: Scanner = session.query(Scanner).filter( + Scanner.uuid == uuid).one_or_none() if scanner: scanner.enabled = False @@ -84,9 +86,11 @@ class Scanner(Base): with SqlDB.sql_session() as session: if scanner_id: - scanner: Scanner = session.query(Scanner).filter(Scanner.id == scanner_id).one_or_none() + scanner: Scanner = session.query(Scanner).filter( + Scanner.id == scanner_id).one_or_none() elif uuid: - scanner: Scanner = session.query(Scanner).filter(Scanner.uuid == uuid).one_or_none() + scanner: Scanner = session.query(Scanner).filter( + Scanner.uuid == uuid).one_or_none() else: return None @@ -104,7 +108,7 @@ class Scanner(Base): scanner = Scanner() scanner.uuid = uuid scanner.enabled = False - scanner.scanners = "None" + scanner.scanners = {} session.add(scanner) session.flush() @@ -113,11 +117,11 @@ class Scanner(Base): except IntegrityError: return None - @classmethod def is_enabled(cls, uuid): with SqlDB.sql_session() as session: - scanner: Scanner = session.query(Scanner).filter(Scanner.uuid == uuid).one_or_none() + scanner: Scanner = session.query(Scanner).filter( + Scanner.uuid == uuid).one_or_none() if scanner is None: return None |