Skip to content

Commit

Permalink
Merge branch 'dev' into feat/upgrade_gpt4
Browse files Browse the repository at this point in the history
Merge changes from dev before testing upgrade to gpt-4 to avoid conflicts
  • Loading branch information
isobel-daley-6point6 committed Jul 11, 2024
2 parents 3d7ad66 + 686e8e2 commit 2039546
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 218 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,17 @@ docs-build: ## Build documentation
poetry run mkdocs build

# Docker
AWS_ACCOUNT_ID=REPLACE_ME

AWS_REGION=eu-west-2
APP_NAME=redbox
APP_NAME=dbt
ECR_URL=$(AWS_ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com
ECR_REPO_URL=$(ECR_URL)/$(ECR_REPO_NAME)
IMAGE=$(ECR_REPO_URL):$(IMAGE_TAG)
IMAGE=latest

ECR_REPO_NAME=$(APP_NAME)
PREV_IMAGE_TAG=$$(git rev-parse HEAD~1)
IMAGE_TAG=$$(git rev-parse HEAD)
PREV_IMAGE_TAG=latest
IMAGE_TAG=latest

tf_build_args=-var "image_tag=$(IMAGE_TAG)"
DOCKER_SERVICES=$$(docker compose config --services | grep -v mlflow)
Expand Down Expand Up @@ -152,7 +154,7 @@ docker_build: ## Build the docker container
PREV_IMAGE="$(ECR_REPO_URL)-$$service:$(PREV_IMAGE_TAG)"; \
echo "Pulling previous image: $$PREV_IMAGE"; \
docker pull $$PREV_IMAGE; \
docker compose build $$service; \
DOCKER_DEFAULT_PLATFORM=linux/amd64 docker compose build $$service; \
else \
echo "Skipping $$service uses default image"; \
fi; \
Expand Down
22 changes: 20 additions & 2 deletions django_app/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions django_app/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ django-gov-notify = "^0.5.0"
websockets = "^12.0"
django-import-export = "^4.0"
dataclasses-json = ">=0.6.7"
django-staff-sso-client = "^4.3.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.1.1"
Expand Down
3 changes: 3 additions & 0 deletions django_app/redbox_app/redbox_core/auth_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.shortcuts import redirect, render
from magic_link.models import MagicLink
from requests import HTTPError
from django.conf import settings

from redbox_app.redbox_core import email_handler, models
from redbox_app.redbox_core.forms import SignInForm
Expand All @@ -15,6 +16,8 @@
def sign_in_view(request: HttpRequest):
if request.user.is_authenticated:
return redirect("homepage")
if settings.LOGIN_METHOD == "sso":
return redirect("/auth/login/")
if request.method == "POST":
form = SignInForm(request.POST)
if form.is_valid():
Expand Down
187 changes: 127 additions & 60 deletions django_app/redbox_app/redbox_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django_use_email_as_username.models import BaseUser, BaseUserManager
from jose import jwt
from yarl import URL

if settings.LOGIN_METHOD == "sso":
from django.contrib.auth.base_user import BaseUserManager
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
else:
from django_use_email_as_username.models import BaseUser, BaseUserManager

logger = logging.getLogger(__name__)


Expand All @@ -37,65 +42,55 @@ class BusinessUnit(UUIDPrimaryKeyBase):
def __str__(self) -> str: # pragma: no cover
return f"{self.name}"


class User(BaseUser, UUIDPrimaryKeyBase):
class UserGrade(models.TextChoices):
AA = "AA", _("AA")
AO = "AO", _("AO")
DEPUTY_DIRECTOR = "DD", _("Deputy Director")
DIRECTOR = "D", _("Director")
DIRECTOR_GENERAL = "DG", _("Director General")
EO = "EO", _("EO")
G6 = "G6", _("G6")
G7 = "G7", _("G7")
HEO = "HEO", _("HEO")
PS = "PS", _("Permanent Secretary")
SEO = "SEO", _("SEO")
OT = "OT", _("Other")

class Profession(models.TextChoices):
AN = "AN", _("Analysis")
CM = "CMC", _("Commercial")
COM = "COM", _("Communications")
CFIN = "CFIN", _("Corporate finance")
CF = "CF", _("Counter fraud")
DDT = "DDT", _("Digital, data and technology")
EC = "EC", _("Economics")
FIN = "FIN", _("Finance")
FEDG = "FEDG", _("Fraud, error, debts and grants")
HR = "HR", _("Human resources")
IA = "IA", _("Intelligence analysis")
IAUD = "IAUD", _("Internal audit")
IT = "IT", _("International trade")
KIM = "KIM", _("Knowledge and information management")
LG = "LG", _("Legal")
MD = "MD", _("Medical")
OP = "OP", _("Occupational psychology")
OD = "OD", _("Operational delivery")
OR = "OR", _("Operational research")
PL = "PL", _("Planning")
PI = "PI", _("Planning inspection")
POL = "POL", _("Policy")
PD = "PD", _("Project delivery")
PR = "PR", _("Property")
SE = "SE", _("Science and engineering")
SC = "SC", _("Security")
SR = "SR", _("Social research")
ST = "ST", _("Statistics")
TX = "TX", _("Tax")
VET = "VET", _("Veterinary")
OT = "OT", _("Other")

username = None
verified = models.BooleanField(default=False, blank=True, null=True)
invited_at = models.DateTimeField(default=None, blank=True, null=True)
invite_accepted_at = models.DateTimeField(default=None, blank=True, null=True)
last_token_sent_at = models.DateTimeField(editable=False, blank=True, null=True)
password = models.CharField("password", max_length=128, blank=True, null=True)
class RedboxUserManager(BaseUserManager):

use_in_migrations = True

def _create_user(self, username, password, **extra_fields):
"""Create and save a User with the given email and password."""
if not username:
raise ValueError("The given email must be set")
#email = self.normalize_email(email)
User = self.model(email=email, **extra_fields)
User.set_password(password)
User.save(using=self._db)
return User

def create_user(self, username, password=None, **extra_fields):
"""Create and save a regular User with the given email and password."""
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(username, password, **extra_fields)

def create_superuser(self, username, password, **extra_fields):
"""Create and save a SuperUser with the given email and password."""
extra_fields.setdefault("is_staff", True)
extra_fields.setdefault("is_superuser", True)

if extra_fields.get("is_staff") is not True:
raise ValueError("Superuser must have is_staff=True.")
if extra_fields.get("is_superuser") is not True:
raise ValueError("Superuser must have is_superuser=True.")

return self._create_user(username, password, **extra_fields)

class User(AbstractBaseUser, PermissionsMixin, UUIDPrimaryKeyBase):
username = models.EmailField(unique=True, default="default@default.co.uk")
password = models.CharField(default="fakepassword")
email = models.EmailField(unique=True)
first_name = models.CharField(max_length=48)
last_name = models.CharField(max_length=48)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
is_superuser = models.BooleanField(default=False)
date_joined = models.DateTimeField(default=timezone.now)
business_unit = models.ForeignKey(BusinessUnit, null=True, blank=True, on_delete=models.SET_NULL)
grade = models.CharField(null=True, blank=True, max_length=3, choices=UserGrade)
profession = models.CharField(null=True, blank=True, max_length=4, choices=Profession)
objects = BaseUserManager()
grade = models.CharField(null=True, blank=True, max_length=3)
profession = models.CharField(null=True, blank=True, max_length=4)

USERNAME_FIELD = 'username'
REQUIRED_FIELDS = []
objects = RedboxUserManager()

def __str__(self) -> str: # pragma: no cover
return f"{self.email}"
Expand All @@ -108,7 +103,79 @@ def get_bearer_token(self) -> str:
"""the bearer token expected by the core-api"""
user_uuid = str(self.id)
bearer_token = jwt.encode({"user_uuid": user_uuid}, key=settings.SECRET_KEY)
return f"Bearer {bearer_token}"
return f"Bearer {bearer_token}"

# class User(BaseUser, UUIDPrimaryKeyBase):
# class UserGrade(models.TextChoices):
# AA = "AA", _("AA")
# AO = "AO", _("AO")
# DEPUTY_DIRECTOR = "DD", _("Deputy Director")
# DIRECTOR = "D", _("Director")
# DIRECTOR_GENERAL = "DG", _("Director General")
# EO = "EO", _("EO")
# G6 = "G6", _("G6")
# G7 = "G7", _("G7")
# HEO = "HEO", _("HEO")
# PS = "PS", _("Permanent Secretary")
# SEO = "SEO", _("SEO")
# OT = "OT", _("Other")

# class Profession(models.TextChoices):
# AN = "AN", _("Analysis")
# CM = "CMC", _("Commercial")
# COM = "COM", _("Communications")
# CFIN = "CFIN", _("Corporate finance")
# CF = "CF", _("Counter fraud")
# DDT = "DDT", _("Digital, data and technology")
# EC = "EC", _("Economics")
# FIN = "FIN", _("Finance")
# FEDG = "FEDG", _("Fraud, error, debts and grants")
# HR = "HR", _("Human resources")
# IA = "IA", _("Intelligence analysis")
# IAUD = "IAUD", _("Internal audit")
# IT = "IT", _("International trade")
# KIM = "KIM", _("Knowledge and information management")
# LG = "LG", _("Legal")
# MD = "MD", _("Medical")
# OP = "OP", _("Occupational psychology")
# OD = "OD", _("Operational delivery")
# OR = "OR", _("Operational research")
# PL = "PL", _("Planning")
# PI = "PI", _("Planning inspection")
# POL = "POL", _("Policy")
# PD = "PD", _("Project delivery")
# PR = "PR", _("Property")
# SE = "SE", _("Science and engineering")
# SC = "SC", _("Security")
# SR = "SR", _("Social research")
# ST = "ST", _("Statistics")
# TX = "TX", _("Tax")
# VET = "VET", _("Veterinary")
# OT = "OT", _("Other")

# username = None
# verified = models.BooleanField(default=False, blank=True, null=True)
# invited_at = models.DateTimeField(default=None, blank=True, null=True)
# invite_accepted_at = models.DateTimeField(default=None, blank=True, null=True)
# last_token_sent_at = models.DateTimeField(editable=False, blank=True, null=True)
# password = models.CharField("password", max_length=128, blank=True, null=True)
# business_unit = models.ForeignKey(BusinessUnit, null=True, blank=True, on_delete=models.SET_NULL)
# grade = models.CharField(null=True, blank=True, max_length=3, choices=UserGrade)
# profession = models.CharField(null=True, blank=True, max_length=4, choices=Profession)
# objects = BaseUserManager()

# def __str__(self) -> str: # pragma: no cover
# return f"{self.email}"

# def save(self, *args, **kwargs):
# self.email = self.email.lower()
# super().save(*args, **kwargs)

# def get_bearer_token(self) -> str:
# """the bearer token expected by the core-api"""
# user_uuid = str(self.id)
# bearer_token = jwt.encode({"user_uuid": user_uuid}, key=settings.SECRET_KEY)
# return f"Bearer {bearer_token}"


class StatusEnum(models.TextChoices):
Expand Down
Loading

0 comments on commit 2039546

Please sign in to comment.