Skip to content

Commit

Permalink
created project folder structure, added apps, added base models
Browse files Browse the repository at this point in the history
  • Loading branch information
bugraahmetcaglar committed Apr 7, 2024
1 parent 2c6fdc6 commit c997826
Show file tree
Hide file tree
Showing 57 changed files with 316 additions and 336 deletions.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions vehicle/apps.py → core/blog/apps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.apps import AppConfig


class VehicleConfig(AppConfig):
class BlogConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "vehicle"
name = "core.blog"
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions core/blog/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions core/question/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class QuestionConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "core.question"
File renamed without changes.
3 changes: 3 additions & 0 deletions core/question/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
File renamed without changes.
3 changes: 3 additions & 0 deletions core/question/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
File renamed without changes.
3 changes: 3 additions & 0 deletions core/support/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions core/support/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class SupportConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "core.support"
Empty file.
3 changes: 3 additions & 0 deletions core/support/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions core/support/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions core/support/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
Empty file added core/user/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions core/user/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
2 changes: 1 addition & 1 deletion user/apps.py → core/user/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class UserConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'user'
name = 'core.user'
File renamed without changes.
Empty file.
12 changes: 3 additions & 9 deletions user/models.py → core/user/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@

class User(AbstractUser):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
email = models.EmailField(max_length=200, unique=True)
email = models.EmailField(max_length=255, unique=True)

def data(self):
def token(self):
refresh = RefreshToken.for_user(self)
return {
'refresh_token': f"{refresh}",
'access_token': str(refresh.access_token)
}

class Meta:
db_table = "user"
return {"refresh_token": f"{refresh}", "access_token": str(refresh.access_token)}
3 changes: 3 additions & 0 deletions core/user/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
Empty file added core/user/v1/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion user/v1/serializers.py → core/user/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rest_framework_simplejwt.exceptions import TokenError
from rest_framework_simplejwt.tokens import RefreshToken

from user.models import User
from core.user.models import User


class UserLoginSerializer(serializers.ModelSerializer):
Expand Down
21 changes: 21 additions & 0 deletions core/user/v1/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from django.urls import re_path
from rest_framework_simplejwt.views import TokenRefreshView

from core.user.v1.views import (
UserLoginAPIView,
UserRegistrationAPIView,
UserLogoutAPIView,
UserListAPIView,
UserDetailAPIView,
)

urlpatterns = [
# Example with Regex (old version django)
re_path(r"^detail/(?P<id>[\w-]+)$", UserDetailAPIView.as_view(), name="user_detail_api"),
re_path(r"^detail/(?P<id>[\w-]+)$", UserDetailAPIView.as_view(), name="user_detail_api"),
re_path(r"^login$", UserLoginAPIView.as_view(), name="user_login_api"),
re_path(r"^list$", UserListAPIView.as_view(), name="user_list_api"),
re_path(r"^login/refresh$", TokenRefreshView.as_view(), name="token_refresh"),
re_path(r"^register$", UserRegistrationAPIView.as_view(), name="user_create_api"),
re_path(r"^logout$", UserLogoutAPIView.as_view(), name="user_logout_api"),
]
4 changes: 2 additions & 2 deletions user/v1/api.py → core/user/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
from rest_framework_jwt.settings import api_settings

from skillforge.constants import BaseResponse
from user.models import User
from user.v1.serializers import (
from core.user.models import User
from core.user.v1.serializers import (
UserLoginSerializer, UserRegisterSerializer,
UserLogoutSerializer, UserListSerializer
)
Expand Down
Empty file added core/user/v2/__init__.py
Empty file.
16 changes: 10 additions & 6 deletions user/serializers.py → core/user/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rest_framework_simplejwt.exceptions import TokenError
from rest_framework_simplejwt.tokens import RefreshToken

from user.models import User
from core.user.models import User


class UserLoginSerializer(serializers.ModelSerializer):
Expand All @@ -20,7 +20,7 @@ class UserLoginSerializer(serializers.ModelSerializer):

class Meta:
model = User
fields = ["email", "password", "username", "data"]
fields = ["email", "password", "username", "token"]

def validate(self, attrs):
username = attrs.get("username", "")
Expand All @@ -33,8 +33,8 @@ def validate(self, attrs):
if not user:
raise AuthenticationFailed("Invalid credential, try again")
user.last_login = datetime.datetime.now()
user.save()
return {"email": user.email, "username": user.username, "data": user.data}
user.save(update_fields=["last_login"])
return {"email": user.email, "username": user.username, "data": user.token}


class UserDetailSerializer(serializers.ModelSerializer):
Expand Down Expand Up @@ -66,7 +66,7 @@ def validate(self, attrs):
self.token = attrs["refresh"]
return attrs

def save(self, **kwargs):
def save(self):
try:
RefreshToken(self.token).blacklist()
except TokenError:
Expand Down Expand Up @@ -100,13 +100,17 @@ def create(self, validated_data):
return user


class UserForgetPasswordSerializer(serializers.Serializer):
class UserForgetPasswordSerializer(serializers.ModelSerializer):
email = serializers.EmailField(required=True)

def validate(self, attrs):
get_object_or_404(User, email=attrs["email"])
return attrs

class Meta:
model = User
fields = ("email",)


class UserResetPasswordSerializer(serializers.ModelSerializer):
password = serializers.CharField(required=True, validators=[validate_password])
Expand Down
20 changes: 20 additions & 0 deletions core/user/v2/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.urls import re_path
from rest_framework_simplejwt.views import TokenRefreshView

from core.user.v2.views import (
UserLoginAPIView,
UserRegistrationAPIView,
UserLogoutAPIView,
UserListAPIView,
UserDetailAPIView,
)


urlpatterns = [
re_path(r"^detail/(?P<id>[\w-]+)$", UserDetailAPIView.as_view(), name="v1_user_detail"),
re_path(r"^login$", UserLoginAPIView.as_view(), name="v2_user_login"),
re_path(r"^list$", UserListAPIView.as_view(), name="v1_user_list"),
re_path(r"^register$", UserRegistrationAPIView.as_view(), name="v1_user_create"),
re_path(r"^logout$", UserLogoutAPIView.as_view(), name="v1_user_logout"),
re_path(r"^login/refresh$", TokenRefreshView.as_view(), name="v1_token_refresh"),
]
36 changes: 25 additions & 11 deletions user/views.py → core/user/v2/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# Bugra Ahmet Caglar
from drf_yasg.utils import swagger_auto_schema

from skillforge.generics import StandardResultsSetPagination
from rest_framework import status
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.generics import CreateAPIView, RetrieveAPIView, ListAPIView
from rest_framework.permissions import (AllowAny, IsAuthenticated)
from rest_framework.generics import (
CreateAPIView, RetrieveAPIView, ListAPIView
)
from rest_framework_jwt.settings import api_settings

from skillforge.constants import BaseResponse
from user.serializers import *
from user.models import User
from core.user.models import User
from core.user.v1.serializers import (
UserLoginSerializer, UserRegisterSerializer,
UserLogoutSerializer, UserListSerializer
)

JWT_PAYLOAD_HANDLER = api_settings.JWT_PAYLOAD_HANDLER
JWT_ENCODE_HANDLER = api_settings.JWT_ENCODE_HANDLER
Expand All @@ -22,26 +28,29 @@ class UserLoginAPIView(CreateAPIView):
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
return BaseResponse(data=serializer.data, status_code=status.HTTP_200_OK).success_data()
return BaseResponse(data=serializer.data, status_code=status.HTTP_200_OK).success_with_data()


class UserRegistrationAPIView(CreateAPIView):
permission_classes = [AllowAny]
serializer_class = UserRegisterSerializer
permission_classes = [AllowAny]

@swagger_auto_schema(operation_summary="Create a new user API")
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return BaseResponse(message="User created successfully.", status_code=status.HTTP_201_CREATED).success()
return BaseResponse(
message="User created successfully.",
status_code=status.HTTP_201_CREATED
).success()


class UserDetailAPIView(RetrieveAPIView):
permission_classes = [AllowAny]
serializer_class = UserListSerializer
queryset = User.objects.all()
lookup_field = "id"
lookup_field = 'id'


class UserLogoutAPIView(CreateAPIView):
Expand All @@ -53,16 +62,21 @@ def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return BaseResponse(message="Successfully logged out.", status_code=status.HTTP_200_OK).success()
return BaseResponse(
message="Successfully logged out.", status_code=status.HTTP_200_OK
).success()


class UserListAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = UserListSerializer
pagination_class = StandardResultsSetPagination
queryset = User.objects.filter(is_active=True)
lookup_field = "id"
lookup_field = 'id'

def list(self, request, *args, **kwargs):
serializer_data = self.serializer_class(self.queryset, many=True).data
return BaseResponse(data=serializer_data, status_code=status.HTTP_200_OK).success_data()
return BaseResponse(
data=serializer_data,
status_code=status.HTTP_200_OK
).success_with_data()
Empty file added core/video/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions core/video/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions core/video/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class VideoConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "core.video"
Empty file.
3 changes: 3 additions & 0 deletions core/video/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions core/video/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
3 changes: 3 additions & 0 deletions core/video/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
9 changes: 5 additions & 4 deletions dev.env
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
SECRET_KEY=django-insecure-(q_xc+d@c4wzz0@80)nraq&7k^^*@yhj-_8h$a5ax!d0zpgfi$
SECRET_KEY="django-insecure-(q_xc+d@c4wzz0@80)nraq&7k^^*@yhj-_8h$a5ax!d0zpgfi$"
DEBUG = True


# DATABASE
DB_NAME="skillforge"
DB_USER="django"
DB_PASSWORD="django"
DB_NAME="db"
DB_USER="postgres"
DB_PASSWORD="postgres"
DB_HOST="db"
DB_PORT="5432"

Expand Down
Loading

0 comments on commit c997826

Please sign in to comment.