Bad taste of code: control structure nesting too deep

Source: Internet
Author: User

Before reconstructionCode

     Def Newprj (self, DB, userid, prjname, password, privacy, Intro = '' , Status = '  Active  ' ):
S = ''
Flag = false
Prjid = 0

Try :
T = dB. Transaction ()
If Privacy =' Public ' And Len (password) <1:
Return ' {"Result": "error", "message": "public access without a password. Anyone can join your project "} '

Folderid, deep = tn. newfolder (dB, prjname, userid, 0, 0)
If Folderid! = 0:
Prjid = dB. insert ( ' Project ' , Folderid = folderid, userid = userid, projectname = prjname,
Password = password, privacy = privacy, Intro = intro, status = status,
Createtime = STR (datetime. datetime. Today (), revision = util. new_revision (dB, userid ))
DB. Update ( " Category " , Where = " Catid = $ folderid " , Projectid = prjid, deep =-1, vars = locals ())

Import JSON
From RoleImport Rolemgr
Rolemgr = rolemgr ()
S = rolemgr. newrole (dB, " Project user " , Prjid)
Dict = JSON. jsondecoder (). Decode (s)
If Dict [ ' Result ' ] = ' OK ' :
Roleid = int (dict [ ' Roleid ' ])
If Rolemgr. bindpermission (dB, roleid, folderid, READ = 1, write = 0) = true:
Flag = true
S = ' {"Result": "OK", "prjid": "% d "} ' % (INT (prjid ))
Else :
S = ' {"Result": "error", "message": "failed to preset project role Permissions "} '
Else :
S = ' {"Result": "error", "message": "preset project role failed "} '
Else :
S = ' {"Result": "error", "message": "a folder with the same name exists "} '

If Flag = true:
T. Commit ()
Else :
T. rollback ()

Return S

Except :
T. rollback ()

Code after reconstruction

 Def Newprj (dB, userid, projectname, password, privacy, intro, status = '  Active  ' ):
Try :
T = dB. Transaction ()
Projectname = projectname. lstrip (). rstrip ()
If Not _ Checkname (projectname ):
T. rollback ()
Return (False, " Invalid project name " )
Folderid, deep = tn. newfolder (dB, projectname, userid, 0, 0)
If Folderid = 0:
T. rollback ()
Return (False, " An error occurred while creating the project root directory. " )
Projectid = _ insert (dB, projectname, folderid, userid, password, privacy, intro, status)
If Projectid = 0:
T. rollback ()
Return (False, " An error occurred while creating the project. " )
If Folder. setrootfolder (dB, folderid, projectid) = false:
T. rollback ()
Return (False," Failed to Set Directory " )
If _ Presetrole (dB, userid, folderid, projectid) = false:
T. rollback ()
Return (False, " An error occurred while creating the default role. " )
If _ Presetfolder (dB, userid, folderid) = false:
T. rollback ()
Return (False, " An error occurred while creating the project directory. " )

T. Commit ()
Return (True, projectid)
Except :
T. rollback ()
Raise

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.