Beispiel #1
0
    def post(self):
        """Parse the requests form data to create a payload for
        the permission endpoint and then post each selected object
        to add it to the permission.
        """
        form_data = request.form
        permission = self.parsePermission(form_data)
        try:
            permission_id = permissions.post_metadata(permission)
        except DataRequestException as e:
            # If permission creation failed, retain form input and reload the
            # page.
            self.set_template_args()
            return render_template(self.template,
                                   form_data=form_data,
                                   errors=e.errors,
                                   **self.template_args)

        # Get the list of object uuids from the form
        objects = filter_form_fields('objects-list-', form_data)

        errors = {}
        for object_id in objects:
            try:
                permissions.add_object(permission_id, object_id)
            except DataRequestException:
                # collect errors about permissions that could not be added
                errors[object_id] = [
                    f'Failed to add {self.data_type} to permission.'
                ]

        # display any errors and redirect to the newly created permission.
        self.flash_api_errors(errors)
        flash('Success: Permission created.')
        return redirect(url_for('admin.permission_view', uuid=permission_id))
Beispiel #2
0
 def post(self, uuid):
     form_data = request.form
     objects = filter_form_fields('objects-list-', form_data)
     for obj in objects:
         try:
             permissions.add_object(uuid, obj)
         except DataRequestException as e:
             # Flash errors and redirect to the object addition form.
             self.flash_api_errors(e.errors)
             return redirect(
                 url_for('admin.permission_object_addition', uuid=uuid))
     return redirect(url_for('admin.permission_view', uuid=uuid))
    def zip_object_pairs(cls, form_data):
        """Create a list of object pair dictionaries containing a
        forecast and either an observation or aggregate.
        """
        # Forecasts can be parsed directly
        fx = utils.filter_form_fields('forecast-id-', form_data)

        # observations and aggregates are passed in as truth-id-{index}
        # and truth-type-{index}, so we must match these with the
        # appropriately indexed forecast.
        truth_ids = utils.filter_form_fields('truth-id-', form_data)
        truth_types = utils.filter_form_fields('truth-type-', form_data)

        reference_forecasts = utils.filter_form_fields('reference-forecast-',
                                                       form_data)

        uncertainty_values = utils.filter_form_fields('deadband-value-',
                                                      form_data)
        forecast_types = utils.filter_form_fields('forecast-type-', form_data)
        pairs = [{
            'forecast': f,
            truth_types[i]: truth_ids[i],
            'reference_forecast': reference_forecasts[i],
            'uncertainty': uncertainty_values[i],
            'forecast_type': forecast_types[i]
        } for i, f in enumerate(fx)]
        return pairs
Beispiel #4
0
 def post(self, uuid):
     form_data = request.form
     perms = filter_form_fields('role-permission-', form_data)
     errors = {}
     messages = {}
     for perm in perms:
         try:
             roles.add_permission(uuid, perm)
         except DataRequestException:
             # Collect errors for each failed addition
             errors[perm] = [f'Failed to add perm {perm} to role.']
         else:
             # Collect success messages for each successful addition
             messages[perm] = [f'Successfully added perm {perm} to role.']
     if errors:
         return self.get(uuid,
                         errors=errors,
                         messages=messages,
                         form_data=form_data)
     return redirect(url_for('admin.role_view', uuid=uuid))
Beispiel #5
0
 def post(self, uuid):
     """Parses a list of role ids and attempts to add them to
     a user.
     """
     form_data = request.form
     roles = filter_form_fields('user-role-', form_data)
     messages = {}
     errors = {}
     for role in roles:
         try:
             users.add_role(uuid, role)
         except DataRequestException:
             errors[role] = [f'Failed to add role {role} to user.']
         else:
             messages[role] = f'Successfully added role {role} to user.'
     if errors:
         return self.get(uuid,
                         errors=errors,
                         messages=messages,
                         form_data=form_data)
     return redirect(url_for('admin.user_view', uuid=uuid))
Beispiel #6
0
 def parse_observations(self, form_data):
     observation_ids = filter_form_fields('observation-', form_data)
     return observation_ids