We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Column types not showing within shared tables and explorations
## Steps to reproduce

1. Create a table share
1. View the shared table page
1. Within the column header cells, expect to see type icons alongside the column names.
1. Instead, observe question marks for all columns.
1. Expect to see numbers right-aligned.
1. Instead, see numbers left-aligned (because they are being treated as text).

    ![image](https://github.com/mathesar-foundation/mathesar/assets/42411/e62571db-5f2a-4647-8a66-6bbfa3869e58)



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `mathesar/views.py`
Content:
```
1 from django.conf import settings
2 from django.contrib.auth.decorators import login_required
3 from django.shortcuts import render, redirect, get_object_or_404
4 from rest_framework import status
5 from rest_framework.decorators import api_view
6 from rest_framework.response import Response
7 
8 from demo.utils import get_is_live_demo_mode, get_live_demo_db_name
9 
10 from mathesar.api.db.permissions.database import DatabaseAccessPolicy
11 from mathesar.api.db.permissions.query import QueryAccessPolicy
12 from mathesar.api.db.permissions.schema import SchemaAccessPolicy
13 from mathesar.api.db.permissions.table import TableAccessPolicy
14 from mathesar.api.serializers.databases import ConnectionSerializer, TypeSerializer
15 from mathesar.api.serializers.schemas import SchemaSerializer
16 from mathesar.api.serializers.tables import TableSerializer
17 from mathesar.api.serializers.queries import QuerySerializer
18 from mathesar.api.ui.serializers.users import UserSerializer
19 from mathesar.api.utils import is_valid_uuid_v4
20 from mathesar.database.types import UIType
21 from mathesar.models.base import Database, Schema, Table
22 from mathesar.models.query import UIQuery
23 from mathesar.models.shares import SharedTable, SharedQuery
24 from mathesar.state import reset_reflection
25 from mathesar import __version__
26 
27 
28 def get_schema_list(request, database):
29     qs = Schema.objects.filter(database=database)
30     permission_restricted_qs = SchemaAccessPolicy.scope_queryset(request, qs)
31     schema_serializer = SchemaSerializer(
32         permission_restricted_qs,
33         many=True,
34         context={'request': request}
35     )
36     return schema_serializer.data
37 
38 
39 def _get_permissible_db_queryset(request):
40     """
41     Returns the queryset for connections a user is permitted to access.
42 
43     Note, connections that a user is permitted to access is the union of those
44     permitted by DatabaseAccessPolicy and those containing Schemas permitted by
45     SchemaAccessPolicy.
46 
47     Note, the live demo mode is an exception where the user is only permitted to
48     access the database generated for him. We treat that as a subset of the
49     connections the user can normally access, just in case someone finds a way
50     to manipulate how we define whether we're in demo mode and which db is a
51     user's demo db.
52     """
53     for deleted in (True, False):
54         dbs_qs = Database.objects.filter(deleted=deleted)
55         permitted_dbs_qs = DatabaseAccessPolicy.scope_queryset(request, dbs_qs)
56         schemas_qs = Schema.objects.all()
57         permitted_schemas_qs = SchemaAccessPolicy.scope_queryset(request, schemas_qs)
58         dbs_containing_permitted_schemas_qs = Database.objects.filter(schemas__in=permitted_schemas_qs, deleted=deleted)
59         permitted_dbs_qs = permitted_dbs_qs | dbs_containing_permitted_schemas_qs
60         permitted_dbs_qs = permitted_dbs_qs.distinct()
61         if get_is_live_demo_mode():
62             live_demo_db_name = get_live_demo_db_name(request)
63             if live_demo_db_name:
64                 permitted_dbs_qs = permitted_dbs_qs.filter(name=live_demo_db_name)
65             else:
66                 raise Exception('This should never happen')
67         if deleted:
68             failed_permitted_dbs_qs = permitted_dbs_qs
69         else:
70             successful_permitted_dbs_qs = permitted_dbs_qs
71     return successful_permitted_dbs_qs, failed_permitted_dbs_qs
72 
73 
74 def get_database_list(request):
75     permission_restricted_db_qs, permission_restricted_failed_db_qs = _get_permissible_db_queryset(request)
76     database_serializer = ConnectionSerializer(
77         permission_restricted_db_qs,
78         many=True,
79         context={'request': request}
80     )
81     failed_db_data = []
82     for db in permission_restricted_failed_db_qs:
83         failed_db_data.append({
84             'id': db.id,
85             'username': db.username,
86             'port': db.port,
87             'host': db.host,
88             'nickname': db.name,
89             'database': db.db_name,
90             'error': 'Error connecting to the database'
91         })
92     return database_serializer.data + failed_db_data
93 
94 
95 def get_table_list(request, schema):
96     if schema is None:
97         return []
98     qs = Table.objects.filter(schema=schema)
99     permission_restricted_qs = TableAccessPolicy.scope_queryset(request, qs)
100     table_serializer = TableSerializer(
101         permission_restricted_qs,
102         many=True,
103         context={'request': request}
104     )
105     return table_serializer.data
106 
107 
108 def get_queries_list(request, schema):
109     if schema is None:
110         return []
111     qs = UIQuery.objects.filter(base_table__schema=schema)
112     permission_restricted_qs = QueryAccessPolicy.scope_queryset(request, qs)
113 
114     query_serializer = QuerySerializer(
115         permission_restricted_qs,
116         many=True,
117         context={'request': request}
118     )
119     return query_serializer.data
120 
121 
122 def get_ui_type_list(request, database):
123     if database is None:
124         return []
125     type_serializer = TypeSerializer(
126         UIType,
127         many=True,
128         context={'request': request}
129     )
130     return type_serializer.data
131 
132 
133 def get_user_data(request):
134     user_serializer = UserSerializer(
135         request.user,
136         many=False,
137         context={'request': request}
138     )
139     return user_serializer.data
140 
141 
142 def get_base_data_all_routes(request, database=None, schema=None):
143     return {
144         'current_connection': database.id if database else None,
145         'current_schema': schema.id if schema else None,
146         'schemas': [],
147         'connections': [],
148         'tables': [],
149         'queries': [],
150         'abstract_types': get_ui_type_list(request, database),
151         'user': get_user_data(request),
152         'is_authenticated': not request.user.is_anonymous,
153         'live_demo_mode': get_is_live_demo_mode(),
154         'current_release_tag_name': __version__,
155         'internal_db_connection': _get_internal_db_meta(),
156     }
157 
158 
159 def _get_internal_db_meta():
160     internal_db = Database.create_from_settings_key('default')
161     if internal_db is not None:
162         return {
163             'type': 'postgres',
164             'user': internal_db.username,
165             'host': internal_db.host,
166             'port': internal_db.port,
167             'database': internal_db.db_name
168         }
169     else:
170         return {'type': 'sqlite'}
171 
172 
173 def get_common_data(request, database=None, schema=None):
174     return {
175         **get_base_data_all_routes(request, database, schema),
176         'schemas': get_schema_list(request, database),
177         'connections': get_database_list(request),
178         'tables': get_table_list(request, schema),
179         'queries': get_queries_list(request, schema),
180         'supported_languages': dict(getattr(settings, 'LANGUAGES', [])),
181         'routing_context': 'normal',
182     }
183 
184 
185 def get_current_database(request, connection_id):
186     """Get database from passed name, with fall back behavior."""
187     successful_dbs, failed_dbs = _get_permissible_db_queryset(request)
188     permitted_databases = successful_dbs | failed_dbs
189     if connection_id is not None:
190         current_database = get_object_or_404(permitted_databases, id=connection_id)
191     else:
192         request_database_name = get_live_demo_db_name(request)
193         try:
194             if request_database_name is not None:
195                 # Try to get the database named specified in the request
196                 current_database = permitted_databases.get(name=request_database_name)
197             else:
198                 # Try to get the first database available
199                 current_database = permitted_databases.order_by('id').first()
200         except Database.DoesNotExist:
201             current_database = None
202     return current_database
203 
204 
205 def get_current_schema(request, schema_id, database):
206     # if there's a schema ID passed in, try to retrieve the schema, or return a 404 error.
207     if schema_id is not None:
208         permitted_schemas = SchemaAccessPolicy.scope_queryset(request, Schema.objects.all())
209         return get_object_or_404(permitted_schemas, id=schema_id)
210     else:
211         try:
212             # Try to get the first schema in the DB
213             return Schema.objects.filter(database=database).order_by('id').first()
214         except Schema.DoesNotExist:
215             return None
216 
217 
218 def render_schema(request, database, schema):
219     # if there's no schema available, redirect to the schemas page.
220     if not schema:
221         return redirect('schemas', db_name=database.name)
222     else:
223         # We are redirecting so that the correct URL is passed to the frontend.
224         return redirect('schema_home', db_name=database.name, schema_id=schema.id)
225 
226 
227 def get_common_data_for_shared_entity(request, schema=None):
228     database = schema.database if schema else None
229     schemas = [schema] if schema else []
230     databases = [database] if database else []
231     serialized_schemas = SchemaSerializer(
232         schemas,
233         many=True,
234         context={'request': request}
235     ).data
236     serialized_databases = ConnectionSerializer(
237         databases,
238         many=True,
239         context={'request': request}
240     ).data
241     return {
242         **get_base_data_all_routes(request, database, schema),
243         'schemas': serialized_schemas,
244         'databases': serialized_databases,
245         'routing_context': 'anonymous',
246     }
247 
248 
249 def get_common_data_for_shared_table(request, table):
250     tables = [table] if table else []
251     serialized_tables = TableSerializer(
252         tables,
253         many=True,
254         context={'request': request}
255     ).data
256     schema = table.schema if table else None
257     return {
258         **get_common_data_for_shared_entity(request, schema),
259         'tables': serialized_tables,
260     }
261 
262 
263 def get_common_data_for_shared_query(request, query):
264     queries = [query] if query else []
265     serialized_queries = QuerySerializer(
266         queries,
267         many=True,
268         context={'request': request}
269     ).data
270     schema = query.base_table.schema if query else None
271     return {
272         **get_common_data_for_shared_entity(request, schema),
273         'queries': serialized_queries,
274     }
275 
276 
277 @login_required
278 @api_view(['POST'])
279 def reflect_all(_):
280     reset_reflection()
281     return Response(status=status.HTTP_200_OK)
282 
283 
284 @login_required
285 def home(request):
286     connection_list = get_database_list(request)
287     number_of_connections = len(connection_list)
288     if number_of_connections > 1:
289         return redirect('connections')
290     elif number_of_connections == 1:
291         db = connection_list[0]
292         return redirect('schemas', connection_id=db['id'])
293     else:
294         return render(request, 'mathesar/index.html', {
295             'common_data': get_common_data(request)
296         })
297 
298 
299 @login_required
300 def connections(request):
301     return render(request, 'mathesar/index.html', {
302         'common_data': get_common_data(request)
303     })
304 
305 
306 @login_required
307 def profile(request):
308     return render(request, 'mathesar/index.html', {
309         'common_data': get_common_data(request)
310     })
311 
312 
313 @login_required
314 def admin_home(request, **kwargs):
315     return render(request, 'mathesar/index.html', {
316         'common_data': get_common_data(request)
317     })
318 
319 
320 @login_required
321 def schema_home(request, connection_id, schema_id, **kwargs):
322     database = get_current_database(request, connection_id)
323     schema = get_current_schema(request, schema_id, database)
324     return render(request, 'mathesar/index.html', {
325         'common_data': get_common_data(request, database, schema)
326     })
327 
328 
329 @login_required
330 def schemas(request, connection_id):
331     database = get_current_database(request, connection_id)
332     return render(request, 'mathesar/index.html', {
333         'common_data': get_common_data(request, database, None)
334     })
335 
336 
337 def shared_table(request, slug):
338     shared_table_link = SharedTable.get_by_slug(slug) if is_valid_uuid_v4(slug) else None
339     table = shared_table_link.table if shared_table_link else None
340 
341     return render(request, 'mathesar/index.html', {
342         'common_data': get_common_data_for_shared_table(request, table),
343         'route_specific_data': {
344             'shared_table': {'table_id': table.id if table else None}
345         }
346     })
347 
348 
349 def shared_query(request, slug):
350     shared_query_link = SharedQuery.get_by_slug(slug) if is_valid_uuid_v4(slug) else None
351     query = shared_query_link.query if shared_query_link else None
352 
353     return render(request, 'mathesar/index.html', {
354         'common_data': get_common_data_for_shared_query(request, query),
355         'route_specific_data': {
356             'shared_query': {'query_id': query.id if query else None}
357         }
358     })
359 
360 
361 def page_not_found_view(request, exception):
362     return render(request, 'mathesar/index.html', {
363         'common_data': get_common_data(request),
364     }, status=404)
365
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/mathesar/views.py b/mathesar/views.py
--- a/mathesar/views.py
+++ b/mathesar/views.py
@@ -241,7 +241,7 @@
     return {
         **get_base_data_all_routes(request, database, schema),
         'schemas': serialized_schemas,
-        'databases': serialized_databases,
+        'connections': serialized_databases,
         'routing_context': 'anonymous',
     }

{"golden_diff": "diff --git a/mathesar/views.py b/mathesar/views.py\n--- a/mathesar/views.py\n+++ b/mathesar/views.py\n@@ -241,7 +241,7 @@\n     return {\n         **get_base_data_all_routes(request, database, schema),\n         'schemas': serialized_schemas,\n-        'databases': serialized_databases,\n+        'connections': serialized_databases,\n         'routing_context': 'anonymous',\n     }\n", "issue": "Column types not showing within shared tables and explorations\n## Steps to reproduce\n\n1. Create a table share\n1. View the shared table page\n1. Within the column header cells, expect to see type icons alongside the column names.\n1. Instead, observe question marks for all columns.\n1. Expect to see numbers right-aligned.\n1. Instead, see numbers left-aligned (because they are being treated as text).\n\n    ![image](https://github.com/mathesar-foundation/mathesar/assets/42411/e62571db-5f2a-4647-8a66-6bbfa3869e58)\n\n\n", "before_files": [{"content": "from django.conf import settings\nfrom django.contrib.auth.decorators import login_required\nfrom django.shortcuts import render, redirect, get_object_or_404\nfrom rest_framework import status\nfrom rest_framework.decorators import api_view\nfrom rest_framework.response import Response\n\nfrom demo.utils import get_is_live_demo_mode, get_live_demo_db_name\n\nfrom mathesar.api.db.permissions.database import DatabaseAccessPolicy\nfrom mathesar.api.db.permissions.query import QueryAccessPolicy\nfrom mathesar.api.db.permissions.schema import SchemaAccessPolicy\nfrom mathesar.api.db.permissions.table import TableAccessPolicy\nfrom mathesar.api.serializers.databases import ConnectionSerializer, TypeSerializer\nfrom mathesar.api.serializers.schemas import SchemaSerializer\nfrom mathesar.api.serializers.tables import TableSerializer\nfrom mathesar.api.serializers.queries import QuerySerializer\nfrom mathesar.api.ui.serializers.users import UserSerializer\nfrom mathesar.api.utils import is_valid_uuid_v4\nfrom mathesar.database.types import UIType\nfrom mathesar.models.base import Database, Schema, Table\nfrom mathesar.models.query import UIQuery\nfrom mathesar.models.shares import SharedTable, SharedQuery\nfrom mathesar.state import reset_reflection\nfrom mathesar import __version__\n\n\ndef get_schema_list(request, database):\n    qs = Schema.objects.filter(database=database)\n    permission_restricted_qs = SchemaAccessPolicy.scope_queryset(request, qs)\n    schema_serializer = SchemaSerializer(\n        permission_restricted_qs,\n        many=True,\n        context={'request': request}\n    )\n    return schema_serializer.data\n\n\ndef _get_permissible_db_queryset(request):\n    \"\"\"\n    Returns the queryset for connections a user is permitted to access.\n\n    Note, connections that a user is permitted to access is the union of those\n    permitted by DatabaseAccessPolicy and those containing Schemas permitted by\n    SchemaAccessPolicy.\n\n    Note, the live demo mode is an exception where the user is only permitted to\n    access the database generated for him. We treat that as a subset of the\n    connections the user can normally access, just in case someone finds a way\n    to manipulate how we define whether we're in demo mode and which db is a\n    user's demo db.\n    \"\"\"\n    for deleted in (True, False):\n        dbs_qs = Database.objects.filter(deleted=deleted)\n        permitted_dbs_qs = DatabaseAccessPolicy.scope_queryset(request, dbs_qs)\n        schemas_qs = Schema.objects.all()\n        permitted_schemas_qs = SchemaAccessPolicy.scope_queryset(request, schemas_qs)\n        dbs_containing_permitted_schemas_qs = Database.objects.filter(schemas__in=permitted_schemas_qs, deleted=deleted)\n        permitted_dbs_qs = permitted_dbs_qs | dbs_containing_permitted_schemas_qs\n        permitted_dbs_qs = permitted_dbs_qs.distinct()\n        if get_is_live_demo_mode():\n            live_demo_db_name = get_live_demo_db_name(request)\n            if live_demo_db_name:\n                permitted_dbs_qs = permitted_dbs_qs.filter(name=live_demo_db_name)\n            else:\n                raise Exception('This should never happen')\n        if deleted:\n            failed_permitted_dbs_qs = permitted_dbs_qs\n        else:\n            successful_permitted_dbs_qs = permitted_dbs_qs\n    return successful_permitted_dbs_qs, failed_permitted_dbs_qs\n\n\ndef get_database_list(request):\n    permission_restricted_db_qs, permission_restricted_failed_db_qs = _get_permissible_db_queryset(request)\n    database_serializer = ConnectionSerializer(\n        permission_restricted_db_qs,\n        many=True,\n        context={'request': request}\n    )\n    failed_db_data = []\n    for db in permission_restricted_failed_db_qs:\n        failed_db_data.append({\n            'id': db.id,\n            'username': db.username,\n            'port': db.port,\n            'host': db.host,\n            'nickname': db.name,\n            'database': db.db_name,\n            'error': 'Error connecting to the database'\n        })\n    return database_serializer.data + failed_db_data\n\n\ndef get_table_list(request, schema):\n    if schema is None:\n        return []\n    qs = Table.objects.filter(schema=schema)\n    permission_restricted_qs = TableAccessPolicy.scope_queryset(request, qs)\n    table_serializer = TableSerializer(\n        permission_restricted_qs,\n        many=True,\n        context={'request': request}\n    )\n    return table_serializer.data\n\n\ndef get_queries_list(request, schema):\n    if schema is None:\n        return []\n    qs = UIQuery.objects.filter(base_table__schema=schema)\n    permission_restricted_qs = QueryAccessPolicy.scope_queryset(request, qs)\n\n    query_serializer = QuerySerializer(\n        permission_restricted_qs,\n        many=True,\n        context={'request': request}\n    )\n    return query_serializer.data\n\n\ndef get_ui_type_list(request, database):\n    if database is None:\n        return []\n    type_serializer = TypeSerializer(\n        UIType,\n        many=True,\n        context={'request': request}\n    )\n    return type_serializer.data\n\n\ndef get_user_data(request):\n    user_serializer = UserSerializer(\n        request.user,\n        many=False,\n        context={'request': request}\n    )\n    return user_serializer.data\n\n\ndef get_base_data_all_routes(request, database=None, schema=None):\n    return {\n        'current_connection': database.id if database else None,\n        'current_schema': schema.id if schema else None,\n        'schemas': [],\n        'connections': [],\n        'tables': [],\n        'queries': [],\n        'abstract_types': get_ui_type_list(request, database),\n        'user': get_user_data(request),\n        'is_authenticated': not request.user.is_anonymous,\n        'live_demo_mode': get_is_live_demo_mode(),\n        'current_release_tag_name': __version__,\n        'internal_db_connection': _get_internal_db_meta(),\n    }\n\n\ndef _get_internal_db_meta():\n    internal_db = Database.create_from_settings_key('default')\n    if internal_db is not None:\n        return {\n            'type': 'postgres',\n            'user': internal_db.username,\n            'host': internal_db.host,\n            'port': internal_db.port,\n            'database': internal_db.db_name\n        }\n    else:\n        return {'type': 'sqlite'}\n\n\ndef get_common_data(request, database=None, schema=None):\n    return {\n        **get_base_data_all_routes(request, database, schema),\n        'schemas': get_schema_list(request, database),\n        'connections': get_database_list(request),\n        'tables': get_table_list(request, schema),\n        'queries': get_queries_list(request, schema),\n        'supported_languages': dict(getattr(settings, 'LANGUAGES', [])),\n        'routing_context': 'normal',\n    }\n\n\ndef get_current_database(request, connection_id):\n    \"\"\"Get database from passed name, with fall back behavior.\"\"\"\n    successful_dbs, failed_dbs = _get_permissible_db_queryset(request)\n    permitted_databases = successful_dbs | failed_dbs\n    if connection_id is not None:\n        current_database = get_object_or_404(permitted_databases, id=connection_id)\n    else:\n        request_database_name = get_live_demo_db_name(request)\n        try:\n            if request_database_name is not None:\n                # Try to get the database named specified in the request\n                current_database = permitted_databases.get(name=request_database_name)\n            else:\n                # Try to get the first database available\n                current_database = permitted_databases.order_by('id').first()\n        except Database.DoesNotExist:\n            current_database = None\n    return current_database\n\n\ndef get_current_schema(request, schema_id, database):\n    # if there's a schema ID passed in, try to retrieve the schema, or return a 404 error.\n    if schema_id is not None:\n        permitted_schemas = SchemaAccessPolicy.scope_queryset(request, Schema.objects.all())\n        return get_object_or_404(permitted_schemas, id=schema_id)\n    else:\n        try:\n            # Try to get the first schema in the DB\n            return Schema.objects.filter(database=database).order_by('id').first()\n        except Schema.DoesNotExist:\n            return None\n\n\ndef render_schema(request, database, schema):\n    # if there's no schema available, redirect to the schemas page.\n    if not schema:\n        return redirect('schemas', db_name=database.name)\n    else:\n        # We are redirecting so that the correct URL is passed to the frontend.\n        return redirect('schema_home', db_name=database.name, schema_id=schema.id)\n\n\ndef get_common_data_for_shared_entity(request, schema=None):\n    database = schema.database if schema else None\n    schemas = [schema] if schema else []\n    databases = [database] if database else []\n    serialized_schemas = SchemaSerializer(\n        schemas,\n        many=True,\n        context={'request': request}\n    ).data\n    serialized_databases = ConnectionSerializer(\n        databases,\n        many=True,\n        context={'request': request}\n    ).data\n    return {\n        **get_base_data_all_routes(request, database, schema),\n        'schemas': serialized_schemas,\n        'databases': serialized_databases,\n        'routing_context': 'anonymous',\n    }\n\n\ndef get_common_data_for_shared_table(request, table):\n    tables = [table] if table else []\n    serialized_tables = TableSerializer(\n        tables,\n        many=True,\n        context={'request': request}\n    ).data\n    schema = table.schema if table else None\n    return {\n        **get_common_data_for_shared_entity(request, schema),\n        'tables': serialized_tables,\n    }\n\n\ndef get_common_data_for_shared_query(request, query):\n    queries = [query] if query else []\n    serialized_queries = QuerySerializer(\n        queries,\n        many=True,\n        context={'request': request}\n    ).data\n    schema = query.base_table.schema if query else None\n    return {\n        **get_common_data_for_shared_entity(request, schema),\n        'queries': serialized_queries,\n    }\n\n\n@login_required\n@api_view(['POST'])\ndef reflect_all(_):\n    reset_reflection()\n    return Response(status=status.HTTP_200_OK)\n\n\n@login_required\ndef home(request):\n    connection_list = get_database_list(request)\n    number_of_connections = len(connection_list)\n    if number_of_connections > 1:\n        return redirect('connections')\n    elif number_of_connections == 1:\n        db = connection_list[0]\n        return redirect('schemas', connection_id=db['id'])\n    else:\n        return render(request, 'mathesar/index.html', {\n            'common_data': get_common_data(request)\n        })\n\n\n@login_required\ndef connections(request):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request)\n    })\n\n\n@login_required\ndef profile(request):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request)\n    })\n\n\n@login_required\ndef admin_home(request, **kwargs):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request)\n    })\n\n\n@login_required\ndef schema_home(request, connection_id, schema_id, **kwargs):\n    database = get_current_database(request, connection_id)\n    schema = get_current_schema(request, schema_id, database)\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request, database, schema)\n    })\n\n\n@login_required\ndef schemas(request, connection_id):\n    database = get_current_database(request, connection_id)\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request, database, None)\n    })\n\n\ndef shared_table(request, slug):\n    shared_table_link = SharedTable.get_by_slug(slug) if is_valid_uuid_v4(slug) else None\n    table = shared_table_link.table if shared_table_link else None\n\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data_for_shared_table(request, table),\n        'route_specific_data': {\n            'shared_table': {'table_id': table.id if table else None}\n        }\n    })\n\n\ndef shared_query(request, slug):\n    shared_query_link = SharedQuery.get_by_slug(slug) if is_valid_uuid_v4(slug) else None\n    query = shared_query_link.query if shared_query_link else None\n\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data_for_shared_query(request, query),\n        'route_specific_data': {\n            'shared_query': {'query_id': query.id if query else None}\n        }\n    })\n\n\ndef page_not_found_view(request, exception):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request),\n    }, status=404)\n", "path": "mathesar/views.py"}], "after_files": [{"content": "from django.conf import settings\nfrom django.contrib.auth.decorators import login_required\nfrom django.shortcuts import render, redirect, get_object_or_404\nfrom rest_framework import status\nfrom rest_framework.decorators import api_view\nfrom rest_framework.response import Response\n\nfrom demo.utils import get_is_live_demo_mode, get_live_demo_db_name\n\nfrom mathesar.api.db.permissions.database import DatabaseAccessPolicy\nfrom mathesar.api.db.permissions.query import QueryAccessPolicy\nfrom mathesar.api.db.permissions.schema import SchemaAccessPolicy\nfrom mathesar.api.db.permissions.table import TableAccessPolicy\nfrom mathesar.api.serializers.databases import ConnectionSerializer, TypeSerializer\nfrom mathesar.api.serializers.schemas import SchemaSerializer\nfrom mathesar.api.serializers.tables import TableSerializer\nfrom mathesar.api.serializers.queries import QuerySerializer\nfrom mathesar.api.ui.serializers.users import UserSerializer\nfrom mathesar.api.utils import is_valid_uuid_v4\nfrom mathesar.database.types import UIType\nfrom mathesar.models.base import Database, Schema, Table\nfrom mathesar.models.query import UIQuery\nfrom mathesar.models.shares import SharedTable, SharedQuery\nfrom mathesar.state import reset_reflection\nfrom mathesar import __version__\n\n\ndef get_schema_list(request, database):\n    qs = Schema.objects.filter(database=database)\n    permission_restricted_qs = SchemaAccessPolicy.scope_queryset(request, qs)\n    schema_serializer = SchemaSerializer(\n        permission_restricted_qs,\n        many=True,\n        context={'request': request}\n    )\n    return schema_serializer.data\n\n\ndef _get_permissible_db_queryset(request):\n    \"\"\"\n    Returns the queryset for connections a user is permitted to access.\n\n    Note, connections that a user is permitted to access is the union of those\n    permitted by DatabaseAccessPolicy and those containing Schemas permitted by\n    SchemaAccessPolicy.\n\n    Note, the live demo mode is an exception where the user is only permitted to\n    access the database generated for him. We treat that as a subset of the\n    connections the user can normally access, just in case someone finds a way\n    to manipulate how we define whether we're in demo mode and which db is a\n    user's demo db.\n    \"\"\"\n    for deleted in (True, False):\n        dbs_qs = Database.objects.filter(deleted=deleted)\n        permitted_dbs_qs = DatabaseAccessPolicy.scope_queryset(request, dbs_qs)\n        schemas_qs = Schema.objects.all()\n        permitted_schemas_qs = SchemaAccessPolicy.scope_queryset(request, schemas_qs)\n        dbs_containing_permitted_schemas_qs = Database.objects.filter(schemas__in=permitted_schemas_qs, deleted=deleted)\n        permitted_dbs_qs = permitted_dbs_qs | dbs_containing_permitted_schemas_qs\n        permitted_dbs_qs = permitted_dbs_qs.distinct()\n        if get_is_live_demo_mode():\n            live_demo_db_name = get_live_demo_db_name(request)\n            if live_demo_db_name:\n                permitted_dbs_qs = permitted_dbs_qs.filter(name=live_demo_db_name)\n            else:\n                raise Exception('This should never happen')\n        if deleted:\n            failed_permitted_dbs_qs = permitted_dbs_qs\n        else:\n            successful_permitted_dbs_qs = permitted_dbs_qs\n    return successful_permitted_dbs_qs, failed_permitted_dbs_qs\n\n\ndef get_database_list(request):\n    permission_restricted_db_qs, permission_restricted_failed_db_qs = _get_permissible_db_queryset(request)\n    database_serializer = ConnectionSerializer(\n        permission_restricted_db_qs,\n        many=True,\n        context={'request': request}\n    )\n    failed_db_data = []\n    for db in permission_restricted_failed_db_qs:\n        failed_db_data.append({\n            'id': db.id,\n            'username': db.username,\n            'port': db.port,\n            'host': db.host,\n            'nickname': db.name,\n            'database': db.db_name,\n            'error': 'Error connecting to the database'\n        })\n    return database_serializer.data + failed_db_data\n\n\ndef get_table_list(request, schema):\n    if schema is None:\n        return []\n    qs = Table.objects.filter(schema=schema)\n    permission_restricted_qs = TableAccessPolicy.scope_queryset(request, qs)\n    table_serializer = TableSerializer(\n        permission_restricted_qs,\n        many=True,\n        context={'request': request}\n    )\n    return table_serializer.data\n\n\ndef get_queries_list(request, schema):\n    if schema is None:\n        return []\n    qs = UIQuery.objects.filter(base_table__schema=schema)\n    permission_restricted_qs = QueryAccessPolicy.scope_queryset(request, qs)\n\n    query_serializer = QuerySerializer(\n        permission_restricted_qs,\n        many=True,\n        context={'request': request}\n    )\n    return query_serializer.data\n\n\ndef get_ui_type_list(request, database):\n    if database is None:\n        return []\n    type_serializer = TypeSerializer(\n        UIType,\n        many=True,\n        context={'request': request}\n    )\n    return type_serializer.data\n\n\ndef get_user_data(request):\n    user_serializer = UserSerializer(\n        request.user,\n        many=False,\n        context={'request': request}\n    )\n    return user_serializer.data\n\n\ndef get_base_data_all_routes(request, database=None, schema=None):\n    return {\n        'current_connection': database.id if database else None,\n        'current_schema': schema.id if schema else None,\n        'schemas': [],\n        'connections': [],\n        'tables': [],\n        'queries': [],\n        'abstract_types': get_ui_type_list(request, database),\n        'user': get_user_data(request),\n        'is_authenticated': not request.user.is_anonymous,\n        'live_demo_mode': get_is_live_demo_mode(),\n        'current_release_tag_name': __version__,\n        'internal_db_connection': _get_internal_db_meta(),\n    }\n\n\ndef _get_internal_db_meta():\n    internal_db = Database.create_from_settings_key('default')\n    if internal_db is not None:\n        return {\n            'type': 'postgres',\n            'user': internal_db.username,\n            'host': internal_db.host,\n            'port': internal_db.port,\n            'database': internal_db.db_name\n        }\n    else:\n        return {'type': 'sqlite'}\n\n\ndef get_common_data(request, database=None, schema=None):\n    return {\n        **get_base_data_all_routes(request, database, schema),\n        'schemas': get_schema_list(request, database),\n        'connections': get_database_list(request),\n        'tables': get_table_list(request, schema),\n        'queries': get_queries_list(request, schema),\n        'supported_languages': dict(getattr(settings, 'LANGUAGES', [])),\n        'routing_context': 'normal',\n    }\n\n\ndef get_current_database(request, connection_id):\n    \"\"\"Get database from passed name, with fall back behavior.\"\"\"\n    successful_dbs, failed_dbs = _get_permissible_db_queryset(request)\n    permitted_databases = successful_dbs | failed_dbs\n    if connection_id is not None:\n        current_database = get_object_or_404(permitted_databases, id=connection_id)\n    else:\n        request_database_name = get_live_demo_db_name(request)\n        try:\n            if request_database_name is not None:\n                # Try to get the database named specified in the request\n                current_database = permitted_databases.get(name=request_database_name)\n            else:\n                # Try to get the first database available\n                current_database = permitted_databases.order_by('id').first()\n        except Database.DoesNotExist:\n            current_database = None\n    return current_database\n\n\ndef get_current_schema(request, schema_id, database):\n    # if there's a schema ID passed in, try to retrieve the schema, or return a 404 error.\n    if schema_id is not None:\n        permitted_schemas = SchemaAccessPolicy.scope_queryset(request, Schema.objects.all())\n        return get_object_or_404(permitted_schemas, id=schema_id)\n    else:\n        try:\n            # Try to get the first schema in the DB\n            return Schema.objects.filter(database=database).order_by('id').first()\n        except Schema.DoesNotExist:\n            return None\n\n\ndef render_schema(request, database, schema):\n    # if there's no schema available, redirect to the schemas page.\n    if not schema:\n        return redirect('schemas', db_name=database.name)\n    else:\n        # We are redirecting so that the correct URL is passed to the frontend.\n        return redirect('schema_home', db_name=database.name, schema_id=schema.id)\n\n\ndef get_common_data_for_shared_entity(request, schema=None):\n    database = schema.database if schema else None\n    schemas = [schema] if schema else []\n    databases = [database] if database else []\n    serialized_schemas = SchemaSerializer(\n        schemas,\n        many=True,\n        context={'request': request}\n    ).data\n    serialized_databases = ConnectionSerializer(\n        databases,\n        many=True,\n        context={'request': request}\n    ).data\n    return {\n        **get_base_data_all_routes(request, database, schema),\n        'schemas': serialized_schemas,\n        'connections': serialized_databases,\n        'routing_context': 'anonymous',\n    }\n\n\ndef get_common_data_for_shared_table(request, table):\n    tables = [table] if table else []\n    serialized_tables = TableSerializer(\n        tables,\n        many=True,\n        context={'request': request}\n    ).data\n    schema = table.schema if table else None\n    return {\n        **get_common_data_for_shared_entity(request, schema),\n        'tables': serialized_tables,\n    }\n\n\ndef get_common_data_for_shared_query(request, query):\n    queries = [query] if query else []\n    serialized_queries = QuerySerializer(\n        queries,\n        many=True,\n        context={'request': request}\n    ).data\n    schema = query.base_table.schema if query else None\n    return {\n        **get_common_data_for_shared_entity(request, schema),\n        'queries': serialized_queries,\n    }\n\n\n@login_required\n@api_view(['POST'])\ndef reflect_all(_):\n    reset_reflection()\n    return Response(status=status.HTTP_200_OK)\n\n\n@login_required\ndef home(request):\n    connection_list = get_database_list(request)\n    number_of_connections = len(connection_list)\n    if number_of_connections > 1:\n        return redirect('connections')\n    elif number_of_connections == 1:\n        db = connection_list[0]\n        return redirect('schemas', connection_id=db['id'])\n    else:\n        return render(request, 'mathesar/index.html', {\n            'common_data': get_common_data(request)\n        })\n\n\n@login_required\ndef connections(request):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request)\n    })\n\n\n@login_required\ndef profile(request):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request)\n    })\n\n\n@login_required\ndef admin_home(request, **kwargs):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request)\n    })\n\n\n@login_required\ndef schema_home(request, connection_id, schema_id, **kwargs):\n    database = get_current_database(request, connection_id)\n    schema = get_current_schema(request, schema_id, database)\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request, database, schema)\n    })\n\n\n@login_required\ndef schemas(request, connection_id):\n    database = get_current_database(request, connection_id)\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request, database, None)\n    })\n\n\ndef shared_table(request, slug):\n    shared_table_link = SharedTable.get_by_slug(slug) if is_valid_uuid_v4(slug) else None\n    table = shared_table_link.table if shared_table_link else None\n\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data_for_shared_table(request, table),\n        'route_specific_data': {\n            'shared_table': {'table_id': table.id if table else None}\n        }\n    })\n\n\ndef shared_query(request, slug):\n    shared_query_link = SharedQuery.get_by_slug(slug) if is_valid_uuid_v4(slug) else None\n    query = shared_query_link.query if shared_query_link else None\n\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data_for_shared_query(request, query),\n        'route_specific_data': {\n            'shared_query': {'query_id': query.id if query else None}\n        }\n    })\n\n\ndef page_not_found_view(request, exception):\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request),\n    }, status=404)\n", "path": "mathesar/views.py"}]}