Skip to content

Commit 7ce9073

Browse files
committed
start on actually making the django webapp work
1 parent 06e5883 commit 7ce9073

12 files changed

Lines changed: 67 additions & 172 deletions

File tree

‎Dockerfile‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ RUN pip install -e /src
1111
COPY tests/ /tests/
1212

1313
WORKDIR /src
14-
ENV FLASK_APP=allocation/entrypoints/flask_app.py FLASK_DEBUG=1 PYTHONUNBUFFERED=1
15-
CMD flask run --host=0.0.0.0 --port=80
14+
ENV DJANGO_SETTINGS_MODULE=djangoproject.django_project.settings
15+
RUN python /src/djangoproject/manage.py migrate
16+
CMD python /src/djangoproject/manage.py runserver 0.0.0.0:80

‎requirements.txt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# app
2-
sqlalchemy
3-
flask
42
psycopg2-binary
3+
django
54

65
# tests
76
pytest

‎src/allocation/adapters/repository.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=no-member, no-self-use
12
from typing import Set
23
import abc
34
from allocation.domain import model
@@ -17,7 +18,7 @@ def get(self, reference) -> model.Batch:
1718
return p
1819

1920
@abc.abstractmethod
20-
def _get(self, sku):
21+
def _get(self, reference):
2122
raise NotImplementedError
2223

2324

@@ -41,3 +42,6 @@ def _get(self, reference):
4142
.first()
4243
.to_domain()
4344
)
45+
46+
def list(self):
47+
return [b.to_domain() for b in self.django_models.Batch.objects.all()]

‎src/allocation/entrypoints/flask_app.py‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎src/djangoproject/alloc/models.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@ def update_from_domain(batch: domain_model.Batch):
1717
b.sku = batch.sku
1818
b.qty = batch._purchased_quantity
1919
b.eta = batch.eta
20-
b.allocation_set.set(Allocation.from_domain(l, b) for l in batch._allocations)
2120
b.save()
21+
b.allocation_set.set(
22+
Allocation.from_domain(l, b)
23+
for l in batch._allocations
24+
)
2225

2326
def to_domain(self) -> domain_model.Batch:
2427
b = domain_model.Batch(
2528
ref=self.reference, sku=self.sku, qty=self.qty, eta=self.eta
2629
)
27-
b._allocations = set(a.line.to_domain() for a in self.allocation_set.all())
30+
b._allocations = set(
31+
a.line.to_domain()
32+
for a in self.allocation_set.all()
33+
)
2834
return b
2935

3036

‎src/djangoproject/alloc/views.py‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1-
from django.shortcuts import render
1+
from datetime import datetime
2+
import json
3+
import os
24

3-
# Create your views here.
5+
import django
6+
from django.http import HttpResponse, JsonResponse
7+
from django.views.decorators.csrf import csrf_exempt
8+
9+
from allocation.domain import model
10+
from allocation.service_layer import services, unit_of_work
11+
12+
os.environ["DJANGO_SETTINGS_MODULE"] = "djangoproject.django_project.settings"
13+
django.setup()
14+
15+
16+
@csrf_exempt
17+
def add_batch(request):
18+
data = json.loads(request.body)
19+
eta = data["eta"]
20+
if eta is not None:
21+
eta = datetime.fromisoformat(eta).date()
22+
services.add_batch(
23+
data["ref"], data["sku"], data["qty"], eta,
24+
unit_of_work.DjangoUnitOfWork(),
25+
)
26+
return HttpResponse("OK", status=201)
27+
28+
29+
@csrf_exempt
30+
def allocate(request):
31+
data = json.loads(request.body)
32+
try:
33+
batchref = services.allocate(
34+
data["orderid"],
35+
data["sku"],
36+
data["qty"],
37+
unit_of_work.DjangoUnitOfWork(),
38+
)
39+
except (model.OutOfStock, services.InvalidSku) as e:
40+
return JsonResponse({"message": str(e)}, status=400)
41+
42+
return JsonResponse({"batchref": batchref}, status=201)

‎src/djangoproject/django_project/urls.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
1. Import the include() function: from django.urls import include, path
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
16-
from django.contrib import admin
1716
from django.urls import path
17+
from alloc import views
1818

1919
urlpatterns = [
20-
path("admin/", admin.site.urls),
20+
path("add_batch", views.add_batch),
21+
path("allocate", views.allocate),
2122
]

‎tests/conftest.py‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# pylint: disable=redefined-outer-name
22
import time
33
from pathlib import Path
4-
54
import pytest
65
import requests
76
from requests.exceptions import ConnectionError
8-
97
from allocation import config
108

119

@@ -16,16 +14,6 @@ def django_models():
1614
return models
1715

1816

19-
def wait_for_postgres_to_come_up(engine):
20-
deadline = time.time() + 10
21-
while time.time() < deadline:
22-
try:
23-
return engine.connect()
24-
except OperationalError:
25-
time.sleep(0.5)
26-
pytest.fail("Postgres never came up")
27-
28-
2917
def wait_for_webapp_to_come_up():
3018
deadline = time.time() + 10
3119
url = config.get_api_url()
@@ -37,21 +25,6 @@ def wait_for_webapp_to_come_up():
3725
pytest.fail("API never came up")
3826

3927

40-
@pytest.fixture(scope="session")
41-
def postgres_db():
42-
engine = create_engine(config.get_postgres_uri())
43-
wait_for_postgres_to_come_up(engine)
44-
metadata.create_all(engine)
45-
return engine
46-
47-
48-
@pytest.fixture
49-
def postgres_session(postgres_db):
50-
start_mappers()
51-
yield sessionmaker(bind=postgres_db)()
52-
clear_mappers()
53-
54-
5528
@pytest.fixture
5629
def restart_api():
5730
(Path(__file__).parent / "../src/allocation/entrypoints/flask_app.py").touch()

‎tests/e2e/test_api.py‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def post_to_add_batch(ref, sku, qty, eta):
2929
assert r.status_code == 201
3030

3131

32-
@pytest.mark.usefixtures("postgres_db")
3332
@pytest.mark.usefixtures("restart_api")
3433
def test_happy_path_returns_201_and_allocated_batch():
3534
sku, othersku = random_sku(), random_sku("other")
@@ -48,7 +47,6 @@ def test_happy_path_returns_201_and_allocated_batch():
4847
assert r.json()["batchref"] == earlybatch
4948

5049

51-
@pytest.mark.usefixtures("postgres_db")
5250
@pytest.mark.usefixtures("restart_api")
5351
def test_unhappy_path_returns_400_and_error_message():
5452
unknown_sku, orderid = random_sku(), random_orderid()

‎tests/integration/test_orm.py‎

Lines changed: 0 additions & 89 deletions
This file was deleted.

0 commit comments

Comments
 (0)