Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 165 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,165 @@
.DS_Store
.DS_Store
db.sqlite3

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
93 changes: 62 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
_Fork this project and send us a pull request_

Write a simple python webservice that uses, manipuates and returns the data found here: [https://www.unisport.dk/api/products/batch/](https://www.unisport.dk/api/products/batch/?list=200776,213591,200775,197250,213590,200780,209588,217706,205990,212703,197237,205989,211651,213626,217710,200783,213576,202483,200777,203860,198079,189052,205946,209125,200784,190711,201338,201440,206026,213587,172011,209592,193539,173432,200785,201442,203854,213577,200802,197362).


**/products/**


should return the first 10 objects ordered with the cheapest first.

**/products/?page=2**

The products should be paginated where **page** in the url above should return the next 10 objects

**/products/id/**

should return the individual product.



**_Remember to test_**
**_Remember to document (why, not how)_**

#### Bonus:
extend the service so the products can also be created, edited and deleted in a backend of choice.


_You are welcome to use any thirdparty python web framework or library that you are familiar with._

#### Forking and Pull Requests
Information on how to work with forks and pull requests can be found here https://help.github.com/categories/collaborating-with-issues-and-pull-requests/
## Overview
The project is realized using Django and DRF. The endpoints are documented and testable at `/docs` and `/redoc`.

Each product can have 0..n stock items tied to itself. Each product can be tied to 0..n labels. The labels are indepentend from the products.

The code is inside a django app called `products`, which exposes the following urls:
- `products/`: Retrieves the paginated list of the products, with the products ordered by the related stock min price. Also allows creation.
- `products/{pk}`: Retrieves the specified product. Allows updates and deletes.
- `products/{pk}/stock`: Retrieves the stock for a given product. Allows the creation of new stock items for the given product.
- `products/{pk}/stock/{pk}`: Retrieves the specified stock for the given product. Allows updates and deletes.
- `products/{pk}/labels`: Retrieves the labels tied to the given product. Allows the addition of new labels to the given product.
- `products/{pk}/labels/{pk}`: Retrieves the specified label for the given stock. Allows the removal of the specified label from the given product.
- `labels/`: Retrieves all labels, paginated. Allows creation.
- `labels/{pk}`: Retrieves the specified label. Allows updates and deletes.

### Structure
Here is the structure at a glance, omitting non-important parts:
```
├── helpers
│   └── base_models.py: classes use for validation and type hints
├── models: contains ORM models divided in subfiles
│   ├── label.py
│   ├── product.py
│   └── stock.py
├── scripts
│   └── import.py: Import script
├── serializers: contains the serializers divided in subfiles
│   ├── label_serializer.py
│   ├── product_serializer.py
│   └── stock_serializer.py
├── tests: containts factories for the models and the tests on the endpoint calls
│   ├── factories
│   │   └── products.py
│   └── views
│   ├── test_label.py
│   ├── test_product.py
│   └── test_stock.py
├── urls.py
└── views: contains the views divided in subfiles
├── label_view.py
├── product_view.py
└── stock_view.py
```
### Data import
In `products/scripts` a `import.py` script is provided to retrieve the data from the provided URL and import it into the database. The script can be run with the `runscript` command, as shown in the "Running" section. It uses pydantic for validation.

### Database
The dbms is sqlite for simplicity.

### Running
From inside the repo directory, run:
```
pip install -r requirements.txt
python manage.py runscript products.scripts.import
python manage.py runserver
```
### Testing
The tests can be found inside `products/tests`. Since there is virtually no logic on the models, the tests are done on the endpoint calls to assess the responses.
From inside the repo directory, run:
```
python manage.py test
```
Empty file added assignment/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions assignment/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for assignment project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "assignment.settings")

application = get_asgi_application()
Loading