Ejemplo n.º 1
0
def sign_up():
    """
    Handles user signup.

    Associated template: create-account.html
    Associated form: SignUpForm
    """
    # Kick the current user back to the index
    # if they're already logged in
    if current_user.is_authenticated:
        return index_redirect()

    # Get active populations and set up the form
    population_choices = active_populations()
    form = SignUpForm(request.form,
                      group_active_populations(population_choices))

    if request.method == 'GET':
        return render_template('create-account.html', form=form)
    else:

        if form.validate_on_submit():
            # Create the user.
            u = User(form.username.data, form.email.data, form.password.data)

            # If we have a display name, use that - otherwise,
            # default to the username.
            if form.display_name.data and not form.display_name.data.isspace():
                u.display_name = form.display_name.data
            else:
                u.display_name = form.username.data

            # Set up populations
            pop_ids = set(form.populations.data)

            for new_pop_id in pop_ids:
                # Find it in our population choices and add it in
                new_pop = next(
                    (p for p in population_choices if p.id == new_pop_id),
                    None)

                if new_pop:
                    u.populations.append(new_pop)

            # Generate an activation code
            u.email_code = str(uuid4())
            u.email_activated = False

            # Save the user and send a confirmation email.
            db.session.add(u)
            db.session.commit()

            send_confirm_account(u)

            # Display the success page
            return render_template('create-account-success.html')

        else:
            flash_errors(form)
            return render_template('create-account.html', form=form), 400
Ejemplo n.º 2
0
    def index(self):
        """
        A view for performing various acts of data maintenance.
        """
        return_url = get_redirect_target() or \
            self.get_url('maintenanceview.index')

        if request.method == 'GET':
            # Get all categories and group them
            # using the remedyblueprint method
            grouped_categories = group_active_categories(
                Category.query.all())

            # Get all populations and group them
            # using the remedyblueprint method
            grouped_populations = group_active_populations(
                Population.query.all())

            return self.render(
                'admin/maintenance.html',
                grouped_populations=grouped_populations,
                grouped_categories=grouped_categories,
                return_url=return_url)
        else:
            query = Resource.query

            # See if there's categories/populations to filter on
            categories = request.form.getlist('categories')
            populations = request.form.getlist('populations')

            if len(categories) > 0:
                query = query.filter(Resource.categories.any(
                    Category.id.in_(categories)))

            if len(populations) > 0:
                query = query.filter(Resource.populations.any(
                    Population.id.in_(populations)))

            # Now apply our filtering.
            target_resources = query.all()

            if len(target_resources) > 0:
                # Touch the last-updated date.
                for resource in target_resources:
                    resource.last_updated = datetime.utcnow()

                # Save our changes.
                self.session.commit()

                # Indicate how many we changed.
                flash(
                    'Updated ' + str(len(target_resources)) + ' resource(s).',
                    'success')
            else:
                flash('No resources matched the provided query.', 'warning')

            return redirect(return_url)
Ejemplo n.º 3
0
def sign_up():
    """
    Handles user signup.

    Associated template: create-account.html
    Associated form: SignUpForm
    """
    # Kick the current user back to the index
    # if they're already logged in
    if current_user.is_authenticated:
        return index_redirect()

    # Get active populations and set up the form
    population_choices = active_populations()
    form = SignUpForm(request.form, group_active_populations(population_choices))

    if request.method == 'GET':
        return render_template('create-account.html', form=form)
    else:

        if form.validate_on_submit():
            # Create the user.
            u = User(form.username.data, form.email.data, form.password.data)

            # If we have a display name, use that - otherwise,
            # default to the username.
            if form.display_name.data and not form.display_name.data.isspace():
                u.display_name = form.display_name.data
            else:
                u.display_name = form.username.data

            # Set up populations
            pop_ids = set(form.populations.data)

            for new_pop_id in pop_ids:
                # Find it in our population choices and add it in
                new_pop = next((p for p in population_choices if p.id == new_pop_id), None)
                if new_pop:
                    u.populations.append(new_pop)

            # Generate an activation code
            u.email_code = str(uuid4())
            u.email_activated = False

            # Save the user and send a confirmation email.
            db.session.add(u)
            db.session.commit()

            send_confirm_account(u)

            # Display the success page
            return render_template('create-account-success.html')

        else:
            flash_errors(form)
            return render_template('create-account.html', form=form), 400
Ejemplo n.º 4
0
    def index(self):
        """
        A view for performing various acts of data maintenance.
        """
        return_url = get_redirect_target() or \
            self.get_url('maintenanceview.index')

        if request.method == 'GET':
            # Get all categories and group them
            # using the remedyblueprint method
            grouped_categories = group_active_categories(Category.query.all())

            # Get all populations and group them
            # using the remedyblueprint method
            grouped_populations = group_active_populations(
                Population.query.all())

            return self.render('admin/maintenance.html',
                               grouped_populations=grouped_populations,
                               grouped_categories=grouped_categories,
                               return_url=return_url)
        else:
            query = Resource.query

            # See if there's categories/populations to filter on
            categories = request.form.getlist('categories')
            populations = request.form.getlist('populations')

            if len(categories) > 0:
                query = query.filter(
                    Resource.categories.any(Category.id.in_(categories)))

            if len(populations) > 0:
                query = query.filter(
                    Resource.populations.any(Population.id.in_(populations)))

            # Now apply our filtering.
            target_resources = query.all()

            if len(target_resources) > 0:
                # Touch the last-updated date.
                for resource in target_resources:
                    resource.last_updated = datetime.utcnow()

                # Save our changes.
                self.session.commit()

                # Indicate how many we changed.
                flash(
                    'Updated ' + str(len(target_resources)) + ' resource(s).',
                    'success')
            else:
                flash('No resources matched the provided query.', 'warning')

            return redirect(return_url)
Ejemplo n.º 5
0
    def index(self):
        """
        A view for mass-assigning resources to populations.
        """
        return_url = get_redirect_target() or \
            self.get_url('population-resourceview.index_view')

        # Load all resources by the set of IDs
        target_resources = Resource.query. \
            filter(Resource.id.in_(request.args.getlist('ids')))

        target_resources = target_resources. \
            order_by(Resource.name.asc()).all()

        # Make sure we have some, and go back to the resources
        # view (for assigning populations) if we don't.
        if len(target_resources) == 0:
            flash('At least one resource must be selected.', 'error')
            return redirect(url_for(return_url))

        if request.method == 'GET':
            # Get all populations
            available_populations = Population.query. \
                order_by(Population.name.asc()).all()

            # Group them using the remedyblueprint method
            grouped_populations = group_active_populations(
                available_populations)

            # Return the view for assigning populations
            return self.render('admin/resource_assign_populations.html',
                               ids=request.args.getlist('ids'),
                               resources=target_resources,
                               grouped_populations=grouped_populations,
                               return_url=return_url)
        else:
            # Get the selected populations - use request.form,
            # not request.args
            target_populations = Population.query.filter(
                Population.id.in_(request.form.getlist('populations'))).all()

            if len(target_populations) > 0:
                # Build a list of all the results
                results = []

                for resource in target_resources:
                    # Build a helpful message string to use for resources.
                    resource_str = 'resource #' + str(resource.id) + \
                        ' (' + resource.name + ')'

                    try:
                        # Assign all populations
                        for population in target_populations:

                            # Make sure we're not double-adding
                            if population not in resource.populations:
                                resource.populations.append(population)
                                resource.last_updated = datetime.utcnow()

                    except Exception as ex:
                        results.append('Error updating ' + resource_str +
                                       ': ' + str(ex))
                    else:
                        results.append('Updated ' + resource_str + '.')

                # Save our changes.
                self.session.commit()

                # Flash the results of everything
                flash("\n".join(msg for msg in results))
            else:
                flash('At least one population must be selected.', 'error')

            return redirect(return_url)
Ejemplo n.º 6
0
    def index(self):
        """
        A view for mass-assigning resources to populations.
        """
        return_url = get_redirect_target() or \
            self.get_url('population-resourceview.index_view')

        # Load all resources by the set of IDs
        target_resources = Resource.query. \
            filter(Resource.id.in_(request.args.getlist('ids')))

        target_resources = target_resources. \
            order_by(Resource.name.asc()).all()

        # Make sure we have some, and go back to the resources
        # view (for assigning populations) if we don't.
        if len(target_resources) == 0:
            flash('At least one resource must be selected.', 'error')
            return redirect(url_for(return_url))

        if request.method == 'GET':
            # Get all populations
            available_populations = Population.query. \
                order_by(Population.name.asc()).all()

            # Group them using the remedyblueprint method
            grouped_populations = group_active_populations(
                available_populations)

            # Return the view for assigning populations
            return self.render(
                'admin/resource_assign_populations.html',
                ids=request.args.getlist('ids'),
                resources=target_resources,
                grouped_populations=grouped_populations,
                return_url=return_url)
        else:
            # Get the selected populations - use request.form,
            # not request.args
            target_populations = Population.query.filter(
                Population.id.in_(request.form.getlist('populations'))).all()

            if len(target_populations) > 0:
                # Build a list of all the results
                results = []

                for resource in target_resources:
                    # Build a helpful message string to use for resources.
                    resource_str = 'resource #' + str(resource.id) + \
                        ' (' + resource.name + ')'

                    try:
                        # Assign all populations
                        for population in target_populations:

                            # Make sure we're not double-adding
                            if population not in resource.populations:
                                resource.populations.append(population)
                                resource.last_updated = datetime.utcnow()

                    except Exception as ex:
                        results.append(
                            'Error updating ' + resource_str + ': ' + str(ex))
                    else:
                        results.append(
                            'Updated ' + resource_str + '.')

                # Save our changes.
                self.session.commit()

                # Flash the results of everything
                flash("\n".join(msg for msg in results))
            else:
                flash('At least one population must be selected.', 'error')

            return redirect(return_url)