Home Information - Your home, your data, organized.
Python
Working on adding entity delete. WIP
Anthony Cassandra55f17c10dc04
diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.pyindex 375c6bf6..36ce3c2b 100644--- a/src/hi/apps/collection/edit/views.py+++ b/src/hi/apps/collection/edit/views.py@@ -82,9 +82,7 @@ class CollectionDeleteView( View ):try:collection = Collection.objects.get( id = collection_id )except Collection.DoesNotExist:- message = f'Collection "{collection_id}" does not exist.'- logger.warning( message )- return bad_request_response( request, message = message )+ return page_not_found_response( request )context = {'collection': collection,@@ -106,9 +104,7 @@ class CollectionDeleteView( View ):try:collection = Collection.objects.get( id = collection_id )except Collection.DoesNotExist:- message = f'Collection "{collection_id}" does not exist.'- logger.warning( message )- return bad_request_response( request, message = message )+ return page_not_found_response( request )collection.delete()@@ -133,7 +129,7 @@ class CollectionDetailsView( View ):try:collection = Collection.objects.get( id = collection_id )except Collection.DoesNotExist:- return page_not_found_response( request, message = f'No collection with id "{collection_id}".' )+ return page_not_found_response( request )location_view = request.view_parameters.location_viewdiff --git a/src/hi/apps/collection/templates/collection/edit/modals/collection_delete.html b/src/hi/apps/collection/templates/collection/edit/modals/collection_delete.htmlindex 97e0848d..b1da2ac5 100644--- a/src/hi/apps/collection/templates/collection/edit/modals/collection_delete.html+++ b/src/hi/apps/collection/templates/collection/edit/modals/collection_delete.html@@ -7,7 +7,7 @@ Delete Collection?{% block body %}<div>- Please confirm that you want to delete the location view:+ Please confirm that you want to delete the collection:<ul>{{ collection.name }}</ul>diff --git a/src/hi/apps/collection/templates/collection/edit/panes/collection_details.html b/src/hi/apps/collection/templates/collection/edit/panes/collection_details.htmlindex 9e0fd5a4..41a85bec 100644--- a/src/hi/apps/collection/templates/collection/edit/panes/collection_details.html+++ b/src/hi/apps/collection/templates/collection/edit/panes/collection_details.html@@ -13,3 +13,10 @@<div id="hi-edit-collection-views">{% include "edit/panes/collection_views.html" %}</div>++<div class="">+ <a href="{% url 'collection_edit_collection_delete' collection_id=collection.id %}"+ class="btn btn-danger btn-control" role="button" data-async="true">+ DELETE COLLECTION+ </a>+</div>diff --git a/src/hi/apps/entity/edit/urls.py b/src/hi/apps/entity/edit/urls.pyindex 9ab7f58e..01bb067d 100644--- a/src/hi/apps/entity/edit/urls.py+++ b/src/hi/apps/entity/edit/urls.py@@ -13,5 +13,9 @@ urlpatterns = [views.EntityAddView.as_view(),name='entity_edit_entity_add' ),+ re_path( r'^entity/delete/(?P<entity_id>\d+)$',+ views.EntityDeleteView.as_view(),+ name='entity_edit_entity_delete' ),+]diff --git a/src/hi/apps/entity/edit/views.py b/src/hi/apps/entity/edit/views.pyindex e338b6e4..622da328 100644--- a/src/hi/apps/entity/edit/views.py+++ b/src/hi/apps/entity/edit/views.py@@ -98,4 +98,33 @@ class EntityAddView( View ):return redirect( redirect_url )+@method_decorator( edit_required, name='dispatch' )+class EntityDeleteView( View ):++ def get( self, request, *args, **kwargs ):+ entity_id = kwargs.get( 'entity_id' )+ if not entity_id:+ return bad_request_response( request, message = 'Missing entity id in request.' )++ try:+ entity = Entity.objects.get( id = entity_id )+ except Entity.DoesNotExist:+ return page_not_found_response( request )++ context = {+ 'entity': entity,+ }+ return antinode.modal_from_template(+ request = request,+ template_name = 'entity/edit/modals/entity_delete.html',+ context = context,+ )+ def post( self, request, *args, **kwargs ):+ entity_id = kwargs.get( 'entity_id' )+ if not entity_id:+ return bad_request_response( request, message = 'Missing entity id in request.' )++++ passdiff --git a/src/hi/apps/entity/models.py b/src/hi/apps/entity/models.pyindex 1b6ceaba..9639122a 100644--- a/src/hi/apps/entity/models.py+++ b/src/hi/apps/entity/models.py@@ -59,13 +59,17 @@ class Entity( IntegrationIdModel ):return self.__str__()@property- def html_id(self):+ def html_id(self) -> str:return f'hi-entity-{self.id}'@property- def entity_type(self):+ def entity_type(self) -> EntityType:return EntityType.from_name_safe( self.entity_type_str )+ @property+ def can_user_delete(self) -> bool:+ return self.integration_type.allow_entity_deletion+@entity_type.setterdef entity_type( self, entity_type : EntityType ):self.entity_type_str = str(entity_type)diff --git a/src/hi/apps/entity/templates/entity/edit/panes/entity_details.html b/src/hi/apps/entity/templates/entity/edit/panes/entity_details.htmlindex 2bfe38a0..57736109 100644--- a/src/hi/apps/entity/templates/entity/edit/panes/entity_details.html+++ b/src/hi/apps/entity/templates/entity/edit/panes/entity_details.html@@ -38,3 +38,16 @@<div id="hi-edit-entity-controllers"><div>TBD - PRINCIPALS</div></div>++{% if entity.can_user_delete %}+<div class="">+ <a href="{% url 'entity_edit_entity_delete' entity_id=entity.id %}"+ class="btn btn-danger btn-control" role="button" data-async="true">+ DELETE ENTITY+ </a>+</div>+{% else %}+<h5>Integration</h5>+<div>{{ entity.integration_type }} [{{ entity.integration_key }}]</div>++{% endif %}diff --git a/src/hi/apps/location/edit/urls.py b/src/hi/apps/location/edit/urls.pyindex c58dc65c..425b655c 100644--- a/src/hi/apps/location/edit/urls.py+++ b/src/hi/apps/location/edit/urls.py@@ -21,6 +21,10 @@ urlpatterns = [views.LocationViewEntityToggleView.as_view(),name='location_edit_location_view_entity_toggle' ),+ re_path( r'^location-view/entity/toggle/(?P<location_view_id>\d+)/(?P<entity_id>\d+)$',+ views.LocationViewEntityToggleView.as_view(),+ name='location_edit_location_view_entity_toggle' ),+re_path( r'^location-view/collection/toggle/(?P<location_view_id>\d+)/(?P<collection_id>\d+)$',views.LocationViewEntityToggleCollectionView.as_view(),name='location_edit_location_view_collection_toggle' ),diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.pyindex 1dbcf70d..33679fdd 100644--- a/src/hi/apps/location/edit/views.py+++ b/src/hi/apps/location/edit/views.py@@ -188,6 +188,22 @@ class LocationViewEntityToggleView( View ):)+@method_decorator( edit_required, name='dispatch' )+class LocationViewEntityRemoveView( View ):++ def post( self, request, *args, **kwargs ):++ location_view_id = kwargs.get('location_view_id')+ if not location_view_id:+ return bad_request_response( request, message = 'Missing location view id in request.' )++ entity_id = kwargs.get('entity_id')+ if not entity_id:+ return bad_request_response( request, message = 'Missing entity id in request.' )++ pass++@method_decorator( edit_required, name='dispatch' )class LocationViewEntityToggleCollectionView( View ):diff --git a/src/hi/integrations/core/enums.py b/src/hi/integrations/core/enums.pyindex 8cc2256c..0470d550 100644--- a/src/hi/integrations/core/enums.py+++ b/src/hi/integrations/core/enums.py@@ -13,4 +13,8 @@ class IntegrationType(LabeledEnum):NONE = ( 'None', 'No integration placeholder.' )ZONEMINDER = ( 'ZoneMinder', 'ZoneMinder camera streaming and motion detection.' )HASS = ( 'Home Assistant (hass)', 'Home Assistant device access' )++ @property+ def allow_entity_deletion(self) -> bool:+ return bool( self == IntegrationType.NONE )