def useradd(self, username, password): ''' 添加一个用户,如果用户存在则更新用户名和密码。 :username 要添加或更新的用户名称 :password 用户要设置或者更新的密码 ''' user = User.select().where(User.username == username).first() if user: user.password = password else: user = User() user.username = username user.password = password user.role = 'ADMIN' user.locked = False user.save()
def add_user(email, password): """ Adds user to database and populates their email and pw :param email: email string :param password: plain text pw :return: person object """ # Instantiate main and populate fields person = User() person.notifications = [Notification()] person.linked_platforms = [] platform_arr = ['fb', 'yt', 'netflix', 'google'] # Create placeholder for platforms for platform in platform_arr: platform_obj = LinkedPlatform() platform_obj.platform = platform person.linked_platforms.append(platform_obj) for lp in person.linked_platforms: data_obj = Data() lp.data = [data_obj] # Create placeholder for summary stats person.summary_stats = [SummaryStats()] for sp in person.summary_stats: sp.timestamp = timezone.now() sp.fb = FacebookEntry() sp.yt = YTEntry() sp.netflix = NetflixEntry() sp.google = GoogleEntry() person.username = email person.password = password person.save() return person
def create_admin(): def validate_username(username): if db.query(User).filter(User.username == username).count() > 0: click.echo('Ta nazwa użytkownika już jest zajęta') return False return True def validate_email(email): if db.query(User).filter(User.email == email).count() > 0: click.echo('Adres e-mail już został użyty podczas rejestracji') return False return True def validate_password(password): if len(password) < 6: click.echo('Hasło jest zbyt słabe') return False return True def collect_input(label, validator=None, is_password=False): while True: result = click.prompt(label, hide_input=is_password) if validator: if validator(result): return result else: return result new_user = User() new_user.username = collect_input('Nazwa użytkownika', validate_username) new_user.email = collect_input('E-mail', validate_email) new_user.password = bcrypt.generate_password_hash(collect_input('Hasło', validator=validate_password, is_password=True)).decode('utf-8') new_user.full_name = collect_input('Pełna Nazwa') db.add(new_user) db.commit() click.echo('Pomyślnie utworzono nowego administratora!')