diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..a02597f --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.9 \ No newline at end of file diff --git a/README.md b/README.md index ede049c..504d0e8 100644 --- a/README.md +++ b/README.md @@ -1,79 +1,97 @@ # Undead Mongoose -Django application for the mongoose backend & board interface, build in Django. +Django application for the mongoose backend & board interface, built in Django. ## Prerequisites -To run Undead Mongoose, you need: +To run Undead Mongoose, you need [docker](https://www.docker.com/) and python 3.9. To install python 3.9 on macos and linux, you can use [pyenv](https://github.com/pyenv/pyenv). On windows, you can just execute the installer for python 3.9 from python's [downloads page](https://www.python.org/downloads/windows/). -- docker -- docker-compose -- nix -- Python 3.8 -- dotenv (`python3-dotenv-cli` on Ubuntu) +Then run the following set of commands to set up the development environment: -## Setting up -First, run - -Copy `sample.env` to `.env` and edit where necessary. +```bash +# Clone the repository +git clone https://github.com/svsticky/undead-mongoose.git +cd undead-mongoose + +# Create virtual environment (platform-specific) +# On linux +pyenv sync # Installs the correct python version, if you haven't done so already +pyenv exec python -m venv .venv +source .venv/bin/activate + +# On windows +python3.9 -m venv .venv +.\.venv\bin\Activate.ps1 + +# Install dependencies (platform-agnostic) +pip install -r requirements.txt +``` -Setup the database with: +## Setting up -``` bash -docker-compose up -d -dotenv nix-shell --run "./manage.py migrate" +Copy `sample.env` to `.env` and make sure the database options are correct. By default the credentials are setup to use the docker database. Then run the following commands to setup the database: +```bash +docker compose up -d +dotenv run ./manage.py migrate ``` In development, create an admin superuser -``` bash -dotenv nix-shell --run "./manage.py createsuperuser" +```bash +dotenv run ./manage.py createsuperuser ``` -### Setup Koala connection (local) +Then depending on whether you want to use a local version of koala, you need to do some additional setup: -(In development) Change `KOALA_DB_NAME` to `koala-development` +- *If you have a locally running version of koala*: (In development) Change `KOALA_DB_NAME` to `koala-development` -Create the `undead_mongoose` user in Koala's database: -```sql -CREATE USER undead_mongoose WITH PASSWORD 'mongoose123'; -``` + Create the `undead_mongoose` user in Koala's database: + ```sql + CREATE USER undead_mongoose WITH PASSWORD 'mongoose123'; + ``` -Configure privileges for the `undead_mongoose` user in Koala's database (Replace `koala` with `koala-development` in development): -```sql -\c "koala" -GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO undead_mongoose; -``` + Configure privileges for the `undead_mongoose` user in Koala's database (Replace `koala` with `koala-development` in development): + ```sql + \c "koala" + GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO undead_mongoose; + ``` -To enable oauth, you should go to koala.rails.local:3000/api/oauth/applications and create a new application with {{ canonical_hostname }}/oidc/callback/ as the callback url. + To enable oauth, you should go to koala.rails.local:3000/api/oauth/applications and create a new application with {{ canonical_hostname }}/oidc/callback/ as the callback url. -It's also possible to generate the client through your CLI in the `constipiated-koala` project by running the command below -```bash -bundle exec rake "doorkeeper:create[undead Mongoose, http://localhost:8000/oidc/callback/, openid profile email member-read]" -``` + It's also possible to generate the client through your CLI in the `constipiated-koala` project by running the command below + ```bash + bundle exec rake "doorkeeper:create[undead Mongoose, http://localhost:8000/oidc/callback/, openid profile email member-read]" + ``` -Ensure scopes 'openid member-read email profile' are present. Also ensure to copy the application_id and secret and put them in your `.env` file. + Ensure scopes 'openid member-read email profile' are present. Also ensure to copy the application_id and secret and put them in your `.env` file. -```ini -API_TOKEN=koala + ```ini + API_TOKEN=koala -OIDC_RP_CLIENT_ID=example_id -OIDC_RP_CLIENT_SECRET=example_secret + OIDC_RP_CLIENT_ID=example_id + OIDC_RP_CLIENT_SECRET=example_secret -ALLOWED_HOSTS=localhost -``` + ALLOWED_HOSTS=localhost + ``` -Make sure that the `OIDC_OP_*_ENDPOINT` endpoints are correct. The ones in sample.env should suffice. + Make sure that the `OIDC_OP_*_ENDPOINT` endpoints are correct. The ones in sample.env should suffice. -Alter Koala's `.env` file such that the following keys have the following values: -```ini -CHECKOUT_TOKEN=koala -``` + Alter Koala's `.env` file such that the following keys have the following values: + ```ini + CHECKOUT_TOKEN=koala + ``` +- *Alternatively, use the staging version of koala:* create an oauth application for your mongoose installation via . Make sure you log in with the dev@svsticky.nl account to access the page. Create a new application with the following information: + - Confidential: `true` + - Callback url: `http://localhost:8000/oidc/callback/` + - Scopes: `openid profile email member-read` + + Copy the application id and secret into the `.env` file and make sure you update the oauth urls to point to koala.dev.svsticky.nl. ## Running ``` bash -# database -docker-compose up -d -# server -nix-shell --run "./manage.py runserver" +# Database +docker compose up -d + +# Server +dotenv run ./manage.py runserver ``` diff --git a/nix/django-constance.nix b/nix/django-constance.nix deleted file mode 100644 index 138d635..0000000 --- a/nix/django-constance.nix +++ /dev/null @@ -1,41 +0,0 @@ -# This file defines a package definition for `django-constance`, which is not -# in Nixpkgs. It takes a Python version to build for: -{ python, }: - -let - # This version and SHA256 should match one of the releases here: - # https://pypi.org/project/django-constance/ - version = "2.8.0"; - sha256 = "0a492454acc78799ce7b9f7a28a00c53427d513f34f8bf6fdc90a46d8864b2af"; - - # The default definition of `django-picklefield` builds for Django 2, which - # causes a conflict if you want a different Django version. - # For the upstream definition, see: - # https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/python-modules/django-picklefield/default.nix - fixed-picklefield = python.pkgs.django-picklefield.overridePythonAttrs (old: { - # Pass Django 4 instead of Django 2 to the build: - propagatedBuildInputs = [ python.pkgs.django_4 ]; - # If you override this version the test dependencies also need to be - # updated, or you disable the tests. The latter is easier, so do that: - doCheck = false; - }); - -in - -python.pkgs.buildPythonPackage rec { - pname = "django-constance"; - inherit version; - - src = python.pkgs.fetchPypi { - inherit pname version sha256; - }; - - # Pass the updated dependencies: - propagatedBuildInputs = [ - python.pkgs.django_4 - fixed-picklefield - ]; - - # No tests here either: - doCheck = false; -} diff --git a/nix/django-oidc.nix b/nix/django-oidc.nix deleted file mode 100644 index 0cfeb8e..0000000 --- a/nix/django-oidc.nix +++ /dev/null @@ -1,25 +0,0 @@ -{ python, }: - -let - version = "2.0.0"; - sha256 = "a8b2f27c69c122d2f4d801c3759761d33debf06ae9dabbab8aed82887bba3bb8"; - -in - -python.pkgs.buildPythonPackage rec { - pname = "mozilla-django-oidc"; - inherit version; - - src = python.pkgs.fetchPypi { - inherit pname version sha256; - }; - - propagatedBuildInputs = [ - python.pkgs.django_4 - python.pkgs.josepy - python.pkgs.requests - python.pkgs.cryptography - ]; - - doCheck = false; -} \ No newline at end of file diff --git a/nix/sources.json b/nix/sources.json deleted file mode 100644 index c479019..0000000 --- a/nix/sources.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "niv": { - "branch": "master", - "description": "Easy dependency management for Nix projects", - "homepage": "https://github.com/nmattia/niv", - "owner": "nmattia", - "repo": "niv", - "rev": "9cb7ef336bb71fd1ca84fc7f2dff15ef4b033f2a", - "sha256": "1ajyqr8zka1zlb25jx1v4xys3zqmdy3prbm1vxlid6ah27a8qnzh", - "type": "tarball", - "url": "https://github.com/nmattia/niv/archive/9cb7ef336bb71fd1ca84fc7f2dff15ef4b033f2a.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - }, - "nixpkgs": { - "branch": "nixpkgs-unstable", - "description": "Nix Packages collection", - "homepage": "", - "owner": "NixOS", - "repo": "nixpkgs", - "rev": "b66b39216b1fef2d8c33cc7a5c72d8da80b79970", - "sha256": "0xqxrmdr3adcdj1ksnwf8w7d0qjzsc4sgzfcmvvrpk7hrc5q9cvg", - "type": "tarball", - "url": "https://github.com/NixOS/nixpkgs/archive/b66b39216b1fef2d8c33cc7a5c72d8da80b79970.tar.gz", - "url_template": "https://github.com///archive/.tar.gz" - } -} diff --git a/nix/sources.nix b/nix/sources.nix deleted file mode 100644 index 1938409..0000000 --- a/nix/sources.nix +++ /dev/null @@ -1,174 +0,0 @@ -# This file has been generated by Niv. - -let - - # - # The fetchers. fetch_ fetches specs of type . - # - - fetch_file = pkgs: name: spec: - let - name' = sanitizeName name + "-src"; - in - if spec.builtin or true then - builtins_fetchurl { inherit (spec) url sha256; name = name'; } - else - pkgs.fetchurl { inherit (spec) url sha256; name = name'; }; - - fetch_tarball = pkgs: name: spec: - let - name' = sanitizeName name + "-src"; - in - if spec.builtin or true then - builtins_fetchTarball { name = name'; inherit (spec) url sha256; } - else - pkgs.fetchzip { name = name'; inherit (spec) url sha256; }; - - fetch_git = name: spec: - let - ref = - if spec ? ref then spec.ref else - if spec ? branch then "refs/heads/${spec.branch}" else - if spec ? tag then "refs/tags/${spec.tag}" else - abort "In git source '${name}': Please specify `ref`, `tag` or `branch`!"; - in - builtins.fetchGit { url = spec.repo; inherit (spec) rev; inherit ref; }; - - fetch_local = spec: spec.path; - - fetch_builtin-tarball = name: throw - ''[${name}] The niv type "builtin-tarball" is deprecated. You should instead use `builtin = true`. - $ niv modify ${name} -a type=tarball -a builtin=true''; - - fetch_builtin-url = name: throw - ''[${name}] The niv type "builtin-url" will soon be deprecated. You should instead use `builtin = true`. - $ niv modify ${name} -a type=file -a builtin=true''; - - # - # Various helpers - # - - # https://github.com/NixOS/nixpkgs/pull/83241/files#diff-c6f540a4f3bfa4b0e8b6bafd4cd54e8bR695 - sanitizeName = name: - ( - concatMapStrings (s: if builtins.isList s then "-" else s) - ( - builtins.split "[^[:alnum:]+._?=-]+" - ((x: builtins.elemAt (builtins.match "\\.*(.*)" x) 0) name) - ) - ); - - # The set of packages used when specs are fetched using non-builtins. - mkPkgs = sources: system: - let - sourcesNixpkgs = - import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) { inherit system; }; - hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath; - hasThisAsNixpkgsPath = == ./.; - in - if builtins.hasAttr "nixpkgs" sources - then sourcesNixpkgs - else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then - import {} - else - abort - '' - Please specify either (through -I or NIX_PATH=nixpkgs=...) or - add a package called "nixpkgs" to your sources.json. - ''; - - # The actual fetching function. - fetch = pkgs: name: spec: - - if ! builtins.hasAttr "type" spec then - abort "ERROR: niv spec ${name} does not have a 'type' attribute" - else if spec.type == "file" then fetch_file pkgs name spec - else if spec.type == "tarball" then fetch_tarball pkgs name spec - else if spec.type == "git" then fetch_git name spec - else if spec.type == "local" then fetch_local spec - else if spec.type == "builtin-tarball" then fetch_builtin-tarball name - else if spec.type == "builtin-url" then fetch_builtin-url name - else - abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}"; - - # If the environment variable NIV_OVERRIDE_${name} is set, then use - # the path directly as opposed to the fetched source. - replace = name: drv: - let - saneName = stringAsChars (c: if isNull (builtins.match "[a-zA-Z0-9]" c) then "_" else c) name; - ersatz = builtins.getEnv "NIV_OVERRIDE_${saneName}"; - in - if ersatz == "" then drv else - # this turns the string into an actual Nix path (for both absolute and - # relative paths) - if builtins.substring 0 1 ersatz == "/" then /. + ersatz else /. + builtins.getEnv "PWD" + "/${ersatz}"; - - # Ports of functions for older nix versions - - # a Nix version of mapAttrs if the built-in doesn't exist - mapAttrs = builtins.mapAttrs or ( - f: set: with builtins; - listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set)) - ); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295 - range = first: last: if first > last then [] else builtins.genList (n: first + n) (last - first + 1); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257 - stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1)); - - # https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269 - stringAsChars = f: s: concatStrings (map f (stringToCharacters s)); - concatMapStrings = f: list: concatStrings (map f list); - concatStrings = builtins.concatStringsSep ""; - - # https://github.com/NixOS/nixpkgs/blob/8a9f58a375c401b96da862d969f66429def1d118/lib/attrsets.nix#L331 - optionalAttrs = cond: as: if cond then as else {}; - - # fetchTarball version that is compatible between all the versions of Nix - builtins_fetchTarball = { url, name ? null, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchTarball; - in - if lessThan nixVersion "1.12" then - fetchTarball ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) - else - fetchTarball attrs; - - # fetchurl version that is compatible between all the versions of Nix - builtins_fetchurl = { url, name ? null, sha256 }@attrs: - let - inherit (builtins) lessThan nixVersion fetchurl; - in - if lessThan nixVersion "1.12" then - fetchurl ({ inherit url; } // (optionalAttrs (!isNull name) { inherit name; })) - else - fetchurl attrs; - - # Create the final "sources" from the config - mkSources = config: - mapAttrs ( - name: spec: - if builtins.hasAttr "outPath" spec - then abort - "The values in sources.json should not have an 'outPath' attribute" - else - spec // { outPath = replace name (fetch config.pkgs name spec); } - ) config.sources; - - # The "config" used by the fetchers - mkConfig = - { sourcesFile ? if builtins.pathExists ./sources.json then ./sources.json else null - , sources ? if isNull sourcesFile then {} else builtins.fromJSON (builtins.readFile sourcesFile) - , system ? builtins.currentSystem - , pkgs ? mkPkgs sources system - }: rec { - # The sources, i.e. the attribute set of spec name to spec - inherit sources; - - # The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers - inherit pkgs; - }; - -in -mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); } diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f8a5537 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,23 @@ +asgiref==3.8.1 +certifi==2024.8.30 +cffi==1.17.1 +charset-normalizer==3.4.0 +cryptography==44.0.0 +Django==4.0 +django-constance==2.8.0 +django-environ==0.11.2 +django-picklefield==3.2 +gunicorn==23.0.0 +idna==3.10 +josepy==1.14.0 +mozilla-django-oidc==2.0.0 +packaging==24.2 +pillow==11.0.0 +psycopg2==2.9.10 +pycparser==2.22 +pyOpenSSL==24.3.0 +requests==2.32.3 +sentry-sdk==2.19.0 +sqlparse==0.5.2 +urllib3==2.2.3 +python-dotenv[cli] diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 5ef789c..0000000 --- a/shell.nix +++ /dev/null @@ -1,25 +0,0 @@ -let - sources = import ./nix/sources.nix {}; - pkgs = import sources.nixpkgs {}; - - pythonEnv = pkgs.python39.withPackages (ps: [ - ps.django_4 - ps.psycopg2 - ps.pillow - ps.requests - ps.sentry-sdk - (ps.callPackage ./nix/django-constance.nix {}) - (ps.callPackage ./nix/django-oidc.nix {}) - ps.gunicorn - ]); - -in - pkgs.mkShell { - packages = [ - # Make the Python environment available - pythonEnv - # Make `niv` available to manage Nix snapshots - pkgs.niv - pkgs.haskellPackages.dotenv - ]; - }