Python tornado implementation class Zen system, pythontornado

Source: Internet
Author: User

Python tornado implementation class Zen system, pythontornado

Recently, the landlord has worked overtime and has not updated my blog for a long time. Ah, it's hard to say a word, so I will not talk about it anymore.

Background: At present, there are many bug management tools on the market, but each has its own characteristics. The most famous one is Zen road. After learning tornado, I accidentally came into contact with python, I thought about how to use NLP. Later I found that my company had no tools to record bugs except the Zen Road.

Language: python3 database 3: tornado, qiniu (for cloud storage files), Database Using sqlite

Why use tornado? Many people actually ask me this question. I feel that tornado can implement Asynchronization. Although the Code has not yet been used Asynchronization, I feel that it is a very good framework and worth learning, many companies are using it now. I personally think this is a good framework worth learning.

View my requirement documents

This is the outline of the big question. When I get this, I will analyze the requirements,

What kind of database and data models are needed? Although there are still many cases where the search function is not configured yet, I believe that with this demo

Then I started to design my main structure.

Handlsers stores views similar to flask

Models database-related,

Static storage static

Template storage template

Untils

Setting configuration file

Url ing

Run the file.

Then I began to design my data. In fact, my data model is also a wave of twists and turns.

I chose sqlalchemy. I used sqlalchemy of flask before. It feels 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(64),unique=True,index=True)email=Column(String(64))password=Column(String(64))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(64))shebei_xitong=Column(String(64))shebei_xinghao=Column(String(255))shebei_jiage=Column(Integer())shebei_fapiaobianhao=Column(String(64))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(16))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()return item@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(64))file_name=Column(String(64))down_count=Column(Integer(),default=0)creat_time=Column(DateTime(),default=datetime.datetime.now())status=Column(Integer(),default=0)down_url=Column(String(64))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(64))bugdengji=Column(String(64))bugtime=Column(DateTime(),default=datetime.datetime.now())bug_miaoshu=Column(String(255))ban_id=Column(Integer(),ForeignKey('banbens.id'))fujian=Column(String(64))is_que=Column(Boolean())bug_status=Column(String(64))bug_jiejuefangan=Column(String(64))bug_send=Column(String(64))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(64))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(64))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 database-related,

1 Database Configuration related 2 3 from sqlalchemy import create_engine 4 from sqlalchemy. orm import scoped_session, sessionmaker 5 from sqlalchemy. ext. declarative import declarative_base 6 engine = create_engine ('sqlite: // shebei. db', convert_unicode = True) 7 Base = declarative_base () 8 db_session = scoped_session (sessionmaker (bind = engine) 9 def create_all (): 10 Base. metadata. create_all (engine) 11 def drop_all (): 12 Base. metadata. drop_all (engine)

 

In fact, many obstacles have been encountered during the development process, such as poor downloading, paging, and implementation by referring to others,

Paging public module

Class Pagination: def _ init _ (self, current_page, all_item): try: page = int (current_page) Partition T: page = 1 if page <1: page = 1 all_pager, c = divmod (all_item, 10) if int (c)> 0: all_pager + = 1 self. current_page = page self. all_pager = all_pager @ property def start (self): return (self. current_page-1) * 10 @ property def end (self): return self. current_page * 10 def string_pager (self, base_url = "/index/"): if self. current_page = 1: prev = '<li> <a href = "javascript: void (0);"> previous page </a> </li> 'else: prev = '<li> <a href = "% s"> previous page </a> </li>' % (base_url, self. current_page-1,) if self. current_page = self. all_pager: nex = '<li> <a href = "javascript: void (0);"> next page </a> </li> 'else: nex = '<li> <a href = "% s"> next page </a> </li>' % (base_url, self. current_page + 1,) last = '<li> <a href = "% s"> last page </a> </li>' % (base_url, self. all_pager,) str_page = "". join (prev, nex, last) return str_page

 

When uploading files, they were originally stored locally. As a result, the downloading process was not good, so I chose qiniu. I needed to go to the qiniu official website to register my 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

Excel parsing, mainly used to upload test cases

import xlrd,xlwtfrom xlutils.copy import copydef datacel(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=[]    for i in range(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)    return porject_id_list,casename_list,case_qianzhi_list,case_buzhou_list,case_yuqi_list

In fact, now that the public module is complete, we can start to write our code, the main code, and the static interface, because the front and back ends are all my own, my front-end is actually a template from the Internet,

The road to learning is painful, but I believe that 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 basic classes integrate the RequestHandler class and perform some simple customization. Then, you can use this class later,

After two weeks of development, we have developed a mature,

Source code has been open-source github

Https://github.com/liwanlei/tornado_guanli

 

In addition, I provide vip guidance, python automation, python learning, python testing and development, I will provide guidance, vip is valid for life, the current price is 700, contact qq: 952943386, I will update the QQ Group later, and my favorite friends can come. I have been able to do things since I was not familiar with python. From 15 k to 17 k +, I feel sad, we can share our ideas with you, provide ideas, and let you go further. I also walked step by step from a blank sheet of paper. If you are a new graduate student, this is also suitable for you, I can give you guidance, but what I can do is to bring you, but I am not a training class and have not provided a job opportunity. I will only recommend you when I have a chance, and there is no end to learning, at this time, I may only teach you some tests and provide career guidance. I may bring you more in the future.

I am a college student, and I am also dedicated to today.

 

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.