auto populates api urls
11 files changed, +66 -25
+28-1rango/backend/api/management/commands/createapi.py
| @@ -16,13 +16,13 @@ class Command(BaseCommand): | ||
| 16 | 16 | # Get the api name from the first argument |
| 17 | 17 | api_name = options['api_name'].lower() |
| 18 | 18 | model_name = api_name.capitalize() |
| 19 | - api_name += 's' | |
| 20 | 19 | # Get the root directory |
| 21 | 20 | root_dir = options['root_dir'].lower() |
| 22 | 21 | # Create context for the templates |
| 23 | 22 | context = { |
| 24 | 23 | 'api_name': api_name, |
| 25 | 24 | 'root_dir': root_dir, |
| 25 | + 'model_name': model_name, | |
| 26 | 26 | } |
| 27 | 27 | # If the api model does not exist raise a CommandError |
| 28 | 28 | try: |
| @@ -36,6 +36,10 @@ class Command(BaseCommand): | ||
| 36 | 36 | # Create the api directory |
| 37 | 37 | api_dir = os.path.join(root_dir, api_name) |
| 38 | 38 | os.makedirs(api_dir, exist_ok=True) |
| 39 | + # Create an __init__.py file | |
| 40 | + init_file = os.path.join(api_dir, '__init__.py') | |
| 41 | + with open(init_file, 'w') as f: | |
| 42 | + f.write('') | |
| 39 | 43 | # Load the viewset template and write it to the views file |
| 40 | 44 | views_file = os.path.join(api_dir, 'views.py') |
| 41 | 45 | with open(views_file, 'w') as f: |
| @@ -44,4 +48,27 @@ class Command(BaseCommand): | ||
| 44 | 48 | serializers_file = os.path.join(api_dir, 'serializers.py') |
| 45 | 49 | with open(serializers_file, 'w') as f: |
| 46 | 50 | f.write(strip_tags(render_to_string('api/serializers.html', context))) |
| 51 | + # Load the urls template and write it to the urls file | |
| 52 | + urls_file = os.path.join(api_dir, 'urls.py') | |
| 53 | + with open(urls_file, 'w') as f: | |
| 54 | + f.write(strip_tags(render_to_string('api/urls.html', context))) | |
| 55 | + # Add the urls to the root urls file | |
| 56 | + root_urls_file = os.path.join(root_dir, 'urls.py') | |
| 57 | + with open(root_urls_file, 'r') as f: | |
| 58 | + lines = f.readlines() | |
| 59 | + with open(root_urls_file, 'w') as f: | |
| 60 | + for line in lines: | |
| 61 | + f.write(line) | |
| 62 | + if 'Auto-generated' in line: | |
| 63 | + f.write(f"\tre_path('', include('{root_dir}.{api_name}.urls')),\n") | |
| 64 | + # Add the url to the api root | |
| 65 | + api_root_file = os.path.join(root_dir, 'views.py') | |
| 66 | + with open(api_root_file, 'r') as f: | |
| 67 | + lines = f.readlines() | |
| 68 | + with open(api_root_file, 'w') as f: | |
| 69 | + for line in lines: | |
| 70 | + f.write(line) | |
| 71 | + if 'Auto-generated' in line: | |
| 72 | + print('found user-list') | |
| 73 | + f.write(f"\t\t\t'{api_name}': reverse('{api_name}-list', request=request),\n") | |
| 47 | 74 | self.stdout.write(self.style.SUCCESS(f"Successfully created API {api_name}")) |
+0-0rango/backend/api/migrations/__init__.py
No content changes (mode or rename only).
+5-5rango/backend/api/templates/api/serializers.html
| @@ -1,9 +1,9 @@ | ||
| 1 | -<pre> | |
| 2 | -from rest_framework import serializers | |
| 3 | -from {{root_dir}}.models import {{api_name|title}} | |
| 1 | +<pre>from rest_framework import serializers | |
| 2 | +from {{root_dir}}.models import {{model_name}} | |
| 4 | 3 | |
| 5 | 4 | |
| 6 | -class {{api_name|title}}Serializer(serializers.HyperlinkedModelSerializer): | |
| 5 | +class {{model_name}}Serializer(serializers.HyperlinkedModelSerializer): | |
| 7 | 6 | class Meta: |
| 8 | - model = {{api_name|title}} | |
| 7 | + model = {{model_name}} | |
| 8 | + fields = '__all__' | |
| 9 | 9 | </pre> |
+11-0rango/backend/api/templates/api/urls.html
| @@ -0,0 +1,11 @@ | ||
| 1 | +<pre>from django.urls import path, include | |
| 2 | +from api.{{api_name}}.views import {{model_name}}ViewSet | |
| 3 | +from rest_framework.routers import SimpleRouter | |
| 4 | + | |
| 5 | +router = SimpleRouter() | |
| 6 | +router.register(r'{{api_name}}', {{model_name}}ViewSet) | |
| 7 | + | |
| 8 | +urlpatterns = [ | |
| 9 | + path('', include(router.urls)), | |
| 10 | +] | |
| 11 | +</pre> |
+7-8rango/backend/api/templates/api/viewset.html
| @@ -1,15 +1,14 @@ | ||
| 1 | -<pre> | |
| 2 | -from rest_framework import viewsets | |
| 1 | +<pre>from rest_framework import viewsets | |
| 3 | 2 | from rest_framework.permissions import AllowAny |
| 4 | -from {{root_dir}}.models import {{api_name|title}} | |
| 5 | -from api.{{api_name}}.serializers import {{api_name|title}}Serializer | |
| 3 | +from {{root_dir}}.models import {{model_name}} | |
| 4 | +from api.{{api_name}}.serializers import {{model_name}}Serializer | |
| 6 | 5 | |
| 7 | 6 | |
| 8 | -class {{api_name|title}}(viewsets.ModelViewSet): | |
| 7 | +class {{model_name}}ViewSet(viewsets.ModelViewSet): | |
| 9 | 8 | """ |
| 10 | - API endpoint that allows {{api_name}} to be viewed or edited. | |
| 9 | + API endpoint that allows {{model_name}} to be viewed or edited. | |
| 11 | 10 | """ |
| 12 | - queryset = {{api_name|title}}.objects.all() | |
| 13 | - serializer_class = UserSerializer | |
| 11 | + queryset = {{model_name}}.objects.all() | |
| 12 | + serializer_class = {{model_name}}Serializer | |
| 14 | 13 | permission_classes = [AllowAny] |
| 15 | 14 | </pre> |
+2-2rango/backend/api/urls.py
| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | from django.urls import include, re_path |
| 2 | 2 | from .views import ApiRoot |
| 3 | -from api.users.urls import router as user_router | |
| 4 | 3 | |
| 5 | 4 | |
| 6 | 5 | urlpatterns = [ |
| 7 | 6 | re_path('$', ApiRoot.as_view(), name='api-root'), |
| 8 | - re_path('', include(user_router.urls)), | |
| 7 | + re_path('', include('api.user.urls')), | |
| 8 | + # Auto-generated: a keyword used by the createapi command to know where to add new urls | |
| 9 | 9 | ] |
+0-0rango/backend/api/users/serializers.py → rango/backend/api/user/serializers.py
No content changes (mode or rename only).
+10-0rango/backend/api/user/urls.py
| @@ -0,0 +1,10 @@ | ||
| 1 | +from django.urls import path, include | |
| 2 | +from api.user.views import UserViewSet | |
| 3 | +from rest_framework.routers import SimpleRouter | |
| 4 | + | |
| 5 | +router = SimpleRouter() | |
| 6 | +router.register(r'user', UserViewSet) | |
| 7 | + | |
| 8 | +urlpatterns = [ | |
| 9 | + path('', include(router.urls)), | |
| 10 | +] |
+1-1rango/backend/api/users/views.py → rango/backend/api/user/views.py
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | from rest_framework import viewsets |
| 2 | 2 | from rest_framework.permissions import IsAuthenticated, AllowAny |
| 3 | 3 | from django.contrib.auth.models import User |
| 4 | -from api.users.serializers import UserSerializer | |
| 4 | +from api.user.serializers import UserSerializer | |
| 5 | 5 | |
| 6 | 6 | |
| 7 | 7 | class UserViewSet(viewsets.ModelViewSet): |
+0-7rango/backend/api/users/urls.py
| @@ -1,7 +0,0 @@ | ||
| 1 | -from django.urls import path, include | |
| 2 | -from api.users.views import UserViewSet | |
| 3 | -from rest_framework.routers import SimpleRouter | |
| 4 | - | |
| 5 | -router = SimpleRouter() | |
| 6 | -router.register(r'users', UserViewSet) | |
| 7 | - |
+2-1rango/backend/api/views.py
| @@ -7,5 +7,6 @@ class ApiRoot(APIRootView): | ||
| 7 | 7 | |
| 8 | 8 | def get(self, request, *args, **kwargs): |
| 9 | 9 | return Response({ |
| 10 | - 'users': reverse('user-list', request=request), | |
| 10 | + 'user': reverse('user-list', request=request), | |
| 11 | + # Auto-generated: a keyword used by the createapi command to know where to add new urls | |
| 11 | 12 | }) |