Example #1
0
    def clean(self):
        """
        1. Strip spaces from character fields.
        2. Location key processing: keep key n only if 1 through n-1
        are also specified.
        3. Call the parent's clean() to run the default behavior.
        4. Clean the annotation-area fields.
        5. Default return behavior of clean() is to return self.cleaned_data.
        """
        data = FormHelper.stripSpacesFromFields(
            self.cleaned_data, self.fields)

        if 'key1' not in data or data['key1'] == u'':
            data['key2'] = u''
        if data['key2'] == u'':
            data['key3'] = u''
        if data['key3'] == u'':
            data['key4'] = u''
        if data['key4'] == u'':
            data['key5'] = u''

        # Check if a location key is used to denote date, year or similar.
        date_strings = {'date', 'year', 'time', 'month', 'day'}
        if ('key1' in data and data['key1'].lower() in date_strings) or ('key2' in data and data['key2'].lower() in date_strings) or ('key3' in data and data['key3'].lower() in date_strings) or ('key4' in data and data['key4'].lower() in date_strings) or ('key5' in data and data['key5'].lower() in date_strings):
            raise ValidationError("The image acquisition date is a default metadata field. Do not use locationkeys to encode temporal information.")

        self.cleaned_data = data

        return super(LocationKeyForm, self).clean()
Example #2
0
    def clean(self):
        """
        1. Strip spaces from character fields.
        2. Add an error if the specified name or code matches that of an existing label.
        3. Call the parent's clean() to finish up with the default behavior.
        """
        data = FormHelper.stripSpacesFromFields(
            self.cleaned_data, self.fields)

        if data.has_key('name'):
            labelsOfSameName = Label.objects.filter(name__iexact=data['name'])
            if len(labelsOfSameName) > 0:
                # mark_safe(), along with the |safe template filter, allows HTML in the message.
                msg = mark_safe('There is already a label with the name %s: <a href="%s" target="_blank">%s</a>' % (
                    data['name'],
                    reverse('label_main', args=[labelsOfSameName[0].id]),
                    labelsOfSameName[0].name,
                ))
                self._errors['name'] = self.error_class([msg])

        if data.has_key('code'):
            labelsOfSameCode = Label.objects.filter(code__iexact=data['code'])
            if len(labelsOfSameCode) > 0:
                msg = mark_safe('There is already a label with the short code %s: <a href="%s" target="_blank">%s</a>' % (
                    data['code'],
                    reverse('label_main', args=[labelsOfSameCode[0].id]),
                    labelsOfSameCode[0].name,
                ))
                self._errors['code'] = self.error_class([msg])

        self.cleaned_data = data
        return super(NewLabelForm, self).clean()
Example #3
0
    def clean(self):
        """
        Strip spaces from the fields.
        """
        data = FormHelper.stripSpacesFromFields(
            self.cleaned_data, self.fields)

        self.cleaned_data = data

        return super(LocationKeyEditForm, self).clean()
Example #4
0
    def clean(self):
        """
        1. Strip spaces from character fields.
        2. Handle the location values.
        3. Call the parent's clean() to finish up with the default behavior.
        """
        data = FormHelper.stripSpacesFromFields(
            self.cleaned_data, self.fields)

        image = Image.objects.get(metadata=self.instance)
        source = image.source

        # Right now, the valueN field's value is the integer id
        # of a ValueN object. We want the ValueN object.
        for key, valueField, valueClass in [
            (source.key1, 'value1', Value1),
            (source.key2, 'value2', Value2),
            (source.key3, 'value3', Value3),
            (source.key4, 'value4', Value4),
            (source.key5, 'value5', Value5) ]:

            # Make sure the form actually has this valueN.
            if data.has_key(valueField):

                # "Other" was chosen.
                if data[valueField] == 'Other':
                    otherValue = data[valueField + '_other']
                    if not otherValue:
                        # Error
                        msg = u"Since you selected Other, you must use this text box to specify the %s." % key
                        self._errors[valueField + '_other'] = self.error_class([msg])

                        # TODO: Make this not a hack.  This sets the valueField to be some arbitrary non-blank
                        # valueN object, so (1) we won't get an error on clean() about 'Other'
                        # not being a valueClass object, and (2) we won't get a
                        # "field cannot be blank" error on the dropdown.
                        # One possible consequence of this hack is that it'll crash if there are no valueClass objects of that value number on the site yet. (e.g. no value5s)
                        data[valueField] = valueClass.objects.all()[0]
                    else:
                        # Add new value to database, or get it if it already exists
                        # (the latter case would be the user not noticing it was already in the choices).
                        # **NOTE: Make sure the view function using this form rolls back
                        # any object creation if the form has errors.
                        newValueObj, created = valueClass.objects.get_or_create(name=otherValue, source=source)
                        data[valueField] = newValueObj

                # Set to ValueN object of the given id.
                else:
                    data[valueField] = valueClass.objects.get(pk=data[valueField])

            
        self.cleaned_data = data
        return super(ImageDetailForm, self).clean()
Example #5
0
    def clean(self):
        """
        1. Strip spaces from character fields.
        2. Call the parent's clean() to run the default behavior.
        3. Default return behavior of clean() is to return self.cleaned_data.
        """
        data = FormHelper.stripSpacesFromFields(
            self.cleaned_data, self.fields)

        self.cleaned_data = data

        return super(ImageSourceForm, self).clean()
Example #6
0
    def clean(self):
        """
        1. Strip spaces from character fields.
        2. Call the parent's clean() to finish up with the default behavior.
        """

        data = FormHelper.stripSpacesFromFields(
            self.cleaned_data, self.fields)

        self.cleaned_data = data

        return super(FeedbackForm, self).clean()
Example #7
0
    def clean(self):
        """
        1. Strip spaces from character fields.
        2. Location key processing: keep key n only if 1 through n-1
        are also specified.
        3. Call the parent's clean() to run the default behavior.
        4. Clean the annotation-area fields.
        5. Default return behavior of clean() is to return self.cleaned_data.
        """
        data = FormHelper.stripSpacesFromFields(
            self.cleaned_data, self.fields)

        if data['key1'] == u'':
            data['key2'] = u''
        if data['key2'] == u'':
            data['key3'] = u''
        if data['key3'] == u'':
            data['key4'] = u''
        if data['key4'] == u'':
            data['key5'] = u''

        self.cleaned_data = data

        return super(ImageSourceForm, self).clean()