second
This commit is contained in:
commit
e591d0f409
34 changed files with 424 additions and 0 deletions
0
todos/__init__.py
Normal file
0
todos/__init__.py
Normal file
BIN
todos/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
todos/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
todos/__pycache__/admin.cpython-312.pyc
Normal file
BIN
todos/__pycache__/admin.cpython-312.pyc
Normal file
Binary file not shown.
BIN
todos/__pycache__/apps.cpython-312.pyc
Normal file
BIN
todos/__pycache__/apps.cpython-312.pyc
Normal file
Binary file not shown.
BIN
todos/__pycache__/models.cpython-312.pyc
Normal file
BIN
todos/__pycache__/models.cpython-312.pyc
Normal file
Binary file not shown.
BIN
todos/__pycache__/serializers.cpython-312.pyc
Normal file
BIN
todos/__pycache__/serializers.cpython-312.pyc
Normal file
Binary file not shown.
BIN
todos/__pycache__/urls.cpython-312.pyc
Normal file
BIN
todos/__pycache__/urls.cpython-312.pyc
Normal file
Binary file not shown.
BIN
todos/__pycache__/views.cpython-312.pyc
Normal file
BIN
todos/__pycache__/views.cpython-312.pyc
Normal file
Binary file not shown.
3
todos/admin.py
Normal file
3
todos/admin.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
6
todos/apps.py
Normal file
6
todos/apps.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class TodosConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'todos'
|
27
todos/migrations/0001_initial.py
Normal file
27
todos/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
# Generated by Django 4.2 on 2025-01-15 13:36
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Todo',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=200)),
|
||||
('completed', models.BooleanField(default=False)),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
0
todos/migrations/__init__.py
Normal file
0
todos/migrations/__init__.py
Normal file
BIN
todos/migrations/__pycache__/0001_initial.cpython-312.pyc
Normal file
BIN
todos/migrations/__pycache__/0001_initial.cpython-312.pyc
Normal file
Binary file not shown.
BIN
todos/migrations/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
todos/migrations/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
12
todos/models.py
Normal file
12
todos/models.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class Todo(models.Model):
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
title = models.CharField(max_length=200)
|
||||
completed = models.BooleanField(default=False)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
return self.title
|
||||
|
21
todos/serializers.py
Normal file
21
todos/serializers.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from rest_framework import serializers
|
||||
from django.contrib.auth.models import User
|
||||
from .models import Todo
|
||||
|
||||
class UserSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = User
|
||||
fields = ('id', 'username')
|
||||
extra_kwargs = {'password': {'write_only': True}}
|
||||
|
||||
def create(self, validated_data):
|
||||
user = User.objects.create_user(
|
||||
username=validated_data['username'],
|
||||
password=validated_data['password']
|
||||
)
|
||||
return user
|
||||
|
||||
class TodoSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Todo
|
||||
fields = ('id', 'title', 'completed', 'created_at')
|
3
todos/tests.py
Normal file
3
todos/tests.py
Normal file
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
12
todos/urls.py
Normal file
12
todos/urls.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from django.urls import path, include
|
||||
from rest_framework.routers import DefaultRouter
|
||||
from .views import TodoViewSet, register_user, login_user
|
||||
|
||||
router = DefaultRouter()
|
||||
router.register(r'todos', TodoViewSet, basename='todo')
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
path('register/', register_user, name='register'),
|
||||
path('login/', login_user, name='login'),
|
||||
]
|
51
todos/views.py
Normal file
51
todos/views.py
Normal file
|
@ -0,0 +1,51 @@
|
|||
from rest_framework import viewsets, permissions, status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
from django.contrib.auth import authenticate
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from .models import Todo
|
||||
from .serializers import TodoSerializer, UserSerializer
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@api_view(['POST'])
|
||||
@permission_classes([permissions.AllowAny])
|
||||
def register_user(request):
|
||||
serializer = UserSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
user = serializer.save()
|
||||
refresh = RefreshToken.for_user(user)
|
||||
return Response({
|
||||
'token': str(refresh.access_token),
|
||||
})
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
@api_view(['POST'])
|
||||
@permission_classes([permissions.AllowAny])
|
||||
def login_user(request):
|
||||
username = request.data.get('username')
|
||||
password = request.data.get('password')
|
||||
user = authenticate(username=username, password=password)
|
||||
if user:
|
||||
refresh = RefreshToken.for_user(user)
|
||||
return Response({
|
||||
'token': str(refresh.access_token),
|
||||
})
|
||||
return Response({'error': 'Invalid credentials'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
class TodoViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = TodoSerializer
|
||||
permission_classes = [permissions.IsAuthenticated]
|
||||
|
||||
def get_queryset(self):
|
||||
print(f"user query: {self.request.user.username}")
|
||||
logger.info(f"query by user: {self.request.user.username}")
|
||||
return Todo.objects.filter(user=self.request.user)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
todo = serializer.save(user=self.request.user)
|
||||
serializer.save(user=self.request.user)
|
||||
print(f"New todo added - Title: '{todo.title}' by user: {self.request.user.username}")
|
||||
logger.info(f"New todo added - Title: '{todo.title}' by user: {self.request.user.username}")
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue