def create_user(username, email, password): user = User( username=username, email=email, is_superuser=True, is_confirmed=True, ) user.set_password(password) user.save()
def create_user(username, email, password): if username is None: username = input('Username(default admin):') or 'admin' if email is None: email = input('Email:') if password is None: password = getpass('Password:') user = User( username=username, email=email, is_superuser=True, is_confirmed=True) user.set_password(password) user.save()
def upgrade_user(): print('upgrade user ...') users = session.execute('select * from users;') User.bulk_save([ User(id=user.id, username=user.username, email=user.email, password=user.password, is_superuser=user.is_superuser, is_confirmed=user.is_confirmed) for user in users ])
def init(): def random_sep(n=6): sep = [' '] * n sep.append("\n") sep.append("\n\n") return choice(sep) def random_word(n=20, sep=True): word = ''.join(sample(ascii_letters + digits, randrange(2, n))) if not sep: return word return word + random_sep() random_users = [ User(username=random_word(12, False), password=random_word(12, False), email=random_word(15, False)) for _ in range(4) ] random_tags = [Tag(name=random_word(12, False)) for i in range(15)] random_categories = [ Category(name=random_word(12, False)) for i in range(5) ] db.session.bulk_save_objects(random_users) db.session.bulk_save_objects(random_tags) db.session.bulk_save_objects(random_categories) db.session.commit() random_users = User.query.all() random_tags = Tag.query.all() for category in Category.query.all(): print(category, category.id) random_blogs = [ Article(category_id=category.id, title=random_word(20, False), content=' '.join([random_word() for _ in range(1000)]), user_id=choice(random_users).id, tags=[choice(random_tags) for _ in range(randrange(1, 4))]) for _ in range(randrange(3, 15)) ] db.session.bulk_save_objects(random_blogs) db.session.commit() random_timelines = [ TimeLine(content=' '.join([random_word() for _ in range(100)]), user_id=choice(random_users).id, is_hidden=choice([True, False])) for _ in range(100) ] db.session.bulk_save_objects(random_timelines) db.session.commit()
def add_author(username): username = "******" email = "*****@*****.**" author = User.query.filter_by(username=username).first() if not author: author = User( username=username, email=email, is_superuser=True, is_confirmed=True, ) author.set_password(getpass("Password: ")) author.save() return author
def write_to_blog(attr, file_type='org'): _author = attr['author'] _category = attr['category'] _tags = attr['tags'].split(',') _title = attr['title'] _date = attr['date'] _content = attr['content'] user = User.query.filter_by(username=_author).first() if not user: user = User(username=_author, email='*****@*****.**') user.set_password('123123') user.save() category = Category.query.filter_by(name=_category).first() if not category: category = Category(name=_category) category.save() tags = [] for _tag in _tags: tag = Tag.query.filter_by(name=_tag).first() if not tag: tag = Tag(name=_tag) tag.save() tags.append(tag) try: _date = datetime.strptime(_date, '%Y-%m-%d'), except ValueError: _date = datetime.strptime(_date, '%Y-%m-%d %H:%M:%S'), blog = Blog.query.filter_by(title=_title).first() if not blog: blog = Blog( title=_title, category_id=category.id, content=_content, created_at=_date, tags=tags, content_type='1' if file_type == 'org' else '0', user_id=user.id) blog.save() return blog
def create_model(self, form): user = User() user.set_password(form.password.data) form.password.data = user.password return super(UserView, self).create_model(form)