Skip to content

Commit

Permalink
config app_name for all url reverse lookups, allow url versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb committed Aug 7, 2024
1 parent e58c8e6 commit 3909baa
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
1 change: 1 addition & 0 deletions arena_account/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"rest_framework.parsers.MultiPartParser",
"rest_framework.parsers.JSONParser",
],
"DEFAULT_VERSIONING_CLASS": "rest_framework.versioning.NamespaceVersioning",
}

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
Expand Down
6 changes: 5 additions & 1 deletion arena_account/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
from django.urls import include, path

urlpatterns = [
path("user/", include("users.urls")),
# configure user api version based endpoints
path("user/", include("users.urls", namespace="v1")),
path("user/v2/", include("users.urls", namespace="v2")),

# include admin paths
path("user/admin/", admin.site.urls),
path("user/accounts/", include("allauth.urls")),
]
2 changes: 1 addition & 1 deletion users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class SceneForm(forms.ModelForm):
editors = forms.ModelMultipleChoiceField(
queryset=User.objects.all().order_by('username'),
widget=autocomplete.ModelSelect2Multiple(
url='user-autocomplete',
url='users:user-autocomplete',
forward=(forward.Self(), ),
attrs={'data-minimum-input-length': 2},
), required=False)
Expand Down
8 changes: 4 additions & 4 deletions users/templates/users/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</head>

<body>
{% url 'login' as login_url %}
{% url 'users:login' as login_url %}
<!-- avoid navbar for users who have not logged in on login form yet-->
{% if user.is_authenticated or request.get_full_path != login_url %}

Expand Down Expand Up @@ -108,13 +108,13 @@
{{user.get_username}}</a>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
<li><a class="dropdown-item" href="{{request.scheme}}://{{request.get_host}}/conf/versions.html">Version</a></li>
<li><a class="dropdown-item" href="{% url 'user_profile' %}">Profile</a></li>
<li><a class="dropdown-item" href="{% url 'logout' %}">Logout</a></li>
<li><a class="dropdown-item" href="{% url 'users:user_profile' %}">Profile</a></li>
<li><a class="dropdown-item" href="{% url 'users:logout' %}">Logout</a></li>
</ul>
</li>
{% else %}
<li class="nav-item">
<a class="nav-link" href="{% url 'login' %}">Login</a>
<a class="nav-link" href="{% url 'users:login' %}">Login</a>
</li>
{% endif %}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion users/templates/users/social_signup.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h3>Sign Up</h3>
<p>You are about to use your
{{account.get_provider.name}} account to login. As a final step, please complete the
following form:</p>
<form class="signup" id="signup_form" method="post" action="{% url 'socialaccount_signup' %}"
<form class="signup" id="signup_form" method="post" action="{% url 'users:socialaccount_signup' %}"
enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
Expand Down
2 changes: 1 addition & 1 deletion users/templates/users/user_profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ <h5>Scene Permissions <button id="button_expand_add_scene" type="button" title="
<form action="profile_update_scene" method="POST">
{% csrf_token %}
<tr>
<td><a href="../../{{ scene.name }}"><b>{{ scene.name }}</b></a></td>
<td><a href="{{ request.scheme }}://{{ request.META.HTTP_HOST }}/{{ scene.name }}"><b>{{ scene.name }}</b></a></td>
<!--public subscribe ok-->
<td>
{% if scene.public_read %}
Expand Down
3 changes: 2 additions & 1 deletion users/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
title="ARENA Users API",
default_version="v1",
description="ARENA Users Django site endpoints.",
terms_of_service="../terms.html",
terms_of_service=f"https://{os.environ['HOSTNAME']}/terms.html",
contact=openapi.Contact(email=os.environ["EMAIL"]),
license=openapi.License(
name="BSD 3-Clause License",
Expand All @@ -24,6 +24,7 @@
permission_classes=(permissions.AllowAny,),
)

app_name = "users"
urlpatterns = [
# pages
path("", views.index, name="index"),
Expand Down
42 changes: 21 additions & 21 deletions users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def index(request):
"""
Root page load, index is treated as Login page.
"""
return redirect("login")
return redirect("users:login")


def login_request(request):
Expand All @@ -63,7 +63,7 @@ def login_request(request):
login(request, user)
messages.info(
request, f"You are now logged in as {username}.")
return redirect("login_callback")
return redirect("users:login_callback")
else:
messages.error(request, "Invalid username or password.")
else:
Expand All @@ -79,7 +79,7 @@ def logout_request(request):
Removes ID and flushes session data, shows login page.
"""
logout(request) # revoke django auth
response = redirect("login")
response = redirect("users:login")
response.delete_cookie("auth") # revoke fs auth
return response

Expand All @@ -98,7 +98,7 @@ def profile_update_scene(request):
form = UpdateSceneForm(request.POST)
if not form.is_valid():
messages.error(request, "Invalid parameters")
return redirect("user_profile")
return redirect("users:user_profile")
if "add" in request.POST:
scenename = request.POST.get("scenename", None)
s = Scene(
Expand All @@ -108,12 +108,12 @@ def profile_update_scene(request):
s.save()
messages.success(
request, f"Created scene {request.user.username}/{scenename}")
return redirect("user_profile")
return redirect("users:user_profile")
elif "edit" in request.POST:
name = form.cleaned_data["edit"]
return redirect(f"profile/scenes/{name}")

return redirect("user_profile")
return redirect("users:user_profile")


@ permission_classes([permissions.IsAuthenticated])
Expand All @@ -130,7 +130,7 @@ def profile_update_device(request):
form = UpdateDeviceForm(request.POST)
if not form.is_valid():
messages.error(request, "Invalid parameters")
return redirect("user_profile")
return redirect("users:user_profile")
if "add" in request.POST:
devicename = request.POST.get("devicename", None)
s = Device(
Expand All @@ -139,12 +139,12 @@ def profile_update_device(request):
s.save()
messages.success(
request, f"Created device {request.user.username}/{devicename}")
return redirect("user_profile")
return redirect("users:user_profile")
elif "edit" in request.POST:
name = form.cleaned_data["edit"]
return redirect(f"profile/devices/{name}")

return redirect("user_profile")
return redirect("users:user_profile")


def scene_perm_detail(request, pk):
Expand All @@ -154,19 +154,19 @@ def scene_perm_detail(request, pk):
"""
if not scene_permission(user=request.user, scene=pk):
messages.error(request, f"User does not have permission for: {pk}.")
return redirect("user_profile")
return redirect("users:user_profile")
# now, make sure scene exists before the other commands are tried
try:
scene = Scene.objects.get(name=pk)
except Scene.DoesNotExist:
messages.error(request, "The scene does not exist")
return redirect("user_profile")
return redirect("users:user_profile")
if request.method == 'POST':
if "save" in request.POST:
form = SceneForm(instance=scene, data=request.POST)
if form.is_valid():
form.save()
return redirect("user_profile")
return redirect("users:user_profile")
elif "delete" in request.POST:
token = generate_arena_token_v1(
user=request.user, username=request.user.username)
Expand All @@ -177,7 +177,7 @@ def scene_perm_detail(request, pk):
messages.error(
request, f"Unable to delete {pk} objects from persistance database.")

return redirect("user_profile")
return redirect("users:user_profile")
else:
form = SceneForm(instance=scene)

Expand All @@ -192,24 +192,24 @@ def device_perm_detail(request, pk):
"""
if not device_permission(user=request.user, device=pk):
messages.error(request, f"User does not have permission for: {pk}.")
return redirect("user_profile")
return redirect("users:user_profile")
# now, make sure device exists before the other commands are tried
try:
device = Device.objects.get(name=pk)
except Device.DoesNotExist:
messages.error(request, "The device does not exist")
return redirect("user_profile")
return redirect("users:user_profile")
token = None
if request.method == 'POST':
if "save" in request.POST:
form = DeviceForm(instance=device, data=request.POST)
if form.is_valid():
form.save()
return redirect("user_profile")
return redirect("users:user_profile")
elif "delete" in request.POST:
# delete account device data
device.delete()
return redirect("user_profile")
return redirect("users:user_profile")
elif "token" in request.POST:
token = generate_arena_token_v1(
user=request.user,
Expand Down Expand Up @@ -336,9 +336,9 @@ def profile_update_staff(request):
if not set_filestore_scope(user):
messages.error(
request, "Unable to update user's filestore status.")
return redirect("user_profile")
return redirect("users:user_profile")

return redirect("user_profile")
return redirect("users:user_profile")


@ api_view(["GET"])
Expand Down Expand Up @@ -486,13 +486,13 @@ def user_profile(request):
if not delete_scene_objects(scene.name, token):
messages.error(
request, f"Unable to delete {scene.name} objects from persistance database.")
return redirect("user_profile")
return redirect("users:user_profile")

# delete filestore files/account
if not delete_filestore_user(request.user):
messages.error(
request, "Unable to delete account/files from the filestore.")
return redirect("user_profile")
return redirect("users:user_profile")

# Be careful of foreign keys, in that case this is suggested:
# user.is_active = False
Expand Down

0 comments on commit 3909baa

Please sign in to comment.