From 397591bef38e6843e0ce9acbc1bbcf39f6c40b87 Mon Sep 17 00:00:00 2001 From: HeAndres Date: Sat, 6 Apr 2024 00:45:58 +0200 Subject: [PATCH 1/5] startproject backend & startapp api --- .gitignore | 3 + backend/api/__init__.py | 0 backend/api/admin.py | 3 + backend/api/apps.py | 6 + backend/api/migrations/__init__.py | 0 backend/api/models.py | 3 + backend/api/tests.py | 3 + backend/api/views.py | 3 + backend/backend/__init__.py | 0 .../__pycache__/__init__.cpython-311.pyc | Bin 0 -> 209 bytes .../__pycache__/settings.cpython-311.pyc | Bin 0 -> 2637 bytes backend/backend/asgi.py | 16 +++ backend/backend/settings.py | 123 ++++++++++++++++++ backend/backend/urls.py | 22 ++++ backend/backend/wsgi.py | 16 +++ backend/manage.py | 22 ++++ requirements.txt | 10 ++ 17 files changed, 230 insertions(+) create mode 100644 .gitignore create mode 100644 backend/api/__init__.py create mode 100644 backend/api/admin.py create mode 100644 backend/api/apps.py create mode 100644 backend/api/migrations/__init__.py create mode 100644 backend/api/models.py create mode 100644 backend/api/tests.py create mode 100644 backend/api/views.py create mode 100644 backend/backend/__init__.py create mode 100644 backend/backend/__pycache__/__init__.cpython-311.pyc create mode 100644 backend/backend/__pycache__/settings.cpython-311.pyc create mode 100644 backend/backend/asgi.py create mode 100644 backend/backend/settings.py create mode 100644 backend/backend/urls.py create mode 100644 backend/backend/wsgi.py create mode 100755 backend/manage.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba938de --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +env/* + +.env \ No newline at end of file diff --git a/backend/api/__init__.py b/backend/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/api/admin.py b/backend/api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/backend/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/backend/api/apps.py b/backend/api/apps.py new file mode 100644 index 0000000..66656fd --- /dev/null +++ b/backend/api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'api' diff --git a/backend/api/migrations/__init__.py b/backend/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/api/models.py b/backend/api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/backend/api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/backend/api/tests.py b/backend/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/backend/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/backend/api/views.py b/backend/api/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/backend/api/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/backend/backend/__init__.py b/backend/backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/backend/__pycache__/__init__.cpython-311.pyc b/backend/backend/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..25402100447a7979a367df168ceb77016f693d19 GIT binary patch literal 209 zcmZ3^%ge<81bMXrX(0MBh=2h`DC095kTIPhg&~+hlhJP_LlF~@{~09tD@8veKQ~oB zF)yVkwOHRJwK%&Zzd%1AwWv5hFEK|ypeVmGHMu0eIKBWX1XPuhm6(^FuUnLwm|UV; zQd*K)=dU}j`w M{J;PsikN|708o-R00000 literal 0 HcmV?d00001 diff --git a/backend/backend/__pycache__/settings.cpython-311.pyc b/backend/backend/__pycache__/settings.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..56a53689b027c8aef5470bb3792cc734bcb22b2f GIT binary patch literal 2637 zcmb7G%Wo4$7@zg~9mg1ApshB70;YHkBD7J3Qa1J`SU9$uwMn~|)y6Y+Hra>ST@$iW zB_CUOTV&QlmXZ>ZQG{#EnzG^&_zyq^7I!H#6URely>Ue@!N12tMt* zbmw^jp})MzcO7#go3i%N65&tU+q2J**`hE#aM$`Q(nEZas z*bPpGCnF(*{X!TAgh&?&(JsQlmy{5LJ_LOnhlKoxGH>IR`@4cx1eCF>du&DxP6psbKv5TC?$ zEURaJSuA3`Z8D=6lMLFrS|nN#92PCTC%4VwB;n%7y|qK=PG3=IMK@0=`1f`x!o zHt464(kJHsiDi(^RG@#n{mfHNpdnd1_Uae~Su;tyZ;-;`^6flP@Aa>Kx_>9%C3{N; zwz6GNmKHnh57;GHN?4?;};a$ft^-$jVTSg|4=oJ4pRd1SSI^Zs$b0HA^~{Q zd9W}C+~oG~AWj+1E@A@JwDHCWzA3BEKd=ZzZtv|v6+tmD->eyuO6ooD)SFXwiE)`R zeM2$r;@FEt)Sd#B#*C$tN?^*vqHB)Drjr?N|1(R38fimPx?$!6_KjILr?nVNw)eDh8?q*=eYHxouC;Sw|J7OTM`h9&=Xm1a!C4*D`YJKx_Gu46q80k4<8L)fb{Ot> zD3_xB@z^aOsp^<0CbKGcp`7$JnJBoHUvR>r2-mkLI^iB%l8U@-_yJlcB8uL{;_(e) z>dI5%gnDqNXqFRMWqHoqX(z^WrA@9WKH|Q0f@N;CweBR@YPG(_mBoj3zR5eON{w#< z3m9-bcqg_|DVM9<7Q4wgiOqVwDYiDNrFw16i8i^7MwM-Hyp!JI*DIcWwNhfeljKCp zY?JlE@=lIzH6Mx%mgl$Xn`QAaTdjbw4hJw0WT($fgdgnbI?{RTQS4k0GBmkjUCgvVI zX2k|EOkI Date: Sat, 6 Apr 2024 00:59:15 +0200 Subject: [PATCH 2/5] set needed settings --- backend/backend/settings.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/backend/backend/settings.py b/backend/backend/settings.py index 978d46e..c1a904d 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -11,6 +11,11 @@ """ from pathlib import Path +from datetime import timedelta +from dotenv import load_dotenv +import os + +load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -25,8 +30,21 @@ # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ["*"] +REST_FRAMEWORK = { + "DEFAULT_AUTHENTICATION_CLASSES": ( + "rest_framework_simplejwt.authentication.JWTAuthentication", + ), + "DEFAULT_PERMISSION_CLASSES": [ + "rest_framework.permissions.IsAuthenticated", + ], +} + +SIMPLE_JWT = { + "ACCESS_TOKEN_LIFETIME": timedelta(minutes=30), + "REFRESH_TOKEN_LIFETIME": timedelta(days=1), +} # Application definition @@ -37,6 +55,9 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + "api", + "rest_framework", + "corsheaders" ] MIDDLEWARE = [ @@ -47,6 +68,7 @@ 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', + "corsheaders.middleware.CorsMiddleware" ] ROOT_URLCONF = 'backend.urls' @@ -121,3 +143,6 @@ # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +CORS_ALLOW_ALL_ORIGINS = True +CORS_ALLOW_CREDENTIALS = True \ No newline at end of file From f9d1dc667b5646d9abdbde2b9e26cc19be41703a Mon Sep 17 00:00:00 2001 From: HeAndres Date: Sat, 6 Apr 2024 11:10:51 +0200 Subject: [PATCH 3/5] connect auth routes --- .gitignore | 6 +++++- backend/api/serializers.py | 12 ++++++++++++ backend/api/views.py | 10 ++++++++++ backend/backend/urls.py | 9 ++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 backend/api/serializers.py diff --git a/.gitignore b/.gitignore index ba938de..7903373 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,7 @@ env/* -.env \ No newline at end of file +.env + +*.pyc + +*.sqlite3 \ No newline at end of file diff --git a/backend/api/serializers.py b/backend/api/serializers.py new file mode 100644 index 0000000..e083fc2 --- /dev/null +++ b/backend/api/serializers.py @@ -0,0 +1,12 @@ +from django.contrib.auth.models import User +from rest_framework import serializers + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ["id", "username", "password"] + extra_kwargs = {"password": {"write_only":True}} + + def create(self, validated_data): + user = User.objects.create_user(**validated_data) + return user \ No newline at end of file diff --git a/backend/api/views.py b/backend/api/views.py index 91ea44a..3eb89d0 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -1,3 +1,13 @@ from django.shortcuts import render +from django.contrib.auth.models import User +from rest_framework import generics +from rest_framework.permissions import IsAuthenticated, AllowAny + +from .serializers import UserSerializer # Create your views here. +class CreateUserView(generics.CreateAPIView): + queryset = User.objects.all() + serializer_class = UserSerializer + permission_classes = [AllowAny] + \ No newline at end of file diff --git a/backend/backend/urls.py b/backend/backend/urls.py index 4392121..3994f1a 100644 --- a/backend/backend/urls.py +++ b/backend/backend/urls.py @@ -15,8 +15,15 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include + +from api.views import CreateUserView +from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView urlpatterns = [ path('admin/', admin.site.urls), + path("api/user/register/", CreateUserView.as_view(), name="register"), + path("api/token/", TokenObtainPairView.as_view(), name = "get_token"), + path("api/token/refresh/", TokenRefreshView.as_view(), name = "refresh"), + path("api-auth/", include("rest_framework.urls")), ] From d4179bd43838c741027a89969d43068f64e426a2 Mon Sep 17 00:00:00 2001 From: HeAndres Date: Sat, 6 Apr 2024 12:24:10 +0200 Subject: [PATCH 4/5] remove pycache --- .../backend/__pycache__/__init__.cpython-311.pyc | Bin 209 -> 0 bytes .../backend/__pycache__/settings.cpython-311.pyc | Bin 2637 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 backend/backend/__pycache__/__init__.cpython-311.pyc delete mode 100644 backend/backend/__pycache__/settings.cpython-311.pyc diff --git a/backend/backend/__pycache__/__init__.cpython-311.pyc b/backend/backend/__pycache__/__init__.cpython-311.pyc deleted file mode 100644 index 25402100447a7979a367df168ceb77016f693d19..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 209 zcmZ3^%ge<81bMXrX(0MBh=2h`DC095kTIPhg&~+hlhJP_LlF~@{~09tD@8veKQ~oB zF)yVkwOHRJwK%&Zzd%1AwWv5hFEK|ypeVmGHMu0eIKBWX1XPuhm6(^FuUnLwm|UV; zQd*K)=dU}j`w M{J;PsikN|708o-R00000 diff --git a/backend/backend/__pycache__/settings.cpython-311.pyc b/backend/backend/__pycache__/settings.cpython-311.pyc deleted file mode 100644 index 56a53689b027c8aef5470bb3792cc734bcb22b2f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 2637 zcmb7G%Wo4$7@zg~9mg1ApshB70;YHkBD7J3Qa1J`SU9$uwMn~|)y6Y+Hra>ST@$iW zB_CUOTV&QlmXZ>ZQG{#EnzG^&_zyq^7I!H#6URely>Ue@!N12tMt* zbmw^jp})MzcO7#go3i%N65&tU+q2J**`hE#aM$`Q(nEZas z*bPpGCnF(*{X!TAgh&?&(JsQlmy{5LJ_LOnhlKoxGH>IR`@4cx1eCF>du&DxP6psbKv5TC?$ zEURaJSuA3`Z8D=6lMLFrS|nN#92PCTC%4VwB;n%7y|qK=PG3=IMK@0=`1f`x!o zHt464(kJHsiDi(^RG@#n{mfHNpdnd1_Uae~Su;tyZ;-;`^6flP@Aa>Kx_>9%C3{N; zwz6GNmKHnh57;GHN?4?;};a$ft^-$jVTSg|4=oJ4pRd1SSI^Zs$b0HA^~{Q zd9W}C+~oG~AWj+1E@A@JwDHCWzA3BEKd=ZzZtv|v6+tmD->eyuO6ooD)SFXwiE)`R zeM2$r;@FEt)Sd#B#*C$tN?^*vqHB)Drjr?N|1(R38fimPx?$!6_KjILr?nVNw)eDh8?q*=eYHxouC;Sw|J7OTM`h9&=Xm1a!C4*D`YJKx_Gu46q80k4<8L)fb{Ot> zD3_xB@z^aOsp^<0CbKGcp`7$JnJBoHUvR>r2-mkLI^iB%l8U@-_yJlcB8uL{;_(e) z>dI5%gnDqNXqFRMWqHoqX(z^WrA@9WKH|Q0f@N;CweBR@YPG(_mBoj3zR5eON{w#< z3m9-bcqg_|DVM9<7Q4wgiOqVwDYiDNrFw16i8i^7MwM-Hyp!JI*DIcWwNhfeljKCp zY?JlE@=lIzH6Mx%mgl$Xn`QAaTdjbw4hJw0WT($fgdgnbI?{RTQS4k0GBmkjUCgvVI zX2k|EOkI Date: Sat, 6 Apr 2024 12:24:31 +0200 Subject: [PATCH 5/5] finish note creation api --- .gitignore | 4 ++-- backend/api/migrations/0001_initial.py | 27 +++++++++++++++++++++++ backend/api/models.py | 12 +++++++++++ backend/api/serializers.py | 9 +++++++- backend/api/urls.py | 7 ++++++ backend/api/views.py | 30 ++++++++++++++++++++++++-- backend/backend/urls.py | 1 + 7 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 backend/api/migrations/0001_initial.py create mode 100644 backend/api/urls.py diff --git a/.gitignore b/.gitignore index 7903373..90f073c 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,6 @@ env/* .env -*.pyc +**.pyc -*.sqlite3 \ No newline at end of file +*.sqlite3 diff --git a/backend/api/migrations/0001_initial.py b/backend/api/migrations/0001_initial.py new file mode 100644 index 0000000..a201350 --- /dev/null +++ b/backend/api/migrations/0001_initial.py @@ -0,0 +1,27 @@ +# Generated by Django 5.0.4 on 2024-04-06 10:08 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.CreateModel( + name='Note', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=100)), + ('content', models.TextField()), + ('created_at', models.DateTimeField(auto_now_add=True)), + ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notes', to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/backend/api/models.py b/backend/api/models.py index 71a8362..ce56d40 100644 --- a/backend/api/models.py +++ b/backend/api/models.py @@ -1,3 +1,15 @@ from django.db import models +from django.contrib.auth.models import User # Create your models here. + +class Note(models.Model): + title = models.CharField(max_length=100) + content = models.TextField() + created_at = models.DateTimeField(auto_now_add=True) + author = models.ForeignKey(User, on_delete=models.CASCADE, related_name="notes") + + def __str__(self): + return self.title + + \ No newline at end of file diff --git a/backend/api/serializers.py b/backend/api/serializers.py index e083fc2..402ffcd 100644 --- a/backend/api/serializers.py +++ b/backend/api/serializers.py @@ -1,5 +1,6 @@ from django.contrib.auth.models import User from rest_framework import serializers +from .models import Note class UserSerializer(serializers.ModelSerializer): class Meta: @@ -9,4 +10,10 @@ class Meta: def create(self, validated_data): user = User.objects.create_user(**validated_data) - return user \ No newline at end of file + return user + +class NoteSerializer(serializers.ModelSerializer): + class Meta: + model = Note + fields = ["id", "title", "content", "created_at", "author"] + extra_kwargs = {"author": {"read_only":True}} \ No newline at end of file diff --git a/backend/api/urls.py b/backend/api/urls.py new file mode 100644 index 0000000..2511575 --- /dev/null +++ b/backend/api/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path("notes/", views.NoteListCreate.as_view(), name = "note-list"), + path("notes/delete//", views.NoteDelete.as_view(), name="delete-note") +] \ No newline at end of file diff --git a/backend/api/views.py b/backend/api/views.py index 3eb89d0..bfe4365 100644 --- a/backend/api/views.py +++ b/backend/api/views.py @@ -3,11 +3,37 @@ from rest_framework import generics from rest_framework.permissions import IsAuthenticated, AllowAny -from .serializers import UserSerializer +from .serializers import UserSerializer, NoteSerializer +from .models import Note # Create your views here. class CreateUserView(generics.CreateAPIView): queryset = User.objects.all() serializer_class = UserSerializer permission_classes = [AllowAny] - \ No newline at end of file + +class NoteListCreate(generics.ListAPIView): + """ + This API View will allow users to retrieve the list of notes and also to + create a note + """ + serializer_class = NoteSerializer + permission_classes = [IsAuthenticated] + + def get_queryset(self): + user = self.request.user + return Note.objects.filter(author=user) + + def perform_create(self, serializer): + if serializer.is_valid(): + serializer.save(author=self.request) + else: + print(serializer.errors) + +class NoteDelete(generics.DestroyAPIView): + serializer_class = NoteSerializer + permission_classes = [IsAuthenticated] + + def get_queryset(self): + user = self.request.user + return Note.objects.filter(author = user) \ No newline at end of file diff --git a/backend/backend/urls.py b/backend/backend/urls.py index 3994f1a..7a8abf8 100644 --- a/backend/backend/urls.py +++ b/backend/backend/urls.py @@ -26,4 +26,5 @@ path("api/token/", TokenObtainPairView.as_view(), name = "get_token"), path("api/token/refresh/", TokenRefreshView.as_view(), name = "refresh"), path("api-auth/", include("rest_framework.urls")), + path('api/', include("api.urls")) ]