Creates the serializer and views from management
5 files changed, +71 -1
+0-0rango/backend/api/management/commands/__init__.py
No content changes (mode or rename only).
+46-0rango/backend/api/management/commands/createapi.py
| @@ -0,0 +1,46 @@ | ||
| 1 | +from django.core.management.base import BaseCommand, CommandError, CommandParser | |
| 2 | +from django.template.loader import render_to_string | |
| 3 | +from django.utils.html import strip_tags | |
| 4 | +import os | |
| 5 | + | |
| 6 | + | |
| 7 | +class Command(BaseCommand): | |
| 8 | + # Show this when the user types help | |
| 9 | + help = "Creates a new API. Usage: python manage.py createapi api_name [root_dir]" | |
| 10 | + | |
| 11 | + def add_arguments(self, parser: CommandParser) -> None: | |
| 12 | + parser.add_argument('api_name', type=str, help='The name of the API model') | |
| 13 | + parser.add_argument('root_dir', type=str, nargs='?', default='api', help='The root directory for the API model. Defaults to "api"') | |
| 14 | + | |
| 15 | + def handle(self, *args, **options): | |
| 16 | + # Get the api name from the first argument | |
| 17 | + api_name = options['api_name'].lower() | |
| 18 | + model_name = api_name.capitalize() | |
| 19 | + api_name += 's' | |
| 20 | + # Get the root directory | |
| 21 | + root_dir = options['root_dir'].lower() | |
| 22 | + # Create context for the templates | |
| 23 | + context = { | |
| 24 | + 'api_name': api_name, | |
| 25 | + 'root_dir': root_dir, | |
| 26 | + } | |
| 27 | + # If the api model does not exist raise a CommandError | |
| 28 | + try: | |
| 29 | + __import__(f'{root_dir}.models.{model_name}') | |
| 30 | + except: | |
| 31 | + raise CommandError(f"Model {model_name} does not exist") | |
| 32 | + # If the root directory does not exist raise a CommandError | |
| 33 | + if not os.path.exists(root_dir): | |
| 34 | + raise CommandError(f"Directory {root_dir} does not exist") | |
| 35 | + # Create the api directory | |
| 36 | + api_dir = os.path.join(root_dir, api_name) | |
| 37 | + os.makedirs(api_dir, exist_ok=True) | |
| 38 | + # Load the viewset template and write it to the views file | |
| 39 | + views_file = os.path.join(api_dir, 'views.py') | |
| 40 | + with open(views_file, 'w') as f: | |
| 41 | + f.write(strip_tags(render_to_string('api/viewset.html', context))) | |
| 42 | + # Load the serializers template and write it to the serializers file | |
| 43 | + serializers_file = os.path.join(api_dir, 'serializers.py') | |
| 44 | + with open(serializers_file, 'w') as f: | |
| 45 | + f.write(strip_tags(render_to_string('api/serializers.html', context))) | |
| 46 | + self.stdout.write(self.style.SUCCESS(f"Successfully created API {api_name}")) |
+9-0rango/backend/api/templates/api/serializers.html
| @@ -0,0 +1,9 @@ | ||
| 1 | +<pre> | |
| 2 | +from rest_framework import serializers | |
| 3 | +from {{root_dir}}.models import {{api_name|title}} | |
| 4 | + | |
| 5 | + | |
| 6 | +class {{api_name|title}}Serializer(serializers.HyperlinkedModelSerializer): | |
| 7 | + class Meta: | |
| 8 | + model = {{api_name|title}} | |
| 9 | +</pre> |
+15-0rango/backend/api/templates/api/viewset.html
| @@ -0,0 +1,15 @@ | ||
| 1 | +<pre> | |
| 2 | +from rest_framework import viewsets | |
| 3 | +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 | |
| 6 | + | |
| 7 | + | |
| 8 | +class {{api_name|title}}(viewsets.ModelViewSet): | |
| 9 | + """ | |
| 10 | + API endpoint that allows {{api_name}} to be viewed or edited. | |
| 11 | + """ | |
| 12 | + queryset = {{api_name|title}}.objects.all() | |
| 13 | + serializer_class = UserSerializer | |
| 14 | + permission_classes = [AllowAny] | |
| 15 | +</pre> |
+1-1rango/backend/backend/management/commands/createsecret.py
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | from django.core.management.base import BaseCommand, CommandError |
| 2 | 2 | |
| 3 | 3 | class Command(BaseCommand): |
| 4 | - help = 'Closes the specified poll for voting' | |
| 4 | + help = 'Create a new secret key for the .env file.' | |
| 5 | 5 | |
| 6 | 6 | def handle(self, *args, **options): |
| 7 | 7 | # Generate a django secret key |