Skip to content

Commit

Permalink
dev db up and running
Browse files Browse the repository at this point in the history
  • Loading branch information
sheenarbw committed Nov 18, 2024
1 parent a6412d4 commit 31f43b2
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#
staticfiles/
gitignore/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,21 @@ Then install the requirements:
pip install -r requirements.txt
```

3. Build the tailwind styles

3. Install npm dependencies

```
npm install
npm run tailwind
```

This will create a file called `tailwind_final.css`. This contains the css we use in in our website.
## Running the application

1. Get the development database up and running:

Yo might be tempted to edit `tailwind_final.css` directly at some point. It is an automatically generated file and all changes will be overwritten.
See: dev_db/README.md

See the "working with Tailwind" section below to learn more.
Remember to run the migrations!

4. Run the server
2. Run the server

Before using the runserver command, you need to load up some environmental variables. `.env_example` has some sensible defaults that just work in a dev environment:

Expand All @@ -52,6 +51,7 @@ source .env_example
python manage.py runserver
```


## Working with tailwind

You can learn about tailwind [here](https://tailwindcss.com/docs/installation). It is installed in this project using the standard "Tailwind CLI" installation.
Expand Down Expand Up @@ -82,4 +82,5 @@ In this case use:

```
npm run tailwind_watch
```
```

22 changes: 4 additions & 18 deletions core/settings_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

import os
import dj_database_url

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
Expand All @@ -29,6 +26,7 @@
"django.contrib.staticfiles",
"template_partials",
"website",
"custom_auth",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -71,21 +69,6 @@
},
}

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

DB_USER = os.environ.get("DATABASE_USER")
DB_HOST = os.environ.get("DATABASE_HOST")
DB_PASSWORD = os.environ.get("DATABASE_PASSWORD")
DB_NAME = os.environ.get("DATABASE_NAME")
DB_PORT = os.environ.get("DATABASE_PORT", 5432)

DATABASES = {
"default": dj_database_url.config(
default=f"postgres://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
)
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
Expand Down Expand Up @@ -133,3 +116,6 @@
STATICFILES_DIRS = [
BASE_DIR / "static",
]


AUTH_USER_MODEL = "custom_auth.User"
23 changes: 22 additions & 1 deletion core/settings_dev.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .settings_base import *

Check failure on line 1 in core/settings_dev.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F403)

core/settings_dev.py:1:1: F403 `from .settings_base import *` used; unable to detect undefined names

import os

Check failure on line 2 in core/settings_dev.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

core/settings_dev.py:2:8: F401 `os` imported but unused
import dj_database_url

SECRET_KEY = "not really a secret"
DEBUG = True
Expand All @@ -12,3 +13,23 @@
"django_browser_reload",
"whitenoise.runserver_nostatic",
]


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

# These settings work for the default dev database.
# You can run this database using docker compose.
# Look inside dev_db/README.md for details!!

DB_USER = "pguser"
DB_HOST = "127.0.0.1"
DB_PASSWORD = "password"
DB_NAME = "db"
DB_PORT = 6543

DATABASES = {
"default": dj_database_url.config(
default=f"postgres://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
)
}
18 changes: 17 additions & 1 deletion core/settings_prod.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .settings_base import *

Check failure on line 1 in core/settings_prod.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F403)

core/settings_prod.py:1:1: F403 `from .settings_base import *` used; unable to detect undefined names

import os

import dj_database_url

ALLOWED_HOSTS = ["localhost", "127.0.0.1", "2025.djangocon.africa"]

Expand All @@ -10,3 +10,19 @@
SECRET_KEY = os.environ["DJANGO_SECRET"]

DEBUG = False


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

DB_USER = os.environ.get("DATABASE_USER")
DB_HOST = os.environ.get("DATABASE_HOST")
DB_PASSWORD = os.environ.get("DATABASE_PASSWORD")
DB_NAME = os.environ.get("DATABASE_NAME")
DB_PORT = os.environ.get("DATABASE_PORT", 5432)

DATABASES = {
"default": dj_database_url.config(
default=f"postgres://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
)
}
Empty file added custom_auth/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions custom_auth/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from . import models


@admin.register(models.User)
class UserAdmin(admin.ModelAdmin):
pass
6 changes: 6 additions & 0 deletions custom_auth/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class CustomAuthConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'custom_auth'
34 changes: 34 additions & 0 deletions custom_auth/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 4.2.13 on 2024-11-18 07:47

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
('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')),
('email', models.EmailField(max_length=50, unique=True)),
('first_name', models.CharField(blank=True, max_length=25)),
('last_name', models.CharField(blank=True, max_length=25)),
('is_active', models.BooleanField(default=True)),
('is_staff', models.BooleanField(default=False, verbose_name='is staff')),
('is_superuser', models.BooleanField(default=False, verbose_name='is superuser')),
('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')),
('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={
'abstract': False,
},
),
]
Empty file.
69 changes: 69 additions & 0 deletions custom_auth/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from django.db import models

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.contrib.auth.models import PermissionsMixin


class UserManager(BaseUserManager):
def create_user(
self,
email,
first_name,
last_name,
password=None,
is_active=True,
is_staff=False,
is_admin=False,
):
if not email:
raise ValueError("User must have an email address")
if not password:
raise ValueError("User must have a password")

user_obj = self.model(
email=self.normalize_email(email),
first_name=first_name,
last_name=last_name,
)
user_obj.set_password(password)
user_obj.is_staff = is_staff
user_obj.is_superuser = is_admin
user_obj.active = is_active
user_obj.save(using=self.db)
return user_obj

def create_staffuser(self, email, first_name, last_name, password=None):
user = self.create_user(
email, first_name, last_name, password=password, is_staff=True
)
return user

def create_superuser(self, email, first_name="", last_name="", password=None):
user = self.create_user(
email,
first_name,
last_name,
password=password,
is_staff=True,
is_admin=True,
)
return user


class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=50, unique=True)

first_name = models.CharField(max_length=25, blank=True)
last_name = models.CharField(max_length=25, blank=True)

is_active = models.BooleanField(default=True)
is_staff = models.BooleanField("is staff", default=False)
is_superuser = models.BooleanField("is superuser", default=False)

USERNAME_FIELD = "email"
REQUIRED_FIELDS = []

objects = UserManager()

def __str__(self):
return self.email
3 changes: 3 additions & 0 deletions custom_auth/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

Check failure on line 1 in custom_auth/views.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

custom_auth/views.py:1:30: F401 `django.shortcuts.render` imported but unused

# Create your views here.

0 comments on commit 31f43b2

Please sign in to comment.