commit 351c733d82f4bc10a2ed3cd456d7834a2896b352 Author: dzecevic Date: Thu Feb 8 22:52:57 2024 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1cf0110 --- /dev/null +++ b/.gitignore @@ -0,0 +1,138 @@ +# Django # +*.log +*.pot +*.pyc +__pycache__ +db.sqlite3 +media + +# Backup files # +*.bak + +# If you are using PyCharm # +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# File-based project format +*.iws + +# IntelliJ +out/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Python # +*.py[cod] +*$py.class + +# Distribution / packaging +.Python build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.whl +*.egg-info/ +.installed.cfg +*.egg +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +.pytest_cache/ +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery +celerybeat-schedule.* + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# Sublime Text # +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache +*.sublime-workspace +*.sublime-project + +# sftp configuration file +sftp-config.json + +# Package control specific files Package +Control.last-run +Control.ca-list +Control.ca-bundle +Control.system-ca-bundle +GitHub.sublime-settings + +# Visual Studio Code # +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +.history \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5c299b --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +### ```python3 -m venv back_venv``` +Creates python virutal env + +### ```. back_venv/bin/activate``` +uses venv + +### ```python manage.py runserver``` +launches the development server at ```127.0.0.1:8000``` + +### ```python manage.py runserver ``` +launches the development server at a custom port + +### ```python manage.py startapp ``` +creates a new app in the current directory + +### ```python manage.py createsuperuser``` +starts admin user creation process in terminal + +### ```pip install -r requirments.txt``` +installs requirements filed diff --git a/backend/__init__.py b/backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/asgi.py b/backend/asgi.py new file mode 100644 index 0000000..023dea8 --- /dev/null +++ b/backend/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for backend project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") + +application = get_asgi_application() diff --git a/backend/settings.py b/backend/settings.py new file mode 100644 index 0000000..40c3805 --- /dev/null +++ b/backend/settings.py @@ -0,0 +1,177 @@ +""" +Django settings for backend project. + +Generated by 'django-admin startproject' using Django 4.1.1. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.1/ref/settings/ +""" + +from pathlib import Path +from datetime import timedelta + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = Path(__file__).resolve().parent.parent + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "django-insecure-o&qo$m$13v0*=0t+#l&$j^vnvpz@vitmabf6h#b@!@4zy1w!qx" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "rest_framework", + 'rest_framework_simplejwt.token_blacklist', + 'corsheaders', + 'base', +] + +CORS_ALLOW_ALL_ORIGINS = True + +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'rest_framework_simplejwt.authentication.JWTAuthentication', + ) +} + +SIMPLE_JWT = { + "ACCESS_TOKEN_LIFETIME": timedelta(minutes=5), + "REFRESH_TOKEN_LIFETIME": timedelta(days=15), + "ROTATE_REFRESH_TOKENS": True, + "BLACKLIST_AFTER_ROTATION": True, + "UPDATE_LAST_LOGIN": False, + + "ALGORITHM": "HS256", + "SIGNING_KEY": SECRET_KEY, + "VERIFYING_KEY": "", + "AUDIENCE": None, + "ISSUER": None, + "JSON_ENCODER": None, + "JWK_URL": None, + "LEEWAY": 0, + + "AUTH_HEADER_TYPES": ("Bearer",), + "AUTH_HEADER_NAME": "HTTP_AUTHORIZATION", + "USER_ID_FIELD": "id", + "USER_ID_CLAIM": "user_id", + "USER_AUTHENTICATION_RULE": "rest_framework_simplejwt.authentication.default_user_authentication_rule", + + "AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",), + "TOKEN_TYPE_CLAIM": "token_type", + "TOKEN_USER_CLASS": "rest_framework_simplejwt.models.TokenUser", + + "JTI_CLAIM": "jti", + + "SLIDING_TOKEN_REFRESH_EXP_CLAIM": "refresh_exp", + "SLIDING_TOKEN_LIFETIME": timedelta(minutes=5), + "SLIDING_TOKEN_REFRESH_LIFETIME": timedelta(days=1), + + "TOKEN_OBTAIN_SERIALIZER": "rest_framework_simplejwt.serializers.MyTokenObtainPairSerializer", + "TOKEN_REFRESH_SERIALIZER": "rest_framework_simplejwt.serializers.TokenRefreshSerializer", + "TOKEN_VERIFY_SERIALIZER": "rest_framework_simplejwt.serializers.TokenVerifySerializer", + "TOKEN_BLACKLIST_SERIALIZER": "rest_framework_simplejwt.serializers.TokenBlacklistSerializer", + "SLIDING_TOKEN_OBTAIN_SERIALIZER": "rest_framework_simplejwt.serializers.TokenObtainSlidingSerializer", + "SLIDING_TOKEN_REFRESH_SERIALIZER": "rest_framework_simplejwt.serializers.TokenRefreshSlidingSerializer", +} + +MIDDLEWARE = [ + "corsheaders.middleware.CorsMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "backend.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "backend.wsgi.application" + +# Database +# https://docs.djangoproject.com/en/4.1/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/4.1/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.1/howto/static-files/ + +STATIC_URL = "static/" + +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/backend/urls.py b/backend/urls.py new file mode 100644 index 0000000..d8e6916 --- /dev/null +++ b/backend/urls.py @@ -0,0 +1,23 @@ +"""backend URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.1/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" + +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path('admin/', admin.site.urls), + path('api/', include('base.api.urls')) +] diff --git a/backend/wsgi.py b/backend/wsgi.py new file mode 100644 index 0000000..7d6cf06 --- /dev/null +++ b/backend/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for backend project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") + +application = get_wsgi_application() diff --git a/base/__init__.py b/base/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/base/admin.py b/base/admin.py new file mode 100644 index 0000000..5c015d9 --- /dev/null +++ b/base/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin +from .models import * + +# Register your models here. + +admin.site.register(Profile) \ No newline at end of file diff --git a/base/api/__init__.py b/base/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/base/api/urls.py b/base/api/urls.py new file mode 100644 index 0000000..e72eb8a --- /dev/null +++ b/base/api/urls.py @@ -0,0 +1,14 @@ + +from django.urls import path +from . import views +from .views import MyTokenObtainPairView + +from rest_framework_simplejwt.views import ( + TokenRefreshView, +) + +urlpatterns = [ + path('profile/', views.get_profile), + path('token/', MyTokenObtainPairView.as_view(), name='token_obtain_pair'), + path('token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), +] diff --git a/base/api/views.py b/base/api/views.py new file mode 100644 index 0000000..e24c9ee --- /dev/null +++ b/base/api/views.py @@ -0,0 +1,28 @@ +from rest_framework.response import Response +from rest_framework.decorators import api_view, permission_classes +from rest_framework.permissions import IsAuthenticated + +from rest_framework_simplejwt.serializers import TokenObtainPairSerializer +from rest_framework_simplejwt.views import TokenObtainPairView + +from base.serializer import ProfileSerializer + +class MyTokenObtainPairSerializer(TokenObtainPairSerializer): + @classmethod + def get_token(cls, user): + token = super().get_token(user) + + token['username'] = user.username + + return token + +class MyTokenObtainPairView(TokenObtainPairView): + serializer_class = MyTokenObtainPairSerializer + +@api_view(['GET']) +@permission_classes([IsAuthenticated]) +def get_profile(request): + user = request.user + profile = user.profile + serializer = ProfileSerializer(profile, many=False) + return Response(serializer.data) diff --git a/base/apps.py b/base/apps.py new file mode 100644 index 0000000..0e7956b --- /dev/null +++ b/base/apps.py @@ -0,0 +1,8 @@ +from django.apps import AppConfig + +class BaseConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "base" + + def ready(self): + from .signals import create_profile, save_profile diff --git a/base/migrations/0001_initial.py b/base/migrations/0001_initial.py new file mode 100644 index 0000000..529959d --- /dev/null +++ b/base/migrations/0001_initial.py @@ -0,0 +1,39 @@ +# Generated by Django 4.1.7 on 2023-02-16 18:11 + +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="Profile", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("first_name", models.CharField(max_length=100)), + ( + "user", + models.OneToOneField( + on_delete=django.db.models.deletion.CASCADE, + to=settings.AUTH_USER_MODEL, + ), + ), + ], + ), + ] diff --git a/base/migrations/0002_profile_email_profile_last_name_and_more.py b/base/migrations/0002_profile_email_profile_last_name_and_more.py new file mode 100644 index 0000000..0e80ece --- /dev/null +++ b/base/migrations/0002_profile_email_profile_last_name_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 4.1.7 on 2023-02-16 18:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("base", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="profile", + name="email", + field=models.EmailField(max_length=254, null=True), + ), + migrations.AddField( + model_name="profile", + name="last_name", + field=models.CharField(max_length=100, null=True), + ), + migrations.AlterField( + model_name="profile", + name="first_name", + field=models.CharField(max_length=100, null=True), + ), + ] diff --git a/base/migrations/0003_alter_profile_email_alter_profile_first_name_and_more.py b/base/migrations/0003_alter_profile_email_alter_profile_first_name_and_more.py new file mode 100644 index 0000000..c28b43e --- /dev/null +++ b/base/migrations/0003_alter_profile_email_alter_profile_first_name_and_more.py @@ -0,0 +1,38 @@ +# Generated by Django 5.0.2 on 2024-02-08 21:41 + +import django.db.models.deletion +import django.utils.timezone +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0002_profile_email_profile_last_name_and_more'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AlterField( + model_name='profile', + name='email', + field=models.EmailField(default=django.utils.timezone.now, max_length=254), + preserve_default=False, + ), + migrations.AlterField( + model_name='profile', + name='first_name', + field=models.CharField(max_length=100), + ), + migrations.AlterField( + model_name='profile', + name='last_name', + field=models.CharField(max_length=100), + ), + migrations.AlterField( + model_name='profile', + name='user', + field=models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='profile', to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/base/migrations/__init__.py b/base/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/base/models.py b/base/models.py new file mode 100644 index 0000000..d9d5665 --- /dev/null +++ b/base/models.py @@ -0,0 +1,11 @@ +from django.db import models +from django.contrib.auth.models import User + +class Profile(models.Model): + user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile') + first_name = models.CharField(max_length=100) + last_name = models.CharField(max_length=100) + email = models.EmailField() + + def __str__(self): + return self.user.username diff --git a/base/serializer.py b/base/serializer.py new file mode 100644 index 0000000..f3d7f0a --- /dev/null +++ b/base/serializer.py @@ -0,0 +1,13 @@ +from rest_framework import serializers +from base.models import * + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = '__all__' + +class ProfileSerializer(serializers.ModelSerializer): + user = UserSerializer(many=False, read_only=True) + class Meta: + model = Profile + fields = ('user', 'first_name', 'last_name', 'email') \ No newline at end of file diff --git a/base/signals.py b/base/signals.py new file mode 100644 index 0000000..e3741b1 --- /dev/null +++ b/base/signals.py @@ -0,0 +1,15 @@ +from django.db.models.signals import post_save #Import a post_save signal when a user is created +from django.contrib.auth.models import User # Import the built-in User model, which is a sender +from django.dispatch import receiver # Import the receiver +from .models import Profile + + +@receiver(post_save, sender=User) +def create_profile(sender, instance, created, **kwargs): + if created: + Profile.objects.create(user=instance) + + +@receiver(post_save, sender=User) +def save_profile(sender, instance, **kwargs): + instance.profile.save() \ No newline at end of file diff --git a/base/tests.py b/base/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/base/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/base/views.py b/base/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/base/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..1917e46 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cbea6c6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,9 @@ +asgiref==3.7.2 +Django==5.0.2 +django-cors-headers==4.3.1 +djangorestframework==3.14.0 +djangorestframework-simplejwt==5.3.1 +PyJWT==2.8.0 +pytz==2024.1 +sqlparse==0.4.4 +typing_extensions==4.9.0