diff --git a/umap/settings/base.py b/umap/settings/base.py index f9fb2c4e..9700fff8 100644 --- a/umap/settings/base.py +++ b/umap/settings/base.py @@ -306,3 +306,10 @@ LOGGING = { }, }, } + +# WebSocket configuration + +WEBSOCKET_ENABLED = False +WEBSOCKET_HOST = "localhost" +WEBSOCKET_PORT = 8001 +WEBSOCKET_URI = "ws://localhost:8001" diff --git a/umap/static/umap/js/umap.js b/umap/static/umap/js/umap.js index b2aec42e..bc60fa18 100644 --- a/umap/static/umap/js/umap.js +++ b/umap/static/umap/js/umap.js @@ -254,6 +254,7 @@ U.Map = L.Map.extend({ }, initSyncEngine: async function () { + if (this.options.websocketEnabled == false) return console.log('this.options.syncEnabled', this.options.syncEnabled) if (this.options.syncEnabled != true) { this.sync.stop() @@ -265,7 +266,7 @@ U.Map = L.Map.extend({ `/map/${this.options.umap_id}/ws-token/` ) if (!error) { - this.sync.start('ws://localhost:8001', response.token) + this.sync.start(this.options.websocketURI, response.token) } } }, @@ -1545,9 +1546,14 @@ U.Map = L.Map.extend({ editCaption: function () { if (!this.editEnabled) return if (this.options.editMode !== 'advanced') return - const container = L.DomUtil.create('div', 'umap-edit-container'), - metadataFields = ['options.name', 'options.description', 'options.syncEnabled'], - title = L.DomUtil.create('h3', '', container) + const container = L.DomUtil.create('div', 'umap-edit-container') + const metadataFields = ['options.name', 'options.description'] + + if (this.options.websocketEnabled) { + metadataFields.push('options.syncEnabled') + } + + const title = L.DomUtil.create('h3', '', container) title.textContent = L._('Edit map details') const builder = new U.FormBuilder(this, metadataFields, { className: 'map-metadata', diff --git a/umap/views.py b/umap/views.py index b3557d12..f37d5423 100644 --- a/umap/views.py +++ b/umap/views.py @@ -326,9 +326,9 @@ class UserDownload(DetailView, SearchMixin): zip_file.writestr(file_name, geojson_file.getvalue()) response = HttpResponse(zip_buffer.getvalue(), content_type="application/zip") - response[ - "Content-Disposition" - ] = 'attachment; filename="umap_backup_complete.zip"' + response["Content-Disposition"] = ( + 'attachment; filename="umap_backup_complete.zip"' + ) return response @@ -503,6 +503,8 @@ class MapDetailMixin: ], "umap_version": VERSION, "featuresHaveOwner": settings.UMAP_DEFAULT_FEATURES_HAVE_OWNERS, + "websocketEnabled": settings.WEBSOCKET_ENABLED, + "websocketURI": settings.WEBSOCKET_URI, } created = bool(getattr(self, "object", None)) if (created and self.object.owner) or (not created and not user.is_anonymous): @@ -623,8 +625,8 @@ class MapView(MapDetailMixin, PermissionsMixin, DetailView): def get_datalayers(self): return [ - l.metadata(self.request.user, self.request) - for l in self.object.datalayer_set.all() + dl.metadata(self.request.user, self.request) + for dl in self.object.datalayer_set.all() ] @property @@ -674,9 +676,9 @@ class MapDownload(DetailView): def render_to_response(self, context, *args, **kwargs): umapjson = self.object.generate_umapjson(self.request) response = simple_json_response(**umapjson) - response[ - "Content-Disposition" - ] = f'attachment; filename="umap_backup_{self.object.slug}.umap"' + response["Content-Disposition"] = ( + f'attachment; filename="umap_backup_{self.object.slug}.umap"' + ) return response diff --git a/umap/ws.py b/umap/ws.py index 76279b89..a2676393 100644 --- a/umap/ws.py +++ b/umap/ws.py @@ -98,8 +98,16 @@ async def handler(websocket): async def main(): - print("WebSocket server waiting for connections") - async with serve(handler, "localhost", 8001): + if not settings.WEBSOCKET_ENABLED: + print("WEBSOCKET_ENABLED should be set to True to run the WebSocket Server") + exit(1) + + async with serve(handler, settings.WEBSOCKET_HOST, settings.WEBSOCKET_PORT): + print( + ( + f"Waiting for connections on {settings.WEBSOCKET_HOST}:{settings.WEBSOCKET_PORT}" + ) + ) await asyncio.Future() # run forever