def save(self): user = User(username=self.username.data, email=self.email.data, password=self.password.data, date_joined=datetime.utcnow(), primary_group_id=4) return user.save()
def get(self): categories = Category.get_all(user=real(current_user)) # Fetch a few stats about the forum user_count = User.query.count() topic_count = Topic.query.count() post_count = Post.query.count() newest_user = User.query.order_by(User.id.desc()).first() print('newest', newest_user) # Check if we use redis or not if not current_app.config['REDIS_ENABLED']: online_users = len(User.get_active()) + random.randint(-3, 3) # Because we do not have server side sessions, we cannot check if there # are online guests online_guests = None else: online_users = len(User.get_active()) + random.randint(-3, 3) online_guests = len(get_online_users(guest=True)) return render_template('forum/index.html', categories=categories, user_count=user_count, topic_count=topic_count, post_count=post_count, newest_user=newest_user, online_users=online_users, online_guests=online_guests)
def user(default_groups): """Creates a user with normal permissions.""" user = User(username="******", email="*****@*****.**", password="******", primary_group=default_groups[3], activated=True) user.save() return user
def admin_user(default_groups): """Creates a admin user.""" user = User(username="******", email="*****@*****.**", password="******", primary_group=default_groups[0], activated=True) user.save() return user
def super_moderator_user(default_groups): """Creates a super moderator user.""" user = User(username="******", email="*****@*****.**", password="******", primary_group=default_groups[1], activated=True) user.save() return user
def create_admin_user(username, password, email): """ Creates the administrator user """ admin_group = Group.query.filter_by(admin=True).first() user = User(username=username, password=password, email=email, date_joined=datetime.utcnow(), primary_group_id=admin_group.id) user.save()
def unactivated_user(default_groups): """ Creates an unactivated user in the default user group """ user = User(username='******', email='*****@*****.**', password='******', primary_group=default_groups[3], activated=False) user.save() return user
def save(self): data = self.data if not data["username"]: data["username"] = None if not data["email"]: data["email"] = None data.pop('submit', None) data.pop('csrf_token', None) user = User(**data) return user.save()
def post(self): args = self.reqparse.parse_args() user = User(username=args['username'], password=args['password'], email=args['email'], date_joined=datetime.utcnow(), primary_group_id=4) user.save() return {'user': marshal(user, user_fields)}, 201
def moderator_user(forum, default_groups): """Creates a test user for whom the permissions should be checked""" user = User(username="******", email="*****@*****.**", password="******", primary_group_id=default_groups[2].id) user.save() forum.moderators.append(user) forum.save() return user
def moderator_user(user, forum, default_groups): """Creates a test user with moderator permissions.""" user = User(username="******", email="*****@*****.**", password="******", primary_group=default_groups[2], activated=True) user.save() forum.moderators.append(user) forum.save() return user
def Fred(default_groups): """Fred is an interloper and bad intentioned user, he attempts to access areas he shouldn't, he posts trollish and spammy content, he does everything he can to destroy the board. Our job is stop Fred. """ fred = User(username='******', email='*****@*****.**', password='******', primary_group=default_groups[3], activated=True) fred.save() return fred
def __add_group_to_user(self, user_discord_id): roles = DiscordRole.query\ .join(DiscordUserRole)\ .join(DiscordUser)\ .filter(DiscordUser.id == user_discord_id)\ .all() user = User.query.filter(User.discord == user_discord_id).first() for role in roles: if role.id in hub_current_server.discord_role_to_group: group_id = hub_current_server.discord_role_to_group[role.id] group = Group.query.filter(Group.id == group_id).first() if not user: discord_user = DiscordUser.query.filter(DiscordUser.id == user_discord_id).first() assert discord_user user = User(discord=user_discord_id, display_name=discord_user.pure_name, activated=True) user.primary_group = group else: last_group = user.primary_group user.primary_group = group user.add_to_group(last_group) user.save() LogAction(current_user, "added user {name} (discord: {discord_id}) to group {group}".format( name=user.display_name, discord_id=user_discord_id, group=group.name )) flash("User {} was added to group {}".format(user.display_name, group.name)) return True flash("Failed to find appropriate group for user {}".format(user.display_name), "danger") return False
def create_test_data(): """Creates 5 users, 2 categories and 2 forums in each category. It also creates a new topic topic in each forum with a post. """ create_default_groups() create_default_settings() # create 5 users for u in range(1, 6): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.save() user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # create 2 categories for i in range(1, 3): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() # create 2 forums in each category for j in range(1, 3): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() # create a topic topic = Topic() post = Post() topic.title = "Test Title %s" % j post.content = "Test Content" topic.save(post=post, user=user1, forum=forum) # create a second post in the forum post = Post() post.content = "Test Post" post.save(user=user2, topic=topic)
def save_user(text): text = text.split("\n") for username in text: # print('saving', username) try: username = username.decode('ascii') user = User.query.filter(User.username == username).all() if len(user) == 0 and len(username) > 1: user = User(username=username, email="{}@gmail.com".format(username), _password="******", primary_group_id=4, activated=1) user.save() except: print('not asciiable')
def login(): """Logs the user in.""" if current_user is not None and current_user.is_authenticated: return redirect_or_next(url_for("forum.index")) current_limit = getattr(g, 'view_rate_limit', None) login_recaptcha = False if current_limit is not None: window_stats = limiter.limiter.get_window_stats(*current_limit) stats_diff = flaskbb_config["AUTH_REQUESTS"] - window_stats[1] login_recaptcha = stats_diff >= flaskbb_config["LOGIN_RECAPTCHA"] form = LoginForm(request.form) if form.validate_on_submit(): try: user = User.authenticate(form.login.data, form.password.data) if not login_user(user, remember=form.remember_me.data): flash( _("In order to use your account you have to activate it " "through the link we have sent to your email " "address."), "danger") return redirect_or_next(url_for("forum.index")) except AuthenticationError: flash(_("Wrong username or password."), "danger") return render_template("auth/login.html", form=form, login_recaptcha=login_recaptcha)
def get(self): if current_app.config['REDIS_ENABLED']: online_users = get_online_users() else: # online_users = User.query.filter(User.lastseen >= time_diff()).all() online_users = User.get_active() return render_template('forum/online_users.html', online_users=online_users)
def create_admin_user(username, password, email): """ Creates the administrator user """ admin_group = Group.query.filter_by(admin=True).first() user = User() user.username = username user.password = password user.email = email user.primary_group_id = admin_group.id user.save()
def create_admin_user(username, password, email): """Creates the administrator user. Returns the created admin user. :param username: The username of the user. :param password: The password of the user. :param email: The email address of the user. """ admin_group = Group.query.filter_by(admin=True).first() user = User() user.username = username user.password = password user.email = email user.primary_group_id = admin_group.id user.activated = True user.save() return user
def post(self): form = self.form() if form.validate_on_submit(): try: user = User.authenticate(form.login.data, form.password.data) if not login_user(user, remember=form.remember_me.data): flash( _("In order to use your account you have to " "activate it through the link we have sent to " "your email address."), "danger") return redirect_or_next(url_for("forum.index")) except AuthenticationError: flash(_("Wrong username or password."), "danger") return render_template("auth/login.html", form=form)
def create_user(username, password, email, groupname): """Creates a user. Returns the created user. :param username: The username of the user. :param password: The password of the user. :param email: The email address of the user. :param groupname: The name of the group to which the user should belong to. """ if groupname == "member": group = Group.get_member_group() else: group = Group.query.filter(getattr(Group, groupname) == True).first() user = User.create(username=username, password=password, email=email, primary_group_id=group.id, activated=True) return user
def login(): """ Logs the user in """ if current_user is not None and current_user.is_authenticated: return redirect(url_for("user.profile")) form = LoginForm(request.form) if form.validate_on_submit(): user, authenticated = User.authenticate(form.login.data, form.password.data) if user and authenticated: login_user(user, remember=form.remember_me.data) return redirect(request.args.get("next") or url_for("forum.index")) flash(_("Wrong Username or Password."), "danger") return render_template("auth/login.html", form=form)
def test_has_rank_returns_false_if_user_doesnt_have_rank(self): assert not Rank.has_rank(User())
def save(self): user = User(**self.data) return user.save()
def test_has_custom_rank_returns_false_if_user_rank_isnt_custom(self): assert not Rank.has_custom_rank(User())
def create_test_data(users=5, categories=2, forums=2, topics=1, posts=1): """Creates 5 users, 2 categories and 2 forums in each category. It also creates a new topic topic in each forum with a post. Returns the amount of created users, categories, forums, topics and posts as a dict. :param users: The number of users. :param categories: The number of categories. :param forums: The number of forums which are created in each category. :param topics: The number of topics which are created in each forum. :param posts: The number of posts which are created in each topic. """ create_default_groups() create_default_settings() data_created = {'users': 0, 'categories': 0, 'forums': 0, 'topics': 0, 'posts': 0} # create 5 users for u in range(1, users + 1): username = "******" % u email = "*****@*****.**" % u user = User(username=username, password="******", email=email) user.primary_group_id = u user.activated = True user.save() data_created['users'] += 1 user1 = User.query.filter_by(id=1).first() user2 = User.query.filter_by(id=2).first() # lets send them a few private messages for i in range(1, 3): # TODO pass # create 2 categories for i in range(1, categories + 1): category_title = "Test Category %s" % i category = Category(title=category_title, description="Test Description") category.save() data_created['categories'] += 1 # create 2 forums in each category for j in range(1, forums + 1): if i == 2: j += 2 forum_title = "Test Forum %s %s" % (j, i) forum = Forum(title=forum_title, description="Test Description", category_id=i) forum.save() data_created['forums'] += 1 for t in range(1, topics + 1): # create a topic topic = Topic(title="Test Title %s" % j) post = Post(content="Test Content") topic.save(post=post, user=user1, forum=forum) data_created['topics'] += 1 for p in range(1, posts + 1): # create a second post in the forum post = Post(content="Test Post") post.save(user=user2, topic=topic) data_created['posts'] += 1 return data_created
def test_has_rank_returns_true_if_user_has_rank(self): assert Rank.has_rank( User(user_rank=UserRank(rank=Rank(requirement=1))))
def test_has_custom_rank_returns_true_if_user_rank_is_custom(self): assert Rank.has_custom_rank( User(user_rank=UserRank(rank=Rank(requirement=None))))
def save(self): data = self.data data.pop('submit', None) data.pop('csrf_token', None) user = User(**data) return user.save()
def verify_password(username, password): user, authenticated = User.authenticate(username, password) if user and authenticated: return True return False