class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) access_token = db.Column(db.String(128)) email = db.Column(db.String(254), unique=True, nullable=False) nick_name = db.Column(db.String(30)) password_hash = db.Column(db.String(128)) email_hash = db.Column(db.String(128)) create_time = db.Column(db.DateTime, default=datetime.now, nullable=False) state = db.Column(db.Integer, nullable=False) def __init__(self, **kwargs): super(User, self).__init__(**kwargs) self.generate_email_hash() def set_password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def generate_email_hash(self): if self.email is not None and self.email_hash is None: self.email_hash = hashlib.md5(self.email.encode('utf-8')).hexdigest() @property def is_admin(self): return self.email == current_app.config['ADMIN_EMAIL']
class FeedBack(db.Model): __tablename__ = "FeedBack" query_class = FeedBackQuery id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Unicode(200), nullable=False) email = db.Column(db.Unicode(200), nullable=False) description = db.Column(db.UnicodeText) create_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): super(FeedBack, self).__init__(*args, **kwargs) def __repr__(self): return "<%s>" % self
class ModuleType(db.Model): __tablename__ = "moduletype" query_class = ModuleTypeQuery PER_PAGE = 20 id = db.Column(db.Integer, primary_key=True) parent_id = db.Column(db.Integer, default=0, nullable=False) name = db.Column(db.Unicode(30), unique=True, nullable=False) description = db.Column(db.UnicodeText) type_create_time = db.Column(db.DateTime, default=datetime.now) type_update_time = db.Column(db.DateTime, default=datetime.now) input_template = db.Column(db.UnicodeText, nullable=False) output_template = db.Column(db.UnicodeText, nullable=False) # level = db.Column(db.Integer, default=1, nullable=False) def __init__(self, *args, **kwargs): super(ModuleType, self).__init__(*args, **kwargs) def __str__(self): return self.name def __repr__(self): return "<%s>" % self @cached_property def json(self): return dict(id=self.id, name=self.name, description=self.description, parent_id=self.parent_id, input_template=self.input_template, output_template=self.output_template, type_create_time=str(self.type_create_time), type_update_time=str(self.type_update_time))
class Case(db.Model): __tablename__ = "cases" query_class = CaseQuery PER_PAGE = 20 PUBLIC = 100 PRIVATE = 300 id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey(User.id, ondelete='CASCADE'), nullable=False) title = db.Column(db.Unicode(200), unique=True, nullable=False) description = db.Column(db.UnicodeText) case_create_time = db.Column(db.DateTime, default=datetime.now) case_update_time = db.Column(db.DateTime, default=datetime.now) access = db.Column(db.Integer, default=PUBLIC) # status = db.Column(db.Boolean, default=True) usecase_input = db.Column(db.UnicodeText) usecase_output = db.Column(db.UnicodeText) module_type = db.Column(db.Integer, db.ForeignKey(ModuleType.id, ondelete='CASCADE'), nullable=False) parent_id = db.Column(db.Integer, nullable=False) author = db.relation(User, innerjoin=True, lazy="joined") casetype = db.relation(ModuleType, innerjoin=True, lazy="joined") class Permissions(object): def __init__(self, obj): self.obj = obj @cached_property def default(self): return Permission(UserNeed(self.obj.author_id)) & moderator @cached_property def view(self): if self.obj.access == Case.PUBLIC: return Permission() return self.default @cached_property def edit(self): return self.default @cached_property def delete(self): return self.default def __init__(self, *args, **kwargs): super(Case, self).__init__(*args, **kwargs) self.access = self.access or self.PUBLIC def __str__(self): return self.title def __repr__(self): return "<%s>" % self @cached_property def permissions(self): return self.Permissions(self) @cached_property def json(self): return dict(id=self.id, author=self.author.username, title=self.title, description=self.description, case_create_time=str(self.case_create_time), case_update_time=str(self.case_update_time), access=get_access(self.access), usecase_input=self.usecase_input, usecase_output=self.usecase_output, casetype=self.casetype.name, checkBox="<input type='checkbox' name='case' value='" + str(self.id) + "' />")
class Machine(db.Model): OK = 1 REALLY = 0 PER_PAGE = 20 __tablename__ = "machine" query_class = MachineQuery id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Unicode(30), unique=True, nullable=False) description = db.Column(db.UnicodeText) m_create_time = db.Column(db.DateTime, default=datetime.now) m_update_time = db.Column(db.DateTime, default=datetime.now) status = db.Column(db.Integer, default=0) key = db.Column(db.Unicode(100)) username = db.Column(db.Unicode(30), unique=True, nullable=False) password = db.Column(db.Unicode(30), unique=True, nullable=False) url_hudson = db.Column(db.Unicode(60), nullable=False) task_count = db.Column(db.Integer, default=0) def __init__(self, *args, **kwargs): super(Machine, self).__init__(*args, **kwargs) def __str__(self): return self.name def __repr__(self): return "<%s>" % self @cached_property def json(self): return dict(id=self.id, name=self.name, description=self.description, status=self.status, m_create_time=str(self.m_create_time), m_update_time=str(self.m_update_time), username=self.username, password=self.password, url_hudson=self.url_hudson, task_count=self.task_count, key=self.key)
class User(db.Model): __tablename__ = "users" PER_PAGE = 20 query_class = UserQuery # user roles MEMBER = 100 MODERATOR = 200 ADMIN = 300 id = db.Column(db.Integer, primary_key=True) username = db.Column(db.Unicode(30), unique=True, nullable=False) email = db.Column(db.String(30), unique=True, nullable=False) status = db.Column(db.Boolean, default=True) user_create_time = db.Column(db.DateTime, default=datetime.now) user_update_time = db.Column(db.DateTime, default=datetime.now) role = db.Column(db.Integer, default=MEMBER) email_alerts = db.Column(db.Boolean, default=False) _password = db.Column("password", db.String(80)) class Permissions(object): pass def __init__(self, *args, **kwargs): super(User, self).__init__(*args, **kwargs) def __str__(self): return self.username def __repr__(self): return "<%s>" % self @cached_property def permissions(self): return self.Permissions(self) def _get_password(self): return self._password def _set_password(self, password): self._password = generate_password_hash(password) password = db.synonym("_password", descriptor=property(_get_password, _set_password)) def check_password(self, password): if self.password is None: return False return check_password_hash(self.password, password) @cached_property def provides(self): needs = [RoleNeed('authenticated'), UserNeed(self.id)] if self.is_moderator: needs.append(RoleNeed('moderator')) if self.is_admin: needs.append(RoleNeed('admin')) return needs @cached_property def json(self): return dict(id=self.id, name=self.username, email=self.email, status=self.status, user_create_time=str(self.user_create_time), user_update_time=str(self.user_update_time), role=get_role(self.role), email_alerts=self.email_alerts) @property def is_moderator(self): return self.role >= self.MODERATOR @property def is_admin(self): return self.role >= self.ADMIN @property def is_member(self): return self.role == self.MEMBER
class Taskcount(db.Model): PASS = 0 FAIL = 1 PER_PAGE = 20 __tablename__ = "task_count" query_class = TaskcountQuery id = db.Column(db.Integer, primary_key=True) task_id = db.Column(db.Integer, db.ForeignKey(Task.id, ondelete='CASCADE'), nullable=False) case_all = db.Column(db.Integer) case_pass = db.Column(db.Integer) case_fail = db.Column(db.Integer) result = db.Column(db.Integer, default=0) result_path = db.Column(db.String(150)) fail_case = db.Column(db.UnicodeText) start_time = db.Column(db.DateTime, default=datetime.now) stop_time = db.Column(db.DateTime, default=datetime.now) timing = db.Column(db.UnicodeText,default=0) other = db.Column(db.UnicodeText) build_id = db.Column(db.Integer) next = db.Column(db.Integer, default=0, nullable=False) task = db.relation(Task, innerjoin=True, lazy="joined") def __init__(self, *args, **kwargs): super(Taskcount, self).__init__(*args, **kwargs) @cached_property def json(self): return dict(id=self.id, task_id=self.task_id, case_all=self.case_all, case_pass=self.case_pass, case_fail=self.case_fail, result=self.result, result_path=self.result_path, fail_case=self.fail_case, start_time=str(self.start_time), stop_time=str(self.stop_time), timing=self.timing, build_id=self.build_id, next=self.next, other=self.other)
class Task(db.Model): __tablename__ = "task" query_class = TaskQuery PUBLIC = 100 PRIVATE = 300 PER_PAGE = 20 id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey(User.id, ondelete='CASCADE'), nullable=True) moduletype_id = db.Column(db.Integer, db.ForeignKey(ModuleType.id, ondelete='CASCADE'), nullable=True) parent_id = db.Column(db.Integer, nullable=False) name = db.Column(db.Unicode(200), unique=True, nullable=False) case = db.relationship("Case", secondary="task_case", backref=db.backref("task", lazy='dynamic')) code = db.relationship("Code", secondary="task_code", backref=db.backref("task", lazy='dynamic')) # machine_id = db.Column(db.Integer,db.ForeignKey(Machine.id, ondelete='CASCADE'),nullable=False) access = db.Column(db.Integer, default=PUBLIC) hudson = db.Column(db.Integer, default=None) task_excute_times = db.Column(db.Integer, default=0) description = db.Column(db.UnicodeText) command = db.Column(db.UnicodeText, nullable=False) runtime = db.Column(db.UnicodeText) timing = db.Column(db.UnicodeText, default=0) svn = db.Column(db.UnicodeText, nullable=False) email = db.Column(db.UnicodeText, nullable=False) email_topic = db.Column(db.UnicodeText, nullable=False) other = db.Column(db.UnicodeText) task_create_time = db.Column(db.DateTime, default=datetime.now) task_update_time = db.Column(db.DateTime, default=datetime.now) author = db.relation(User, innerjoin=True, lazy="joined") moduletype = db.relation(ModuleType, innerjoin=True, lazy="joined") # machine = db.relation(Machine, innerjoin=True, lazy="joined") class Permissions(object): def __init__(self, obj): self.obj = obj @cached_property def default(self): return Permission(UserNeed(self.obj.author_id)) & moderator @cached_property def view(self): if self.obj.access == Task.PUBLIC: return Permission() return self.default @cached_property def run(self): if self.obj.access == Task.PUBLIC: return Permission() return self.default @cached_property def edit(self): return self.default @cached_property def delete(self): return self.default def __init__(self, *args, **kwargs): super(Task, self).__init__(*args, **kwargs) self.access = self.access or self.PUBLIC @staticmethod def count_task(self): print self.task @cached_property def permissions(self): return self.Permissions(self) @cached_property def attach_task_url(self): result = [] if self.task_path: result = self.task_path.split(";").remove('') return result @cached_property def attach_code_url(self): result = [] if self.code_path: result = self.code_path.split(";").remove('') return result @cached_property def attach_other_url(self): result = [] if self.other_path: result = self.other_path.split(";").remove('') return result @cached_property def json(self): return dict(id=self.id, author=self.author.username, name=self.name, description=self.description, hudson=get_hudson(self.hudson), times=self.task_excute_times, command=self.command, runtime=self.runtime, timing=self.timing, svn=self.svn, email=self.email, email_topic=self.email_topic, other=self.other, case=str([i.title for i in self.case]), code=str([i.name for i in self.code]), task_create_time=str(self.task_create_time), task_update_time=str(self.task_update_time), access=get_access(self.access), tasktype=self.moduletype.name)
""" from datetime import datetime from werkzeug import cached_property from sqlalchemy import event, DDL from flask.ext.sqlalchemy import BaseQuery from flask.ext.principal import Permission, UserNeed from autotest.permissions import moderator from autotest.models.cases import Case from autotest.models.admin import ModuleType from autotest.models.codes import Code from autotest.models.users import User from autotest.extensions import db from autotest.helpers import get_access, get_hudson task_case = db.Table('task_case', db.Column('cases_id', db.Integer, db.ForeignKey('cases.id')), db.Column('task_id', db.Integer, db.ForeignKey('task.id')) ) task_code = db.Table('task_code', db.Column('code_id', db.Integer, db.ForeignKey('code.id')), db.Column('task_id', db.Integer, db.ForeignKey('task.id')) ) """ task_machine = db.Table('task_machine', db.Column('machine_id', db.Integer, db.ForeignKey('machine.id')), db.Column('task_id', db.Integer, db.ForeignKey('task.id')) ) """ class TaskQuery(BaseQuery):