Python tornado implementation of Zen-like path system

Source: Internet
Author: User
Tags auth

Recently landlord overtime, long time no update my blog, Hey, long story, nonsense I will not say, to start on the essence.

Background: There are many bug management tools on the market, but each has its own characteristics, the most famous, the most popular is Zen road, an accidental opportunity to contact the Python, learned tornado, think about how to use to implement, Later found that their company in addition to Zen Road to record bugs there is no tool to use.

Language: Python3 Third Library: Tornado,qiniu (for cloud storage files), database with SQLite

Why use Tornado? A lot of people will actually ask me, I feel that tornado can be asynchronous, although the code is not used to asynchronous, I feel still very good framework, it is worth learning, now a lot of companies are in use, personal feeling this is a good, worthy of all of us to learn the framework.

Take a look at my requirements document

The big question is such a contour, then when I get this, I will carry on the demand analysis,

Need to what kind of database, data model of the relationship between, although there are still a lot of places are written dead have not yet set up the search function, but I believe that with the current demo

So I started to design my main structure

Handlsers store views similar to Flask

Models storage Database related,

Static storage

Template Storage Templates

Untils Storing public libraries

Setting configuration file

URLs URL address mapping

Run the file.

So I'm going to start designing my data, and actually my data model is twists.

Choose to use the SQLAlchemy, previously used flask SQLAlchemy feel good

From models.dataconfig import db_session,base,create_allfrom sqlalchemy import Column,integer,datetime,boolean, String,foreignkey,desc,asc,textfrom sqlalchemy.orm Import relationship,backreffrom Untils.common Import Encryptimport datetimeclass User (Base): __tablename__= ' users ' Id=column (Integer (), primary_key=true) Username=column (string), Unique=true,index=true) Email=column (String) Password=column (String ()) Last_logtime=column ( DateTime ()) Status=column (Integer ()) Leves=column (Integer ()) Iphone=column (Integer ()) projects=relationship (' Project ', backref= ' users ') shebei=relationship (' Shebei ', backref= ' users ') file=relationship (' Filepan ', backref= ' Users ') banben=relationship (' Banbenwrite ', backref= ' users ') testresult=relationship (' TestResult ', backref= ' users ') Testcase=relationship (' testcase ', backref= ' users ') buglog=relationship (' Buglog ', backref= ' users ') def __repr__ (self ): Return self.username@classmethoddef get_by_id (CLS, id): item = Db_session.query (User). Filter (User.id==id). First () Return ITEM@CLASSMEthoddef get_by_username (CLS, username): item = Db_session.query (User). Filter (user.username== username). First () Return Item@classmethoddef get_count (CLS): Return Db_session.query (Shebei). Count () @classmethoddef add_new (CLS, username,password,iphone,email,leves): New=user (username=username,iphone=iphone,email=email,leves=leves) New.password=encrypt (password) new.status=0db_session.add (new) Try:db_session.commit () Except:db_session.rollback () class Shebei (Base): __tablename__= ' Shebeis ' Id=column (Integer (), primary_key=true) Shebei_id=column (String (32), Unique=true) Shebei_name=column (String) shebei_xitong=column (string (255)) Shebei_xinghao=column (String) Shebei_jiage=column (Integer ()) Shebei_fapiaobianhao=column (String) Shebei_quanxian=column (Boolean ()) Shebei_ Jie=column (Boolean ()) Shebei_shuyu=column (String ()) Shebei_date=column (DateTime ()) Shebei_user=column (String ()) Gou_date=column (DateTime ()) Shebei_status=column (String ()) She_sta=column (Integer (), default=0) Ruku_user=column (Integer (), ForeignKey (' USers.id ') def __repr__ (self): return self.shebei_name@classmethoddef get_by_name (cls,name): Item=db_session.query ( Shebei). Filter (Shebei.shebei_name==name). First () return item@classmethoddef get_by_id (cls,id): item=db_ Session.query (Shebei). Filter (Shebei.id==id). First () return Item@classmethoddef get_count (CLS): Return db_ Session.query (Shebei). Count () class TestResult (Base): __tablename__= ' TestResults ' Id=column (Integer (), primary_key= True) Porject_id=column (Integer (), ForeignKey (' projects.id ')) Creat_time=column (DateTime ()) Bug_first=column ( Integer ()) Ceshirenyuan=column (String (255)) Is_send=column (Boolean (), Default=true) Filepath=column (String (64)) Status=column (Integer (), default=0) User_id=column (Integer (), ForeignKey (' Users.id ')) def __repr__ (self): return Self.porject_name@classmethoddef Get_by_name (cls,name): Item=db_session.query (TestResult). Filter ( Testresult.porject_name==name). First () return item@classmethoddef get_by_id (cls,id): Item=db_session.query ( TestResult). Filter (Testresult.id==id). First () returnItem@classmethoddef get_by_user_id (cls,user_id): Item=db_session.query (TestResult). Filter (TestResult.user_id== USER_ID). First () return Item@classmethoddef get_count (CLS): Return Db_session.query (TestResult). Count () class Banbenwrite (Base): __tablename__= ' Banbens ' Id=column (Integer (), primary_key=true) Porject_id=column (Integer (), ForeignKey (' projects.id ')) Creat_time=column (DateTime (), Default=datetime.datetime.now ()) Banbenhao=column (String (32)) Is_xian=column (Boolean (), Default=false) Is_test=column (Boolean (), Default=false) Status=column (Integer ()) user_id= Column (Integer (), ForeignKey (' users.id ')) bugadmin=relationship (' Bugadmin ', backref= ' Banbens ') def __repr__ (self): Return self.banbenhao@classmethoddef Get_by_name (cls,name): Item=db_session.query (Banbenwrite). Filter ( Banbenwrite.porject_name==name). First () return item@classmethoddef get_by_id (cls,id): Item=db_session.query ( Banbenwrite). Filter (Banbenwrite.id==id). First () return item@classmethoddef get_by_user_id (cls,user_id): item=db_ Session.query (Banbenwrite). Filter (BANBENWRITE.USER_ID==USER_ID). First () return Item@classmethoddef get_count (CLS): Return Db_session.query ( Banbenwrite). Count () class Filepan (Base): __tablename__= ' Files ' Id=column (Integer (), primary_key=true) file_fenlei= Column (String) file_name=column (String) Down_count=column (Integer (), default=0) Creat_time=column (DateTime (), Default=datetime.datetime.now ()) Status=column (Integer (), default=0) Down_url=column (String ()) Is_tui=column ( Boolean (), Default=false) User_id=column (Integer (), ForeignKey (' Users.id ')) def __repr__ (self): return Self.file_ Name@classmethoddef Get_by_file_name (cls,name): Item=db_session.query (Filepan). Filter (Filepan.file_name==name). First () return item@classmethoddef get_by_id (cls,id): Item=db_session.query (Filepan). Filter (Filepan.id==id). First ( ) return Item@classmethoddef get_by_user_id (cls,user_id): Item=db_session.query (Filepan). Filter (filepan.user_id== USER_ID). First () return Item@classmethoddef get_count (CLS): Return Db_session.query (Filepan). Count () class Bugadmin ( Base): __tablename__= ' bugadmins ' Id=column (Integer (), primary_key=true) Porject_id=column (Integer (), ForeignKey (' Projects.id ') Bugname=column (String ()) Bugdengji=column (String ()) Bugtime=column (DateTime (), default= Datetime.datetime.now ()) Bug_miaoshu=column (String (255)) Ban_id=column (Integer (), ForeignKey (' banbens.id ')) Fujian =column (String) Is_que=column (Boolean ()) Bug_status=column (String ()) Bug_jiejuefangan=column (string ()) bug _send=column (String) Status=column (Integer (), default=0) bug_log=relationship (' Buglog ', backref= ' bugadmins ') User_id=column (Integer (), ForeignKey (' Users.id ')) def __repr__ (self): return self.bugname@classmethoddef Get_by_ Bugname (cls,bugname): Item=db_session.query (Bugadmin). Filter (Bugadmin.bugname==bugname). First () return Item@classmethoddef get_by_id (Cls,id): Item=db_session.query (Bugadmin). Filter (Bugadmin.id==id). First () return Item@classmethoddef Get_by_porject_name (cls,porject_name): Item=db_session.query (BugAdmin). Filter ( Bugadmin.porject_name==porject_name). First () return ITem@classmethoddef get_count (CLS): Return Db_session.query (Bugadmin). Count () class TestCase (Base): __tablename__= ' Testcases ' Id=column (Integer (), primary_key=true) Porject_id=column (Integer (), ForeignKey (' projects.id ')) casename= Column (String)) Case_qianzhi=column (String ()) Case_buzhou=column (String ()) Case_yuqi=column (String ()) status= Column (Integer (), default=0) Case_crea_time=column (DateTime (), Default=datetime.datetime.now ()) User_id=column ( Integer (), ForeignKey (' Users.id ')) def __repr__ (self): return self.casename@classmethoddef get_by_project_name (Cls, PROJECT_NAME): Item=db_session.query (TestCase). Filter (Testcase.project_name==project_name). First () return Item@classmethoddef Get_by_casename (cls,casename): Item=db_session.query (TestCase). Filter (testcase.casename== Casename). First () return item@classmethoddef get_by_id (cls,id): Item=db_session.query (TestCase). Filter (testcase.id ==id). First () return Item@classmethoddef get_count (CLS): Return Db_session.query (TestCase). Count () class Buglog (Base) : __tablename__= ' buglogs ' Id=column (Integer (), primary_key=true) Bug_id=column (Integer (), ForeignKey (' bugadmins.id ')) caozuo= Column (String ()) Caozuo_time=column (DateTime ()) User_id=column (Integer (), ForeignKey (' Users.id ')) def __repr__ (self ): Return self.caozuo@classmethoddef get_by_id (cls,id): Item=db_session.query (Buglog). Filter (Buglog.id==id). First ( ) return Item@classmethoddef get_by_user_id (cls,user_id): Item=db_session.query (Buglog). Filter (buglog.user_id== USER_ID). First () return item@classmethoddef get_by_bug_id (cls,bug_id): Item=db_session.query (Buglog). Filter ( buglog.bug_id==bug_id). First () return Itemclass Project (Base): __tablename__= ' Projects ' Id=column (Integer (), Primary _key=true) Name=column (String ()) User_id=column (Integer (), ForeignKey (' users.id ')) bug_log=relationship (' Bugadmin ', backref= ' projects ') banben=relationship (' Banbenwrite ', backref= ' projects ') testresult=relationship (' TestResult ', backref= ' projects ') testcase=relationship (' TestCase ', backref= ' projects ') def __repr__ (self): return Self.name@classmethoddef gET_BY_ID (Cls,id): Item=db_session.query (Project). Filter (Project.id==id). First () return item @classmethoddef Get_by_ Name (Cls,name): Item=db_session.query (Project). Filter (Project.name==name). First () return item

This is a database-related,

1 Database configuration-related2 3  fromSQLAlchemyImportCreate_engine4  fromSqlalchemy.ormImportScoped_session,sessionmaker5  fromSqlalchemy.ext.declarativeImportDeclarative_base6Engine=create_engine ('sqlite:///shebei.db', convert_unicode=True)7Base=declarative_base ()8Db_session=scoped_session (Sessionmaker (bind=engine))9 defCreate_all ():Ten Base.metadata.create_all (Engine) One defDrop_all (): ABase.metadata.drop_all (Engine)

In fact, in the process of development, also encountered a lot of resistance, such as the download has been achieved bad, such as paging, but also reference to the realization of others,

Paging Public Module

Class Pagination:def __init__ (self, Current_page, all_item): try:page = Int (current_page) ex Cept:page = 1 if page < 1:page = 1 All_pager, c = Divmod (All_item, ten) if    int (c) > 0:all_pager + = 1 self.current_page = page Self.all_pager = All_pager @property def start: Return (self.current_page-1) * @property def End (self): return Self.current_pag  E * def string_pager (self, base_url= "/index/"): if self.current_page = = 1:prev = ' <li><a href= "javascript:void (0);" > Prev </a></li> ' else:prev = ' <li><a href= '%s%s ' > previous </a></li> '% ( Base_url, self.current_page-1,) if self.current_page = = Self.all_pager:nex = ' <li><a href= ' j Avascript:void (0); " > Next </a></li> ' Else:nex = ' <li><a href= '%s%s ' > Next </A></li> '% (base_url, Self.current_page + 1,) last = ' <li><a href= '%s%s ' > End &LT;/A&GT;&LT;/LI&G t; '% (Base_url, Self.all_pager,) Str_page = "". Join ((prev,nex,last)) return Str_page

When uploading the file, originally stored in the local, the result, the download processing is not good, then chose seven cattle, need to seven cattle official website to register their own account

From Qiniu import Auth,put_file,etag,urlsafe_base64_encodeimport qiniu.configaccess_key= ' Uvxowducyx641ivtub111wbei4112l3d117jhnm_aotskrh4 ' secret_key= ' pdxu9xrxtltp1n21bhu1frm1fdzqe1qhjkeae9d1xvlz5c ' def sendfile (key,file):    q=auth (access_key,secret_key)    bucket_name= ' leilei22 '    token = Q.upload_token ( Bucket_name, key)    ret, info = put_file (token, key, file)    me= ret[' hash ']    f=etag (file)    if me==f:        Assert_t=true    Else:        assert_t=false    return assert_t

Parsing Excel, primarily for uploading test cases

ImportXLRD,XLWT fromXlutils.copyImportCopydefDatacel (filepath): File=Xlrd.open_workbook (filepath) Me=file.sheets () [0] nrows=me.nrows porject_id_list=[] casename_list=[] case_qianzhi_list=[] case_buzhou_list=[] case_yuqi_list=[]     forIinchRange (1, nrows): Porject_id_list.append (Me.cell (i,0). Value) Casename_list.append (Me.cell (i,2). Value) Case_qianzhi_list.append (Me.cell (i,3). Value) Case_buzhou_list.append (Me.cell (i,4). Value) Case_yuqi_list.append (Me.cell (i,1). Value)returnPorject_id_list,casename_list,case_qianzhi_list,case_buzhou_list,case_yuqi_list

In fact, so now the public module is finished, in fact, can now start to write our code, the main code, as well as static interface, because the front and back are my own, my front-end is actually from the Internet to find the template,

The path of learning is painful, but I believe I can succeed,

From tornado.web import requesthandlerfrom models.model_py import userclass Basehandler (RequestHandler): @propertydef db (self): return self.application.dbdef Get_current_user (self): user_id = Self.get_secure_cookie (' user_id ') if not user _id:return nonereturn user.get_by_id (int (user_id))

The underlying class integrates the RequestHandler class, makes some simple customizations, and then later can use this,

In the two weeks of development, has formed a mature,

Source is already open source GitHub

Https://github.com/liwanlei/tornado_guanli

In addition I provide VIP guidance, Python automation, Python learning above, Python to do test development above, I will provide guidance, VIP lifetime effective, pay 700, contact qq:952943386, later will update QQ group, like friends can come, I also from a do not know what is python to now can do things, from 15 of the 2k to 17 of the 10k+ are bitter tears, you can share with everyone, to provide ideas, can let you go farther, I also from the white Paper a step by step, if you are a new graduate student This is also suitable for you, can give you guidance, But I can do is to take you, but I am not a training course, do not provide opportunities for employment, I will only give you the opportunity to recommend, learning, at this time may I can only teach you some tests, provide career guidance, I may later will bring you more.

I am a college students, but also a person to fight to this day.

Python tornado implementation of Zen-like path system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.