From 4f0dcc0a217baa435e933871263c3f019acc530d Mon Sep 17 00:00:00 2001 From: Robert Douglass Date: Wed, 17 Jul 2024 19:01:35 +0200 Subject: [PATCH] Add source code --- .../.environment | 8 + .../.gitignore | 1 + .../.upsun/config.yaml | 154 ++++++++++++++++++ .../myproject/manage.py | 22 +++ .../myproject/myapp/__init__.py | 0 .../myproject/myapp/admin.py | 3 + .../myproject/myapp/apps.py | 6 + .../myproject/myapp/migrations/__init__.py | 0 .../myproject/myapp/models.py | 3 + .../myproject/myapp/tests.py | 3 + .../myproject/myapp/views.py | 3 + .../myproject/myproject/__init__.py | 0 .../__pycache__/__init__.cpython-312.pyc | Bin 0 -> 167 bytes .../__pycache__/settings.cpython-312.pyc | Bin 0 -> 2550 bytes .../__pycache__/urls.cpython-312.pyc | Bin 0 -> 1036 bytes .../__pycache__/wsgi.cpython-312.pyc | Bin 0 -> 659 bytes .../myproject/myproject/asgi.py | 16 ++ .../myproject/myproject/settings.py | 131 +++++++++++++++ .../myproject/myproject/settings_psh.py | 76 +++++++++ .../myproject/myproject/urls.py | 22 +++ .../myproject/myproject/wsgi.py | 16 ++ .../requirements.txt | 6 + ...nstall-django-postgresql-pgvector-upsun.md | 4 +- 23 files changed, 472 insertions(+), 2 deletions(-) create mode 100644 02_install_django_postgresql_pgvector_upsun/.environment create mode 100644 02_install_django_postgresql_pgvector_upsun/.gitignore create mode 100644 02_install_django_postgresql_pgvector_upsun/.upsun/config.yaml create mode 100755 02_install_django_postgresql_pgvector_upsun/myproject/manage.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myapp/__init__.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myapp/admin.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myapp/apps.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myapp/migrations/__init__.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myapp/models.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myapp/tests.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myapp/views.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/__init__.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/__init__.cpython-312.pyc create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/settings.cpython-312.pyc create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/urls.cpython-312.pyc create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/wsgi.cpython-312.pyc create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/asgi.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings_psh.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/urls.py create mode 100644 02_install_django_postgresql_pgvector_upsun/myproject/myproject/wsgi.py create mode 100644 02_install_django_postgresql_pgvector_upsun/requirements.txt diff --git a/02_install_django_postgresql_pgvector_upsun/.environment b/02_install_django_postgresql_pgvector_upsun/.environment new file mode 100644 index 00000000..5f587c7b --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/.environment @@ -0,0 +1,8 @@ +# Set database environment variables +export DB_HOST="$POSTGRESQL_HOST" +export DB_PORT="$POSTGRESQL_PORT" +export DB_PATH="$POSTGRESQL_PATH" +export DB_USERNAME="$POSTGRESQL_USERNAME" +export DB_PASSWORD="$POSTGRESQL_PASSWORD" +export DB_SCHEME="postgresql" +export DATABASE_URL="${DB_SCHEME}://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_PATH}" \ No newline at end of file diff --git a/02_install_django_postgresql_pgvector_upsun/.gitignore b/02_install_django_postgresql_pgvector_upsun/.gitignore new file mode 100644 index 00000000..c3cb9b24 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/.gitignore @@ -0,0 +1 @@ +myenv diff --git a/02_install_django_postgresql_pgvector_upsun/.upsun/config.yaml b/02_install_django_postgresql_pgvector_upsun/.upsun/config.yaml new file mode 100644 index 00000000..592a9f38 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/.upsun/config.yaml @@ -0,0 +1,154 @@ +# Complete list of all available properties: https://docs.upsun.com/create-apps/app-reference.html +applications: + upsun_django_sqlite: + # Application source code directory + source: + root: "/" + + # The runtime the application uses. + # Complete list of available runtimes: https://docs.upsun.com/create-apps/app-reference.html#types + type: "python:3.11" + + # Choose which container profile (ratio CPU+RAM) your app will use. Default value comes from the image itself. + # More information: https://docs.upsun.com/manage-resources/adjust-resources.html#adjust-a-container-profile + # container_profile: + + # The relationships of the application with services or other applications. + # The left-hand side is the name of the relationship as it will be exposed + # to the application in the PLATFORM_RELATIONSHIPS variable. The right-hand + # side is in the form `:`. + # More information: https://docs.upsun.com/create-apps/app-reference.html#relationships + relationships: + postgresql: + + + # Mounts define directories that are writable after the build is complete. + # More information: https://docs.upsun.com/create-apps/app-reference.html#mounts + # mounts: + # "/var/uploads": # Represents the path in the app. + # source: "storage" # "storage" sources are unique to the app, but shared among instances of the app. "service" sources can be shared among apps. + # source_path: "var/uploads" # The subdirectory within the mounted disk (the source) where the mount should point. + + # The web key configures the web server running in front of your app. + # More information: https://docs.upsun.com/create-apps/app-reference.html#web + web: + # Commands are run once after deployment to start the application process. + # More information: https://docs.upsun.com/create-apps/app-reference.html#web-commands + commands: + # The command to launch your app. If it terminates, it’s restarted immediately. + # You can use the $PORT or the $SOCKET environment variable depending on the socket family of your upstream + start: "gunicorn --pythonpath=myproject -b unix:$SOCKET myproject.wsgi --log-file -" + # You can listen to a UNIX socket (unix) or a TCP port (tcp, default). + # Whether your app should speak to the webserver via TCP or Unix socket. Defaults to tcp + # More information: https://docs.upsun.com/create-apps/app-reference.html#where-to-listen + upstream: + # Whether your app should speak to the webserver via TCP or Unix socket. Defaults to tcp + # More information: https://docs.upsun.com/create-apps/app-reference.html#where-to-listen + socket_family: unix + # Each key in locations is a path on your site with a leading /. + # More information: https://docs.upsun.com/create-apps/app-reference.html#locations + locations: + "/": + passthru: true + + "/static": + allow: true + expires: "1h" + root: "static" + + + + # Alternate copies of the application to run as background processes. + # More information: https://docs.upsun.com/create-apps/app-reference.html#workers + # workers: + + # The timezone for crons to run. Format: a TZ database name. Defaults to UTC, which is the timezone used for all logs + # no matter the value here. More information: https://docs.upsun.com/create-apps/timezone.html + # timezone: + + # Access control for roles accessing app environments. + # More information: https://docs.upsun.com/create-apps/app-reference.html#access + # access: + + # Variables to control the environment. More information: https://docs.upsun.com/create-apps/app-reference.html#variables + # variables: + # env: + # # Add environment variables here that are static. + # PYTHONUNBUFFERED: "1" + + # Outbound firewall rules for the application. More information: https://docs.upsun.com/create-apps/app-reference.html#firewall + # firewall: + + # Specifies a default set of build tasks to run. Flavors are language-specific. + # More information: https://docs.upsun.com/create-apps/app-reference.html#build + build: + flavor: none + + # Installs global dependencies as part of the build process. They’re independent of your app’s dependencies and + # are available in the PATH during the build process and in the runtime environment. They’re installed before + # the build hook runs using a package manager for the language. + # More information: https://docs.upsun.com/create-apps/app-reference.html#dependencies + # dependencies: + # python3: # Specify one Python 3 package per line. + # numpy: '*' + + # Hooks allow you to customize your code/environment as the project moves through the build and deploy stages + # More information: https://docs.upsun.com/create-apps/app-reference.html#hooks + hooks: + # The build hook is run after any build flavor. + # More information: https://docs.upsun.com/create-apps/hooks/hooks-comparison.html#build-hook + build: | + set -eux + pip install -r requirements.txt + # Collect static files so that they can be served by Upsun + python myproject/manage.py collectstatic --noinput + + # The deploy hook is run after the app container has been started, but before it has started accepting requests. + # More information: https://docs.upsun.com/create-apps/hooks/hooks-comparison.html#deploy-hook + deploy: | + set -eux + python myproject/manage.py migrate + + + # The post_deploy hook is run after the app container has been started and after it has started accepting requests. + # More information: https://docs.upsun.com/create-apps/hooks/hooks-comparison.html#deploy-hook + # post_deploy: | + + # Scheduled tasks for the app. + # More information: https://docs.upsun.com/create-apps/app-reference.html#crons + # crons: + + # Customizations to your PHP or Lisp runtime. More information: https://docs.upsun.com/create-apps/app-reference.html#runtime + # runtime: + + # More information: https://docs.upsun.com/create-apps/app-reference.html#additional-hosts + # additional_hosts: + +# The services of the project. +# +# Each service listed will be deployed +# to power your Upsun project. +# More information: https://docs.upsun.com/add-services.html +# Full list of available services: https://docs.upsun.com/add-services.html#available-services +services: + postgresql: + type: postgresql:16 # All available versions are: 15, 14, 13, 12, 11 + configuration: + extensions: + - vector + + +# The routes of the project. +# +# Each route describes how an incoming URL is going +# to be processed by Upsun. +# More information: https://docs.upsun.com/define-routes.html +routes: + "https://{default}/": + type: upstream + upstream: "upsun_django_sqlite:http" + # A basic redirect definition + # More information: https://docs.upsun.com/define-routes.html#basic-redirect-definition + "https://www.{default}": + type: redirect + to: "https://{default}/" diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/manage.py b/02_install_django_postgresql_pgvector_upsun/myproject/manage.py new file mode 100755 index 00000000..92bb9a3b --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/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', 'myproject.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/02_install_django_postgresql_pgvector_upsun/myproject/myapp/__init__.py b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myapp/admin.py b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/admin.py new file mode 100644 index 00000000..8c38f3f3 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myapp/apps.py b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/apps.py new file mode 100644 index 00000000..c34fb20e --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MyappConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'myapp' diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myapp/migrations/__init__.py b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/migrations/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myapp/models.py b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myapp/tests.py b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/tests.py new file mode 100644 index 00000000..7ce503c2 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myapp/views.py b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/views.py new file mode 100644 index 00000000..91ea44a2 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myapp/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__init__.py b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/__init__.cpython-312.pyc b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..dd4a499aef4522641b9cb0fc8a324a690a90dda8 GIT binary patch literal 167 zcmX@j%ge<81S)&QD3zFD)o8&5KXTO3X{ok1sCF$t+3L&#f#d%FjwoE fu*uC&Da}c>D`Ev2%Lv59AjU^#Mn=XWW*`dyrJyRz literal 0 HcmV?d00001 diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/settings.cpython-312.pyc b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/settings.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4249be1f6f90f04b3939571279f272ca5a161757 GIT binary patch literal 2550 zcmb7Gy>Ht_6esol9a&Lg$7ul?36dzODW`7Ywn)>^672|)B~_&2mO}|zcao`4q{5Hb z=A^w#JGDd5AzP6@rAv$sF*q6MRG@>Lpi8DcQj#sm2I>kD@9us4-n-wuchv96WQ>5% z^~z7JUlRoJr@I(`LZiw1zhUw-fe3*>B=RX_*C+T$!be~q_P-{FR-NeB?PPpCns&Sx zCvtaomP`mCzYsgD!f{toOVmlkI?zMwf*dG%I8v-bMIj(VTDzT@fzt z&F8L4G;AwY>_}=`qYP}Cvf4JNmZnqPy`HXhaMPqlBOQyaV-@R?i4nE6N8Lo8*P?{F zvPv1Iq?;qlo0Me$4mE*leV2f)0k_2j0JAE%rj!kgQeYFUaxDY9WwTI*y>l8PV@mUZfd zq*&PaKe2S&nh5lld#X9o3DhKW+g=^9Agcy$S~_0*V)uL8>Fh4ZcuCZ6^_q8H+~{oQ zTAyB9P?i;acV+tyvKAgKi+lI%=S%ljmcBxKE@WqiFmzK>O}V6YJ5764!sjeCJEdTqy#)P|Gs3Sie1p-Y;OmZlrIfPHHk=Cl=!@RrrK zZ=b_b$Iqc>O#9Br_P9)4PR~Ggxwe<5jE{Ld!(pt;1)T6IQ+UEwicX+ds&h_+sntYp z7OvL#QnkX_^Cy(;7;TxBdOcv;J4Ppra7(fj^SG|j)(L~gb451svK<5AXrA*qA-1w! zs<2L=!fdcQT&jfqcq(}Wa?;RcbB7mjWiSZ7SPWi1nPa#vO)wnq8X<_)Mayvf4ZdK9;hu+< z$=e@~V1dc5hOlDLt8yD!N?Vh$f-1RLCoGC^g^QvS?!iT=$XmJ}sC6Qu=w2?aT*ro{ zyuePV2lt9LyzhA6Ge^b5f-W$Abt2 zP#~NW+b9)_Wp^uXE`Um$*q@M`*NwkxV7X&iww`W zVL4}xY4DH58pCm$)p}8U%9Kl>tU?JU%S>gx!K|}lp;}}Cu(ZL7LKR9T(tw0ZcR#N< zA$P_LkU0;?1vk2rDY9!!qs)tdTou}Z42vWz5o>Ft)5}2 z;*eKySf2UgN}rgG-+6Rz??dDPux}~&p?(8k-M=UX{9_>oev%}Qd@(Zc&m2KsISLVp z^pBAr9=?q)?8g`Wh+lgfzr7#7{WgAgKYq6#8Kj6*o$SX3AtIXW2M^Pke)3%`obmNj z?=q2;4>VaKP4yFl2$5yUgLLK~J9n6%-VwoTsewP7jU7c3!O*~;O8WZI0Yzk&-VkX( z4Wb+6 mX6E|I!^BKKdKkaZj~qhMp+SO3%|fCv;_|(HBBNgiF8&409#S#@ literal 0 HcmV?d00001 diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/urls.cpython-312.pyc b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/urls.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..a85e49b8e450238f573f2df75cc0ae222cc41e34 GIT binary patch literal 1036 zcmb7D&ui2`6i%|)wq|X6_26MdAsg6cROltBMT^CP^dR=K2x&GmyPGAMi8B**J?YJ( zd)mK2{8K!73wZM6t)+M=2)@Y=Sp`7{l6f<{`QG<^Z|2kHrenakakLo!ungm?UbMq* zg7aq(EDd4|4Pp{2H8X2ynL6&Ioy;EErePV>CN{NZ6h zZ!LNQ8_w~Q$0+7(oJ4VRah^^oIw?e&M@mt_sgU4> zq7ng)M2e#q34JMjbf7`FpeSL0m(|F5N+U^;q%?34jHyz&3@|1pP8VjC*q%g|! zX0xVQ<=<}dwE`_*MIjsovD3cm4jNrkp3wnfQAY2&I(9v0KaHf^8G$geg8DZWTurIa zMEr_zOXm?_{SOv_&MeIq@y>_#L@7zDO<{PkM3#7Evn zxgLgaKfe%e}?kXZ!kT$J(~u_Jn?-KLIZFImQ40 literal 0 HcmV?d00001 diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/wsgi.cpython-312.pyc b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/__pycache__/wsgi.cpython-312.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0777005fe49c5ecb19080e872af0131ef767403c GIT binary patch literal 659 zcmYjPF>ezw6t?d|Q(b6;D%#mXT_V9b3@oTZ0urJiNQbmS%5u4!UlKDGyX ziHYBU`cqhnKtkLELJVx6Y@P7+k~VMf`<|cP_kGXur>!jy+1Ts6AHKr~{mP3~l$KyI zu;2q?sD~J4j=()^vn!m~?YS5Mo@}m0uJfdFjaP`3*o`x%s4#L6xM#~&Z)5J%Hcq~K zuez-^iIg1j5gDqK#PcLo6Nn61X5aJLhQOOdY0$)sffOU55aB=o2{j2xtk_h*JpnTi zWEQ6UvL(Y980q)JBoRCc4Og7Ngw!iI#$O>RC z${=SvRWkGJAcJ8z6(*~!X?{eD+|$JqdZZ!MG*tsg4SlK@&}pKlGAM`x{aSDX^vcN^ z%H{lIezCb0$a^8BU-Z!-K8SR|!`sek= SSJeDjz4N8IcUnnr+M)k(s>5*r literal 0 HcmV?d00001 diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/asgi.py b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/asgi.py new file mode 100644 index 00000000..05782a12 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for myproject 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/5.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') + +application = get_asgi_application() diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings.py b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings.py new file mode 100644 index 00000000..51fdc5d2 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings.py @@ -0,0 +1,131 @@ +""" +Django settings for myproject project. + +Generated by 'django-admin startproject' using Django 5.0.7. + +For more information on this file, see +https://docs.djangoproject.com/en/5.0/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.0/ref/settings/ +""" + +from pathlib import Path +import os + +# 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/5.0/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'django-insecure-9x^ejjx!ie0_r(pc+v&jh)f2#!l3lrx7h+du!@3_y5zq0670=d' + +# 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', +] + +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 = 'myproject.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 = 'myproject.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/5.0/ref/settings/#databases + +# Set the DB_DIR to one level higher than BASE_DIR +DB_DIR = BASE_DIR.parent + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': 'db/db.sqlite3', + 'NAME': os.path.join(DB_DIR, 'db', 'db.sqlite3'), + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.0/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/5.0/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/5.0/howto/static-files/ + +STATIC_URL = 'static/' + +# Default primary key field type +# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + + +from .settings_psh import * diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings_psh.py b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings_psh.py new file mode 100644 index 00000000..7291a1e2 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/settings_psh.py @@ -0,0 +1,76 @@ +################################################################################# +# Upsun-specific configuration + +import base64 +import json +import os +import sys +from urllib.parse import urlparse + +# This variable should always match the primary database relationship name +PLATFORM_DB_RELATIONSHIP = "postgresql" + +# Helper function for decoding base64-encoded JSON variables. +def decode(variable): + """Decodes a Upsun environment variable. + Args: + variable (string): + Base64-encoded JSON (the content of an environment variable). + Returns: + An dict (if representing a JSON object), or a scalar type. + Raises: + JSON decoding error. + """ + try: + if sys.version_info[1] > 5: + return json.loads(base64.b64decode(variable)) + else: + return json.loads(base64.b64decode(variable).decode("utf-8")) + except json.decoder.JSONDecodeError: + print("Error decoding JSON, code %d", json.decoder.JSONDecodeError) + + +# Import some Upsun settings from the environment. +# Read more on Upsun variables at https://docs.upsun.com/development/variables/use-variables.html#use-platformsh-provided-variables +if os.getenv("PLATFORM_APPLICATION_NAME"): + DEBUG = False + + if os.getenv("PLATFORM_APP_DIR"): + STATIC_ROOT = os.path.join(os.getenv("PLATFORM_APP_DIR"), "static") + + if os.getenv("PLATFORM_PROJECT_ENTROPY"): + SECRET_KEY = os.getenv("PLATFORM_PROJECT_ENTROPY") + + if os.getenv("PLATFORM_ROUTES"): + platform_routes = decode(os.getenv("PLATFORM_ROUTES")) + ALLOWED_HOSTS = list(map( + lambda key: urlparse(key).hostname, + platform_routes.keys(), + )) + + # Database service configuration, post-build only. + if os.getenv("PLATFORM_RELATIONSHIPS") and PLATFORM_DB_RELATIONSHIP: + platform_relationships = decode(os.getenv("PLATFORM_RELATIONSHIPS")) + if PLATFORM_DB_RELATIONSHIP in platform_relationships: + db_settings = platform_relationships[PLATFORM_DB_RELATIONSHIP][0] + engine = None + if ( + "mariadb" in db_settings["type"] + or "mysql" in db_settings["type"] + or "oracle-mysql" in db_settings["type"] + ): + engine = "django.db.backends.mysql" + elif "postgresql" in db_settings["type"]: + engine = "django.db.backends.postgresql" + + if engine: + DATABASES = { + "default": { + "ENGINE": engine, + "NAME": db_settings["path"], + "USER": db_settings["username"], + "PASSWORD": db_settings["password"], + "HOST": db_settings["host"], + "PORT": db_settings["port"], + }, + } diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/urls.py b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/urls.py new file mode 100644 index 00000000..03c4c0e5 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/urls.py @@ -0,0 +1,22 @@ +""" +URL configuration for myproject project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/5.0/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 + +urlpatterns = [ + path('admin/', admin.site.urls), +] diff --git a/02_install_django_postgresql_pgvector_upsun/myproject/myproject/wsgi.py b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/wsgi.py new file mode 100644 index 00000000..d4d3c81b --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/myproject/myproject/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for myproject 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/5.0/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings') + +application = get_wsgi_application() diff --git a/02_install_django_postgresql_pgvector_upsun/requirements.txt b/02_install_django_postgresql_pgvector_upsun/requirements.txt new file mode 100644 index 00000000..60636426 --- /dev/null +++ b/02_install_django_postgresql_pgvector_upsun/requirements.txt @@ -0,0 +1,6 @@ +asgiref==3.8.1 +Django==5.0.7 +gunicorn==22.0.0 +packaging==24.1 +psycopg2==2.9.9 +sqlparse==0.5.0 diff --git a/hugosite/content/posts/install-django-postgresql-pgvector-upsun.md b/hugosite/content/posts/install-django-postgresql-pgvector-upsun.md index 596758b2..fbbf38f1 100644 --- a/hugosite/content/posts/install-django-postgresql-pgvector-upsun.md +++ b/hugosite/content/posts/install-django-postgresql-pgvector-upsun.md @@ -4,7 +4,7 @@ date = 2024-07-17T11:00:00+02:00 draft = false +++ -In my article ["Install Django with SQLite on Upsun"](/posts/install-django-sqlite-upsun/), I explain why I love the [Upsun PaaS](https://upsun.com) and some of the great features you get when you use it. The next step in developing a production-worthy Django site on Upsun is to move to using an enterprise grade database, PostgreSQL. In this tutorial, I also show how to install and use the [PGVector](https://github.com/pgvector/pgvector) extension because the apps that I'm building need the ability to do semantic queries on vectors, in part to do Retrieval Augmented Generation (RAG) with Large Language Models (LLM) such as ChatGPT or Claude. +In my article ["Install Django with SQLite on Upsun"](/posts/install-django-sqlite-upsun/), I explain why I love the [Upsun PaaS](https://upsun.com) and some of the great features you get when you use it. The next step in developing a production-worthy Django site on Upsun is to move to using an enterprise-grade database, PostgreSQL. In this tutorial, I also show how to install and use the [PGVector](https://github.com/pgvector/pgvector) extension because the apps that I'm building need the ability to do semantic queries on vectors, in part to do Retrieval Augmented Generation (RAG) with Large Language Models (LLM) such as ChatGPT or Claude. Here is a summary of steps that I explained in the previous tutorial. @@ -27,7 +27,7 @@ cd .. ## 2. Make the project compatible with Upsun -Assuming you have started an Upsun project and installed the Upsun CLI, run this in the root directory (`upsun_django_postgresql`): +Assuming you have started an Upsun project and [installed the Upsun CLI](https://docs.upsun.com/administration/cli.html), run this in the root directory (`upsun_django_postgresql`): ```bash upsun project:init