Esempio n. 1
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)
    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)
Esempio n. 3
0
    def index(self):
        """
        A view for mass-assigning resources to categories.
        """
        return_url = get_redirect_target() or \
            self.get_url('category-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 categories) 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 categories
            available_categories = Category.query. \
                order_by(Category.name.asc()).all()

            # Group them using the remedyblueprint method
            grouped_categories = group_active_categories(available_categories)

            # Return the view for assigning categories
            return self.render('admin/resource_assign_categories.html',
                               ids=request.args.getlist('ids'),
                               resources=target_resources,
                               grouped_categories=grouped_categories,
                               return_url=return_url)
        else:
            # Get the selected categories - use request.form,
            # not request.args
            target_categories = Category.query.filter(
                Category.id.in_(request.form.getlist('categories'))).all()

            if len(target_categories) > 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 categories
                        for category in target_categories:

                            # Make sure we're not double-adding
                            if category not in resource.categories:
                                resource.categories.append(category)
                                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 category must be selected.', 'error')

            return redirect(return_url)
Esempio n. 4
0
    def index(self):
        """
        A view for mass-assigning resources to categories.
        """
        return_url = get_redirect_target() or \
            self.get_url('category-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 categories) 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 categories
            available_categories = Category.query. \
                order_by(Category.name.asc()).all()

            # Group them using the remedyblueprint method
            grouped_categories = group_active_categories(
                available_categories)

            # Return the view for assigning categories
            return self.render(
                'admin/resource_assign_categories.html',
                ids=request.args.getlist('ids'),
                resources=target_resources,
                grouped_categories=grouped_categories,
                return_url=return_url)
        else:
            # Get the selected categories - use request.form,
            # not request.args
            target_categories = Category.query.filter(
                Category.id.in_(request.form.getlist('categories'))).all()

            if len(target_categories) > 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 categories
                        for category in target_categories:

                            # Make sure we're not double-adding
                            if category not in resource.categories:
                                resource.categories.append(category)
                                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 category must be selected.', 'error')

            return redirect(return_url)