Skip to content

Home Information - Your home, your data, organized.

Python

New entities now added to collection/location view on creation

Anthony Cassandrad241680b3679
diff --git a/src/hi/apps/collection/collection_manager.py b/src/hi/apps/collection/collection_manager.py
index 0d84022f..0ba423f5 100644
--- a/src/hi/apps/collection/collection_manager.py
+++ b/src/hi/apps/collection/collection_manager.py
@@ -1,7 +1,8 @@
from hi.apps.common.singleton import Singleton
+from hi.apps.entity.models import Entity
from .collection_data import CollectionData
-from .models import Collection
+from .models import Collection, CollectionEntity
class CollectionManager(Singleton):
@@ -20,3 +21,24 @@ class CollectionManager(Singleton):
collection = collection,
entity_list = entity_list,
)
+
+ def create_collection_entity( self,
+ entity : Entity,
+ collection : Collection ) -> CollectionEntity:
+
+ last_item = CollectionEntity.objects.order_by( '-order_id' ).first()
+ return CollectionEntity.objects.create(
+ entity = entity,
+ collection = collection,
+ order_id = last_item.order_id + 1,
+ )
+
+ def remove_collection_entity( self,
+ entity : Entity,
+ collection : Collection ) -> bool:
+ try:
+ collection_entity = CollectionEntity.objects.get( entity = entity, collection = collection )
+ collection_entity.delete()
+ return True
+ except CollectionEntity.DoesNotExist:
+ return False
diff --git a/src/hi/apps/collection/edit/helpers.py b/src/hi/apps/collection/edit/helpers.py
index a6ebf343..9b8a1ce9 100644
--- a/src/hi/apps/collection/edit/helpers.py
+++ b/src/hi/apps/collection/edit/helpers.py
@@ -3,6 +3,7 @@ from typing import List
from django.db import transaction
from django.http import HttpRequest
+from hi.apps.collection.collection_manager import CollectionManager
from hi.apps.collection.enums import CollectionType
from hi.apps.collection.models import (
Collection,
@@ -69,22 +70,38 @@ class CollectionEditHelpers:
@classmethod
def toggle_entity_in_collection( cls, entity : Entity, collection : Collection ) -> bool:
- try:
- collection_entity = CollectionEntity.objects.get(
+ if CollectionEntity.objects.filter( entity = entity, collection = collection ).exists():
+ cls.remove_entity_from_collection( entity = entity, collection = collection )
+ return False
+ else:
+ cls.add_entity_to_collection( entity = entity, collection = collection )
+ return True
+
+ @classmethod
+ def add_entity_to_collection_by_id( cls, entity : Entity, collection_id : int ) -> bool:
+ collection = Collection.objects.get( id = collection_id )
+ cls.add_entity_to_collection( entity = entity, collection = collection )
+ return
+
+ @classmethod
+ def add_entity_to_collection( cls, entity : Entity, collection : Collection ) -> bool:
+
+ with transaction.atomic():
+ CollectionManager().create_collection_entity(
entity = entity,
collection = collection,
)
- collection_entity.delete()
- return False
+ return
- except CollectionEntity.DoesNotExist:
- last_item = CollectionEntity.objects.order_by( '-order_id' ).first()
- _ = CollectionEntity.objects.create(
+ @classmethod
+ def remove_entity_from_collection( cls, entity : Entity, collection : Collection ) -> bool:
+
+ with transaction.atomic():
+ CollectionManager().remove_collection_entity(
entity = entity,
collection = collection,
- order_id = last_item.order_id + 1,
)
- return True
+ return
@classmethod
def set_collection_entity_order( cls,
diff --git a/src/hi/apps/collection/templates/collection/edit/panes/collection_add_remove_item.html b/src/hi/apps/collection/templates/collection/edit/panes/collection_add_remove_item.html
index a4d70b5a..fbd083b1 100644
--- a/src/hi/apps/collection/templates/collection/edit/panes/collection_add_remove_item.html
+++ b/src/hi/apps/collection/templates/collection/edit/panes/collection_add_remove_item.html
@@ -1,5 +1,14 @@
<div class="">ADD/REMOVE</div>
+<div class="">
+ <a href="{% url 'entity_edit_entity_add' %}"
+ class="btn btn-success btn-control" role="button"
+ data-async="modal" >
+ NEW ITEM
+ </a>
+</div>
+
+
{% for entity_collection_group in entity_collection_group_list %}
<div class="">
<div class="">{{ entity_collection_group.entity_type.label }}</div>
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.html
index 41a85bec..82b4c84d 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
@@ -1,4 +1,5 @@
-<h4>{{ collection.name }}</h4>
+<h5>Collection</h5>
+<div>{{ collection.name }}</div>
{% if svg_position_form %}
<h5>Position</h5>
@@ -9,6 +10,12 @@
{% endwith %}
{% endif %}
+<h5>View Type</h5>
+<div>{{ collection.collection_type.label }}</div>
+
+<h5>Order Index</h5>
+<div>{{ collection.order_id }}</div>
+
<h5>Views</h5>
<div id="hi-edit-collection-views">
{% include "edit/panes/collection_views.html" %}
diff --git a/src/hi/apps/collection/views.py b/src/hi/apps/collection/views.py
index bd18c41b..30f61bd9 100644
--- a/src/hi/apps/collection/views.py
+++ b/src/hi/apps/collection/views.py
@@ -73,12 +73,19 @@ class CollectionView( HiGridView ):
)
context = {
'is_async_request': is_ajax( request ),
+ 'collection': collection,
'collection_data': collection_data,
}
+
+ side_template_name = None
+ if request.is_editing:
+ side_template_name = 'edit/panes/side.html'
+
return self.hi_grid_response(
request = request,
context = context,
main_template_name = 'collection/collection_view.html',
+ side_template_name = side_template_name,
push_url_name = 'collection_view',
push_url_kwargs = kwargs,
)
diff --git a/src/hi/apps/entity/edit/views.py b/src/hi/apps/entity/edit/views.py
index 782971fd..ceb58737 100644
--- a/src/hi/apps/entity/edit/views.py
+++ b/src/hi/apps/entity/edit/views.py
@@ -1,15 +1,18 @@
import logging
+from django.db import transaction
from django.shortcuts import redirect
from django.template.loader import get_template
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.views.generic import View
+from hi.apps.collection.edit.helpers import CollectionEditHelpers
import hi.apps.common.antinode as antinode
from hi.apps.entity.entity_manager import EntityManager
from hi.apps.entity.enums import EntityType
from hi.apps.entity.models import Entity, EntityPosition
+from hi.apps.location.edit.helpers import LocationEditHelpers
from hi.apps.location.forms import SvgPositionForm
from hi.constants import DIVID
@@ -87,17 +90,32 @@ class EntityAddView( View ):
name = cleaned_data.get('name')
try:
- _ = EntityManager().create_entity(
- entity_type = entity_type,
- name = name,
- )
+ with transaction.atomic():
+ entity = EntityManager().create_entity(
+ entity_type = entity_type,
+ name = name,
+ )
+ if ( request.view_parameters.view_type.is_location_view
+ and request.view_parameters.location_view_id ):
+ LocationEditHelpers.add_entity_to_view_by_id(
+ entity = entity,
+ location_view_id = request.view_parameters.location_view_id,
+ )
+
+ elif ( request.view_parameters.view_type.is_collection
+ and request.view_parameters.collection_id ):
+ CollectionEditHelpers.add_entity_to_collection_by_id(
+ entity = entity,
+ collection_id = request.view_parameters.collection_id,
+ )
+
+ redirect_url = reverse('home')
+ return redirect( redirect_url )
+
except ValueError as e:
return bad_request_response( request, message = str(e) )
- redirect_url = reverse('home')
- return redirect( redirect_url )
-
@method_decorator( edit_required, name='dispatch' )
class EntityDeleteView( View ):
diff --git a/src/hi/apps/entity/enums.py b/src/hi/apps/entity/enums.py
index 1e1fe890..9aed5bf2 100644
--- a/src/hi/apps/entity/enums.py
+++ b/src/hi/apps/entity/enums.py
@@ -31,6 +31,7 @@ class EntityType(LabeledEnum):
BAROMETER = ( 'Barometer', '' )
CAMERA = ( 'Camera', '' )
COMPUTER = ( 'Computer', '' )
+ CONSUMABLE = ( 'Consumable', '' )
CONTROL_WIRE = ( 'Control Wire', '', True )
DISPLAY = ( 'Display', '' )
DOOR = ( 'Door', '' )
diff --git a/src/hi/apps/location/edit/helpers.py b/src/hi/apps/location/edit/helpers.py
index 7671fb1e..36ad51d8 100644
--- a/src/hi/apps/location/edit/helpers.py
+++ b/src/hi/apps/location/edit/helpers.py
@@ -141,6 +141,12 @@ class LocationEditHelpers:
return
+ @classmethod
+ def add_entity_to_view_by_id( cls, entity : Entity, location_view_id : int ):
+ location_view = LocationView.objects.get( id = location_view_id )
+ cls.add_entity_to_view( entity = entity, location_view = location_view )
+ return
+
@classmethod
def add_entity_to_view( cls, entity : Entity, location_view : LocationView ):
diff --git a/src/hi/apps/location/templates/location/edit/panes/location_view_details.html b/src/hi/apps/location/templates/location/edit/panes/location_view_details.html
index 26b9223b..d78d497f 100644
--- a/src/hi/apps/location/templates/location/edit/panes/location_view_details.html
+++ b/src/hi/apps/location/templates/location/edit/panes/location_view_details.html
@@ -4,7 +4,7 @@
<div>{{ location_view_data.location_view.name }}</div>
<h5>View Type</h5>
-<div>{{ location_view_data.location_view.location_view_type }}</div>
+<div>{{ location_view_data.location_view.location_view_type.label }}</div>
<h5>View Box</h5>
<div>{{ location_view_data.location_view.svg_view_box }}</div>
diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.py
index ffd35cf9..3c386f0b 100644
--- a/src/hi/apps/location/views.py
+++ b/src/hi/apps/location/views.py
@@ -78,10 +78,16 @@ class LocationViewView( HiGridView ):
'is_async_request': is_ajax( request ),
'location_view_data': location_view_data,
}
+
+ side_template_name = None
+ if request.is_editing:
+ side_template_name = 'edit/panes/side.html'
+
return self.hi_grid_response(
request = request,
context = context,
main_template_name = 'location/location_view.html',
+ side_template_name = side_template_name,
push_url_name = 'location_view',
push_url_kwargs = kwargs,
)