Skip to content

Commit

Permalink
user api implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian333Dev committed Aug 30, 2023
1 parent 0c13d44 commit 982d197
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 38 deletions.
127 changes: 90 additions & 37 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,65 +1,118 @@
# Makefile

# Docker Compose commands
# Variables
DC=docker-compose
DC_BUILD=$(DC) build
DC_UP=$(DC) up
DC_DOWN=$(DC) down
DC_RUN=$(DC) run --rm app sh -c
FLAKE8=$(DC_RUN) "flake8"
STARTPROJECT=$(DC_RUN) django-admin startproject
TEST="python manage.py test"


# Commands
.PHONY: help create-and-run-migrations migrate createmigration run-server superuser shell docker-build docker-up docker-down update test create-project create-app
default: help

dcup:
$(DC_UP)
up: dcup
create-and-run-migrations:
@echo "Creating and running migrations..."
$(DC_RUN) "python manage.py makemigrations && python manage.py migrate"
crm: create-and-run-migrations

dcdown:
$(DC_DOWN)
down: dcdown
migrate:
@echo "Running migrations..."
$(DC_RUN) "python manage.py migrate"
m: migrate

createmigration:
@echo "Creating migrations..."
$(DC_RUN) "python manage.py makemigrations"
cm: createmigration

run-server:
@echo "Running server..."
$(DC_RUN) "python manage.py runserver"
r: run-server

superuser:
@echo "Creating superuser..."
$(DC_RUN) "python manage.py createsuperuser"
su: superuser

shell:
@echo "Running shell..."
$(DC_RUN) "python manage.py shell"
sh: shell

docker-build:
@echo "Building docker image..."
$(DC) build
build: docker-build

docker-up:
@echo "Running docker containers..."
$(DC) up
up: docker-up

dcbuild:
$(DC_BUILD)
build: dcbuild
docker-down:
@echo "Stopping docker containers..."
$(DC) down
down: docker-down

update: makemigrations docker-build

test:
@echo "Running tests..."
$(DC_RUN) "python manage.py test && flake8"
t: test


createproject:
create-project:
ifeq ($(filter-out $@,$(MAKECMDGOALS)),)
@echo "Usage: make createproject project_name [optional: .]"
@echo "Please provide a project name"
else
$(DC_RUN) "django-admin startproject $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))"
@echo "Creating project..."
django-admin startproject $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) .
endif
cp: createproject
cp: create-project

createapp:
create-app:
ifeq ($(filter-out $@,$(MAKECMDGOALS)),)
@echo "Usage: make createapp app_name"
@echo "Please provide an app name"
else
@echo "Creating app..."
$(DC_RUN) "python manage.py startapp $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))"
endif
ca: createapp
ca: create-app

createsuperuser:
$(DC_RUN) "python manage.py createsuperuser"
su: createsuperuser

migrations:
$(DC_RUN) "python manage.py makemigrations"
ms: migrations

migrate:
$(DC_RUN) "python manage.py wait_for_db && python manage.py migrate"
m: migrate

# Help

help:
@echo "Usage: make [target]"
@echo "Usage: make [command]"
@echo ""
@echo "Commands:"
@echo " crm, create-and-run-migrations Create and run migrations"
@echo " m, migrate Run migrations"
@echo " cm, createmigration Create migrations"
@echo " r, run-server Run server"
@echo " su, superuser Create superuser"
@echo " sh, shell Run shell"
@echo " u, update Make migrations and and rebuild docker image"
@echo " build Build docker image"
@echo " up Run docker containers"
@echo " down Stop docker containers"
@echo " t, test Run tests"
@echo " cp, create-project Create project"
@echo " ca, create-app Create app"
@echo " help Show this help message and exit"
@echo ""
@echo "Targets:"
@echo "Examples:"
@echo " make crm"
@echo " make m"
@echo " make cm"
@echo " make r"
@echo " make su"
@echo " make sh"
@echo " make u"
@echo " make build"
@echo " make up"
@echo " make down"
@echo " make t"
@echo " make cp <project_name>"
@echo " make ca <app_name>"
@echo " make help"
2 changes: 2 additions & 0 deletions app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
"django.contrib.staticfiles",
# Third party apps
"rest_framework",
"rest_framework.authtoken",
"drf_spectacular",
# Local apps
"core",
"user",
]

MIDDLEWARE = [
Expand Down
3 changes: 2 additions & 1 deletion app/config/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from drf_spectacular.views import (
SpectacularAPIView,
SpectacularSwaggerView,
Expand All @@ -13,4 +13,5 @@
SpectacularSwaggerView.as_view(url_name="api-schema"),
name="api-docs",
),
path("api/user/", include("user.urls")),
]
Empty file added app/user/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions app/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'
62 changes: 62 additions & 0 deletions app/user/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"""
Serializers for the user API View.
"""
from django.contrib.auth import get_user_model, authenticate
from django.utils.translation import gettext as _

from rest_framework.serializers import (
ModelSerializer,
Serializer,
CharField,
EmailField,
ValidationError,
)


class UserSerializer(ModelSerializer):
"""
Serializer for the User model.
"""

class Meta:
model = get_user_model()
fields = ["username", "email", "password"]
extra_kwargs = {"password": {"write_only": True, "min_length": 8}}

def create(self, validated_data):
"""Create a new user with encrypted password and return it."""
return get_user_model().objects.create_user(**validated_data)

def update(self, instance, validated_data):
"""Update a user, setting the password correctly and return it."""
password = validated_data.pop("password", None)
user = super().update(instance, validated_data)

if password:
user.set_password(password)
user.save()

return user


class AuthTokenSerializer(Serializer):
"""Serializer for the user authentication object."""

email = EmailField()
password = CharField(style={"input_type": "password"}, trim_whitespace=False)

def validate(self, attrs):
"""Validate and authenticate the user."""
email = attrs.get("email")
password = attrs.get("password")

user = authenticate(
request=self.context.get("request"), username=email, password=password
)

if not user:
msg = _("Unable to authenticate with provided credentials.")
raise ValidationError(msg, code="authentication")

attrs["user"] = user
return attrs
Empty file added app/user/tests/__init__.py
Empty file.
Loading

0 comments on commit 982d197

Please sign in to comment.