class KMGroup(adapter.Model): __tablename__ = 'km_group' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) name = adapter.Column(adapter.String(50)) parent_id = adapter.Column(adapter.Integer) def __init__(self, data=None): if data is None: self.name = '' self.parent_id = -1 else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.name = data.get_request_parameter('name', default='') self.parent_id = data.get_request_parameter('parent_id', default=-1) @classmethod def get(cls, id): if id is None: group = KMGroup() else: group = super(KMGroup, cls).get(id=id) return group
class KMBlogComment(adapter.Model): __tablename__ = 'km_blog_comment' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) article_id = adapter.Column(adapter.Integer) comment = adapter.Column(adapter.Text) def __init__(self, data=None): if data is None: self.comment = '' else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.article_id = data.get_request_parameter('article_id', default='', decode=True) self.comment = data.get_request_parameter('comment', default='', decode=True) def validate(self): return True
class KMBlogInfo(adapter.Model): __tablename__ = 'km_blog_info' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) name = adapter.Column(adapter.Text) url = adapter.Column(adapter.Text) description = adapter.Column(adapter.Text) def __init__(self, data=None): if data is None: self.name = '' self.url = '' self.description = '' else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.name = data.get_request_parameter('name', default='', decode=True) self.url = data.get_request_parameter('url', default='') self.description = data.get_request_parameter('description', default='', decode=True) def validate(self): self.error = KMValidateError() if self.name == '': self.error.add_data(id='name', message='ブログ名は必須です。') if self.url == '': self.error.add_data(id='url', message='URLは必須です。') if self.error.size() == 0: return True else: return False @classmethod def get(cls, id): if id is None: info = KMBlogInfo() else: info = super(KMBlogInfo, cls).get(id=id) return info @classmethod def save_data(cls, id, data): if id is None: info = KMBlogInfo(data) else: info = KMBlogInfo.get(id=id) info.set_data(data) if info.validate(): info.save() return info
class KMParameter(adapter.Model): __tablename__ = 'km_parameter' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) key = adapter.Column(adapter.String(50), index=True, unique=True) json = adapter.Column(adapter.Text()) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self)
class KMBlogCategory(adapter.Model): __tablename__ = 'km_blog_category' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) info_id = adapter.Column(adapter.Integer) name = adapter.Column(adapter.Text) def __init__(self, data=None): if data is None: self.name = '' else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.name = data.get_request_parameter('name', default='', decode=True) self.info_id = data.get_request_parameter('info_id', default=None) def validate(self): self.error = KMValidateError() if self.name == '': self.error.add_data(id='name', message='カテゴリ名は必須です。') if self.error.size() == 0: return True else: return False @classmethod def get(self, id): if id is None: info = KMBlogCategory() else: info = super(KMBlogCategory, self).get(id=id) return info @classmethod def save_data(self, id, data): if id is None: category = KMBlogCategory(data) else: category = KMBlogCategory.get(id=id) category.set_data(data) if category.validate(): category.save() return category
class KMBlogArticle(adapter.Model): __tablename__ = 'km_blog_article' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) info_id = adapter.Column(adapter.Integer) category_id = adapter.Column(adapter.Integer) title = adapter.Column(adapter.Text) article = adapter.Column(adapter.Text) post_date = adapter.Column(adapter.DateTime) def __init__(self, data=None): if data is None: self.title = "" self.article = "" self.post_date = datetime.datetime.now() else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.info_id = data.get_request_parameter('info_id') self.category_id = data.get_request_parameter('category_id') self.title = data.get_request_parameter('title', default='', decode=True) self.article = data.get_request_parameter('article', default='', decode=True) def validate(self): self.error = KMValidateError() if self.title == '': self.error.add_data(id='title', message='記事名は必須です。') if self.error.size() == 0: return True else: return False @classmethod def get(self, id): if id is None: info = KMBlogArticle() else: info = super(KMBlogArticle, self).get(id=id) return info @classmethod def save_data(self, id, data): if id is None: article = KMBlogArticle(data) else: article = KMBlogArticle.get(id=id) article.set_data(data) if article.validate(): article.save() return article
class KMRole(adapter.Model): __tablename__ = 'km_role' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) name = adapter.Column(adapter.String(50)) target = adapter.Column(adapter.String(100)) is_allow = adapter.Column(adapter.Boolean) def __init__(self, data=None): if data is None: self.name = '' self.target = '' self.is_allow = False else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.name = data.get_request_parameter('name', default='') self.target = data.get_request_parameter('target', default='') self.is_allow = data.get_request_parameter('is_allow', default=False) @classmethod def get(cls, id): if id is None: role = KMRole() else: role = super(KMRole, cls).get(id=id) return role
class KMBlogSubscription(adapter.Model): __tablename__ = 'km_blog_subscription' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) user_id = adapter.Column(adapter.Integer) target_id = adapter.Column(adapter.Integer) def __init__(self, data=None): if data is None: self.name = '' self.url = '' self.description = '' else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.name = data.get_request_parameter('name', default='', decode=True) self.url = data.get_request_parameter('url', default='') self.description = data.get_request_parameter('description', default='', decode=True) def validate(self): self.error = KMValidateError() if self.name == '': self.error.add_data(id='name', message='ブログ名は必須です。') if self.url == '': self.error.add_data(id='url', message='URLは必須です。') if self.error.size() == 0: return True else: return False
class KMUser(adapter.Model): __tablename__ = 'km_user' id = adapter.Column(adapter.Integer, autoincrement=True, primary_key=True) user_id = adapter.Column(adapter.String(20)) name = adapter.Column(adapter.String(50)) password = adapter.Column(adapter.String(20)) mail_address = adapter.Column(adapter.String(254)) group_id = adapter.Column(adapter.Integer) role_id = adapter.Column(adapter.Integer) def __init__(self, data=None): if data is None: self.user_id = '' self.name = '' self.password = '' self.mail_address = '' else: self.set_data(data) def __repr__(self): return create_repr_str(self) def get_json(self): return create_json(self) def set_data(self, data): self.error = None self.user_id = data.get_request_parameter('user_id', default='') self.name = data.get_request_parameter('name', default='') self.password = data.get_request_parameter('password', default='') self.mail_address = data.get_request_parameter('mail_address', default='') self.group_id = data.get_request_parameter('group_id', default=None) self.role_id = data.get_request_parameter('role_id', default=None) def save(self, validate=True): self.password = bcrypt.hashpw( self.password.encode(SETTINGS.CHARACTER_SET), bcrypt.gensalt()) super(KMUser, self).save() @classmethod def get(cls, id): if id is None: user = KMUser() else: user = super(KMUser, cls).get(id=id) return user
class User(adapter.Model): name = adapter.Column(adapter.String(50)) def validate(self): self.name = self.name.lower() # for test