From 35fc3960502dd386d8bc03cc3f8d95fe3df9e797 Mon Sep 17 00:00:00 2001 From: Alexander Laevens Date: Tue, 22 Nov 2022 16:16:34 -0700 Subject: [PATCH] Complete primitive backend --- django_backend/.gitignore | 256 ++++++++++++++++++ django_backend/api/__init__.py | 0 django_backend/api/admin.py | 43 +++ django_backend/api/apps.py | 6 + django_backend/api/migrations/0001_initial.py | 47 ++++ .../api/migrations/0002_homegroup_name.py | 19 ++ django_backend/api/migrations/__init__.py | 0 django_backend/api/models.py | 33 +++ django_backend/api/serializers.py | 24 ++ django_backend/api/tests.py | 3 + django_backend/api/urls.py | 12 + django_backend/api/views.py | 16 ++ django_backend/django_backend/__init__.py | 0 django_backend/django_backend/asgi.py | 16 ++ django_backend/django_backend/settings.py | 136 ++++++++++ django_backend/django_backend/urls.py | 23 ++ django_backend/django_backend/wsgi.py | 16 ++ django_backend/manage.py | 22 ++ django_backend/users/__init__.py | 0 django_backend/users/admin.py | 31 +++ django_backend/users/apps.py | 6 + .../users/migrations/0001_initial.py | 47 ++++ django_backend/users/migrations/__init__.py | 0 django_backend/users/models.py | 6 + django_backend/users/serializers.py | 28 ++ django_backend/users/tests.py | 3 + django_backend/users/urls.py | 11 + django_backend/users/views.py | 36 +++ planning/backend-structure.dia | Bin 0 -> 2002 bytes 29 files changed, 840 insertions(+) create mode 100644 django_backend/.gitignore create mode 100644 django_backend/api/__init__.py create mode 100644 django_backend/api/admin.py create mode 100644 django_backend/api/apps.py create mode 100644 django_backend/api/migrations/0001_initial.py create mode 100644 django_backend/api/migrations/0002_homegroup_name.py create mode 100644 django_backend/api/migrations/__init__.py create mode 100644 django_backend/api/models.py create mode 100644 django_backend/api/serializers.py create mode 100644 django_backend/api/tests.py create mode 100644 django_backend/api/urls.py create mode 100644 django_backend/api/views.py create mode 100644 django_backend/django_backend/__init__.py create mode 100644 django_backend/django_backend/asgi.py create mode 100644 django_backend/django_backend/settings.py create mode 100644 django_backend/django_backend/urls.py create mode 100644 django_backend/django_backend/wsgi.py create mode 100755 django_backend/manage.py create mode 100644 django_backend/users/__init__.py create mode 100644 django_backend/users/admin.py create mode 100644 django_backend/users/apps.py create mode 100644 django_backend/users/migrations/0001_initial.py create mode 100644 django_backend/users/migrations/__init__.py create mode 100644 django_backend/users/models.py create mode 100644 django_backend/users/serializers.py create mode 100644 django_backend/users/tests.py create mode 100644 django_backend/users/urls.py create mode 100644 django_backend/users/views.py create mode 100644 planning/backend-structure.dia diff --git a/django_backend/.gitignore b/django_backend/.gitignore new file mode 100644 index 0000000..512d4bb --- /dev/null +++ b/django_backend/.gitignore @@ -0,0 +1,256 @@ +# Created by https://www.toptal.com/developers/gitignore/api/django,python +# Edit at https://www.toptal.com/developers/gitignore?templates=django,python + +### Django ### +*.log +*.pot +*.pyc +__pycache__/ +local_settings.py +db.sqlite3 +db.sqlite3-journal +media + +# If your build process includes running collectstatic, then you probably don't need or want to include staticfiles/ +# in your Git repository. Update and uncomment the following line accordingly. +# /staticfiles/ + +### Django.Python Stack ### +# Byte-compiled / optimized / DLL files +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo + +# Django stuff: + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +### Python ### +# Byte-compiled / optimized / DLL files + +# C extensions + +# Distribution / packaging + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. + +# Installer logs + +# Unit test / coverage reports + +# Translations + +# Django stuff: + +# Flask stuff: + +# Scrapy stuff: + +# Sphinx documentation + +# PyBuilder + +# Jupyter Notebook + +# IPython + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm + +# Celery stuff + +# SageMath parsed files + +# Environments + +# Spyder project settings + +# Rope project settings + +# mkdocs documentation + +# mypy + +# Pyre type checker + +# pytype static type analyzer + +# Cython debug symbols + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. + +# End of https://www.toptal.com/developers/gitignore/api/django,python diff --git a/django_backend/api/__init__.py b/django_backend/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_backend/api/admin.py b/django_backend/api/admin.py new file mode 100644 index 0000000..4d7ec65 --- /dev/null +++ b/django_backend/api/admin.py @@ -0,0 +1,43 @@ +from django.contrib import admin +from django.contrib.contenttypes.admin import GenericTabularInline +from users.models import User +import nested_admin + +from api.models import * +# Register your models here. + +class IngredientInline(nested_admin.NestedGenericTabularInline): + model = Ingredient + extra = 0 + +class UserInline(nested_admin.NestedTabularInline): + model = User + fields = ("username",) + readonly_fields = ("username",) + extra = 0 + + def has_add_permission(self, request, obj=None): + return False + + def has_delete_permission(self, request, obj=None): + return False + +class RecipeInline(nested_admin.NestedTabularInline): + model = Recipe + inlines = (IngredientInline,) + extra = 0 + +@admin.register(Recipe) +class RecipeAdmin(nested_admin.NestedModelAdmin): + list_display = ("name",) + inlines = (IngredientInline,) + +@admin.register(Homegroup) +class HomegroupAdmin(nested_admin.NestedModelAdmin): + list_display = ("id", "name") + inlines = (UserInline, RecipeInline, IngredientInline) + +@admin.register(List) +class ListAdmin(nested_admin.NestedModelAdmin): + list_display = ("id",) + inlines = (RecipeInline, IngredientInline) diff --git a/django_backend/api/apps.py b/django_backend/api/apps.py new file mode 100644 index 0000000..66656fd --- /dev/null +++ b/django_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/django_backend/api/migrations/0001_initial.py b/django_backend/api/migrations/0001_initial.py new file mode 100644 index 0000000..961ae9d --- /dev/null +++ b/django_backend/api/migrations/0001_initial.py @@ -0,0 +1,47 @@ +# Generated by Django 4.1.3 on 2022-11-22 20:42 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('contenttypes', '0002_remove_content_type_name'), + ] + + operations = [ + migrations.CreateModel( + name='Homegroup', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + ), + migrations.CreateModel( + name='List', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + ), + migrations.CreateModel( + name='Recipe', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=50)), + ('homegroup', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='recipes', to='api.homegroup')), + ('list', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='recipes', to='api.list')), + ], + ), + migrations.CreateModel( + name='Ingredient', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('object_id', models.PositiveBigIntegerField()), + ('name', models.CharField(max_length=50)), + ('in_stock', models.BooleanField(default=False)), + ('content_type', models.ForeignKey(limit_choices_to=models.Q(models.Q(('app_label', 'api'), ('model', 'recipe')), models.Q(('app_label', 'api'), ('model', 'list')), _connector='OR'), on_delete=django.db.models.deletion.CASCADE, to='contenttypes.contenttype')), + ], + ), + ] diff --git a/django_backend/api/migrations/0002_homegroup_name.py b/django_backend/api/migrations/0002_homegroup_name.py new file mode 100644 index 0000000..9213778 --- /dev/null +++ b/django_backend/api/migrations/0002_homegroup_name.py @@ -0,0 +1,19 @@ +# Generated by Django 4.1.3 on 2022-11-22 21:43 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='homegroup', + name='name', + field=models.CharField(default="Alex's Kitchen", max_length=50), + preserve_default=False, + ), + ] diff --git a/django_backend/api/migrations/__init__.py b/django_backend/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_backend/api/models.py b/django_backend/api/models.py new file mode 100644 index 0000000..1995543 --- /dev/null +++ b/django_backend/api/models.py @@ -0,0 +1,33 @@ +from django.db import models +from django.contrib.contenttypes.models import ContentType +from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation + +# Create your models here. +class Ingredient(models.Model): + limit = models.Q(app_label="api", model="recipe") | models.Q(app_label="api", model="list") + content_type = models.ForeignKey(ContentType, limit_choices_to=limit, on_delete=models.CASCADE) + object_id = models.PositiveBigIntegerField() + content_object = GenericForeignKey() + + + name = models.CharField(max_length=50) + in_stock = models.BooleanField(default=False) + +class List(models.Model): + # Foreign Key Recipe -> List [as recipes] + extra_ingredients = GenericRelation(Ingredient, related_query_name="extra_ingredients") + + +class Homegroup(models.Model): + # Foreign Key Recipe -> Homegroup [as recipes] + # Foreign Key User -> Homegroup [as users] + name = models.CharField(max_length=50) + + +class Recipe(models.Model): + name = models.CharField(max_length=50) + homegroup = models.ForeignKey(Homegroup, related_name="recipes", on_delete=models.CASCADE) + list = models.ForeignKey(List, related_name="recipes", on_delete=models.SET_NULL, blank=True, null=True) + ingredients = GenericRelation(Ingredient, related_query_name="ingredients") + + diff --git a/django_backend/api/serializers.py b/django_backend/api/serializers.py new file mode 100644 index 0000000..8a0948a --- /dev/null +++ b/django_backend/api/serializers.py @@ -0,0 +1,24 @@ +from rest_framework import serializers +from api.models import * + +class RecipeSerializer(serializers.ModelSerializer): + ingredients = serializers.PrimaryKeyRelatedField(many=True, read_only=True) + + class Meta: + model = Recipe + fields = ["id", "name", "ingredients"] + read_only_fields = ["id"] + +class IngredientSerializer(serializers.ModelSerializer): + class Meta: + model = Ingredient + fields = ["id", "name", "in_stock"] + read_only_fields = ["id"] + +class HomegroupSerializer(serializers.ModelSerializer): + users = serializers.PrimaryKeyRelatedField(many=True, read_only=True) + recipes = serializers.PrimaryKeyRelatedField(many=True, read_only=True) + + class Meta: + model = Homegroup + fields = ["id", "recipes", "users"] \ No newline at end of file diff --git a/django_backend/api/tests.py b/django_backend/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/django_backend/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/django_backend/api/urls.py b/django_backend/api/urls.py new file mode 100644 index 0000000..7daa8e5 --- /dev/null +++ b/django_backend/api/urls.py @@ -0,0 +1,12 @@ +from django.urls import include, path +from rest_framework import routers +from api import views + +router = routers.DefaultRouter() +router.register(r'recipes', views.RecipeView) +router.register(r'ingredients', views.IngredientView) +router.register(r'homegroups', views.HomegroupView) + +urlpatterns = [ + path('', include(router.urls)) +] \ No newline at end of file diff --git a/django_backend/api/views.py b/django_backend/api/views.py new file mode 100644 index 0000000..49e5b88 --- /dev/null +++ b/django_backend/api/views.py @@ -0,0 +1,16 @@ +from rest_framework import viewsets + +from api.serializers import * +from api.models import * + +class RecipeView(viewsets.ModelViewSet): + serializer_class = RecipeSerializer + queryset = Recipe.objects.all() + +class IngredientView(viewsets.ModelViewSet): + serializer_class = IngredientSerializer + queryset = Ingredient.objects.all() + +class HomegroupView(viewsets.ModelViewSet): + serializer_class = HomegroupSerializer + queryset = Homegroup.objects.all() diff --git a/django_backend/django_backend/__init__.py b/django_backend/django_backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_backend/django_backend/asgi.py b/django_backend/django_backend/asgi.py new file mode 100644 index 0000000..4812516 --- /dev/null +++ b/django_backend/django_backend/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for django_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', 'django_backend.settings') + +application = get_asgi_application() diff --git a/django_backend/django_backend/settings.py b/django_backend/django_backend/settings.py new file mode 100644 index 0000000..93ab1b6 --- /dev/null +++ b/django_backend/django_backend/settings.py @@ -0,0 +1,136 @@ +""" +Django settings for django_backend project. + +Generated by 'django-admin startproject' using Django 4.1.3. + +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 + +# 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-tz%&(g*jikac%ogq%vaf&%i!6m99q_lshu9g-&sz&bw8x!&zk3' + +AUTH_USER_MODEL = 'users.User' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + +REST_FRAMEWORK = { + 'DEFAULT_AUTHENTICATION_CLASSES': [ + 'rest_framework.authentication.TokenAuthentication', + 'rest_framework.authentication.SessionAuthentication', + ] +} + +# Application definition + +INSTALLED_APPS = [ + 'api', + 'users', + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'rest_framework', + 'rest_framework.authtoken', + 'nested_admin' +] + +MIDDLEWARE = [ + '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 = 'django_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 = 'django_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/django_backend/django_backend/urls.py b/django_backend/django_backend/urls.py new file mode 100644 index 0000000..4232d93 --- /dev/null +++ b/django_backend/django_backend/urls.py @@ -0,0 +1,23 @@ +"""django_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("api.urls")), + path('auth/', include("users.urls")) +] diff --git a/django_backend/django_backend/wsgi.py b/django_backend/django_backend/wsgi.py new file mode 100644 index 0000000..653f066 --- /dev/null +++ b/django_backend/django_backend/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for django_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', 'django_backend.settings') + +application = get_wsgi_application() diff --git a/django_backend/manage.py b/django_backend/manage.py new file mode 100755 index 0000000..ea62d66 --- /dev/null +++ b/django_backend/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', 'django_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/django_backend/users/__init__.py b/django_backend/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_backend/users/admin.py b/django_backend/users/admin.py new file mode 100644 index 0000000..41a20df --- /dev/null +++ b/django_backend/users/admin.py @@ -0,0 +1,31 @@ +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin +from .models import User + +class CustomUserAdmin(UserAdmin): + fieldsets = ( + (None, {"fields": ("username", "password", "homegroup")}), + ("Personal info", {"fields": ("first_name", "last_name", "email")}), + ( + "Permissions", + { + "fields": ( + "is_active", + "is_staff", + "is_superuser", + ), + }, + ), + ("Important dates", {"fields": ("last_login", "date_joined")}), + ) + add_fieldsets = ( + ( + None, + { + "classes": ("wide",), + "fields": ("username", "password1", "password2", "homegroup"), + }, + ), + ) + +admin.site.register(User, CustomUserAdmin) \ No newline at end of file diff --git a/django_backend/users/apps.py b/django_backend/users/apps.py new file mode 100644 index 0000000..72b1401 --- /dev/null +++ b/django_backend/users/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'users' diff --git a/django_backend/users/migrations/0001_initial.py b/django_backend/users/migrations/0001_initial.py new file mode 100644 index 0000000..e13852a --- /dev/null +++ b/django_backend/users/migrations/0001_initial.py @@ -0,0 +1,47 @@ +# Generated by Django 4.1.3 on 2022-11-22 20:42 + +import django.contrib.auth.models +import django.contrib.auth.validators +from django.db import migrations, models +import django.db.models.deletion +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ('api', '0001_initial'), + ('auth', '0012_alter_user_first_name_max_length'), + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), + ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')), + ('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')), + ('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')), + ('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')), + ('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')), + ('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')), + ('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')), + ('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')), + ('homegroup', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='users', to='api.homegroup')), + ('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')), + ], + options={ + 'verbose_name': 'user', + 'verbose_name_plural': 'users', + 'abstract': False, + }, + managers=[ + ('objects', django.contrib.auth.models.UserManager()), + ], + ), + ] diff --git a/django_backend/users/migrations/__init__.py b/django_backend/users/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/django_backend/users/models.py b/django_backend/users/models.py new file mode 100644 index 0000000..9df4a6b --- /dev/null +++ b/django_backend/users/models.py @@ -0,0 +1,6 @@ +from django.contrib.auth.models import AbstractUser +from django.db import models +from api.models import Homegroup + +class User(AbstractUser): + homegroup = models.ForeignKey(Homegroup, related_name="users", on_delete=models.SET_NULL, blank=True, null=True) \ No newline at end of file diff --git a/django_backend/users/serializers.py b/django_backend/users/serializers.py new file mode 100644 index 0000000..417116e --- /dev/null +++ b/django_backend/users/serializers.py @@ -0,0 +1,28 @@ +from rest_framework import serializers +from users.models import User + +class UserSerializer(serializers.ModelSerializer): # https://stackoverflow.com/a/29867704/17834235 + def create(self, validated_data): + user = User.objects.create() + + user.set_password(validated_data["password"]) + validated_data.pop("password") + + for field in validated_data: + setattr(user, field, validated_data[field]) + + user.save() + return user + + def update(self, instance, validated_data): + if "password" in validated_data: + password = validated_data.pop("password") + instance.set_password(password) + + return super().update(instance, validated_data) + + class Meta: + model = User + fields = ("id", "username", "email", "password") + write_only_fields = ("password",) + read_only_fields = ("id",) diff --git a/django_backend/users/tests.py b/django_backend/users/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/django_backend/users/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/django_backend/users/urls.py b/django_backend/users/urls.py new file mode 100644 index 0000000..4aceda4 --- /dev/null +++ b/django_backend/users/urls.py @@ -0,0 +1,11 @@ +from django.urls import path, include +from rest_framework.authtoken import views as authviews +from rest_framework import routers + +from users import views + +urlpatterns = [ + path('token', authviews.obtain_auth_token, name="api-token-auth"), + path('users', views.RegisterUserView.as_view()), # Exposes POST to everyone for registering + path('users/me', views.ModifyUserView.as_view()) # exposes GET / PUT / PATCH / DELETE for registered users for their user object +] diff --git a/django_backend/users/views.py b/django_backend/users/views.py new file mode 100644 index 0000000..cbdbad6 --- /dev/null +++ b/django_backend/users/views.py @@ -0,0 +1,36 @@ +from rest_framework import generics, permissions, views, status +from rest_framework.response import Response +from users import serializers, models + + +class IsOwner(permissions.BasePermission): + def has_object_permission(self, request, view, obj): + if obj == request.user: + return True + else: + return False + + +# Anyone can register +class RegisterUserView(generics.CreateAPIView): + model = models.User + serializer_class = serializers.UserSerializer + permission_classes = [permissions.AllowAny] + + +# Allows user to modify their own data only +class ModifyUserView(generics.RetrieveUpdateDestroyAPIView): + permission_classes = [ + permissions.IsAuthenticated, + IsOwner + ] + model = models.User + serializer_class = serializers.UserSerializer + + def get_object(self): + return models.User.objects.get(pk=self.request.user.id) + + def retrieve(self, request, *args, **kwargs): + user = models.User.objects.get(pk=request.user.id) + serializer = serializers.UserSerializer(user) + return Response(serializer.data) diff --git a/planning/backend-structure.dia b/planning/backend-structure.dia new file mode 100644 index 0000000000000000000000000000000000000000..386f17cecc6b0eec68dd3245239ea2e48f7b9b99 GIT binary patch literal 2002 zcmV;@2QBy?iwFP!000021MOVfZreBzea}}I>T5#lYROHLV!Mk)7wDou(LNir#n{~F z;w9;%e%Wtd>f+dvEL$=qH*QBj99SOBkV6i6=7{9G_us={@I)A=adf8{NY@4=n#Dei z=6Bk+uYcZn+WY&fcRs~$<=>p)a3G(Ec=GK|TL`gyI~+bgKcisHv4|N8=n8Q%{0|2K z9!jC1c7HV(>?-iFz{$P*E*65(=}M46ghO(tP4Vn;&f-<%YnfCoH;aRq4W4jtr@eW| zFKw7B8WuDi(7wcTGGzom)+^WLH6EX)d`VbYakz{*l_FxjtccX;Nj`UEaw#r_qWS%s zPecg9-ekqfZ*KMND{QSH5n?u{QPr08P>{L9M2mwW!@*mSsL|rD<$pTswMm|-FBX&UJvB+DechAxC(7wGAp1&BBhi`qbvNqEJs_PY zwvQ|JxJ+B^MC5zKAF#ixUOw8={{$jgs+ZedxSr_HyW{+=~l-jBci{b_JB_>J>; zMsZSoZ41dwZ9x4y?XzACg|h#oJ_!dZ;hefLbCzWz-FC*;9@2HovCENK^;(^o>9lNx zC6$im0m)I0hs?2a?IP2WhHfn)`3WGW2BTz z6*8$6mr=Gik!oYXrz{(^w5}bL42qLl^@rw@Hdb&l?o5&>>bwF>mT-dW+^_8Ua1{u; zq%$hk^;QgJGwCQ#gS}R^F0vHb7&Df9_SM2^GfdCY47FCGE}LQ6XERLuY6d~Ri^)S= z9f6a3g9n*525VUo!svrG;9{Mi2wlq`{z^l_Z@$DKjufmf2%RrPb^hKYb~^I(;$Sd1HVeZ>e(ZA3t(O3I0>G! zE*ikr`LAA7!cy-DEKNiW(`hAY0fg-g!UmKJK(GxEtO*Df5Udh{H2}djL$F~S#e9ip zq>5sHm*|NS1_%}q><$VYw?VM(k3z7fGJ+i=V|*3{*7A^@FpwJrYdgpoT{;0<=e>GS z2}&Kqa1M#uHga62jpVOMz$#1{hv2L1w{%H>g9QW&2o^Y4K(K&dFB8EUMjHt0{U``) zsY2Kd2(ppeBYd?@iQnvmZyh_g_6XUg#ByUa8kzRx;@7>ds~2;TVeMkgy^^}^8LdP; zA2qI!`S`aJ0RyG^9VY%Tx2;JkZmjqK(=EbD~DNzCje(*T`a&^ zfU^K+0nT0=oOQc{vwA0R_75!Z6mudm?bl>e*bbW2b2RI8wXMrRR^N*Xik5CWS5aatG>rp)w>G>tyuJC(j|~Xv z4%^9#e07)b(F0Y=EWTfJoXUZ1%yq3&%op~&Fc6*DsY&3x7S`fn{m zcHV2lLC)ne4GNxW!Y_=_$SXv*(<8rNo!StG2!(IF=44CY5`aqpE&;d%NP>QPE+ILU z3-|=!6Cjk7LurXQe~#JtW~2q?3kW0#Bp~L1m;+)Ch&dqU6o@%jd2NuwVQb9}%9HwU zEf1MW%jsXm8lcl%)r6U|5*Kx_*sc*XXCSXMm~>Cned^-eIUwqQr~{%7h&o7$ASr^R z2$CX5iXbUEmK1r)N7$JCu2x9(-`XxRRL;qH4LOm5r+&%Z5MR>63?VCmtO&9q$ci8< zf~^p+bOGMD0=WR>0+0(pE&#dk`pJc{@*CGq&Zlbh?zL#?c5}q5X(H>D9x&GMO>7q~ z{55t<{Pl8L7myA>IsoYaqyvx+Kso^F0Hgzi(P{HgG~ptiJ)SL;lBR(`f