Skip to content

Commit

Permalink
Merge pull request #115 from forza-mor-rotterdam/develop
Browse files Browse the repository at this point in the history
develop -> main
  • Loading branch information
mguikema authored Feb 7, 2024
2 parents 867355d + 58ed761 commit 7a04a0c
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 24 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/ci-cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ jobs:
uses: actions/checkout@v3

- name: Build Docker test image
run: docker-compose -f docker-compose.test.yaml build
run: docker compose -f docker-compose.test.yaml build

- name: Create Docker network
run: docker network create mbc_network

- name: Start Docker Containers
run: docker-compose -f docker-compose.test.yaml up -d
run: docker compose -f docker-compose.test.yaml up -d

- name: Run Tests
run: docker-compose -f docker-compose.test.yaml run app python manage.py test
run: docker compose -f docker-compose.test.yaml run app python manage.py test

deploy-acc:
name: Acceptance release
Expand Down Expand Up @@ -76,6 +76,8 @@ jobs:
push: true
tags: ${{ steps.meta_app.outputs.tags }}
labels: ${{ steps.meta_app.outputs.labels }}
build-args: |
GIT_SHA=${{ github.sha }}
- name: Start rollout on k8s platform
uses: actions-hub/kubectl@master
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy-prd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
push: true
tags: ${{ steps.meta_app.outputs.tags }}
labels: ${{ steps.meta_app.outputs.labels }}
build-args: |
GIT_SHA=${{ github.sha }}
- name: Start rollout on k8s platform
uses: actions-hub/kubectl@master
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/deploy-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
push: true
tags: ${{ steps.meta_app.outputs.tags }}
labels: ${{ steps.meta_app.outputs.labels }}
build-args: |
GIT_SHA=${{ github.sha }}
- name: Start rollout on k8s platform
uses: actions-hub/kubectl@master
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Build scripts to run commands within the Docker container or create local environments

# Docker variables
RUN_IN_NEW_WEBCONTEXT = docker-compose run -it mbc_app
EXEC_IN_WEB = docker-compose run mbc_app
RUN_IN_NEW_WEBCONTEXT = docker compose run -it mbc_app
EXEC_IN_WEB = docker compose run mbc_app
EXEC_IN_WEB_CMD = $(EXEC_IN_WEB) python manage.py

# General
Expand All @@ -23,12 +23,12 @@ run_frontend:

stop: ## Stop containers
@echo Stopping containers.
docker-compose down
docker compose down

clear_docker_volumes: ## clear docker volumes
check_clean_db
@echo Stopping and removing containers.
docker-compose down -v
docker compose down -v

create_superuser: ## create superuser for public tenant
@echo Create superuser. You will be prompted for email and password
Expand Down
3 changes: 3 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,7 @@ RUN mkdir -p /media \

USER $APP_USER

ARG GIT_SHA
ENV GIT_SHA=$GIT_SHA

CMD ["bash", "/app/deploy/docker-entrypoint.sh"]
2 changes: 1 addition & 1 deletion app/apps/main/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _to_base64(self, file):

def get_onderwerp_urls(self, onderwerp_ids):
return [
c
{"bron_url": c}
for c in Categorie.objects.all()
.filter(pk__in=onderwerp_ids)
.values_list("onderwerp", flat=True)
Expand Down
1 change: 1 addition & 0 deletions app/apps/main/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="git_sha" value="{{ GIT_SHA }}">
<title>{% block title %}Begraven en Cremeren{% endblock %}</title>
<link rel="apple-touch-icon" sizes="180x180" href="{{base_url}}{%webpack_static 'images/apple-touch-icon.png' %}"/>
<link rel="icon" type="image/png" sizes="32x32" href="{{base_url}}{%webpack_static 'images/favicon-32x32.png' %}"/>
Expand Down
57 changes: 57 additions & 0 deletions app/apps/main/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,60 @@ def snake_case(s):
"([A-Z][a-z]+)", r" \1", sub("([A-Z]+)", r" \1", s.replace("-", " "))
).split()
).lower()


def get_bijlagen(melding):
melding_bijlagen = [
{
**bijlage,
"aangemaakt_op": melding.get("aangemaakt_op"),
"label": "Foto van melder",
}
for bijlage in melding.get("bijlagen", [])
]
signaal_bijlagen = [
{
**bijlage,
"signaal": signaal,
"aangemaakt_op": signaal.get("aangemaakt_op"),
"label": f"Foto van melder({signaal.get('bron_id')}): {signaal.get('bron_signaal_id')}",
}
for signaal in melding.get("signalen_voor_melding", [])
for bijlage in signaal.get("bijlagen", [])
]
meldinggebeurtenis_bijlagen = [
{
**bijlage,
"meldinggebeurtenis": meldinggebeurtenis,
"aangemaakt_op": meldinggebeurtenis.get("aangemaakt_op"),
"label": "Foto van medewerker",
}
for meldinggebeurtenis in melding.get("meldinggebeurtenissen", [])
for bijlage in meldinggebeurtenis.get("bijlagen", [])
]
taakgebeurtenis_bijlagen = [
{
**bijlage,
"taakgebeurtenis": meldinggebeurtenis.get("taakgebeurtenis", {}),
"aangemaakt_op": meldinggebeurtenis.get("taakgebeurtenis", {}).get(
"aangemaakt_op"
),
"label": "Foto van medewerker",
}
for meldinggebeurtenis in melding.get("meldinggebeurtenissen", [])
for bijlage in (
meldinggebeurtenis.get("taakgebeurtenis", {}).get("bijlagen", [])
if meldinggebeurtenis.get("taakgebeurtenis")
else []
)
]
alle_bijlagen = (
signaal_bijlagen
+ meldinggebeurtenis_bijlagen
+ taakgebeurtenis_bijlagen
+ melding_bijlagen
)
alle_bijlagen_gesorteerd = sorted(
alle_bijlagen, key=lambda b: b.get("aangemaakt_op")
)
return alle_bijlagen_gesorteerd
67 changes: 67 additions & 0 deletions app/apps/services/basis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import logging
from urllib.parse import urlencode

import requests
from django.core.cache import cache
from requests import Request, Response

logger = logging.getLogger(__name__)


class BasisService:
_api_base_url = None
_timeout: tuple[int, ...] = (5, 10)
_api_path: str = "/api/v1"

class BasisUrlFout(Exception):
...

class AntwoordFout(Exception):
...

class DataOphalenFout(Exception):
...

class NaarJsonFout(Exception):
...

def get_url(self, url):
return url

def get_headers(self):
return {}

def naar_json(self, response):
try:
return response.json()
except Exception:
raise BasisService.NaarJsonFout(
f"Json antwoord verwacht: url={response.url}, status code={response.status_code}, tekst={response.text}"
)

def do_request(
self, url, method="get", data={}, params={}, raw_response=True, cache_timeout=0
) -> Response | dict:
action: Request = getattr(requests, method)
url = self.get_url(url)
action_params: dict = {
"url": url,
"headers": self.get_headers(),
"json": data,
"params": params,
"timeout": self._timeout,
}

if cache_timeout and method == "get":
cache_key = f"{url}?{urlencode(params)}"
response = cache.get(cache_key)
if not response:
response: Response = action(**action_params)
if int(response.status_code) == 200:
cache.set(cache_key, response, cache_timeout)
else:
response: Response = action(**action_params)

if raw_response:
return response
return self.naar_json(response)
30 changes: 16 additions & 14 deletions app/apps/services/mail.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import json
import logging
import os.path
import re

import magic
from apps.main.models import Begraafplaats, Categorie
from apps.main.utils import get_bijlagen
from apps.services.meldingen import MeldingenService
from apps.services.onderwerpen import OnderwerpenService
from django.conf import settings
from django.core.files.storage import default_storage
from django.core.mail import EmailMultiAlternatives, SafeMIMEMultipart
Expand Down Expand Up @@ -149,33 +152,32 @@ def melding_afgesloten_email(
melding = MeldingenService().melding_ophalen_met_signaal_url(
signaal.meldingen_signaal_url
)
bijlagen = get_bijlagen(melding)
send_to = []
begraafplaats_id = melding.get("locaties_voor_melding", [])[0].get(
"begraafplaats"
)
begraafplaats = Begraafplaats.objects.get(pk=begraafplaats_id)

melding_bijlagen = [
[
b.get("afbeelding")
for b in (
meldinggebeurtenis.get("taakgebeurtenis", {}).get("bijlagen", [])
if meldinggebeurtenis.get("taakgebeurtenis")
else []
)
]
for meldinggebeurtenis in melding.get("meldinggebeurtenissen", [])
bijlagen_flat = [
url
for url in reversed(
[b.get("afbeelding") for b in bijlagen if b.get("afbeelding")]
)
]
bijlagen_flat = [b for bl in melding_bijlagen for b in bl if b is not None]

logger.info(f"Signaal: {signaal}")
logger.info(f"Bijlage urls: {bijlagen_flat}")
logger.info(f"Melding data: {json.dumps(melding, indent=4)}")
email_context = {
"melding": melding,
"begraafplaats": begraafplaats,
"signaal": signaal,
"onderwerpen": ", ".join(
[o.get("naam") for o in melding.get("onderwerpen", [])]
[
OnderwerpenService().get_onderwerp(o).get("name")
for o in melding.get("onderwerpen", [])
]
),
"melding_bijlagen": melding_bijlagen,
"bijlagen": [b.split("/")[-1].replace(" ", "_") for b in bijlagen_flat],
}
if begraafplaats.email:
Expand Down
23 changes: 23 additions & 0 deletions app/apps/services/onderwerpen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging

from apps.services.basis import BasisService
from django.template.loader import get_template
from django.utils.safestring import mark_safe

logger = logging.getLogger(__name__)


def render_onderwerp(onderwerp_url, standaar_naam=None):
onderwerp = OnderwerpenService().get_onderwerp(onderwerp_url)
standaard_naam = onderwerp.get(
"name", "Niet gevonden!" if not standaar_naam else standaar_naam
)
if onderwerp.get("priority") == "high":
spoed_badge = get_template("badges/spoed.html")
return mark_safe(f"{standaard_naam}{spoed_badge.render()}")
return standaard_naam


class OnderwerpenService(BasisService):
def get_onderwerp(self, url) -> dict:
return self.do_request(url, cache_timeout=60 * 10, raw_response=False)
4 changes: 3 additions & 1 deletion app/apps/signalen/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def signaal_aanmaken(self, signaal_data, request, db="default"):
"v1:signaal-detail",
kwargs={"uuid": signaal.uuid},
request=request,
)
),
"bron_id": "B&C Service formulier",
"bron_signaal_id": str(signaal.uuid),
}
)
signaal_response = MeldingenService().signaal_aanmaken(
Expand Down
1 change: 1 addition & 0 deletions app/config/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ def general_settings(context):
"SESSION_STATE": token_decoded.get("session_state"),
"LOGOUT_URL": reverse("oidc_logout"),
"LOGIN_URL": f"{reverse('oidc_authentication_init')}?next={absolute(context).get('FULL_URL')}",
"GIT_SHA": settings.GIT_SHA,
}
1 change: 1 addition & 0 deletions app/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"DJANGO_SECRET_KEY", os.environ.get("SECRET_KEY", os.environ.get("APP_SECRET"))
)

GIT_SHA = os.getenv("GIT_SHA")
ENVIRONMENT = os.getenv("ENVIRONMENT")
DEBUG = ENVIRONMENT == "development"

Expand Down
2 changes: 1 addition & 1 deletion app/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ psycopg2-binary==2.9.5
django-webpack-loader==1.7.0
requests
debugpy
django-health-check
django-health-check==3.17.0
django_redis
django-permissions-policy==4.14.0
django-cors-headers==3.13.0
Expand Down

0 comments on commit 7a04a0c

Please sign in to comment.