Home Information - Your home, your data, organized.
Python
Refactor of creating LocationView andn Collection.
Anthony Cassandraa6b89975017c
diff --git a/src/hi/apps/collection/edit/helpers.py b/src/hi/apps/collection/edit/helpers.pyindex dfe7430c..59971fd4 100644--- a/src/hi/apps/collection/edit/helpers.py+++ b/src/hi/apps/collection/edit/helpers.py@@ -1,5 +1,8 @@from typing import List+from django.http import HttpRequest++from hi.apps.collection.enums import CollectionTypefrom hi.apps.collection.models import (Collection,CollectionEntity,@@ -16,6 +19,18 @@ from .transient_models import (class CollectionEditHelpers:+ @classmethod+ def create_collection( cls,+ request : HttpRequest,+ name : str ) -> Collection:+ last_collection = Collection.objects.all().order_by( '-order_id' ).first()++ return Collection.objects.create(+ name = name,+ collection_type_str = CollectionType.default(),+ order_id = last_collection.order_id + 1,+ )+@classmethoddef create_entity_collection_group_list( cls, collection : Collection ) -> List[EntityCollectionGroup]:diff --git a/src/hi/apps/collection/edit/views.py b/src/hi/apps/collection/edit/views.pyindex 915c208c..9b01185f 100644--- a/src/hi/apps/collection/edit/views.py+++ b/src/hi/apps/collection/edit/views.py@@ -49,18 +49,13 @@ class CollectionAddView( View ):context = context,)- # Move to helper class- # Extend NameForm to add type dropdown---- last_collection = Collection.objects.all().order_by( '-order_id' ).first()-- collection = Collection.objects.create(- name = name_form.cleaned_data.get('name'),- collection_type_str = CollectionType.default(),- order_id = last_collection.order_id + 1,- )+ try:+ collection = CollectionEditHelpers.create_collection(+ request = request,+ name = name_form.cleaned_data.get('name'),+ )+ except ValueError as e:+ return bad_request_response( request, message = str(e) )request.view_parameters.view_type = ViewType.COLLECTIONrequest.view_parameters.collection_id = collection.iddiff --git a/src/hi/apps/location/edit/helpers.py b/src/hi/apps/location/edit/helpers.pyindex bc083bf4..baa05e49 100644--- a/src/hi/apps/location/edit/helpers.py+++ b/src/hi/apps/location/edit/helpers.py@@ -2,6 +2,7 @@ from decimal import Decimalfrom typing import Listfrom django.db import transaction+from django.http import HttpRequestfrom hi.apps.collection.models import (Collection,@@ -14,7 +15,8 @@ from hi.apps.entity.models import (EntityPosition,EntityPath,)-from hi.apps.location.models import LocationView+from hi.apps.location.enums import LocationViewType+from hi.apps.location.models import Location, LocationViewfrom .transient_models import (CollectionViewItem,@@ -26,6 +28,33 @@ from .transient_models import (class LocationEditHelpers:+ @classmethod+ def create_location_view( cls,+ request : HttpRequest,+ name : str ) -> LocationView:++ location = cls.get_location_for_new_location_view( request )+ if not location:+ raise ValueError( 'No locations defined.' )++ last_location_view = location.views.order_by( '-order_id' ).first()++ 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 = last_location_view.order_id + 1,+ )++ @classmethod+ def get_location_for_new_location_view( cls, request : HttpRequest ) -> Location:+ if request.view_parameters.location_view:+ return request.view_parameters.location_view.location+ return Location.objects.order_by( 'order_id' ).first()++@classmethoddef create_entity_view_group_list( cls, location_view : LocationView ) -> List[EntityViewGroup]:diff --git a/src/hi/apps/location/edit/views.py b/src/hi/apps/location/edit/views.pyindex 8b598127..00efce99 100644--- a/src/hi/apps/location/edit/views.py+++ b/src/hi/apps/location/edit/views.py@@ -1,7 +1,5 @@-from decimal import Decimalimport logging-from django.http import HttpRequestfrom django.shortcuts import redirectfrom django.template.loader import get_templatefrom django.urls import reverse@@ -12,9 +10,8 @@ from hi.apps.collection.models import Collectionimport hi.apps.common.antinode as antinodefrom hi.apps.edit.forms import NameFormfrom hi.apps.entity.models import Entity-from hi.apps.location.enums import LocationViewTypefrom hi.apps.location.location_view_manager import LocationViewManager-from hi.apps.location.models import Location, LocationView+from hi.apps.location.models import LocationViewfrom hi.decorators import edit_requiredfrom hi.enums import ViewTypefrom hi.views import bad_request_response@@ -52,29 +49,14 @@ class LocationViewAddView( View ):context = context,)+ try:+ location_view = LocationEditHelpers.create_location_view(+ request = request,+ name = name_form.cleaned_data.get('name'),+ )+ except ValueError as e:+ return bad_request_response( request, message = str(e) )--- # Move to helper class- # Extend NameForm to add type dropdown---- location = self.get_location_for_new_location_view( request )- if not location:- return bad_request_response( request, message = 'No locations defined.' )-- last_location_view = location.views.order_by( '-order_id' ).first()-- location_view = LocationView.objects.create(- location = location,- location_view_type_str = LocationViewType.default(),- name = name_form.cleaned_data.get('name'),- svg_view_box_str = str( location.svg_view_box ),- svg_rotate = Decimal( 0.0 ),- order_id = last_location_view.order_id + 1,- )-request.view_parameters.view_type = ViewType.LOCATION_VIEWrequest.view_parameters.location_view_id = location_view.idrequest.view_parameters.to_session( request )@@ -82,11 +64,6 @@ class LocationViewAddView( View ):redirect_url = reverse('home')return redirect( redirect_url )- def get_location_for_new_location_view( self, request : HttpRequest ) -> Location:- if request.view_parameters.location_view:- return request.view_parameters.location_view.location- return Location.objects.order_by( 'order_id' ).first()-@method_decorator( edit_required, name='dispatch' )class LocationViewDeleteView( View ):