Home Information - Your home, your data, organized.
Python
ZoneMinder attribute form and enable modal working.
Anthony Cassandra058a20c8dd4a
diff --git a/src/hi/apps/attribute/forms.py b/src/hi/apps/attribute/forms.pyindex 5dcc0a45..5c4e2674 100644--- a/src/hi/apps/attribute/forms.py+++ b/src/hi/apps/attribute/forms.py@@ -13,23 +13,11 @@ class AttributeForm(forms.ModelForm):class Meta:abstract = True- fields = (- 'name',- 'value',- )- widgets = {- 'name': forms.TextInput(- attrs = {- 'class': 'form-control',- }- ),- 'value': forms.TextInput(- attrs = {- 'class': 'form-control',- }- ),- }+ @property+ def is_editable(self):+ return self._is_editable+def __init__(self, *args, **kwargs):self._is_editable = kwargs.pop( 'is_editable', True )super().__init__(*args, **kwargs)@@ -37,8 +25,9 @@ class AttributeForm(forms.ModelForm):# Access the instance's value_type fieldinstance = kwargs.get('instance')- # Always set the name field as a TextInput widget- self.fields['name'].widget = forms.TextInput(attrs={'class': 'form-control'})+ # Name is not always present and editable+ if 'name' in self.fields:+ self.fields['name'].widget = forms.TextInput(attrs={'class': 'form-control'})# Customize the value field based on the value_type in the instanceif instance and instance.value_type:@@ -53,7 +42,8 @@ class AttributeForm(forms.ModelForm):attrs={'step': 'any', 'class': 'form-control'},)elif value_type == AttributeValueType.BOOLEAN:- self.fields['value'].widget = forms.CheckboxInput()+ self.fields['value'].widget = forms.CheckboxInput(+ )elif value_type == AttributeValueType.DATE:self.fields['value'].widget = forms.DateInput(attrs={'type': 'date', 'class': 'form-control'},@@ -78,13 +68,10 @@ class AttributeForm(forms.ModelForm):self.fields['value'].widget = forms.URLInput(attrs={'class': 'form-control'},)- elif value_type == AttributeValueType.PASSWORD:- self.fields['value'].widget = forms.PasswordInput(- attrs={'class': 'form-control'},- )elif value_type in { AttributeValueType.IMAGE,AttributeValueType.PDF,- AttributeValueType.VIDEO, AttributeValueType.AUDIO }:+ AttributeValueType.VIDEO,+ AttributeValueType.AUDIO }:# File input for media typesself.fields['value'].widget = forms.FileInput(attrs={'class': 'form-control'},@@ -99,6 +86,13 @@ class AttributeForm(forms.ModelForm):self.fields['value'].widget = forms.TextInput(attrs={'class': 'form-control'},)++ if not self._is_editable:+ for field in self.fields.values():+ field.widget.attrs['disabled'] = 'disabled'+ continue++ returndef clean(self):cleaned_data = super().clean()@@ -130,9 +124,8 @@ class AttributeForm(forms.ModelForm):self.add_error('value', 'Not a valid email address.')elif value_type == AttributeValueType.LINK:- validate_url = URLValidator()try:- validate_url(value)+ URLValidator(value)except ValidationError:self.add_error('value', 'Not a valid URL.')@@ -172,7 +165,4 @@ class AttributeForm(forms.ModelForm):return cleaned_data- @property- def is_editable(self):- return self._is_editablediff --git a/src/hi/apps/config/templates/config/panes/home.html b/src/hi/apps/config/templates/config/panes/home.htmlindex b9c1043f..84d6e5f6 100644--- a/src/hi/apps/config/templates/config/panes/home.html+++ b/src/hi/apps/config/templates/config/panes/home.html@@ -3,29 +3,89 @@<ul class="nav nav-tabs" id="hi-config-tab" role="tablist"><li class="nav-item">- <a class="nav-link active" id="hi-settings-tab" data-toggle="tab" href="#hi-settings" role="tab" aria-controls="hi-settings" aria-selected="true">Settings</a>+ <a class="nav-link active" id="hi-settings-tab" data-toggle="tab" href="#hi-settings" role="tab"+ aria-controls="hi-settings" aria-selected="true">Settings</a></li><li class="nav-item">- <a class="nav-link" id="hi-integrations-tab" data-toggle="tab" href="#hi-integrations" role="tab" aria-controls="hi-integrations" aria-selected="false">Integrations</a>+ <a class="nav-link" id="hi-integrations-tab" data-toggle="tab" href="#hi-integrations" role="tab"+ aria-controls="hi-integrations" aria-selected="false">Integrations</a></li><li class="nav-item">- <a class="nav-link" id="hi-other-tab" data-toggle="tab" href="#hi-other" role="tab" aria-controls="hi-other" aria-selected="false">Other</a>+ <a class="nav-link" id="hi-other-tab" data-toggle="tab" href="#hi-other" role="tab"+ aria-controls="hi-other" aria-selected="false">Other</a></li></ul><div class="tab-content" id="hi-config-tab-content">- <div class="tab-pane fade show active" id="hi-settings" role="tabpanel" aria-labelledby="hi-settings-tab">+ <div class="tab-pane fade show active" id="hi-settings" role="tabpanel"+ aria-labelledby="hi-settings-tab">TBD for Settings</div>- <div class="tab-pane fade" id="hi-integrations" role="tabpanel" aria-labelledby="hi-integrations-tab">+ <div class="tab-pane fade" id="hi-integrations" role="tabpanel"+ aria-labelledby="hi-integrations-tab"><div id="{{ DIVID.INTEGRATION_TAB }}">{% include "core/panes/config_tab.html" with integration_list=integration_list %}</div></div>- <div class="tab-pane fade" id="hi-other" role="tabpanel" aria-labelledby="hi-other-tab">+ <div class="tab-pane fade" id="hi-other" role="tabpanel"+ aria-labelledby="hi-other-tab">TBD for Other</div></div></div>+<script>+ let gHiPreviousTabId = ''; // Track the previously active tab+ let gHiIgnorePushState = false; // Flag to ignore pushState when triggered by popstate++ $(document).ready(function () {++ $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {+ let tabId = $(e.target).attr('href');++ if ( gHiIgnorePushState ) {+ if ( Hi.DEBUG ) { console.log( `Ignoring Tab change: ${tabId}` ); }+ gHiIgnorePushState = false;+ return;+ }+++ if ( Hi.DEBUG ) { console.log( `Tab change event: ${tabId} [prev=${gHiPreviousTabId}]` ); }++ if (tabId === gHiPreviousTabId) {+ return;+ }+ gHiPreviousTabId = tabId;++ let newUrl = window.location.protocol + "//" + window.location.hostname;+ if (window.location.port) {+ newUrl += ':' + window.location.port;+ }+ newUrl += window.location.pathname + tabId;+ window.history.pushState(null, null, newUrl);++ if ( Hi.DEBUG ) { console.log( `Pushed URL: ${newUrl}` ); }++ });++ // On page load, show the correct tab based on the URL hash+ let hash = window.location.hash;+ if (hash) {+ $('.nav-tabs a[href="' + hash + '"]').tab('show');+ }+ });+++ window.addEventListener('popstate', function() {+ let hash = window.location.hash;+ if ( Hi.DEBUG ) { console.log( `Pop State: ${hash}` ); }+ gHiIgnorePushState = true;+ if (hash) {+ $('.nav-tabs a[href="' + hash + '"]').tab('show');+ } else {+ // Fallback if no hash exists+ $('.nav-tabs a:first').tab('show');+ }+ });+</script>diff --git a/src/hi/integrations/core/forms.py b/src/hi/integrations/core/forms.pyindex cc3256af..6677cfb2 100644--- a/src/hi/integrations/core/forms.py+++ b/src/hi/integrations/core/forms.py@@ -10,7 +10,6 @@ class IntegrationAttributeForm( AttributeForm ):class Meta:model = IntegrationAttributefields = (- 'name','value',)diff --git a/src/hi/integrations/zoneminder/views.py b/src/hi/integrations/zoneminder/views.pyindex bd990846..adddd11a 100644--- a/src/hi/integrations/zoneminder/views.py+++ b/src/hi/integrations/zoneminder/views.py@@ -26,7 +26,9 @@ class ZmEnableView( View ):integration_attribute_formset = IntegrationAttributeFormSet(instance = integration,- form_kwargs = { 'is_editable': True },+ form_kwargs = {+ 'is_editable': True,+ },)return self.get_modal_response(request = request,@@ -36,6 +38,8 @@ class ZmEnableView( View ):def post(self, request, *args, **kwargs):integration = self.get_or_create_integration()+ if integration.is_enabled:+ return bad_request_response( request, message = 'ZoneMinder is already enabled' )integration_attribute_formset = IntegrationAttributeFormSet(request.POST,@@ -48,22 +52,12 @@ class ZmEnableView( View ):status = 400,)- # TODO:- # Check if have all needed properties- # If not, render form for entering with button to activate------ raise NotImplementedError('FORCED')-+ with transaction.atomic():+ integration.is_enabled = True+ integration.save()+ integration_attribute_formset.save()- integration_attribute_formset.save()-- context = {- }- return render( request, 'zoneminder/panes/activate.html', context )+ return antinode.refresh_response()def get_or_create_integration( self ):try: