class LayerGroupLayersForm(forms.ModelForm): """ A form for LayerGroupLayers model """ #publish = GroupedModelChoiceField('workspace',queryset=Publish.objects.filter(status=ResourceStatus.Enabled.name,completed__gt=0),required=False,choice_family="publish",choice_name="publish_choices") layer = GroupedModelChoiceField( 'server', queryset=WmsLayer.objects.filter( status__in=ResourceStatus.interested_layer_status_names), required=True, choice_family="interested_wmslayer", choice_name="interested_wmslayer_choices", widget=BorgSelect()) group = GroupedModelChoiceField('workspace', queryset=LayerGroup.objects.all(), required=True, choice_family="layergroup", choice_name="layergroup_choices", widget=BorgSelect()) #sub_group = GroupedModelChoiceField('workspace',queryset=LayerGroup.objects.all(),choice_family="layergroup",choice_name="layergroup_choices",required=False) def __init__(self, *args, **kwargs): super(LayerGroupLayersForm, self).__init__(*args, **kwargs) if 'instance' in kwargs and kwargs['instance'] and kwargs[ 'instance'].pk: self.fields['group'].widget.attrs['readonly'] = True self.fields['layer'].widget.attrs['readonly'] = True class Meta: model = LayerGroupLayers fields = "__all__"
class InputForm(BorgModelForm): """ A form for Input Model """ INSERT_FIELDS = 100 CHANGE_DATA_SOURCE = 101 CHANGE_FOREIGN_TABLE = 102 foreign_table = CachedModelChoiceField(queryset=ForeignTable.objects.all(),label_func=lambda table:table.name,required=False,choice_family="foreigntable",choice_name="foreigntable_options", widget=BorgSelect(attrs={"onChange":"$('#input_form').append(\"<input type='hidden' name='_change_foreign_table' value=''>\"); $('#input_form').submit()"})) def __init__(self, *args, **kwargs): super(InputForm, self).__init__(*args, **kwargs) #remote the "+" icon from html page because this will trigger onchange event and cause recusive submit html form to server self.fields['data_source'].widget = self.fields['data_source'].widget.widget if 'instance' in kwargs and kwargs['instance'] and kwargs['instance'].pk: self.fields['name'].widget.attrs['readonly'] = True self.fields['data_source'].widget.attrs['readonly'] = True self.fields['foreign_table'].widget.attrs['readonly'] = True def get_mode(self,data): if data and "_insert_fields" in data: return (InputForm.INSERT_FIELDS,"insert_fields",True,False,None) elif data and "_change_data_source" in data: return (InputForm.CHANGE_DATA_SOURCE,"change_data_source",True,False,('name','data_source')) elif data and "_change_foreign_table" in data: return (InputForm.CHANGE_DATA_SOURCE,"change_foreign_table",True,False,('name','data_source','foreign_table')) return super(InputForm,self).get_mode(data) def insert_fields(self): self.data['source'] = self.instance.source self.fields['foreign_table'].queryset = ForeignTable.objects.filter(server=self.instance.data_source) self.fields['foreign_table'].choice_name = "foreigntable_options_{}".format(self.instance.data_source.name) self.fields['foreign_table'].widget.choices = self.fields['foreign_table'].choices def change_data_source(self): if not hasattr(self.instance,"data_source"): self.data['source'] = "" elif self.instance.data_source.type == DatasourceType.FILE_SYSTEM: self.data['source'] = self.instance.data_source.vrt elif self.instance.data_source.type == DatasourceType.DATABASE: self.fields['foreign_table'].queryset = ForeignTable.objects.filter(server=self.instance.data_source) self.fields['foreign_table'].choice_name = "foreigntable_options_{}".format(self.instance.data_source.name) self.fields['foreign_table'].widget.choices = self.fields['foreign_table'].choices self.data['source'] = "" else: self.data['source'] = "" def change_foreign_table(self): self.data['source'] = str(Template(self.instance.data_source.vrt).render(Context({'self':self.instance,'db':Input.DB_TEMPLATE_CONTEXT}))) self.fields['foreign_table'].queryset = ForeignTable.objects.filter(server=self.instance.data_source) self.fields['foreign_table'].choice_name = "foreigntable_options_{}".format(self.instance.data_source.name) self.fields['foreign_table'].widget.choices = self.fields['foreign_table'].choices class Meta: model = Input fields = "__all__" widgets = { 'data_source': BorgSelect(attrs={"onChange":"$('#input_form').append(\"<input type='hidden' name='_change_data_source' value=''>\"); $('#input_form').submit();"}), }
class Meta: model = DataSource fields = "__all__" widgets = { 'type': BorgSelect(attrs={"onChange":"django.jQuery('#datasource_form').append(\"<input type='hidden' name='_change_type' value=''>\");django.jQuery('#datasource_form').submit()"}), 'description': forms.TextInput(attrs={"style":"width:95%"}) }
class Meta: model = Input fields = "__all__" widgets = { 'data_source': BorgSelect( attrs={ "onChange": "$('#input_form').append(\"<input type='hidden' name='_change_data_source' value=''>\"); $('#input_form').submit();" }), }
class LayerGroupForm(GeoserverSettingForm, forms.ModelForm): """ A form for LayerGroup Model """ create_cache_layer = forms.BooleanField(required=False, label="create_cache_layer", initial={"enabled": True}) create_cache_layer.setting_type = "geoserver_setting" server_cache_expire = forms.IntegerField( label="server_cache_expire", min_value=0, required=False, initial=0, help_text= "Expire server cache after n seconds (set to 0 to use source setting)") server_cache_expire.setting_type = "geoserver_setting" client_cache_expire = forms.IntegerField( label="client_cache_expire", min_value=0, required=False, initial=0, help_text= "Expire client cache after n seconds (set to 0 to use source setting)") client_cache_expire.setting_type = "geoserver_setting" workspace = GroupedModelChoiceField('publish_channel', queryset=Workspace.objects.all(), required=True, choice_family="workspace", choice_name="workspace_choices", widget=BorgSelect()) def __init__(self, *args, **kwargs): kwargs['initial'] = kwargs.get('initial', {}) self.get_setting_from_model(*args, **kwargs) super(LayerGroupForm, self).__init__(*args, **kwargs) if 'instance' in kwargs and kwargs['instance'] and kwargs[ 'instance'].pk: self.fields['name'].widget.attrs['readonly'] = True self.fields['workspace'].widget.attrs['readonly'] = True def _post_clean(self): if self.errors: return self.set_setting_to_model() super(LayerGroupForm, self)._post_clean() class Meta: model = LayerGroup fields = "__all__"
class WmsServerForm(forms.ModelForm, GeoserverSettingForm): """ A form for WmsServer model """ max_connections = forms.IntegerField(label="Max concurrent connections", initial=6, min_value=1, max_value=128) max_connections.setting_type = "geoserver_setting" max_connections.key = "max_connections" connect_timeout = forms.IntegerField(label="Connect timeout in seconds", initial=30, min_value=1, max_value=3600) connect_timeout.setting_type = "geoserver_setting" connect_timeout.key = "connect_timeout" read_timeout = forms.IntegerField(label="Read timeout in seconds", initial=60, min_value=1, max_value=3600) read_timeout.setting_type = "geoserver_setting" read_timeout.key = "read_timeout" workspace = GroupedModelChoiceField('publish_channel', queryset=Workspace.objects.all(), required=True, choice_family="workspace", choice_name="workspace_choices", widget=BorgSelect()) def __init__(self, *args, **kwargs): kwargs['initial'] = kwargs.get('initial', {}) self.get_setting_from_model(*args, **kwargs) super(WmsServerForm, self).__init__(*args, **kwargs) if 'instance' in kwargs and kwargs['instance'] and kwargs[ 'instance'].pk: self.fields['name'].widget.attrs['readonly'] = True self.fields['workspace'].widget.attrs['readonly'] = True def _post_clean(self): if self.errors: return self.set_setting_to_model() super(WmsServerForm, self)._post_clean() class Meta: model = WmsServer fields = "__all__"
class Meta: model = ForeignTable fields = "__all__" widgets = { 'server': BorgSelect(), }
class PublishForm(GeoserverSettingForm, BorgModelForm): """ A form for normal table's Publish Model """ create_cache_layer = forms.BooleanField(required=False, label="create_cache_layer", initial=True) create_cache_layer.setting_type = "geoserver_setting" server_cache_expire = forms.IntegerField( label="server_cache_expire", min_value=0, required=False, initial=0, help_text= "Expire server cache after n seconds (set to 0 to use source setting)") server_cache_expire.setting_type = "geoserver_setting" client_cache_expire = forms.IntegerField( label="client_cache_expire", min_value=0, required=False, initial=0, help_text= "Expire client cache after n seconds (set to 0 to use source setting)") client_cache_expire.setting_type = "geoserver_setting" workspace = GroupedModelChoiceField('publish_channel', queryset=Workspace.objects.all(), required=True, choice_family="workspace", choice_name="workspace_choices", widget=BorgSelect()) input_table = GroupedModelChoiceField('data_source', queryset=Input.objects.all(), required=False, choice_family="input", choice_name="input_options") dependents = forms.ModelMultipleChoiceField( queryset=NormalTable.objects.all(), required=False) def __init__(self, *args, **kwargs): kwargs['initial'] = kwargs.get('initial', {}) self.get_setting_from_model(*args, **kwargs) if 'instance' in kwargs and kwargs['instance']: #populate the dependents field value from table data dependents = [] for relation in (kwargs['instance'].relations): if relation: for normal_table in relation.normal_tables: if normal_table: dependents.append(normal_table) kwargs['initial']['dependents'] = dependents super(PublishForm, self).__init__(*args, **kwargs) if 'instance' in kwargs and kwargs['instance'] and kwargs[ 'instance'].pk: self.fields['name'].widget.attrs['readonly'] = True self.fields['workspace'].widget.attrs['readonly'] = True def _post_clean(self): super(PublishForm, self)._post_clean() if self.errors: return #populate the value of the relation columns if 'dependents' in self.cleaned_data: sorted_dependents = self.cleaned_data['dependents'].order_by('pk') else: sorted_dependents = [] pos = 0 normal_table_pos = 0 relation_index = 0 length = len(sorted_dependents) for relation in (self.instance.relations): normal_table_pos = 0 if pos < length: if relation is None: relation = Publish_NormalTable() self.instance.set_relation(relation_index, relation) if relation is not None: for normal_table in relation.normal_tables: if pos < length: relation.set_normal_table(normal_table_pos, sorted_dependents[pos]) elif relation: relation.set_normal_table(normal_table_pos, None) pos += 1 normal_table_pos += 1 relation_index += 1 if self.instance and self.instance.is_spatial: self.set_setting_to_model() class Meta: model = Publish fields = ('name', 'workspace', 'interval', 'status', 'input_table', 'dependents', 'priority', 'sql', 'create_extra_index_sql')
class NormaliseForm(BorgModelForm): """ A form for Normalise Model """ input_table = GroupedModelChoiceField('data_source', queryset=Input.objects.all(), required=True, choice_family="input", choice_name="input_options") dependents = forms.ModelMultipleChoiceField( queryset=NormalTable.objects.all(), required=False) output_table = forms.ModelChoiceField(queryset=NormalTable.objects.all(), required=False, widget=BorgSelect()) def __init__(self, *args, **kwargs): kwargs['initial'] = kwargs.get('initial', {}) if 'instance' in kwargs and kwargs['instance']: try: kwargs['initial']['output_table'] = kwargs[ 'instance'].normaltable except ObjectDoesNotExist: pass dependents = [] for relation in (kwargs['instance'].relations): if relation: for normal_table in relation.normal_tables: if normal_table: dependents.append(normal_table) kwargs['initial']['dependents'] = dependents super(NormaliseForm, self).__init__(*args, **kwargs) if 'instance' in kwargs and kwargs['instance'] and kwargs[ 'instance'].pk: self.fields['name'].widget.attrs['readonly'] = True self.fields['output_table'].widget.attrs['readonly'] = True def _post_clean(self): super(NormaliseForm, self)._post_clean() if self.errors: return if 'output_table' in self.cleaned_data: self.instance.normal_table = self.cleaned_data['output_table'] else: self.instance.normal_table = None if 'dependents' in self.cleaned_data: sorted_dependents = self.cleaned_data['dependents'].order_by('pk') else: sorted_dependents = [] pos = 0 normal_table_pos = 0 relation_index = 0 length = len(sorted_dependents) for relation in (self.instance.relations): normal_table_pos = 0 if pos < length: if relation is None: relation = Normalise_NormalTable() self.instance.set_relation(relation_index, relation) if relation is not None: for normal_table in relation.normal_tables: if pos < length: relation.set_normal_table(normal_table_pos, sorted_dependents[pos]) elif relation: relation.set_normal_table(normal_table_pos, None) pos += 1 normal_table_pos += 1 relation_index += 1 class Meta: model = Normalise fields = ('name', 'input_table', 'dependents', 'output_table', 'sql')
class Meta: model = Workspace fields = "__all__" widgets = { 'publish_channel': BorgSelect(), }
class DatasourceForm(BorgModelForm, GeoserverSettingForm): """ A form for Datasource model """ max_connections = forms.IntegerField(label="Max concurrent connections", initial=10, min_value=1, max_value=128) max_connections.setting_type = "geoserver_setting" max_connections.key = "max connections" connect_timeout = forms.IntegerField(label="Connect timeout in seconds", initial=30, min_value=1, max_value=3600) connect_timeout.setting_type = "geoserver_setting" connect_timeout.key = "Connection timeout" min_connections = forms.IntegerField(label="Min concurrent connections", initial=1, min_value=1, max_value=128) min_connections.setting_type = "geoserver_setting" min_connections.key = "min connections" max_connection_idle_time = forms.IntegerField( label="Max connection idle time", initial=300, min_value=1) max_connection_idle_time.setting_type = "geoserver_setting" max_connection_idle_time.key = "Max connection idle time" fetch_size = forms.IntegerField(label="Fetch size", initial=1000, min_value=1) fetch_size.setting_type = "geoserver_setting" fetch_size.key = "fetch size" workspace = GroupedModelChoiceField('publish_channel', queryset=Workspace.objects.all(), required=True, choice_family="workspace", choice_name="workspace_choices", widget=BorgSelect()) def __init__(self, *args, **kwargs): kwargs['initial'] = kwargs.get('initial', {}) self.get_setting_from_model(*args, **kwargs) super(DatasourceForm, self).__init__(*args, **kwargs) if 'instance' in kwargs and kwargs['instance'] and kwargs[ 'instance'].pk: self.fields['workspace'].widget.attrs['readonly'] = True def _post_clean(self): if self.errors: return self.set_setting_to_model() super(DatasourceForm, self)._post_clean() class Meta: model = Datasource fields = "__all__"