示例#1
0
def createSuperuser():
    from django.utils import timezone
    from account.models import User, Profile
    print(bcolors.OKBLUE + "\n 建立超級管理員帳號" + bcolors.ENDC)
    try:
        while(True):
            username = raw_input("帳號: ")
            check = len(list(User.objects.filter(username=username)))
            if check==0:
                break
            print(bcolors.FAIL + "帳號已經被註冊 \n" + bcolors.ENDC)
        
        password = ""
        password2 = ""
        while(True):
            password = getpass.getpass("密碼: ")
            password2 = getpass.getpass("密碼(再一次): ")
            if password==password2:
                break
            else:
                print(bcolors.FAIL + "密碼不一樣,重新輸入 \n"  + bcolors.ENDC)
                
        while(True):
            email = raw_input("電子郵件: ")
            
            if email_valid(email):
                break
            print(bcolors.FAIL + "請輸入正確的電子郵件"  + bcolors.ENDC)
        
        admin = User()
        admin.username = username
        admin.set_password(password)
        admin.email = email
        admin.is_superuser = True
        admin.is_staff = True
        admin.is_active = True
        admin.date_joined = timezone.now()
        admin.save()
        
        userProflie = Profile()
        userProflie.user = admin
        userProflie.fullName=username
        userProflie.type = 2  #0=normal user, 1=manager, 2=administrator
        userProflie.isActive = True
        userProflie.isAuth = False
        userProflie.save()
        
        print(bcolors.OKBLUE + "\n "+ username +"超級管理員帳號建立成功 \n \n" + bcolors.ENDC)
    except Exception as e:
        s = str(e)
        print(bcolors.FAIL + "\n\n取消建立帳號 \n"  + bcolors.ENDC)
        if """does not exist""" in s:
            print(bcolors.FAIL + "資料庫有問題,請檢查 \n"  + bcolors.ENDC)
示例#2
0
def create():
    try:
        while(True):
            username = raw_input("帳號: ")
            check = len(list(User.objects.filter(username=username)))
            if check==0:
                break
            print(bcolors.FAIL + "帳號已經被註冊 \n" + bcolors.ENDC)
        
        password = ""
        password2 = ""
        while(True):
            password = getpass.getpass("密碼: ")
            password2 = getpass.getpass("密碼(再一次): ")
            if password==password2:
                break
            else:
                print(bcolors.FAIL + "密碼不一樣,重新輸入 \n"  + bcolors.ENDC)
                
        while(True):
            email = raw_input("電子郵件: ")
            
            if email_valid(email):
                break
            print(bcolors.FAIL + "請輸入正確的電子郵件"  + bcolors.ENDC)
        
        admin = User()
        admin.username = username
        admin.set_password(password)
        admin.email = email
        admin.is_superuser = True
        admin.is_staff = True
        admin.is_active = True
        admin.date_joined = timezone.now()
        admin.save()
        
        userProflie = Profile()
        userProflie.username = username
        userProflie.user = admin
        userProflie.save()
        print(bcolors.OKBLUE + "\n "+ username +"帳號建立成功 \n \n" + bcolors.ENDC)
    except Exception as e:
        s = str(e)
        print(bcolors.FAIL + "\n\n取消建立帳號 \n"  + bcolors.ENDC)
        if """does not exist""" in s:
            print(bcolors.FAIL + "資料庫有問題,請檢查 \n"  + bcolors.ENDC)