Skip to content

Home Information - Your home, your data, organized.

Python

Refactor to remove edit helper classes.

Anthony Cassandra34d2a02b1967
diff --git a/src/hi/apps/collection/collection_manager.py b/src/hi/apps/collection/collection_manager.py
index e342fcd2..f43801a4 100644
--- a/src/hi/apps/collection/collection_manager.py
+++ b/src/hi/apps/collection/collection_manager.py
@@ -1,4 +1,5 @@
from decimal import Decimal
+from typing import List
from django.db import transaction
from django.http import HttpRequest
@@ -19,6 +20,10 @@ from .models import (
CollectionPosition,
CollectionView,
)
+from .transient_models import (
+ EntityCollectionItem,
+ EntityCollectionGroup,
+)
class CollectionManager(Singleton):
@@ -91,6 +96,14 @@ class CollectionManager(Singleton):
return
+ def create_collection_view_by_id( self, collection : Collection, location_view_id : int ):
+ location_view = LocationView.objects.get( id = location_view_id )
+ _ = self.create_collection_view(
+ collection = collection,
+ location_view = location_view,
+ )
+ return
+
def create_collection_view( self, collection : Collection, location_view : LocationView ):
with transaction.atomic():
@@ -165,8 +178,8 @@ class CollectionManager(Singleton):
collection = collection,
location = location,
)
- @classmethod
- def add_collection_position_if_needed( cls,
+
+ def add_collection_position_if_needed( self,
collection : Collection,
location_view : LocationView ) -> CollectionPosition:
try:
@@ -239,3 +252,98 @@ class CollectionManager(Singleton):
location_item_position_form = location_item_position_form,
)
+ def create_entity_collection_group_list( self, collection : Collection ) -> List[EntityCollectionGroup]:
+
+ entity_queryset = Entity.objects.all()
+
+ entity_collection_group_dict = dict()
+ for entity in entity_queryset:
+
+ exists_in_collection = False
+ for collection_entity in entity.collections.all():
+ if collection_entity.collection == collection:
+ exists_in_collection = True
+ break
+ continue
+
+ entity_collection_item = EntityCollectionItem(
+ entity = entity,
+ exists_in_collection = exists_in_collection,
+ )
+
+ if entity.entity_type not in entity_collection_group_dict:
+ entity_collection_group = EntityCollectionGroup(
+ collection = collection,
+ entity_type = entity.entity_type,
+ )
+ entity_collection_group_dict[entity.entity_type] = entity_collection_group
+ entity_collection_group_dict[entity.entity_type].item_list.append( entity_collection_item )
+ continue
+
+ entity_collection_group_list = list( entity_collection_group_dict.values() )
+ entity_collection_group_list.sort( key = lambda item : item.entity_type.label )
+ return entity_collection_group_list
+
+ def toggle_entity_in_collection( self, entity : Entity, collection : Collection ) -> bool:
+
+ if CollectionEntity.objects.filter( entity = entity, collection = collection ).exists():
+ self.remove_entity_from_collection( entity = entity, collection = collection )
+ return False
+ else:
+ self.add_entity_to_collection( entity = entity, collection = collection )
+ return True
+
+ def add_entity_to_collection_by_id( self, entity : Entity, collection_id : int ) -> bool:
+ collection = Collection.objects.get( id = collection_id )
+ self.add_entity_to_collection( entity = entity, collection = collection )
+ return
+
+ def add_entity_to_collection( self, entity : Entity, collection : Collection ) -> bool:
+
+ with transaction.atomic():
+ self.create_collection_entity(
+ entity = entity,
+ collection = collection,
+ )
+ return
+
+ def remove_entity_from_collection( self, entity : Entity, collection : Collection ) -> bool:
+
+ with transaction.atomic():
+ self.remove_collection_entity(
+ entity = entity,
+ collection = collection,
+ )
+ return
+
+ def set_collection_entity_order( self,
+ collection_id : int,
+ entity_id_list : List[int] ):
+ item_id_to_order_id = {
+ item_id: order_id for order_id, item_id in enumerate( entity_id_list )
+ }
+
+ collection_entity_queryset = CollectionEntity.objects.filter(
+ collection_id = collection_id,
+ entity_id__in = entity_id_list,
+ )
+ with transaction.atomic():
+ for collection_entity in collection_entity_queryset:
+ collection_entity.order_id = item_id_to_order_id.get( collection_entity.entity.id )
+ collection_entity.save()
+ continue
+ return
+
+ def set_collection_order( self, collection_id_list : List[int] ):
+ item_id_to_order_id = {
+ item_id: order_id for order_id, item_id in enumerate( collection_id_list )
+ }
+
+ collection_queryset = Collection.objects.filter( id__in = collection_id_list )
+ with transaction.atomic():
+ for collection in collection_queryset:
+ collection.order_id = item_id_to_order_id.get( collection.id )
+ collection.save()
+ continue
+ return
+
diff --git a/src/hi/apps/collection/edit/helpers.py b/src/hi/apps/collection/edit/helpers.py
deleted file mode 100644
index 694169a4..00000000
--- a/src/hi/apps/collection/edit/helpers.py
+++ /dev/null
@@ -1,123 +0,0 @@
-from typing import List
-
-from django.db import transaction
-
-from hi.apps.collection.collection_manager import CollectionManager
-from hi.apps.collection.models import (
- Collection,
- CollectionEntity,
-)
-from hi.apps.entity.models import (
- Entity,
-)
-
-from .transient_models import (
- EntityCollectionItem,
- EntityCollectionGroup,
-)
-
-
-class CollectionEditHelpers:
-
- @classmethod
- def create_entity_collection_group_list( cls, collection : Collection ) -> List[EntityCollectionGroup]:
-
- entity_queryset = Entity.objects.all()
-
- entity_collection_group_dict = dict()
- for entity in entity_queryset:
-
- exists_in_collection = False
- for collection_entity in entity.collections.all():
- if collection_entity.collection == collection:
- exists_in_collection = True
- break
- continue
-
- entity_collection_item = EntityCollectionItem(
- entity = entity,
- exists_in_collection = exists_in_collection,
- )
-
- if entity.entity_type not in entity_collection_group_dict:
- entity_collection_group = EntityCollectionGroup(
- collection = collection,
- entity_type = entity.entity_type,
- )
- entity_collection_group_dict[entity.entity_type] = entity_collection_group
- entity_collection_group_dict[entity.entity_type].item_list.append( entity_collection_item )
- continue
-
- entity_collection_group_list = list( entity_collection_group_dict.values() )
- entity_collection_group_list.sort( key = lambda item : item.entity_type.label )
- return entity_collection_group_list
-
- @classmethod
- def toggle_entity_in_collection( cls, entity : Entity, collection : Collection ) -> bool:
-
- 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,
- )
- return
-
- @classmethod
- def remove_entity_from_collection( cls, entity : Entity, collection : Collection ) -> bool:
-
- with transaction.atomic():
- CollectionManager().remove_collection_entity(
- entity = entity,
- collection = collection,
- )
- return
-
- @classmethod
- def set_collection_entity_order( cls,
- collection_id : int,
- entity_id_list : List[int] ):
- item_id_to_order_id = {
- item_id: order_id for order_id, item_id in enumerate( entity_id_list )
- }
-
- collection_entity_queryset = CollectionEntity.objects.filter(
- collection_id = collection_id,
- entity_id__in = entity_id_list,
- )
- with transaction.atomic():
- for collection_entity in collection_entity_queryset:
- collection_entity.order_id = item_id_to_order_id.get( collection_entity.entity.id )
- collection_entity.save()
- continue
- return
-
- @classmethod
- def set_collection_order( cls, collection_id_list : List[int] ):
- item_id_to_order_id = {
- item_id: order_id for order_id, item_id in enumerate( collection_id_list )
- }
-
- collection_queryset = Collection.objects.filter( id__in = collection_id_list )
- with transaction.atomic():
- for collection in collection_queryset:
- collection.order_id = item_id_to_order_id.get( collection.id )
- collection.save()
- continue
- return
-
diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.py
index d09fa155..7cd0870c 100644
--- a/src/hi/apps/collection/edit/views.py
+++ b/src/hi/apps/collection/edit/views.py
@@ -12,7 +12,6 @@ from hi.apps.collection.collection_manager import CollectionManager
from hi.apps.collection.enums import CollectionType
from hi.apps.collection.models import Collection
from hi.apps.entity.models import Entity
-from hi.apps.location.edit.helpers import LocationEditHelpers
from hi.decorators import edit_required
from hi.enums import ViewType
from hi.views import bad_request_response, page_not_found_response
@@ -20,7 +19,6 @@ from hi.views import bad_request_response, page_not_found_response
from hi.constants import DIVID
from . import forms
-from .helpers import CollectionEditHelpers
logger = logging.getLogger(__name__)
@@ -63,7 +61,7 @@ class CollectionAddView( View ):
)
if ( request.view_parameters.view_type.is_location_view
and request.view_parameters.location_view_id ):
- LocationEditHelpers.add_collection_to_view_by_id(
+ CollectionManager().create_collection_view_by_id(
collection = collection,
location_view_id = request.view_parameters.location_view_id,
)
@@ -134,7 +132,7 @@ class CollectionAddRemoveItemView( View ):
def get(self, request, *args, **kwargs):
collection = request.view_parameters.collection
- entity_collection_group_list = CollectionEditHelpers.create_entity_collection_group_list(
+ entity_collection_group_list = CollectionManager().create_entity_collection_group_list(
collection = collection,
)
@@ -160,7 +158,7 @@ class CollectionEntityToggleView( View ):
entity = Entity.objects.get( id = entity_id )
collection = Collection.objects.get( id = collection_id )
- exists_in_collection = CollectionEditHelpers.toggle_entity_in_collection(
+ exists_in_collection = CollectionManager().toggle_entity_in_collection(
entity = entity,
collection = collection,
)
@@ -206,7 +204,7 @@ class CollectionReorderEntitiesView( View ):
if not entity_id_list:
return bad_request_response( request, message = 'Missing entity ids.' )
- CollectionEditHelpers.set_collection_entity_order(
+ CollectionManager().set_collection_entity_order(
collection_id = collection_id,
entity_id_list = entity_id_list,
)
@@ -225,7 +223,7 @@ class CollectionReorder( View ):
if not collection_id_list:
return bad_request_response( request, message = 'Missing collection ids.' )
- CollectionEditHelpers.set_collection_order(
+ CollectionManager().set_collection_order(
collection_id_list = collection_id_list,
)
return antinode.response( main_content = 'OK' )
diff --git a/src/hi/apps/collection/edit/transient_models.py b/src/hi/apps/collection/transient_models.py
similarity index 90%
rename from src/hi/apps/collection/edit/transient_models.py
rename to src/hi/apps/collection/transient_models.py
index 860afe56..6a215d68 100644
--- a/src/hi/apps/collection/edit/transient_models.py
+++ b/src/hi/apps/collection/transient_models.py
@@ -1,10 +1,11 @@
from dataclasses import dataclass, field
from typing import List
-from hi.apps.collection.models import Collection
from hi.apps.entity.enums import EntityType
from hi.apps.entity.models import Entity
+from .models import Collection
+
@dataclass
class EntityCollectionItem:
diff --git a/src/hi/apps/entity/edit/views.py b/src/hi/apps/entity/edit/views.py
index 6aba84bc..eb8fd839 100644
--- a/src/hi/apps/entity/edit/views.py
+++ b/src/hi/apps/entity/edit/views.py
@@ -6,12 +6,12 @@ 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
+from hi.apps.collection.collection_manager import CollectionManager
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
-from hi.apps.location.edit.helpers import LocationEditHelpers
+from hi.apps.location.location_manager import LocationManager
from hi.decorators import edit_required
from hi.views import bad_request_response, not_authorized_response, page_not_found_response
@@ -58,14 +58,14 @@ class EntityAddView( View ):
)
if ( request.view_parameters.view_type.is_location_view
and request.view_parameters.location_view_id ):
- LocationEditHelpers.add_entity_to_view_by_id(
+ LocationManager().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(
+ CollectionManager().add_entity_to_collection_by_id(
entity = entity,
collection_id = request.view_parameters.collection_id,
)
diff --git a/src/hi/apps/location/edit/helpers.py b/src/hi/apps/location/edit/helpers.py
deleted file mode 100644
index 9fb7db80..00000000
--- a/src/hi/apps/location/edit/helpers.py
+++ /dev/null
@@ -1,206 +0,0 @@
-from decimal import Decimal
-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.models import (
- Collection,
- CollectionView,
-)
-from hi.apps.entity.delegation_manager import DelegationManager
-from hi.apps.entity.entity_manager import EntityManager
-from hi.apps.entity.models import (
- Entity,
- EntityView,
-)
-from hi.apps.location.models import Location, LocationView
-
-from .transient_models import (
- EntityViewGroup,
- EntityViewItem,
- CollectionViewItem,
- CollectionViewGroup,
-)
-
-
-class LocationEditHelpers:
-
- @classmethod
- def create_entity_view_group_list( cls, location_view : LocationView ) -> List[EntityViewGroup]:
-
- entity_queryset = Entity.objects.all()
-
- entity_view_group_dict = dict()
- for entity in entity_queryset:
-
- exists_in_view = False
- for entity_view in entity.entity_views.all():
- if entity_view.location_view == location_view:
- exists_in_view = True
- break
- continue
-
- entity_view_item = EntityViewItem(
- entity = entity,
- exists_in_view = exists_in_view,
- )
-
- if entity.entity_type not in entity_view_group_dict:
- entity_view_group = EntityViewGroup(
- location_view = location_view,
- entity_type = entity.entity_type,
- )
- entity_view_group_dict[entity.entity_type] = entity_view_group
- entity_view_group_dict[entity.entity_type].item_list.append( entity_view_item )
- continue
-
- entity_view_group_list = list( entity_view_group_dict.values() )
- entity_view_group_list.sort( key = lambda item : item.entity_type.label )
- return entity_view_group_list
-
- @classmethod
- def create_collection_view_group( cls, location_view : LocationView ) -> CollectionViewGroup:
-
- collection_queryset = Collection.objects.all()
-
- collection_view_group = CollectionViewGroup(
- location_view = location_view,
- )
- for collection in collection_queryset:
-
- exists_in_view = False
- for collection_view in collection.collection_views.all():
- if collection_view.location_view == location_view:
- exists_in_view = True
- break
- continue
-
- collection_view_item = CollectionViewItem(
- collection = collection,
- exists_in_view = exists_in_view,
- )
- collection_view_group.item_list.append( collection_view_item )
- continue
-
- collection_view_group.item_list.sort( key = lambda item : item.collection.name )
- return collection_view_group
-
- @classmethod
- def toggle_entity_in_view( cls, entity : Entity, location_view : LocationView ) -> bool:
-
- try:
- cls.remove_entity_from_view( entity = entity, location_view = location_view )
- return False
- except EntityView.DoesNotExist:
- cls.add_entity_to_view( entity = entity, location_view = location_view )
- return True
-
- @classmethod
- def remove_entity_from_view( cls, entity : Entity, location_view : LocationView ):
-
- with transaction.atomic():
- EntityManager().remove_entity_view(
- entity = entity,
- location_view = location_view,
- )
-
- DelegationManager().remove_delegate_entities_from_view_if_needed(
- entity = entity,
- location_view = location_view,
- )
-
- 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 ):
-
- with transaction.atomic():
- # Only create delegate entities the first time an entity is added to a view.
- if not entity.entity_views.all().exists():
- delegate_entity_list = DelegationManager().get_delegate_entities_with_defaults(
- entity = entity,
- )
- else:
- delegate_entity_list = DelegationManager().get_delegate_entities(
- entity = entity,
- )
-
- _ = EntityManager().create_entity_view(
- entity = entity,
- location_view = location_view,
- )
-
- for delegate_entity in delegate_entity_list:
- _ = EntityManager().create_entity_view(
- entity = delegate_entity,
- location_view = location_view,
- )
- continue
-
- return
-
- @classmethod
- def toggle_collection_in_view( cls,
- collection : Collection,
- location_view : LocationView ) -> bool:
-
- try:
- cls.remove_collection_from_view( collection = collection, location_view = location_view )
- return False
-
- except CollectionView.DoesNotExist:
- cls.add_collection_to_view( collection = collection, location_view = location_view )
- return True
-
- @classmethod
- def remove_collection_from_view( cls,
- collection : Collection,
- location_view : LocationView ):
- with transaction.atomic():
- CollectionManager().remove_collection_view(
- collection = collection,
- location_view = location_view,
- )
- return
-
- @classmethod
- def add_collection_to_view_by_id( cls, collection : Collection, location_view_id : int ):
- location_view = LocationView.objects.get( id = location_view_id )
- cls.add_collection_to_view( collection = collection, location_view = location_view )
- return
-
- @classmethod
- def add_collection_to_view( cls,
- collection : Collection,
- location_view : LocationView ):
-
- with transaction.atomic():
- _ = CollectionManager().create_collection_view(
- collection = collection,
- location_view = location_view,
- )
- return
-
- @classmethod
- def set_location_view_order( cls, location_view_id_list : List[int] ):
-
- item_id_to_order_id = {
- item_id: order_id for order_id, item_id in enumerate( location_view_id_list )
- }
-
- location_view_queryset = LocationView.objects.filter( id__in = location_view_id_list )
- with transaction.atomic():
- for location_view in location_view_queryset:
- location_view.order_id = item_id_to_order_id.get( location_view.id )
- location_view.save()
- continue
- return
-
diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.py
index c6e5372a..4d4191fc 100644
--- a/src/hi/apps/location/edit/views.py
+++ b/src/hi/apps/location/edit/views.py
@@ -18,7 +18,6 @@ import hi.apps.entity.views as entity_views
from hi.apps.entity.models import Entity
from hi.apps.location.forms import LocationItemPositionForm
from hi.apps.location.location_manager import LocationManager
-from hi.apps.location.location_view_manager import LocationViewManager
from hi.apps.location.models import Location, LocationView
from hi.apps.location.svg_item_factory import SvgItemFactory
import hi.apps.location.views as location_views
@@ -34,7 +33,6 @@ from hi.views import (
from hi.constants import DIVID
from . import forms
-from .helpers import LocationEditHelpers
logger = logging.getLogger(__name__)
@@ -163,7 +161,7 @@ class LocationViewAddView( View ):
location = Location.objects.order_by( 'order_id' ).first()
try:
- location_view = LocationViewManager().create_location_view(
+ location_view = LocationManager().create_location_view(
location = location,
name = location_view_form.cleaned_data.get('name'),
)
@@ -219,7 +217,7 @@ class LocationViewReorder( View ):
if not location_view_id_list:
return bad_request_response( request, message = 'Missing location view ids.' )
- LocationEditHelpers.set_location_view_order(
+ LocationManager().set_location_view_order(
location_view_id_list = location_view_id_list,
)
return antinode.response( main_content = 'OK' )
@@ -283,10 +281,11 @@ class LocationViewAddRemoveItemView( View ):
location_view = request.view_parameters.location_view
- entity_view_group_list = LocationEditHelpers.create_entity_view_group_list(
+ location_manager = LocationManager()
+ entity_view_group_list = location_manager.create_entity_view_group_list(
location_view = location_view,
)
- collection_view_group = LocationEditHelpers.create_collection_view_group(
+ collection_view_group = location_manager.create_collection_view_group(
location_view = location_view,
)
@@ -313,7 +312,7 @@ class LocationViewEntityToggleView( View ):
entity = Entity.objects.get( id = entity_id )
location_view = LocationView.objects.get( id = location_view_id )
- exists_in_view = LocationEditHelpers.toggle_entity_in_view(
+ exists_in_view = LocationManager().toggle_entity_in_view(
entity = entity,
location_view = location_view,
)
@@ -326,7 +325,7 @@ class LocationViewEntityToggleView( View ):
template = get_template( 'location/edit/panes/location_view_entity_toggle.html' )
main_content = template.render( context, request = request )
- location_view_data = LocationViewManager().get_location_view_data(
+ location_view_data = LocationManager().get_location_view_data(
location_view = location_view,
)
context = {
@@ -353,7 +352,7 @@ class LocationViewCollectionToggleView( View ):
collection = Collection.objects.get( id = collection_id )
location_view = LocationView.objects.get( id = location_view_id )
- exists_in_view = LocationEditHelpers.toggle_collection_in_view(
+ exists_in_view = LocationManager().toggle_collection_in_view(
collection = collection,
location_view = location_view,
)
@@ -366,7 +365,7 @@ class LocationViewCollectionToggleView( View ):
template = get_template( 'location/edit/panes/location_view_collection_toggle.html' )
main_content = template.render( context, request = request )
- location_view_data = LocationViewManager().get_location_view_data(
+ location_view_data = LocationManager().get_location_view_data(
location_view = location_view,
)
context = {
diff --git a/src/hi/apps/location/location_manager.py b/src/hi/apps/location/location_manager.py
index 691a8a3b..32ec2dd5 100644
--- a/src/hi/apps/location/location_manager.py
+++ b/src/hi/apps/location/location_manager.py
@@ -1,17 +1,37 @@
+from decimal import Decimal
import os
+from typing import List
from django.core.files.storage import default_storage, FileSystemStorage
from django.db import transaction
+from hi.apps.collection.collection_manager import CollectionManager
+from hi.apps.collection.models import (
+ Collection,
+ CollectionView,
+)
from hi.apps.common.singleton import Singleton
from hi.apps.common.svg_models import SvgViewBox
+from hi.apps.entity.delegation_manager import DelegationManager
+from hi.apps.entity.entity_manager import EntityManager
+from hi.apps.entity.models import (
+ Entity,
+ EntityView,
+)
+from .enums import LocationViewType
from .location_detail_data import LocationDetailData
-from .location_view_manager import LocationViewManager
+from .location_view_data import LocationViewData
from .models import (
Location,
LocationView,
)
+from .transient_models import (
+ EntityViewGroup,
+ EntityViewItem,
+ CollectionViewItem,
+ CollectionViewGroup,
+)
class LocationManager(Singleton):
@@ -45,7 +65,7 @@ class LocationManager(Singleton):
order_id = order_id,
)
- _ = LocationViewManager().create_location_view(
+ _ = self.create_location_view(
location = location,
name = self.INITIAL_LOCATION_VIEW_NAME,
)
@@ -66,3 +86,230 @@ class LocationManager(Singleton):
location = location,
)
+ def create_location_view( self,
+ location : Location,
+ name : str ) -> LocationView:
+
+ last_location_view = location.views.order_by( '-order_id' ).first()
+ if last_location_view:
+ order_id = last_location_view.order_id + 1
+ else:
+ order_id = 0
+
+ return LocationView.objects.create(
+ location = location,
+ location_view_type_str = LocationViewType.default(),
+ name = name,
+ svg_view_box_str = str( location.svg_view_box ),
+ svg_rotate = Decimal( 0.0 ),
+ order_id = order_id,
+ )
+
+ def get_location_view_data( self, location_view : LocationView ):
+
+ location = location_view.location
+ entity_positions = list()
+ entity_paths = list()
+ non_displayed_entities = set()
+ for entity_view in location_view.entity_views.all():
+ entity = entity_view.entity
+ is_visible = False
+ entity_position = entity.positions.filter( location = location ).first()
+ if entity_position:
+ is_visible = True
+ entity_positions.append( entity_position )
+ entity_path = entity.paths.filter( location = location ).first()
+ if entity_path:
+ is_visible = True
+ entity_paths.append( entity_path )
+ if not is_visible:
+ non_displayed_entities.add( entity )
+ continue
+
+ collection_positions = list()
+ collection_paths = list()
+ unpositioned_collections = list()
+ for collection_view in location_view.collection_views.all():
+ collection = collection_view.collection
+ collection_position = collection.positions.filter( location = location ).first()
+ if collection_position:
+ collection_positions.append( collection_position )
+ else:
+ unpositioned_collections.append( collection )
+ collection_path = collection.paths.filter( location = location ).first()
+ if collection_path:
+ collection_paths.append( collection_path )
+ continue
+
+ # These are used for reporting entities that might otherwise be
+ # invisible to the user. (not displayed on SVG and nor part of any
+ # viewable collection).
+ #
+ orphan_entities = set()
+ for entity in non_displayed_entities:
+ if not entity.collections.exists():
+ orphan_entities.add( entity )
+ continue
+
+ # These become bottom buttons, which can be ordered
+ unpositioned_collections.sort( key = lambda item : item.order_id )
+
+ return LocationViewData(
+ location_view = location_view,
+ entity_positions = entity_positions,
+ entity_paths = entity_paths,
+ collection_positions = collection_positions,
+ collection_paths = collection_paths,
+ unpositioned_collections = unpositioned_collections,
+ orphan_entities = orphan_entities,
+ )
+
+ def create_entity_view_group_list( self, location_view : LocationView ) -> List[EntityViewGroup]:
+
+ entity_queryset = Entity.objects.all()
+
+ entity_view_group_dict = dict()
+ for entity in entity_queryset:
+
+ exists_in_view = False
+ for entity_view in entity.entity_views.all():
+ if entity_view.location_view == location_view:
+ exists_in_view = True
+ break
+ continue
+
+ entity_view_item = EntityViewItem(
+ entity = entity,
+ exists_in_view = exists_in_view,
+ )
+
+ if entity.entity_type not in entity_view_group_dict:
+ entity_view_group = EntityViewGroup(
+ location_view = location_view,
+ entity_type = entity.entity_type,
+ )
+ entity_view_group_dict[entity.entity_type] = entity_view_group
+ entity_view_group_dict[entity.entity_type].item_list.append( entity_view_item )
+ continue
+
+ entity_view_group_list = list( entity_view_group_dict.values() )
+ entity_view_group_list.sort( key = lambda item : item.entity_type.label )
+ return entity_view_group_list
+
+ def create_collection_view_group( self, location_view : LocationView ) -> CollectionViewGroup:
+
+ collection_queryset = Collection.objects.all()
+
+ collection_view_group = CollectionViewGroup(
+ location_view = location_view,
+ )
+ for collection in collection_queryset:
+
+ exists_in_view = False
+ for collection_view in collection.collection_views.all():
+ if collection_view.location_view == location_view:
+ exists_in_view = True
+ break
+ continue
+
+ collection_view_item = CollectionViewItem(
+ collection = collection,
+ exists_in_view = exists_in_view,
+ )
+ collection_view_group.item_list.append( collection_view_item )
+ continue
+
+ collection_view_group.item_list.sort( key = lambda item : item.collection.name )
+ return collection_view_group
+
+ def toggle_entity_in_view( self, entity : Entity, location_view : LocationView ) -> bool:
+
+ try:
+ self.remove_entity_from_view( entity = entity, location_view = location_view )
+ return False
+ except EntityView.DoesNotExist:
+ self.add_entity_to_view( entity = entity, location_view = location_view )
+ return True
+
+ def remove_entity_from_view( self, entity : Entity, location_view : LocationView ):
+
+ with transaction.atomic():
+ EntityManager().remove_entity_view(
+ entity = entity,
+ location_view = location_view,
+ )
+
+ DelegationManager().remove_delegate_entities_from_view_if_needed(
+ entity = entity,
+ location_view = location_view,
+ )
+
+ return
+
+ def add_entity_to_view_by_id( self, entity : Entity, location_view_id : int ):
+ location_view = LocationView.objects.get( id = location_view_id )
+ self.add_entity_to_view( entity = entity, location_view = location_view )
+ return
+
+ def add_entity_to_view( self, entity : Entity, location_view : LocationView ):
+
+ with transaction.atomic():
+ # Only create delegate entities the first time an entity is added to a view.
+ if not entity.entity_views.all().exists():
+ delegate_entity_list = DelegationManager().get_delegate_entities_with_defaults(
+ entity = entity,
+ )
+ else:
+ delegate_entity_list = DelegationManager().get_delegate_entities(
+ entity = entity,
+ )
+
+ _ = EntityManager().create_entity_view(
+ entity = entity,
+ location_view = location_view,
+ )
+
+ for delegate_entity in delegate_entity_list:
+ _ = EntityManager().create_entity_view(
+ entity = delegate_entity,
+ location_view = location_view,
+ )
+ continue
+
+ return
+
+ def toggle_collection_in_view( self,
+ collection : Collection,
+ location_view : LocationView ) -> bool:
+
+ try:
+ with transaction.atomic():
+ CollectionManager().remove_collection_view(
+ collection = collection,
+ location_view = location_view,
+ )
+ return False
+
+ except CollectionView.DoesNotExist:
+ with transaction.atomic():
+ _ = CollectionManager().create_collection_view(
+ collection = collection,
+ location_view = location_view,
+ )
+ return True
+
+ def set_location_view_order( self, location_view_id_list : List[int] ):
+
+ item_id_to_order_id = {
+ item_id: order_id for order_id, item_id in enumerate( location_view_id_list )
+ }
+
+ location_view_queryset = LocationView.objects.filter( id__in = location_view_id_list )
+ with transaction.atomic():
+ for location_view in location_view_queryset:
+ location_view.order_id = item_id_to_order_id.get( location_view.id )
+ location_view.save()
+ continue
+ return
+
+
diff --git a/src/hi/apps/location/location_view_manager.py b/src/hi/apps/location/location_view_manager.py
deleted file mode 100644
index f5b24e98..00000000
--- a/src/hi/apps/location/location_view_manager.py
+++ /dev/null
@@ -1,91 +0,0 @@
-from decimal import Decimal
-
-from hi.apps.common.singleton import Singleton
-
-from .enums import LocationViewType
-from .location_view_data import LocationViewData
-from .models import Location, LocationView
-
-
-class LocationViewManager(Singleton):
-
- def __init_singleton__(self):
- return
-
- def create_location_view( self,
- location : Location,
- name : str ) -> LocationView:
-
- last_location_view = location.views.order_by( '-order_id' ).first()
- if last_location_view:
- order_id = last_location_view.order_id + 1
- else:
- order_id = 0
-
- return LocationView.objects.create(
- location = location,
- location_view_type_str = LocationViewType.default(),
- name = name,
- svg_view_box_str = str( location.svg_view_box ),
- svg_rotate = Decimal( 0.0 ),
- order_id = order_id,
- )
-
- def get_location_view_data( self, location_view : LocationView ):
-
- location = location_view.location
- entity_positions = list()
- entity_paths = list()
- non_displayed_entities = set()
- for entity_view in location_view.entity_views.all():
- entity = entity_view.entity
- is_visible = False
- entity_position = entity.positions.filter( location = location ).first()
- if entity_position:
- is_visible = True
- entity_positions.append( entity_position )
- entity_path = entity.paths.filter( location = location ).first()
- if entity_path:
- is_visible = True
- entity_paths.append( entity_path )
- if not is_visible:
- non_displayed_entities.add( entity )
- continue
-
- collection_positions = list()
- collection_paths = list()
- unpositioned_collections = list()
- for collection_view in location_view.collection_views.all():
- collection = collection_view.collection
- collection_position = collection.positions.filter( location = location ).first()
- if collection_position:
- collection_positions.append( collection_position )
- else:
- unpositioned_collections.append( collection )
- collection_path = collection.paths.filter( location = location ).first()
- if collection_path:
- collection_paths.append( collection_path )
- continue
-
- # These are used for reporting entities that might otherwise be
- # invisible to the user. (not displayed on SVG and nor part of any
- # viewable collection).
- #
- orphan_entities = set()
- for entity in non_displayed_entities:
- if not entity.collections.exists():
- orphan_entities.add( entity )
- continue
-
- # These become bottom buttons, which can be ordered
- unpositioned_collections.sort( key = lambda item : item.order_id )
-
- return LocationViewData(
- location_view = location_view,
- entity_positions = entity_positions,
- entity_paths = entity_paths,
- collection_positions = collection_positions,
- collection_paths = collection_paths,
- unpositioned_collections = unpositioned_collections,
- orphan_entities = orphan_entities,
- )
diff --git a/src/hi/apps/location/edit/transient_models.py b/src/hi/apps/location/transient_models.py
similarity index 93%
rename from src/hi/apps/location/edit/transient_models.py
rename to src/hi/apps/location/transient_models.py
index 3b4e9d16..0ae5bca1 100644
--- a/src/hi/apps/location/edit/transient_models.py
+++ b/src/hi/apps/location/transient_models.py
@@ -4,7 +4,8 @@ from typing import List
from hi.apps.collection.models import Collection
from hi.apps.entity.enums import EntityType
from hi.apps.entity.models import Entity
-from hi.apps.location.models import LocationView
+
+from .models import LocationView
@dataclass
diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.py
index 3cc1622d..3f3fe660 100644
--- a/src/hi/apps/location/views.py
+++ b/src/hi/apps/location/views.py
@@ -14,7 +14,6 @@ from hi.hi_grid_view import HiGridView
from hi.views import bad_request_response, page_not_found_response
from .location_manager import LocationManager
-from .location_view_manager import LocationViewManager
from .models import Location, LocationView
logger = logging.getLogger(__name__)
@@ -104,7 +103,7 @@ class LocationViewView( HiGridView ):
sync_url = reverse( 'location_view', kwargs = kwargs )
return antinode.redirect_response( url = sync_url )
- location_view_data = LocationViewManager().get_location_view_data(
+ location_view_data = LocationManager().get_location_view_data(
location_view = location_view,
)
context = {