Skip to content

Commit

Permalink
Standard way to link to model with multi-word name
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanCoding committed Feb 6, 2024
1 parent 3eb2b78 commit 3bee8bf
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 31 deletions.
6 changes: 5 additions & 1 deletion ansible_base/lib/abstract_models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ def related_fields(self, request):
reverse_view = f"{underscore(self.__class__.__name__)}-{underscore(field.related_model.__name__)}s-list"
pk = self.pk
else:
reverse_view = f"{underscore(field.related_model.__name__)}-detail"
rel_model = field.related_model
if hasattr(rel_model, 'router_basename'):
reverse_view = f"{underscore(rel_model.router_basename)}-detail"
else:
reverse_view = f"{underscore(rel_model.__name__)}-detail"
pk = getattr(self, field.name).pk
try:
response[field.name] = reverse(reverse_view, kwargs={'pk': pk})
Expand Down
2 changes: 2 additions & 0 deletions test_app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ class Migration(migrations.Migration):
('description', models.TextField(blank=True, default='', help_text='The team description.')),
('created_by', models.ForeignKey(default=None, editable=False, help_text='The user who created this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_created+', to=settings.AUTH_USER_MODEL)),
('modified_by', models.ForeignKey(default=None, editable=False, help_text='The user who last modified this resource', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='%(app_label)s_%(class)s_modified+', to=settings.AUTH_USER_MODEL)),
('scrum_master', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
('organization', models.ForeignKey(help_text='The organization of this team.', on_delete=django.db.models.deletion.CASCADE, related_name='teams', to='test_app.organization')),
('encryptioner', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='test_app.encryptionmodel'))
],
options={
'ordering': ('organization__name', 'name'),
Expand Down
25 changes: 14 additions & 11 deletions test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@
from ansible_base.lib.utils.models import user_summary_fields


class EncryptionModel(NamedCommonModel):
class Meta:
app_label = "test_app"

encrypted_fields = ['testing1', 'testing2']

testing1 = models.CharField(max_length=1, null=True, default='a')
testing2 = models.CharField(max_length=1, null=True, default='b')


class Organization(AbstractOrganization):
pass

Expand All @@ -26,8 +16,21 @@ def summary_fields(self):


class Team(AbstractTeam):
pass
scrum_master = models.ForeignKey(User, on_delete=models.SET_NULL, null=True) # used to test related links
encryptioner = models.ForeignKey('EncryptionModel', on_delete=models.SET_NULL, null=True)


class ResourceMigrationTestModel(models.Model):
name = models.CharField(max_length=255)


class EncryptionModel(NamedCommonModel):
router_basename = 'encryption_test_model'

class Meta:
app_label = "test_app"

encrypted_fields = ['testing1', 'testing2']

testing1 = models.CharField(max_length=1, null=True, default='a')
testing2 = models.CharField(max_length=1, null=True, default='b')
11 changes: 11 additions & 0 deletions test_app/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from rest_framework.routers import SimpleRouter

from test_app import views

router = SimpleRouter()

router.register(r'organizations', views.UserViewSet, basename='organization')
router.register(r'teams', views.TeamViewSet, basename='team')
router.register(r'users', views.UserViewSet, basename='user')
# using an intentionally unpredictable basename
router.register(r'encrypted_models', views.EncryptionModelViewSet, basename='encryption_test_model')
26 changes: 20 additions & 6 deletions test_app/serializers.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
from rest_framework.serializers import ModelSerializer

from ansible_base.lib.serializers.common import NamedCommonModelSerializer
from test_app.models import EncryptionModel, User
from test_app import models


class EncryptionTestSerializer(NamedCommonModelSerializer):
reverse_url_name = None
class OrganizationSerializer(NamedCommonModelSerializer):
class Meta:
model = models.Organization
fields = '__all__'


class TeamSerializer(NamedCommonModelSerializer):
reverse_url_name = 'team-detail'

class Meta:
model = EncryptionModel
fields = NamedCommonModelSerializer.Meta.fields + [x.name for x in EncryptionModel._meta.concrete_fields]
model = models.Team
fields = '__all__'


class UserSerializer(ModelSerializer):
class Meta:
model = User
model = models.User
fields = '__all__'


class EncryptionTestSerializer(NamedCommonModelSerializer):
reverse_url_name = None

class Meta:
model = models.EncryptionModel
fields = NamedCommonModelSerializer.Meta.fields + [x.name for x in models.EncryptionModel._meta.concrete_fields]
3 changes: 0 additions & 3 deletions test_app/tests.py

This file was deleted.

4 changes: 2 additions & 2 deletions test_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

from ansible_base.lib.dynamic_config.dynamic_urls import api_urls, api_version_urls, root_urls
from ansible_base.resource_registry.urls import urlpatterns as resource_api_urls
from test_app.views import router as user_router
from test_app.router import router as test_app_router

urlpatterns = [
path('api/v1/', include(api_version_urls)),
path('api/', include(api_urls)),
path('', include(root_urls)),
# views specific to test_app
path('api/v1/', include(user_router.urls)),
path('api/v1/', include(test_app_router.urls)),
# Admin application
re_path(r"^admin/", admin.site.urls, name="admin"),
path('api/v1/', include(resource_api_urls)),
Expand Down
26 changes: 18 additions & 8 deletions test_app/views.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
from rest_framework import permissions
from rest_framework.routers import SimpleRouter
from rest_framework.viewsets import ModelViewSet

from test_app.models import User
from test_app.serializers import UserSerializer
from test_app import serializers


class UserViewSet(ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
class TestAppViewSet(ModelViewSet):
permission_classes = [permissions.IsAuthenticated]

def get_queryset(self):
return self.serializer_class.Meta.model.objects.all()

router = SimpleRouter()

router.register(r'users', UserViewSet)
class OrganizationViewSet(TestAppViewSet):
serializer_class = serializers.OrganizationSerializer


class TeamViewSet(TestAppViewSet):
serializer_class = serializers.TeamSerializer


class UserViewSet(TestAppViewSet):
serializer_class = serializers.UserSerializer


class EncryptionModelViewSet(TestAppViewSet):
serializer_class = serializers.EncryptionTestSerializer

0 comments on commit 3bee8bf

Please sign in to comment.