from app import db
from sqlalchemy.dialects.postgresql import JSONB


class Ruleset(db.Model):
    __tablename__ = 'project'
    uid = db.Column(db.String, primary_key=True)
    created_date = db.Column(db.DateTime)
    modified_date = db.Column(db.DateTime)
    query = db.Column(db.String)
    connection_string = db.Column(db.String)
    rule_groups = db.Column(JSONB)

    def __repr__(self):
        return f'<Ruleset {self.uid}>'

    def from_dict(self, dct):
        for field in ['uid', 'created_date', 'modified_date', 'query', 'connection_string', 'rule_group']:
            if field in dct:
                setattr(self, field, dct[field])

    def to_dict(self, keys=['uid', 'created_date', 'modified_date', 'connection_string', 'query', 'rule_groups']):
        dct = {'uid': self.n_metadata, 'created_date': self.created_date, 'connection_string': self.connection_string,
               'modified_date': self.modified_date, 'query': self.query, 'rule_groups': self.rule_groups}
        out = {}
        for key in keys:
            out[key] = dct[key]
        return out
