We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Remove "Short Name" field on user form
I'd like to remove the "Short Name" field on the user form. In a [Matrix discussion](https://matrix.to/#/!xInuTkBwjXZXYatIlm:matrix.mathesar.org/$2e6ULgkergV1BZwlHxpNBNUKcG4W-bnetG9bApfg23M?via=matrix.mathesar.org&via=matrix.org) @kgodey said this was fine. It's an optional field so we can keep it in the API but just remove the form UI until we have a use-case for short names.



--- 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/urls.py`
Content:
```
1 from django.contrib.auth.views import LoginView
2 from django.urls import include, path, re_path
3 from rest_framework_nested import routers
4 
5 from mathesar import views
6 from mathesar.api.db import viewsets as db_viewsets
7 from mathesar.api.ui import viewsets as ui_viewsets
8 from mathesar.users.password_reset import MathesarPasswordResetConfirmView
9 
10 db_router = routers.DefaultRouter()
11 db_router.register(r'tables', db_viewsets.TableViewSet, basename='table')
12 db_router.register(r'queries', db_viewsets.QueryViewSet, basename='query')
13 db_router.register(r'links', db_viewsets.LinkViewSet, basename='links')
14 db_router.register(r'schemas', db_viewsets.SchemaViewSet, basename='schema')
15 db_router.register(r'databases', db_viewsets.DatabaseViewSet, basename='database')
16 db_router.register(r'data_files', db_viewsets.DataFileViewSet, basename='data-file')
17 
18 db_table_router = routers.NestedSimpleRouter(db_router, r'tables', lookup='table')
19 db_table_router.register(r'records', db_viewsets.RecordViewSet, basename='table-record')
20 db_table_router.register(r'settings', db_viewsets.TableSettingsViewSet, basename='table-setting')
21 db_table_router.register(r'columns', db_viewsets.ColumnViewSet, basename='table-column')
22 db_table_router.register(r'constraints', db_viewsets.ConstraintViewSet, basename='table-constraint')
23 
24 ui_router = routers.DefaultRouter()
25 ui_router.register(r'version', ui_viewsets.VersionViewSet, basename='version')
26 ui_router.register(r'databases', ui_viewsets.DatabaseViewSet, basename='database')
27 ui_router.register(r'users', ui_viewsets.UserViewSet, basename='user')
28 ui_router.register(r'database_roles', ui_viewsets.DatabaseRoleViewSet, basename='database_role')
29 ui_router.register(r'schema_roles', ui_viewsets.SchemaRoleViewSet, basename='schema_role')
30 
31 urlpatterns = [
32     path('api/db/v0/', include(db_router.urls)),
33     path('api/db/v0/', include(db_table_router.urls)),
34     path('api/ui/v0/', include(ui_router.urls)),
35     path('api/ui/v0/reflect/', views.reflect_all, name='reflect_all'),
36     path('auth/password_reset_confirm', MathesarPasswordResetConfirmView.as_view(), name='password_reset_confirm'),
37     path('auth/login/', LoginView.as_view(redirect_authenticated_user=True), name='login'),
38     path('auth/', include('django.contrib.auth.urls')),
39     path('', views.home, name='home'),
40     path('<db_name>/', views.schemas, name='schemas'),
41     re_path(
42         r'^(?P<db_name>\w+)/(?P<schema_id>\w+)/',
43         views.schema_home,
44         name='schema_home'
45     ),
46 ]
47
```

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 mathesar.api.db.permissions.database import DatabaseAccessPolicy
9 from mathesar.api.db.permissions.query import QueryAccessPolicy
10 from mathesar.api.db.permissions.schema import SchemaAccessPolicy
11 from mathesar.api.db.permissions.table import TableAccessPolicy
12 from mathesar.api.serializers.databases import DatabaseSerializer, TypeSerializer
13 from mathesar.api.serializers.schemas import SchemaSerializer
14 from mathesar.api.serializers.tables import TableSerializer
15 from mathesar.api.serializers.queries import QuerySerializer
16 from mathesar.api.ui.serializers.users import UserSerializer
17 from mathesar.database.types import UIType
18 from mathesar.models.base import Database, Schema, Table
19 from mathesar.models.query import UIQuery
20 from mathesar.state import reset_reflection
21 
22 
23 def get_schema_list(request, database):
24     qs = Schema.objects.filter(database=database)
25     permission_restricted_qs = SchemaAccessPolicy.scope_queryset(request, qs)
26     schema_serializer = SchemaSerializer(
27         permission_restricted_qs,
28         many=True,
29         context={'request': request}
30     )
31     return schema_serializer.data
32 
33 
34 def _get_permissible_db_queryset(request):
35     qs = Database.objects.all()
36     permission_restricted_qs = DatabaseAccessPolicy.scope_queryset(request, qs)
37     schema_qs = Schema.objects.all()
38     permitted_schemas = SchemaAccessPolicy.scope_queryset(request, schema_qs)
39     databases_from_permitted_schema = Database.objects.filter(schemas__in=permitted_schemas)
40     permission_restricted_qs = permission_restricted_qs | databases_from_permitted_schema
41     return permission_restricted_qs.distinct()
42 
43 
44 def get_database_list(request):
45     permission_restricted_db_qs = _get_permissible_db_queryset(request)
46     database_serializer = DatabaseSerializer(
47         permission_restricted_db_qs,
48         many=True,
49         context={'request': request}
50     )
51     return database_serializer.data
52 
53 
54 def get_table_list(request, schema):
55     if schema is None:
56         return []
57     qs = Table.objects.filter(schema=schema)
58     permission_restricted_qs = TableAccessPolicy.scope_queryset(request, qs)
59     table_serializer = TableSerializer(
60         permission_restricted_qs,
61         many=True,
62         context={'request': request}
63     )
64     return table_serializer.data
65 
66 
67 def get_queries_list(request, schema):
68     if schema is None:
69         return []
70     qs = UIQuery.objects.filter(base_table__schema=schema)
71     permission_restricted_qs = QueryAccessPolicy.scope_queryset(request, qs)
72 
73     query_serializer = QuerySerializer(
74         permission_restricted_qs,
75         many=True,
76         context={'request': request}
77     )
78     return query_serializer.data
79 
80 
81 def get_ui_type_list(request, database):
82     if database is None:
83         return []
84     type_serializer = TypeSerializer(
85         UIType,
86         many=True,
87         context={'request': request}
88     )
89     return type_serializer.data
90 
91 
92 def get_user_data(request):
93     user_serializer = UserSerializer(
94         request.user,
95         many=False,
96         context={'request': request}
97     )
98     return user_serializer.data
99 
100 
101 def get_common_data(request, database, schema=None):
102     return {
103         'current_db': database.name if database else None,
104         'current_schema': schema.id if schema else None,
105         'schemas': get_schema_list(request, database),
106         'databases': get_database_list(request),
107         'tables': get_table_list(request, schema),
108         'queries': get_queries_list(request, schema),
109         'abstract_types': get_ui_type_list(request, database),
110         'user': get_user_data(request),
111         'live_demo_mode': getattr(settings, 'MATHESAR_LIVE_DEMO', False),
112     }
113 
114 
115 def get_current_database(request, db_name):
116     """Get database from passed name, with fall back behavior."""
117     permitted_databases = _get_permissible_db_queryset(request)
118     if db_name is not None:
119         current_database = get_object_or_404(permitted_databases, name=db_name)
120     else:
121         request_database_name = request.GET.get('database')
122         try:
123             if request_database_name is not None:
124                 # Try to get the database named specified in the request
125                 current_database = permitted_databases.get(name=request_database_name)
126             else:
127                 # Try to get the first database available
128                 current_database = permitted_databases.order_by('id').first()
129         except Database.DoesNotExist:
130             current_database = None
131     return current_database
132 
133 
134 def get_current_schema(request, schema_id, database):
135     # if there's a schema ID passed in, try to retrieve the schema, or return a 404 error.
136     if schema_id is not None:
137         permitted_schemas = SchemaAccessPolicy.scope_queryset(request, Schema.objects.all())
138         return get_object_or_404(permitted_schemas, id=schema_id)
139     else:
140         try:
141             # Try to get the first schema in the DB
142             return Schema.objects.filter(database=database).order_by('id').first()
143         except Schema.DoesNotExist:
144             return None
145 
146 
147 def render_schema(request, database, schema):
148     # if there's no schema available, redirect to the schemas page.
149     if not schema:
150         return redirect('schemas', db_name=database.name)
151     else:
152         # We are redirecting so that the correct URL is passed to the frontend.
153         return redirect('schema_home', db_name=database.name, schema_id=schema.id)
154 
155 
156 @login_required
157 @api_view(['POST'])
158 def reflect_all(_):
159     reset_reflection()
160     return Response(status=status.HTTP_200_OK)
161 
162 
163 @login_required
164 def home(request):
165     database = get_current_database(request, None)
166     if database is None:
167         return render(request, 'mathesar/index.html', {
168             'common_data': get_common_data(request, database)
169         })
170     return redirect('schemas', db_name=database.name)
171 
172 
173 @login_required
174 def schema_home(request, db_name, schema_id, **kwargs):
175     database = get_current_database(request, db_name)
176     schema = get_current_schema(request, schema_id, database)
177     return render(request, 'mathesar/index.html', {
178         'common_data': get_common_data(request, database, schema)
179     })
180 
181 
182 @login_required
183 def schemas(request, db_name):
184     database = get_current_database(request, db_name)
185     return render(request, 'mathesar/index.html', {
186         'common_data': get_common_data(request, database, None)
187     })
188
```
--- 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/urls.py b/mathesar/urls.py
--- a/mathesar/urls.py
+++ b/mathesar/urls.py
@@ -37,6 +37,11 @@
     path('auth/login/', LoginView.as_view(redirect_authenticated_user=True), name='login'),
     path('auth/', include('django.contrib.auth.urls')),
     path('', views.home, name='home'),
+    path('profile/', views.profile, name='profile'),
+    path('administration/', views.admin_home, name='admin_home'),
+    path('administration/users/', views.admin_home, name='admin_users_home'),
+    path('administration/users/<user_id>/', views.admin_home, name='admin_users_edit'),
+    path('administration/general/', views.admin_home, name='admin_general'),
     path('<db_name>/', views.schemas, name='schemas'),
     re_path(
         r'^(?P<db_name>\w+)/(?P<schema_id>\w+)/',
diff --git a/mathesar/views.py b/mathesar/views.py
--- a/mathesar/views.py
+++ b/mathesar/views.py
@@ -98,7 +98,7 @@
     return user_serializer.data
 
 
-def get_common_data(request, database, schema=None):
+def get_common_data(request, database=None, schema=None):
     return {
         'current_db': database.name if database else None,
         'current_schema': schema.id if schema else None,
@@ -170,6 +170,20 @@
     return redirect('schemas', db_name=database.name)
 
 
+@login_required
+def profile(request):
+    return render(request, 'mathesar/index.html', {
+        'common_data': get_common_data(request)
+    })
+
+
+@login_required
+def admin_home(request, **kwargs):
+    return render(request, 'mathesar/index.html', {
+        'common_data': get_common_data(request)
+    })
+
+
 @login_required
 def schema_home(request, db_name, schema_id, **kwargs):
     database = get_current_database(request, db_name)

{"golden_diff": "diff --git a/mathesar/urls.py b/mathesar/urls.py\n--- a/mathesar/urls.py\n+++ b/mathesar/urls.py\n@@ -37,6 +37,11 @@\n     path('auth/login/', LoginView.as_view(redirect_authenticated_user=True), name='login'),\n     path('auth/', include('django.contrib.auth.urls')),\n     path('', views.home, name='home'),\n+    path('profile/', views.profile, name='profile'),\n+    path('administration/', views.admin_home, name='admin_home'),\n+    path('administration/users/', views.admin_home, name='admin_users_home'),\n+    path('administration/users/<user_id>/', views.admin_home, name='admin_users_edit'),\n+    path('administration/general/', views.admin_home, name='admin_general'),\n     path('<db_name>/', views.schemas, name='schemas'),\n     re_path(\n         r'^(?P<db_name>\\w+)/(?P<schema_id>\\w+)/',\ndiff --git a/mathesar/views.py b/mathesar/views.py\n--- a/mathesar/views.py\n+++ b/mathesar/views.py\n@@ -98,7 +98,7 @@\n     return user_serializer.data\n \n \n-def get_common_data(request, database, schema=None):\n+def get_common_data(request, database=None, schema=None):\n     return {\n         'current_db': database.name if database else None,\n         'current_schema': schema.id if schema else None,\n@@ -170,6 +170,20 @@\n     return redirect('schemas', db_name=database.name)\n \n \n+@login_required\n+def profile(request):\n+    return render(request, 'mathesar/index.html', {\n+        'common_data': get_common_data(request)\n+    })\n+\n+\n+@login_required\n+def admin_home(request, **kwargs):\n+    return render(request, 'mathesar/index.html', {\n+        'common_data': get_common_data(request)\n+    })\n+\n+\n @login_required\n def schema_home(request, db_name, schema_id, **kwargs):\n     database = get_current_database(request, db_name)\n", "issue": "Remove \"Short Name\" field on user form\nI'd like to remove the \"Short Name\" field on the user form. In a [Matrix discussion](https://matrix.to/#/!xInuTkBwjXZXYatIlm:matrix.mathesar.org/$2e6ULgkergV1BZwlHxpNBNUKcG4W-bnetG9bApfg23M?via=matrix.mathesar.org&via=matrix.org) @kgodey said this was fine. It's an optional field so we can keep it in the API but just remove the form UI until we have a use-case for short names.\n\n\n", "before_files": [{"content": "from django.contrib.auth.views import LoginView\nfrom django.urls import include, path, re_path\nfrom rest_framework_nested import routers\n\nfrom mathesar import views\nfrom mathesar.api.db import viewsets as db_viewsets\nfrom mathesar.api.ui import viewsets as ui_viewsets\nfrom mathesar.users.password_reset import MathesarPasswordResetConfirmView\n\ndb_router = routers.DefaultRouter()\ndb_router.register(r'tables', db_viewsets.TableViewSet, basename='table')\ndb_router.register(r'queries', db_viewsets.QueryViewSet, basename='query')\ndb_router.register(r'links', db_viewsets.LinkViewSet, basename='links')\ndb_router.register(r'schemas', db_viewsets.SchemaViewSet, basename='schema')\ndb_router.register(r'databases', db_viewsets.DatabaseViewSet, basename='database')\ndb_router.register(r'data_files', db_viewsets.DataFileViewSet, basename='data-file')\n\ndb_table_router = routers.NestedSimpleRouter(db_router, r'tables', lookup='table')\ndb_table_router.register(r'records', db_viewsets.RecordViewSet, basename='table-record')\ndb_table_router.register(r'settings', db_viewsets.TableSettingsViewSet, basename='table-setting')\ndb_table_router.register(r'columns', db_viewsets.ColumnViewSet, basename='table-column')\ndb_table_router.register(r'constraints', db_viewsets.ConstraintViewSet, basename='table-constraint')\n\nui_router = routers.DefaultRouter()\nui_router.register(r'version', ui_viewsets.VersionViewSet, basename='version')\nui_router.register(r'databases', ui_viewsets.DatabaseViewSet, basename='database')\nui_router.register(r'users', ui_viewsets.UserViewSet, basename='user')\nui_router.register(r'database_roles', ui_viewsets.DatabaseRoleViewSet, basename='database_role')\nui_router.register(r'schema_roles', ui_viewsets.SchemaRoleViewSet, basename='schema_role')\n\nurlpatterns = [\n    path('api/db/v0/', include(db_router.urls)),\n    path('api/db/v0/', include(db_table_router.urls)),\n    path('api/ui/v0/', include(ui_router.urls)),\n    path('api/ui/v0/reflect/', views.reflect_all, name='reflect_all'),\n    path('auth/password_reset_confirm', MathesarPasswordResetConfirmView.as_view(), name='password_reset_confirm'),\n    path('auth/login/', LoginView.as_view(redirect_authenticated_user=True), name='login'),\n    path('auth/', include('django.contrib.auth.urls')),\n    path('', views.home, name='home'),\n    path('<db_name>/', views.schemas, name='schemas'),\n    re_path(\n        r'^(?P<db_name>\\w+)/(?P<schema_id>\\w+)/',\n        views.schema_home,\n        name='schema_home'\n    ),\n]\n", "path": "mathesar/urls.py"}, {"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 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 DatabaseSerializer, 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.database.types import UIType\nfrom mathesar.models.base import Database, Schema, Table\nfrom mathesar.models.query import UIQuery\nfrom mathesar.state import reset_reflection\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    qs = Database.objects.all()\n    permission_restricted_qs = DatabaseAccessPolicy.scope_queryset(request, qs)\n    schema_qs = Schema.objects.all()\n    permitted_schemas = SchemaAccessPolicy.scope_queryset(request, schema_qs)\n    databases_from_permitted_schema = Database.objects.filter(schemas__in=permitted_schemas)\n    permission_restricted_qs = permission_restricted_qs | databases_from_permitted_schema\n    return permission_restricted_qs.distinct()\n\n\ndef get_database_list(request):\n    permission_restricted_db_qs = _get_permissible_db_queryset(request)\n    database_serializer = DatabaseSerializer(\n        permission_restricted_db_qs,\n        many=True,\n        context={'request': request}\n    )\n    return database_serializer.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_common_data(request, database, schema=None):\n    return {\n        'current_db': database.name if database else None,\n        'current_schema': schema.id if schema else None,\n        'schemas': get_schema_list(request, database),\n        'databases': get_database_list(request),\n        'tables': get_table_list(request, schema),\n        'queries': get_queries_list(request, schema),\n        'abstract_types': get_ui_type_list(request, database),\n        'user': get_user_data(request),\n        'live_demo_mode': getattr(settings, 'MATHESAR_LIVE_DEMO', False),\n    }\n\n\ndef get_current_database(request, db_name):\n    \"\"\"Get database from passed name, with fall back behavior.\"\"\"\n    permitted_databases = _get_permissible_db_queryset(request)\n    if db_name is not None:\n        current_database = get_object_or_404(permitted_databases, name=db_name)\n    else:\n        request_database_name = request.GET.get('database')\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\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    database = get_current_database(request, None)\n    if database is None:\n        return render(request, 'mathesar/index.html', {\n            'common_data': get_common_data(request, database)\n        })\n    return redirect('schemas', db_name=database.name)\n\n\n@login_required\ndef schema_home(request, db_name, schema_id, **kwargs):\n    database = get_current_database(request, db_name)\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, db_name):\n    database = get_current_database(request, db_name)\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request, database, None)\n    })\n", "path": "mathesar/views.py"}], "after_files": [{"content": "from django.contrib.auth.views import LoginView\nfrom django.urls import include, path, re_path\nfrom rest_framework_nested import routers\n\nfrom mathesar import views\nfrom mathesar.api.db import viewsets as db_viewsets\nfrom mathesar.api.ui import viewsets as ui_viewsets\nfrom mathesar.users.password_reset import MathesarPasswordResetConfirmView\n\ndb_router = routers.DefaultRouter()\ndb_router.register(r'tables', db_viewsets.TableViewSet, basename='table')\ndb_router.register(r'queries', db_viewsets.QueryViewSet, basename='query')\ndb_router.register(r'links', db_viewsets.LinkViewSet, basename='links')\ndb_router.register(r'schemas', db_viewsets.SchemaViewSet, basename='schema')\ndb_router.register(r'databases', db_viewsets.DatabaseViewSet, basename='database')\ndb_router.register(r'data_files', db_viewsets.DataFileViewSet, basename='data-file')\n\ndb_table_router = routers.NestedSimpleRouter(db_router, r'tables', lookup='table')\ndb_table_router.register(r'records', db_viewsets.RecordViewSet, basename='table-record')\ndb_table_router.register(r'settings', db_viewsets.TableSettingsViewSet, basename='table-setting')\ndb_table_router.register(r'columns', db_viewsets.ColumnViewSet, basename='table-column')\ndb_table_router.register(r'constraints', db_viewsets.ConstraintViewSet, basename='table-constraint')\n\nui_router = routers.DefaultRouter()\nui_router.register(r'version', ui_viewsets.VersionViewSet, basename='version')\nui_router.register(r'databases', ui_viewsets.DatabaseViewSet, basename='database')\nui_router.register(r'users', ui_viewsets.UserViewSet, basename='user')\nui_router.register(r'database_roles', ui_viewsets.DatabaseRoleViewSet, basename='database_role')\nui_router.register(r'schema_roles', ui_viewsets.SchemaRoleViewSet, basename='schema_role')\n\nurlpatterns = [\n    path('api/db/v0/', include(db_router.urls)),\n    path('api/db/v0/', include(db_table_router.urls)),\n    path('api/ui/v0/', include(ui_router.urls)),\n    path('api/ui/v0/reflect/', views.reflect_all, name='reflect_all'),\n    path('auth/password_reset_confirm', MathesarPasswordResetConfirmView.as_view(), name='password_reset_confirm'),\n    path('auth/login/', LoginView.as_view(redirect_authenticated_user=True), name='login'),\n    path('auth/', include('django.contrib.auth.urls')),\n    path('', views.home, name='home'),\n    path('profile/', views.profile, name='profile'),\n    path('administration/', views.admin_home, name='admin_home'),\n    path('administration/users/', views.admin_home, name='admin_users_home'),\n    path('administration/users/<user_id>/', views.admin_home, name='admin_users_edit'),\n    path('administration/general/', views.admin_home, name='admin_general'),\n    path('<db_name>/', views.schemas, name='schemas'),\n    re_path(\n        r'^(?P<db_name>\\w+)/(?P<schema_id>\\w+)/',\n        views.schema_home,\n        name='schema_home'\n    ),\n]\n", "path": "mathesar/urls.py"}, {"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 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 DatabaseSerializer, 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.database.types import UIType\nfrom mathesar.models.base import Database, Schema, Table\nfrom mathesar.models.query import UIQuery\nfrom mathesar.state import reset_reflection\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    qs = Database.objects.all()\n    permission_restricted_qs = DatabaseAccessPolicy.scope_queryset(request, qs)\n    schema_qs = Schema.objects.all()\n    permitted_schemas = SchemaAccessPolicy.scope_queryset(request, schema_qs)\n    databases_from_permitted_schema = Database.objects.filter(schemas__in=permitted_schemas)\n    permission_restricted_qs = permission_restricted_qs | databases_from_permitted_schema\n    return permission_restricted_qs.distinct()\n\n\ndef get_database_list(request):\n    permission_restricted_db_qs = _get_permissible_db_queryset(request)\n    database_serializer = DatabaseSerializer(\n        permission_restricted_db_qs,\n        many=True,\n        context={'request': request}\n    )\n    return database_serializer.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_common_data(request, database=None, schema=None):\n    return {\n        'current_db': database.name if database else None,\n        'current_schema': schema.id if schema else None,\n        'schemas': get_schema_list(request, database),\n        'databases': get_database_list(request),\n        'tables': get_table_list(request, schema),\n        'queries': get_queries_list(request, schema),\n        'abstract_types': get_ui_type_list(request, database),\n        'user': get_user_data(request),\n        'live_demo_mode': getattr(settings, 'MATHESAR_LIVE_DEMO', False),\n    }\n\n\ndef get_current_database(request, db_name):\n    \"\"\"Get database from passed name, with fall back behavior.\"\"\"\n    permitted_databases = _get_permissible_db_queryset(request)\n    if db_name is not None:\n        current_database = get_object_or_404(permitted_databases, name=db_name)\n    else:\n        request_database_name = request.GET.get('database')\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\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    database = get_current_database(request, None)\n    if database is None:\n        return render(request, 'mathesar/index.html', {\n            'common_data': get_common_data(request, database)\n        })\n    return redirect('schemas', db_name=database.name)\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, db_name, schema_id, **kwargs):\n    database = get_current_database(request, db_name)\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, db_name):\n    database = get_current_database(request, db_name)\n    return render(request, 'mathesar/index.html', {\n        'common_data': get_common_data(request, database, None)\n    })\n", "path": "mathesar/views.py"}]}