class SubjectsForm(forms.Form): value = forms.CharField( max_length=500, label='', widget=forms.TextInput(attrs={'placeholder': 'Keywords'}), help_text='Enter each keyword separated by a comma.') def __init__(self, allow_edit=True, res_short_id=None, element_id=None, *args, **kwargs): super(SubjectsForm, self).__init__(*args, **kwargs) self.helper = SubjectsFormHelper(allow_edit, res_short_id, element_id, element_name='subject') self.number = 0 self.delete_modal_form = None if res_short_id: self.action = "/hsapi/_internal/%s/subject/add-metadata/" % res_short_id else: self.action = "" if not allow_edit: for field in self.fields.values(): field.widget.attrs['readonly'] = True field.widget.attrs['style'] = "background-color:white;"
class CoverageSpatialForm(forms.Form): TYPE_CHOICES = (('box', 'Box'), ('point', 'Point')) type = forms.ChoiceField( choices=TYPE_CHOICES, widget=forms.RadioSelect(renderer=HorizontalRadioRenderer), label='') name = forms.CharField(max_length=200, required=False, label='Place/Area Name') projection = forms.CharField( max_length=100, required=False, label='Coordinate System/Geographic Projection') east = forms.DecimalField(label='Longitude', widget=forms.TextInput()) north = forms.DecimalField(label='Latitude', widget=forms.TextInput()) units = forms.CharField(max_length=50, label='Coordinate Units') northlimit = forms.DecimalField(label='North Latitude', widget=forms.TextInput()) eastlimit = forms.DecimalField(label='East Longitude', widget=forms.TextInput()) southlimit = forms.DecimalField(label='South Latitude', widget=forms.TextInput()) westlimit = forms.DecimalField(label='West Longitude', widget=forms.TextInput()) def __init__(self, allow_edit=True, res_short_id=None, element_id=None, *args, **kwargs): file_type = kwargs.pop('file_type', False) super(CoverageSpatialForm, self).__init__(*args, **kwargs) self.helper = CoverageSpatialFormHelper( allow_edit, res_short_id, element_id, element_name='Spatial Coverage', file_type=file_type) self.number = 0 self.delete_modal_form = None if self.errors: self.errors.clear() if res_short_id: self.action = "/hsapi/_internal/%s/coverage/add-metadata/" % res_short_id else: self.action = "" if len(self.initial) > 0: self.initial['projection'] = 'WGS 84 EPSG:4326' self.initial['units'] = 'Decimal degrees' else: self.fields['type'].widget.attrs['checked'] = 'checked' self.fields['projection'].widget.attrs[ 'value'] = 'WGS 84 EPSG:4326' self.fields['units'].widget.attrs['value'] = 'Decimal degrees' if not allow_edit: for field in self.fields.values(): field.widget.attrs['readonly'] = True else: self.fields['projection'].widget.attrs['readonly'] = True self.fields['units'].widget.attrs['readonly'] = True def clean(self): # modify the form's cleaned_data dictionary super(CoverageSpatialForm, self).clean() temp_cleaned_data = copy.deepcopy(self.cleaned_data) spatial_coverage_type = temp_cleaned_data['type'] is_form_errors = False if self.errors: self.errors.clear() if spatial_coverage_type == 'point': north = temp_cleaned_data.get('north', None) east = temp_cleaned_data.get('east', None) if not north: self._errors['north'] = ["Data for north is missing"] is_form_errors = True del self.cleaned_data['north'] if not east: self._errors['east'] = ["Data for east is missing"] is_form_errors = True del self.cleaned_data['east'] if is_form_errors: return self.cleaned_data if 'northlimit' in temp_cleaned_data: del temp_cleaned_data['northlimit'] if 'eastlimit' in self.cleaned_data: del temp_cleaned_data['eastlimit'] if 'southlimit' in temp_cleaned_data: del temp_cleaned_data['southlimit'] if 'westlimit' in temp_cleaned_data: del temp_cleaned_data['westlimit'] if 'uplimit' in temp_cleaned_data: del temp_cleaned_data['uplimit'] if 'downlimit' in temp_cleaned_data: del temp_cleaned_data['downlimit'] temp_cleaned_data['north'] = str(temp_cleaned_data['north']) temp_cleaned_data['east'] = str(temp_cleaned_data['east']) else: # box type coverage if 'north' in temp_cleaned_data: del temp_cleaned_data['north'] if 'east' in temp_cleaned_data: del temp_cleaned_data['east'] if 'elevation' in temp_cleaned_data: del temp_cleaned_data['elevation'] for limit in ('northlimit', 'eastlimit', 'southlimit', 'westlimit'): limit_data = temp_cleaned_data.get(limit, None) if not limit_data: self._errors[limit] = ["Data for %s is missing" % limit] is_form_errors = True del self.cleaned_data[limit] if is_form_errors: return self.cleaned_data temp_cleaned_data['northlimit'] = str( temp_cleaned_data['northlimit']) temp_cleaned_data['eastlimit'] = str( temp_cleaned_data['eastlimit']) temp_cleaned_data['southlimit'] = str( temp_cleaned_data['southlimit']) temp_cleaned_data['westlimit'] = str( temp_cleaned_data['westlimit']) del temp_cleaned_data['type'] if 'projection' in temp_cleaned_data: if len(temp_cleaned_data['projection']) == 0: del temp_cleaned_data['projection'] if 'name' in temp_cleaned_data: if len(temp_cleaned_data['name']) == 0: del temp_cleaned_data['name'] self.cleaned_data['value'] = copy.deepcopy(temp_cleaned_data) if 'northlimit' in self.cleaned_data: del self.cleaned_data['northlimit'] if 'eastlimit' in self.cleaned_data: del self.cleaned_data['eastlimit'] if 'southlimit' in self.cleaned_data: del self.cleaned_data['southlimit'] if 'westlimit' in self.cleaned_data: del self.cleaned_data['westlimit'] if 'uplimit' in self.cleaned_data: del self.cleaned_data['uplimit'] if 'downlimit' in self.cleaned_data: del self.cleaned_data['downlimit'] if 'north' in self.cleaned_data: del self.cleaned_data['north'] if 'east' in self.cleaned_data: del self.cleaned_data['east'] if 'elevation' in self.cleaned_data: del self.cleaned_data['elevation'] if 'name' in self.cleaned_data: del self.cleaned_data['name'] if 'units' in self.cleaned_data: del self.cleaned_data['units'] if 'zunits' in self.cleaned_data: del self.cleaned_data['zunits'] if 'projection' in self.cleaned_data: del self.cleaned_data['projection'] return self.cleaned_data