Exemple #1
0
    def Add(self, **unused_args):
        """Add a task to the schedule."""

        if not acl.UserIsScheduleAdmin():
            self.error(404)
            return

        # new request or type changed, so show a blank form
        if (not self.request.POST.has_key('selectedType')
                or self.request.POST['type'] !=
                self.request.POST['selectedType']):

            # assume new request, update type if otherwise
            try:
                if self.request.POST.has_key('type'):
                    thetype = self.request.POST['type']
                    measurement = MeasurementType.GetMeasurement(thetype)
                else:
                    measurement = MeasurementType.GetDefaultMeasurement()
            except RuntimeError, error:
                # occurs if someone specifies an invalid measurement type
                logging.warning('Type in POST is invalid: %s', error)
                self.error(501)
                return

            # dynamically creates a form based on the specified fields
            add_to_schedule_form = type(
                'AddToScheduleForm', (forms.BaseForm, ),
                {'base_fields': self._BuildFields(measurement)})()
Exemple #2
0
    def _BuildFields(self,
                     mymeasurement=MeasurementType.GetDefaultMeasurement()):
        """Builds the ordered set of fields to display in the form for the 
    specified measurement type.
       
    Args:
      measurement: A MeasurementType object.
      
    Returns:
      A sorted dict of field name to form field.
    """
        fields = SortedDict()
        fields['type'] = forms.ChoiceField(
            measurement.MEASUREMENT_TYPES,
            initial=mymeasurement.kind,
            widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

        for field, text in mymeasurement.field_to_description.iteritems():
            fields[field] = forms.CharField(required=False, label=text)

        fields['count'] = forms.IntegerField(required=False,
                                             initial=-1,
                                             min_value=-1,
                                             max_value=1000)
        fields['interval'] = forms.IntegerField(required=True,
                                                label='Interval (sec)',
                                                min_value=1,
                                                initial=600)
        fields['tag'] = forms.CharField(required=False)
        fields['filter'] = forms.CharField(required=False)
        fields['priority'] = forms.IntegerField(
            required=False, label='Priority (larger is higher)', min_value=1)
        fields['selectedType'] = forms.CharField(
            initial=mymeasurement.kind, widget=forms.widgets.HiddenInput())
        return fields
Exemple #3
0
            except RuntimeError, error:
                # occurs if someone specifies an invalid measurement type
                logging.warning('Type in POST is invalid: %s', error)
                self.error(501)
                return

            # dynamically creates a form based on the specified fields
            add_to_schedule_form = type(
                'AddToScheduleForm', (forms.BaseForm, ),
                {'base_fields': self._BuildFields(measurement)})()

        else:
            # data was submitted for a new measurement schedule item
            try:
                thetype = self.request.POST['type']
                measurement = MeasurementType.GetMeasurement(thetype)
            except:
                # occurs if someone specifies an invalid measurement type
                logging.warning('Type in POST is invalid: %s', error)
                self.error(501)
                return

            # build completed form dynamically from POST and fields
            add_to_schedule_form = type(
                'AddToScheduleForm', (forms.BaseForm, ),
                {'base_fields': self._BuildFields(measurement)})(
                    self.request.POST)

            add_to_schedule_form.full_clean()
            if add_to_schedule_form.is_valid():