Skip to content

Commit

Permalink
feat: api docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Akay7 committed Jun 5, 2024
1 parent 97bb5ed commit ece6fdc
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
"env/dev.env"
],
"network": "socialnetwork_default",
"volumes": [{
"localPath": "${workspaceFolder}/backend/",
"containerPath": "/app/"
}],
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ COPY ./backend /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
#USER appuser

# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "social_network.wsgi"]
27 changes: 27 additions & 0 deletions backend/social_network/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"rest_framework",
"drf_spectacular",
"drf_spectacular_sidecar", # required for Django collectstatic discovery
"user",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -86,6 +90,11 @@
}


# User model
# https://docs.djangoproject.com/en/5.0/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project
AUTH_USER_MODEL = "user.User"


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

Expand Down Expand Up @@ -126,3 +135,21 @@
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# DRF

REST_FRAMEWORK = {
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}


# DRF Spectacular
# https://drf-spectacular.readthedocs.io/en/latest/readme.html#self-contained-ui-installation

SPECTACULAR_SETTINGS = {
"SWAGGER_UI_DIST": "SIDECAR", # shorthand to use the sidecar instead
"SWAGGER_UI_FAVICON_HREF": "SIDECAR",
"REDOC_DIST": "SIDECAR",
# OTHER SETTINGS
}
28 changes: 27 additions & 1 deletion backend/social_network/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,34 @@
"""

from django.contrib import admin
from django.urls import path
from django.urls import include, path
from rest_framework import routers
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularRedocView,
SpectacularSwaggerView,
)

from user.views import UserViewSet


router = routers.DefaultRouter()
router.register(r"users", UserViewSet)


urlpatterns = [
path("api/", include(router.urls)),
path("api-auth/", include("rest_framework.urls")),
path("api/schema/", SpectacularAPIView.as_view(), name="schema"),
path(
"api/schema/swagger-ui/",
SpectacularSwaggerView.as_view(url_name="schema"),
name="swagger-ui",
),
path(
"api/schema/redoc/",
SpectacularRedocView.as_view(url_name="schema"),
name="redoc",
),
path("admin/", admin.site.urls),
]
Empty file added backend/user/__init__.py
Empty file.
1 change: 1 addition & 0 deletions backend/user/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Register your models here.
6 changes: 6 additions & 0 deletions backend/user/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UserConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "user"
132 changes: 132 additions & 0 deletions backend/user/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# Generated by Django 5.0.6 on 2024-06-05 09:00

import django.contrib.auth.models
import django.contrib.auth.validators
import django.utils.timezone
import uuid
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

dependencies = [
("auth", "0012_alter_user_first_name_max_length"),
]

operations = [
migrations.CreateModel(
name="User",
fields=[
("password", models.CharField(max_length=128, verbose_name="password")),
(
"last_login",
models.DateTimeField(
blank=True, null=True, verbose_name="last login"
),
),
(
"is_superuser",
models.BooleanField(
default=False,
help_text="Designates that this user has all permissions without explicitly assigning them.",
verbose_name="superuser status",
),
),
(
"username",
models.CharField(
error_messages={
"unique": "A user with that username already exists."
},
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
max_length=150,
unique=True,
validators=[
django.contrib.auth.validators.UnicodeUsernameValidator()
],
verbose_name="username",
),
),
(
"first_name",
models.CharField(
blank=True, max_length=150, verbose_name="first name"
),
),
(
"last_name",
models.CharField(
blank=True, max_length=150, verbose_name="last name"
),
),
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="email address"
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text="Designates whether the user can log into this admin site.",
verbose_name="staff status",
),
),
(
"is_active",
models.BooleanField(
default=True,
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
verbose_name="active",
),
),
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
),
),
(
"id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
(
"groups",
models.ManyToManyField(
blank=True,
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
related_name="user_set",
related_query_name="user",
to="auth.group",
verbose_name="groups",
),
),
(
"user_permissions",
models.ManyToManyField(
blank=True,
help_text="Specific permissions for this user.",
related_name="user_set",
related_query_name="user",
to="auth.permission",
verbose_name="user permissions",
),
),
],
options={
"verbose_name": "user",
"verbose_name_plural": "users",
"abstract": False,
},
managers=[
("objects", django.contrib.auth.models.UserManager()),
],
),
]
Empty file.
8 changes: 8 additions & 0 deletions backend/user/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import uuid

from django.db import models
from django.contrib.auth.models import AbstractUser


class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
13 changes: 13 additions & 0 deletions backend/user/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from rest_framework import serializers

from .models import User


class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = [
"email",
"first_name",
"last_name",
]
1 change: 1 addition & 0 deletions backend/user/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Create your tests here.
11 changes: 11 additions & 0 deletions backend/user/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.contrib.auth import get_user_model
from rest_framework import viewsets

from .serializers import UserSerializer

User = get_user_model()


class UserViewSet(viewsets.ReadOnlyModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ Django==5.0.6
gunicorn==22.0.0
psycopg[binary,pool]==3.1.19
dj-database-url==2.2.0
djangorestframework==3.15.1
django-filter==24.2
drf-spectacular[sidecar]==0.27.2

0 comments on commit ece6fdc

Please sign in to comment.