예제 #1
0
    def _process(self):
        form = SearchForm(obj=FormDefaults(exact=True))
        form_data = form.data
        search_results = None
        num_of_users = User.query.count()
        num_deleted_users = User.find(is_deleted=True).count()

        if form.validate_on_submit():
            search_results = []
            exact = form_data.pop('exact')
            include_deleted = form_data.pop('include_deleted')
            include_pending = form_data.pop('include_pending')
            external = form_data.pop('external')
            form_data = {
                k: v
                for (k, v) in form_data.iteritems() if v and v.strip()
            }
            matches = search_users(exact=exact,
                                   include_deleted=include_deleted,
                                   include_pending=include_pending,
                                   external=external,
                                   allow_system_user=True,
                                   **form_data)
            for entry in matches:
                if isinstance(entry, User):
                    search_results.append(
                        UserEntry(profile_url=url_for('.user_profile', entry),
                                  user=entry,
                                  **{
                                      k: getattr(entry, k)
                                      for k in IDENTITY_ATTRIBUTES
                                  }))
                else:
                    search_results.append(
                        UserEntry(profile_url=None,
                                  user=None,
                                  full_name="{first_name} {last_name}".format(
                                      **entry.data.to_dict()),
                                  **{
                                      k: entry.data.get(k)
                                      for k in (IDENTITY_ATTRIBUTES -
                                                {'full_name'})
                                  }))
            search_results.sort(key=attrgetter('first_name', 'last_name'))

        num_reg_requests = RegistrationRequest.query.count()
        return WPUsersAdmin.render_template(
            'users_admin.html',
            'users',
            form=form,
            search_results=search_results,
            num_of_users=num_of_users,
            num_deleted_users=num_deleted_users,
            num_reg_requests=num_reg_requests)
예제 #2
0
def create(grant_admin):
    """Creates a new user"""
    user_type = 'user' if not grant_admin else 'admin'
    while True:
        email = prompt_email()
        if email is None:
            return
        email = email.lower()
        if not User.find(User.all_emails.contains(email), ~User.is_deleted,
                         ~User.is_pending).count():
            break
        print(cformat('%{red}Email already exists'))
    first_name = click.prompt("First name").strip()
    last_name = click.prompt("Last name").strip()
    affiliation = click.prompt("Affiliation", '').strip()
    print()
    while True:
        username = click.prompt("Enter username").lower().strip()
        if not Identity.find(provider='fossir', identifier=username).count():
            break
        print(cformat('%{red}Username already exists'))
    password = prompt_pass()
    if password is None:
        return

    identity = Identity(provider='fossir',
                        identifier=username,
                        password=password)
    user = create_user(
        email, {
            'first_name': to_unicode(first_name),
            'last_name': to_unicode(last_name),
            'affiliation': to_unicode(affiliation)
        }, identity)
    user.is_admin = grant_admin
    _print_user_info(user)

    if click.confirm(cformat("%{yellow}Create the new {}?").format(user_type),
                     default=True):
        db.session.add(user)
        db.session.commit()
        print(
            cformat("%{green}New {} created successfully with ID: %{green!}{}"
                    ).format(user_type, user.id))
예제 #3
0
 def _count_users(self):
     return User.find(is_deleted=False, is_pending=False).count()
예제 #4
0
def get_admin_emails():
    """Get the email addresses of all fossir admins"""
    return {u.email for u in User.find(is_admin=True, is_deleted=False)}