Skip to content

Commit

Permalink
Quickstart guide followed
Browse files Browse the repository at this point in the history
  • Loading branch information
ineshbose committed Dec 22, 2021
1 parent 4291290 commit df9c1dd
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 169 deletions.
285 changes: 147 additions & 138 deletions src/backend/portion_mate/settings.py
Original file line number Diff line number Diff line change
@@ -1,138 +1,147 @@
from pathlib import Path
from environ import Env
from email.utils import getaddresses
from django.core.management.utils import get_random_secret_key


# Initialise environment object
env = Env()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Take environment variables from .env file
Env.read_env(BASE_DIR / ".env")

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env("SECRET_KEY", default=get_random_secret_key())

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("DEBUG", default=False)

ADMINS = getaddresses(env("ADMINS", default=[]))

ALLOWED_HOSTS = env("ALLOWED_HOSTS", default=[])


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"main.apps.MainConfig",
"rest.apps.RestConfig",
"django_cleanup.apps.CleanupConfig",
"django_extensions.apps.DjangoExtensionsConfig",
]

AUTH_USER_MODEL = "main.User"

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 = "portion_mate.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 = "portion_mate.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/topics/i18n/

LANGUAGE_CODE = env("LANGUAGE_CODE", default="en-gb")

TIME_ZONE = env("TIME_ZONE", default="Europe/London")

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static & Media files (CSS, JavaScript, Images)

STATIC_URL = "/static/"

MEDIA_URL = "/media/"

STATICFILES_DIR = Path.joinpath(BASE_DIR, "static/")

STATIC_ROOT = Path.joinpath(BASE_DIR, "static/")

MEDIA_ROOT = Path.joinpath(BASE_DIR, "media/")


# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
from pathlib import Path
from environ import Env
from email.utils import getaddresses
from django.core.management.utils import get_random_secret_key


# Initialise environment object
env = Env()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Take environment variables from .env file
Env.read_env(BASE_DIR / ".env")

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env("SECRET_KEY", default=get_random_secret_key())

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env("DEBUG", default=False)

ADMINS = getaddresses(env("ADMINS", default=[]))

ALLOWED_HOSTS = env("ALLOWED_HOSTS", default=[])


# Application definition

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"main.apps.MainConfig",
"rest.apps.RestConfig",
"django_cleanup.apps.CleanupConfig",
"django_extensions.apps.DjangoExtensionsConfig",
"rest_framework.apps.RestFrameworkConfig",
]

AUTH_USER_MODEL = "main.User"

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 = "portion_mate.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 = "portion_mate.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


# Password validation
# https://docs.djangoproject.com/en/3.2/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/3.2/topics/i18n/

LANGUAGE_CODE = env("LANGUAGE_CODE", default="en-gb")

TIME_ZONE = env("TIME_ZONE", default="Europe/London")

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static & Media files (CSS, JavaScript, Images)

STATIC_URL = "/static/"

MEDIA_URL = "/media/"

STATICFILES_DIR = Path.joinpath(BASE_DIR, "static/")

STATIC_ROOT = Path.joinpath(BASE_DIR, "static/")

MEDIA_ROOT = Path.joinpath(BASE_DIR, "media/")


# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"


# REST Framework

REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 10,
}
35 changes: 18 additions & 17 deletions src/backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
asgiref==3.4.1
black==21.10b0
click==8.0.3
colorama==0.4.4
Django==3.2.8
django-cleanup==5.2.0
django-environ==0.8.1
django-extensions==3.1.5
mypy-extensions==0.4.3
pathspec==0.9.0
Pillow==8.4.0
platformdirs==2.4.0
pytz==2021.3
regex==2021.11.9
sqlparse==0.4.2
tomli==1.2.2
typing-extensions==3.10.0.2
asgiref==3.4.1
black==21.10b0
click==8.0.3
colorama==0.4.4
Django==3.2.8
django-cleanup==5.2.0
django-environ==0.8.1
django-extensions==3.1.5
djangorestframework==3.13.1
mypy-extensions==0.4.3
pathspec==0.9.0
Pillow==8.4.0
platformdirs==2.4.0
pytz==2021.3
regex==2021.11.9
sqlparse==0.4.2
tomli==1.2.2
typing-extensions==3.10.0.2
35 changes: 35 additions & 0 deletions src/backend/rest/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from rest_framework import serializers
from main import models


class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.User
fields = [
"url",
"email",
"first_name",
"last_name",
"picture",
"age",
"height",
"weight",
]


class PortionItemSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.PortionItem
fields = ["url", "name", "is_default"]


class TrackItemSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.TrackItem
fields = ["url", "item", "user", "target", "order", "frequency"]


class UserLogSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = models.UserLog
fields = ["url", "item", "timestamp"]
23 changes: 15 additions & 8 deletions src/backend/rest/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from django.urls import path

from . import views


urlpatterns = [
path("", views.index, name="index"),
]
from django.urls import include, path
from rest_framework import routers
from . import views


router = routers.DefaultRouter()
router.register(r"users", views.UserViewSet)
router.register(r"portionitems", views.PortionItemViewSet)
router.register(r"trackitem", views.TrackItemViewSet)
router.register(r"userlogs", views.UserLogViewSet)

urlpatterns = [
path("", include(router.urls)),
path("auth/", include("rest_framework.urls", namespace="rest_framework")),
]
Loading

0 comments on commit df9c1dd

Please sign in to comment.