diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ccca24a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +*.pyc \ No newline at end of file diff --git a/.gitignore b/.gitignore index 740bb18..b3f6532 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ - +*.pyc .vagrant - +*.log \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5457092 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM alpine:3.6 + +RUN apk add --update py3-pip python3 postgresql postgresql-dev zlib-dev libjpeg-turbo-dev gcc python3-dev musl-dev make \ + && pip3 install --upgrade pip +RUN if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/python3 /usr/bin/python; fi + +WORKDIR /srv/python-code-test +ADD requirements.in /srv/python-code-test/ +RUN pip3 install -r requirements.in \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e8b801d --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Starship Marketplace Backend + +## Quick start + +This test works with either [Docker](https://docs.docker.com/compose/install/#install-compose) or +[Vagrant](https://www.vagrantup.com/downloads.html). But docker were not tested by me. Seems like here +is some bugs, maybe with docker too. Vagrant also had some problems, as of latest vagrant + vitrualbox +provider versions. It's fixed and works well for me. + +```shell +$ vagrant up +$ vagrant ssh +$ runserver - new shortcut +``` +The DRF swagger must be available on http://localhost:8008/api/v1/docs/ + +## Tasks + +- [+] We need to be able to import all existing + [Starships](https://swapi.co/documentation#starships) to the provided Starship + Model (use ./manage.py commands `fetch_stsarships` and `fetch_listings`) +- [+] A potential buyer can browse all Starships +- [+] A potential buyer can browse all the listings for a given `starship_class` +- [+] A potential buyer can sort listings by price or time of listing +- [+] To list a Starship as for sale, the user should supply the Starship name and + list price +- [+] A seller can deactivate and reactivate their listing + +## P.S. + +Django 1.11? Really? Let's use django-south migrations =) diff --git a/Vagrantfile b/Vagrantfile index 9f95721..b56063a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -5,25 +5,14 @@ VAGRANTFILE_API_VERSION = "2" Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| - config.vm.box = "ubuntu/trusty64" - - # local box - config.vm.define :local, primary: true do |local| - local.vm.box_url = "https://vagrantcloud.com/ubuntu/trusty64" - local.vm.hostname = "local" + config.vm.box = "ubuntu/xenial64" # Django runserver networking - local.vm.network "forwarded_port", guest: 8888, host: 8888 - local.vm.network "forwarded_port", guest: 80, host: 8989 - local.vm.network "forwarded_port", guest: 4000, host: 4000 + config.vm.network "forwarded_port", guest: 8008, host: 8008 - local.vm.provision "shell", - path:"./scripts/server-setup.sh", args: ["dev", "vagrant"] + config.vm.provision "shell", path:"./scripts/setup-vagrant-server.sh" - # VirtualBox Provider config - local.vm.provider "virtualbox" do |vb| - vb.customize ["modifyvm", :id, "--memory", "512"] + config.vm.provider :virtualbox do |vb, override| + vb.memory = 512 end - end - end diff --git a/testsite/items/__init__.py b/api_v1/__init__.py similarity index 100% rename from testsite/items/__init__.py rename to api_v1/__init__.py diff --git a/api_v1/admin.py b/api_v1/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/api_v1/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/testsite/items/migrations/__init__.py b/api_v1/migrations/__init__.py similarity index 100% rename from testsite/items/migrations/__init__.py rename to api_v1/migrations/__init__.py diff --git a/api_v1/models.py b/api_v1/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/api_v1/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/testsite/items/tests.py b/api_v1/tests.py similarity index 100% rename from testsite/items/tests.py rename to api_v1/tests.py diff --git a/api_v1/urls.py b/api_v1/urls.py new file mode 100644 index 0000000..f2b8aed --- /dev/null +++ b/api_v1/urls.py @@ -0,0 +1,23 @@ +from rest_framework import routers +from django.conf.urls import url, include +from rest_framework.schemas import get_schema_view + +from api_v1.views import ( + shiptrader +) + + +router = routers.DefaultRouter() + +router.register(r'starship', shiptrader.StarshipViewset) +router.register(r'listing', shiptrader.ListingViewset) +router.register(r'store', shiptrader.MarketViewset) + +schema = get_schema_view(title='Shiptrader API', version='1.0.0') + + +app_name = 'api_v1' +urlpatterns = [ + url(r'^schema/$', schema), + url(r'', include(router.urls)), +] diff --git a/testsite/stream/__init__.py b/api_v1/views/__init__.py similarity index 100% rename from testsite/stream/__init__.py rename to api_v1/views/__init__.py diff --git a/api_v1/views/shiptrader.py b/api_v1/views/shiptrader.py new file mode 100644 index 0000000..bc96765 --- /dev/null +++ b/api_v1/views/shiptrader.py @@ -0,0 +1,234 @@ +import coreapi +from rest_framework import viewsets, filters + +from shiptrader import ( + models as shiptrader_models, + serializers as shiptrader_serializers +) + + +class StarshipViewset(viewsets.ReadOnlyModelViewSet): + queryset = shiptrader_models.Starship.objects.all() + serializer_class = shiptrader_serializers.StarshipSerializer + + def list(self, request, *args, **kwargs): + """ + Returns list of all known starships. + + ## 200 Response data + + Starships list + + { + "count": int, + "next": next page url, + "previous": prev page url, + "results": [ + { + "id": int, + "starship_class": string, + "manufacturer": string, + "length": float, + "hyperdrive_rating": float, + "cargo_capacity": int, + "crew": int, + "passengers": int + } + ] + } + + """ + return super().list(request, *args, **kwargs) + + def retrieve(self, request, *args, **kwargs): + """ + Returns starship object information. + + ## 200 Response data + + Starship information + + { + "id": int, + "starship_class": string, + "manufacturer": string, + "length": float, + "hyperdrive_rating": float, + "cargo_capacity": int, + "crew": int, + "passengers": int + } + + """ + return super().retrieve(request, *args, **kwargs) + + +class MarketFilter(filters.BaseFilterBackend): + def get_schema_fields(self, view): + return [ + coreapi.Field( + name='starship_class', + location='query', + required=False, + type='string', + description='Starship class must contain', + example='death star' + ), + coreapi.Field( + name='sort', + location='query', + required=False, + type='string', + description=( + 'Sort by. Possible values are: price (asc.), -price (desc.), created_at (asc.), -created_at (desc.)' + ) + ) + ] + + +class MarketViewset(viewsets.generics.ListAPIView, + viewsets.generics.CreateAPIView, + viewsets.GenericViewSet): + queryset = shiptrader_models.Listing.objects.all() + serializer_class = shiptrader_serializers.ListingSerializer + + filter_backends = (MarketFilter, ) + + def filter_queryset(self, queryset): + return queryset.active_only()\ + .filter_by_starship_class(self.request.GET.get('starship_class'))\ + .sort(self.request.GET.get('sort'), available=('created_at', 'price')) + + def list(self, request, *args, **kwargs): + """ + Find optimal starship. + + ## 200 Response + + Listing objects paginated response. + + { + "count": int, + "next": next page url, + "previous": prev page url, + "results": [ + { + "id": int, + "ship_type": int, + "name": string, + "price": int, + "is_active": bool, + "created_at": string date, + "updated_at": string date + } + ] + } + + """ + return super().list(request, *args, **kwargs) + + def create(self, request, *args, **kwargs): + """ + Create listing object for the starship. + + ## 201 Response + + Created successfully. + + { + "id": int, + "ship_type": int, + "name": string, + "price": int, + "is_active": bool, + "created_at": string date, + "updated_at": string date + } + + """ + return super().create(request, *args, **kwargs) + + +class ListingViewset(viewsets.generics.RetrieveAPIView, + viewsets.generics.UpdateAPIView, + viewsets.generics.DestroyAPIView, + viewsets.GenericViewSet): + queryset = shiptrader_models.Listing.objects.all() + serializer_class = shiptrader_serializers.ListingSerializer + + def retrieve(self, request, *args, **kwargs): + """ + Returns listing object. + + ## 200 Response + + Listing information + + { + "id": int, + "ship_type": int, + "name": string, + "price": int, + "is_active": bool, + "created_at": string date, + "updated_at": string date + } + + """ + return super().retrieve(request, *args, **kwargs) + + def destroy(self, request, *args, **kwargs): + """ + Delete listing object. + + ## 204 Response + + Deleted successfully. + + """ + return super().destroy(request, *args, **kwargs) + + def update(self, request, *args, **kwargs): + """ + Update listing object. + + ## 204 Response + + Updated successfully. + + { + "id": int, + "ship_type": int, + "name": string, + "price": int, + "is_active": bool, + "created_at": string date, + "updated_at": string date + } + + """ + return super().update(request, *args, **kwargs) + + def partial_update(self, request, *args, **kwargs): + """ + Partial update listing object. + + ## 204 Response + + Updated successfully. + + { + "id": int, + "ship_type": int, + "name": string, + "price": int, + "is_active": bool, + "created_at": string date, + "updated_at": string date + } + + """ + return super().partial_update(request, *args, **kwargs) + + + diff --git a/config/dev.sh b/config/dev.sh deleted file mode 100644 index 1e55fbc..0000000 --- a/config/dev.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/bash -# Dev environment-specific settings - -DB_NAME="lecodetest" -DB_USER="devuser" -DB_PASS="devpass" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0513217 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: "3" +services: + code-test: + build: + context: . + command: "scripts/runserver" + volumes: + - .:/srv/python-code-test + ports: + - "8008:8008" + links: + - postgresql + postgresql: + image: postgres:9.6 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: postgres diff --git a/fabfile.py b/fabfile.py deleted file mode 100644 index 18a53bb..0000000 --- a/fabfile.py +++ /dev/null @@ -1,18 +0,0 @@ -from fabric.api import local - - -def run_manage(command): - local('/home/vagrant/.virtualenvs/le-code-test/bin/python /vagrant/testsite/manage.py %s' % command) - - -def web(): - run_manage('runserver 0.0.0.0:8888') - -def migrate(): - run_manage('migrate') - -def make_migrations(): - run_manage('makemigrations') - -def requirements(): - local('/home/vagrant/.virtualenvs/le-code-test/bin/pip install -r requirements.txt ') \ No newline at end of file diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..b99c032 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +import os +import sys + +if __name__ == "__main__": + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings") + try: + from django.core.management import execute_from_command_line + except ImportError: + # The above import may fail for some other reason. Ensure that the + # issue is really that Django is missing to avoid masking other + # exceptions on Python 2. + try: + import django + except ImportError: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) + raise + execute_from_command_line(sys.argv) diff --git a/manapy b/manapy new file mode 100755 index 0000000..b667b52 --- /dev/null +++ b/manapy @@ -0,0 +1,3 @@ +#!/bin/bash + +docker-compose run --rm code-test ./manage.py $* diff --git a/requirements.in b/requirements.in new file mode 100644 index 0000000..5f25e32 --- /dev/null +++ b/requirements.in @@ -0,0 +1,5 @@ +django>=1.11,<2.0 +Pillow>=3.4.1,<3.5 +psycopg2>=2.6.2,<2.7 +djangorestframework==3.10.3 +django-rest-swagger==2.2.0 diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 88e7096..0000000 --- a/requirements.txt +++ /dev/null @@ -1,7 +0,0 @@ -Django==1.7.2 -ecdsa==0.11 -Fabric==1.10.1 -paramiko==1.15.2 -Pillow==2.7.0 -psycopg2==2.5.4 -pycrypto==2.6.1 diff --git a/scripts/runserver b/scripts/runserver new file mode 100755 index 0000000..e53be2b --- /dev/null +++ b/scripts/runserver @@ -0,0 +1,10 @@ +#!/bin/sh + +until PGPASSWORD=postgres psql --host postgresql --username postgres -c '\l' > /dev/null; do + echo "Postgres is unavailable - sleeping" + sleep 1 +done + +export PYTHONUNBUFFERED=0 +./manage.py migrate +./manage.py runserver 0.0.0.0:8008 diff --git a/scripts/server-setup-user.sh b/scripts/server-setup-user.sh deleted file mode 100755 index b1e5862..0000000 --- a/scripts/server-setup-user.sh +++ /dev/null @@ -1,40 +0,0 @@ -#!/bin/bash -# VirtualEnv and Django setup -# -# This is a distinct file as it's meant to be run as the primary user we SSH in as - -# Grab the environment var, default to 'dev' -ENV=${1-dev} -# ... and pick up related vars -source /vagrant/config/$ENV.sh - -# Grab the user var, default to 'vagrant' -USER=${2-vagrant} - -echo -e "\033[0;34m > Running main-user setup script, with the following parameters:\033[0m" -echo -e "\033[0;34m > Environment: $ENV\033[0m" -echo -e "\033[0;34m > Main User: $USER\033[0m" - -# Set up virtualenv directory for the user if required -if [ ! -d /home/$USER/.virtualenvs ]; then - echo -e "\033[0;31m > Creating .virtualenvs folder" - mkdir /home/$USER/.virtualenvs -fi - -# write all the profile stuff for the user if required -grep -q WORKON /home/$USER/.bashrc -if [ $? -ne 0 ]; then - echo -e "\033[0;31m > Updating profile file\033[0m" - echo "export WORKON_HOME=~/.virtualenvs" >> /home/$USER/.bashrc - echo "source /usr/local/bin/virtualenvwrapper.sh" >> /home/$USER/.bashrc - echo "export PIP_VIRTUALENV_BASE=~/.virtualenvs" >> /home/$USER/.bashrc - echo "workon le-code-test" >> /home/$USER/.bashrc - echo "cd /vagrant/" >> /home/$USER/.bashrc -fi - -echo -e "\033[0;34m > Setting up virtualenv\033[0m" -export WORKON_HOME=/home/$USER/.virtualenvs -source /usr/local/bin/virtualenvwrapper.sh -export PIP_VIRTUALENV_BASE=/home/$USER/.virtualenvs -mkvirtualenv le-code-test -workon le-code-test diff --git a/scripts/server-setup.sh b/scripts/server-setup.sh deleted file mode 100755 index 64bb430..0000000 --- a/scripts/server-setup.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/bin/bash -# Server Setup -# -# Script to install all the requirements for the server-side part of the Infinity Health project - -# Note that we may want to tighten it up a little for production - e.g. better DB user privs. - -# Grab the environment var, default to 'dev' -ENV=${1-dev} -# ... and pick up related vars -source /vagrant/config/$ENV.sh - -# Grab the user var, default to 'vagrant' -USER=${2-vagrant} - -echo -e "\033[0;34m > Provisioning Vagrant server, with the following parameters:\033[0m" -echo -e "\033[0;34m > Environment: $ENV\033[0m" -echo -e "\033[0;34m > Main User: $USER\033[0m" - -# Housekeeping -apt-get update -apt-get install -y git vim - -# Python environment and tools -apt-get install -y python-setuptools python2.7 build-essential python-dev libncurses5-dev fabric -easy_install pip -pip install virtualenv virtualenvwrapper - -# Postgres DB setup -apt-get install -y postgresql-9.3 postgresql-client-9.3 postgresql-server-dev-9.3 -echo -e "\033[0;34m > Setting up DB. If it already exists this will generate warnings, but no harm will be done.\033[0m" -sudo -u postgres psql -c "CREATE DATABASE $DB_NAME ENCODING='UTF8' TEMPLATE=template0;" -sudo -u postgres psql -c "CREATE USER $DB_USER WITH PASSWORD '$DB_PASS';" -sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;" -if [ $ENV == 'dev' -o $ENV == 'test' ] - then - sudo -u postgres psql -c "ALTER USER $DB_USER CREATEDB;" -fi - -echo -e "\033[0;34m > Installing all the image support libs for pillow.\033[0m" -sudo apt-get install -y libjpeg62-dev zlib1g-dev libfreetype6-dev liblcms1-dev - -# do the rest as the user we'll be logging in as through SSH -chmod +x /vagrant/scripts/server-setup-user.sh -sudo -u $USER /vagrant/scripts/server-setup-user.sh $ENV $USER - -# install requirements -echo -e "\033[0;34m > Installing the pip requirements.\033[0m" -sudo -H -u vagrant /home/vagrant/.virtualenvs/le-code-test/bin/pip install -r /vagrant/requirements.txt diff --git a/scripts/setup-server-user.sh b/scripts/setup-server-user.sh new file mode 100755 index 0000000..3215a14 --- /dev/null +++ b/scripts/setup-server-user.sh @@ -0,0 +1,33 @@ +#!/bin/bash +# VirtualEnv and Django setup + +USER=ubuntu + +# Set up virtualenv directory for the user if required +if [ ! -d /home/$USER/.virtualenvs ]; then + mkdir /home/$USER/.virtualenvs +fi + +# write all the profile stuff for the user if required +grep -q virtualenvs /home/$USER/.bashrc +if [ $? -ne 0 ]; then + echo -e "\033[0;31m > Updating profile file\033[0m" + echo "source ~/.virtualenvs/code-test/bin/activate" >> /home/$USER/.bashrc + echo "cd /vagrant/" >> /home/$USER/.bashrc +fi + +echo -e "\033[0;34m > Setting up virtualenv\033[0m" +export WORKON_HOME=/home/$USER/.virtualenvs +export PIP_VIRTUALENV_BASE=/home/$USER/.virtualenvs +python3 -m venv $PIP_VIRTUALENV_BASE/code-test +source $PIP_VIRTUALENV_BASE/code-test/bin/activate + +# install requirements +echo -e "\033[0;34m > Installing the pip requirements.\033[0m" +$PIP_VIRTUALENV_BASE/code-test/bin/pip install -U pip +$PIP_VIRTUALENV_BASE/code-test/bin/pip install wheel==0.29.0 +$PIP_VIRTUALENV_BASE/code-test/bin/pip install -r /vagrant/requirements.in + +# setup db state +cd /vagrant +./manage.py migrate diff --git a/scripts/setup-server.sh b/scripts/setup-server.sh new file mode 100755 index 0000000..c9cbdc9 --- /dev/null +++ b/scripts/setup-server.sh @@ -0,0 +1,15 @@ +#!/bin/bash +USER=ubuntu + +apt-get update +apt-get install -y git vim build-essential python3.5-dev python3-venv \ + libncurses5-dev fabric postgresql-9.5 postgresql-server-dev-9.5 \ + libjpeg62-dev zlib1g-dev libfreetype6-dev + +sudo -u postgres psql -c "CREATE DATABASE postgres ENCODING='UTF8' TEMPLATE=template0;" +sudo -u postgres psql -c "CREATE USER ubuntu;" +sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE postgres TO ubuntu;" +sudo -u postgres psql -c "ALTER USER ubuntu CREATEDB;" + +chmod +x /vagrant/scripts/setup-server-user.sh +sudo -H -u $USER /vagrant/scripts/setup-server-user.sh diff --git a/scripts/setup-vagrant-server-user.sh b/scripts/setup-vagrant-server-user.sh new file mode 100755 index 0000000..efac980 --- /dev/null +++ b/scripts/setup-vagrant-server-user.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# VirtualEnv and Django setup + +USR=vagrant + +# Set up virtualenv directory for the user if required +if [ ! -d /home/$USR/.virtualenvs ]; then + mkdir /home/$USR/.virtualenvs +fi + +# write all the profile stuff for the user if required +grep -q virtualenvs /home/$USR/.bashrc +if [ $? -ne 0 ]; then + echo -e "\033[0;31m > Updating profile file\033[0m" + echo "source ~/.virtualenvs/code-test/bin/activate" >> /home/$USR/.bashrc + echo "cd /vagrant/" >> /home/$USR/.bashrc + echo "alias runserver='/vagrant/manage.py runserver 0:8008'" >> /home/$USR/.bashrc +fi + +echo -e "\033[0;34m > Setting up virtualenv\033[0m" +export WORKON_HOME=/home/$USR/.virtualenvs +export PIP_VIRTUALENV_BASE=/home/$USR/.virtualenvs +python3 -m venv $PIP_VIRTUALENV_BASE/code-test +source $PIP_VIRTUALENV_BASE/code-test/bin/activate + +# install requirements +echo -e "\033[0;34m > Installing the pip requirements.\033[0m" +$PIP_VIRTUALENV_BASE/code-test/bin/pip install -U pip +$PIP_VIRTUALENV_BASE/code-test/bin/pip install wheel==0.29.0 +$PIP_VIRTUALENV_BASE/code-test/bin/pip install -r /vagrant/requirements.in + +# setup db state +cd /vagrant +./manage.py migrate diff --git a/scripts/setup-vagrant-server.sh b/scripts/setup-vagrant-server.sh new file mode 100755 index 0000000..d2cf761 --- /dev/null +++ b/scripts/setup-vagrant-server.sh @@ -0,0 +1,15 @@ +#!/bin/bash +USR=vagrant + +apt-get update +apt-get install -y git vim build-essential python3.5-dev python3-venv \ + libncurses5-dev fabric postgresql-9.5 postgresql-server-dev-9.5 \ + libjpeg62-dev zlib1g-dev libfreetype6-dev + +sudo -u postgres psql -c "CREATE DATABASE shiptrader ENCODING='UTF8' TEMPLATE=template0;" +sudo -u postgres psql -c "CREATE USER $USR;" +sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE shiptrader TO $USR;" +sudo -u postgres psql -c "ALTER USER $USR CREATEDB;" + +chmod +x /vagrant/scripts/setup-vagrant-server-user.sh +sudo -H -u $USR /vagrant/scripts/setup-vagrant-server-user.sh diff --git a/testsite/stream/migrations/__init__.py b/shiptrader/__init__.py similarity index 100% rename from testsite/stream/migrations/__init__.py rename to shiptrader/__init__.py diff --git a/shiptrader/admin.py b/shiptrader/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/shiptrader/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/testsite/testsite/__init__.py b/shiptrader/management/__init__.py similarity index 100% rename from testsite/testsite/__init__.py rename to shiptrader/management/__init__.py diff --git a/shiptrader/management/commands/__init__.py b/shiptrader/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shiptrader/management/commands/fetch_listings.py b/shiptrader/management/commands/fetch_listings.py new file mode 100644 index 0000000..f9621b2 --- /dev/null +++ b/shiptrader/management/commands/fetch_listings.py @@ -0,0 +1,48 @@ +# coding=utf-8 +import time +import requests + +from django.core.management.base import BaseCommand + +from shiptrader import models as shiptrader_models + + +class Command(BaseCommand): + help = 'Convert starships from https://swapi.co/documentation#starships to listings' + + provider = 'https://swapi.co/api/starships/' + + def filter_numerical(self, value): + if isinstance(value, str): + if ',' in value: + return float(value.replace(',', '.')) + if value == 'unknown': + return 0 + return value + + def fetch_objects(self, provider=provider): + self.stdout.write(self.style.SUCCESS(f'Fetching page {provider}...')) + response = requests.get(provider).json() + buffer = [] + for starship in response['results']: + buffer.append(shiptrader_models.Listing( + name=starship['name'], + ship_type=shiptrader_models.Starship.objects.filter(starship_class=starship['starship_class']).first(), + price=self.filter_numerical(starship['cost_in_credits']) + )) + + shiptrader_models.Listing.objects.bulk_create(buffer) + self.stdout.write(self.style.SUCCESS(f'\t>> Fetched {len(buffer)} starships.')) + if response['next']: + return self.fetch_objects(response['next']) + + def handle(self, **options): + start_time = time.time() + try: + self.fetch_objects() + except Exception as e: + raise e + finally: + self.stdout.write(self.style.SUCCESS( + '\n\nFinished in %.2f seconds.' % (time.time() - start_time) + )) diff --git a/shiptrader/management/commands/fetch_starships.py b/shiptrader/management/commands/fetch_starships.py new file mode 100644 index 0000000..92ccb68 --- /dev/null +++ b/shiptrader/management/commands/fetch_starships.py @@ -0,0 +1,52 @@ +# coding=utf-8 +import time +import requests + +from django.core.management.base import BaseCommand + +from shiptrader import models as shiptrader_models + + +class Command(BaseCommand): + help = 'Fetch all starships from https://swapi.co/documentation#starships' + + provider = 'https://swapi.co/api/starships/' + + def filter_numerical(self, value): + if isinstance(value, str): + if ',' in value: + return float(value.replace(',', '.')) + if value == 'unknown': + return 0 + return value + + def fetch_objects(self, provider=provider): + self.stdout.write(self.style.SUCCESS(f'Fetching page {provider}...')) + response = requests.get(provider).json() + buffer = [] + for starship in response['results']: + buffer.append(shiptrader_models.Starship( + starship_class=starship['starship_class'], + manufacturer=starship['manufacturer'], + length=self.filter_numerical(starship['length']), + hyperdrive_rating=self.filter_numerical(starship['hyperdrive_rating']), + cargo_capacity=self.filter_numerical(starship['cargo_capacity']), + crew=self.filter_numerical(starship['crew']), + passengers=self.filter_numerical(starship['passengers']), + )) + + shiptrader_models.Starship.objects.bulk_create(buffer) + self.stdout.write(self.style.SUCCESS(f'\t>> Fetched {len(buffer)} starships.')) + if response['next']: + return self.fetch_objects(response['next']) + + def handle(self, **options): + start_time = time.time() + try: + self.fetch_objects() + except Exception as e: + raise e + finally: + self.stdout.write(self.style.SUCCESS( + '\n\nFinished in %.2f seconds.' % (time.time() - start_time) + )) diff --git a/shiptrader/migrations/0001_initial.py b/shiptrader/migrations/0001_initial.py new file mode 100644 index 0000000..5a3bfc5 --- /dev/null +++ b/shiptrader/migrations/0001_initial.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.9 on 2018-01-16 13:15 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Listing', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ('price', models.IntegerField()), + ], + ), + migrations.CreateModel( + name='Starship', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('starship_class', models.CharField(max_length=255)), + ('manufacturer', models.CharField(max_length=255)), + ('length', models.FloatField()), + ('hyperdrive_rating', models.FloatField()), + ('cargo_capacity', models.BigIntegerField()), + ('crew', models.IntegerField()), + ('passengers', models.IntegerField()), + ], + ), + migrations.AddField( + model_name='listing', + name='ship_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='listings', to='shiptrader.Starship'), + ), + ] diff --git a/shiptrader/migrations/0002_auto_20191002_1519.py b/shiptrader/migrations/0002_auto_20191002_1519.py new file mode 100644 index 0000000..27b7536 --- /dev/null +++ b/shiptrader/migrations/0002_auto_20191002_1519.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.6 on 2019-10-02 15:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('shiptrader', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='listing', + name='price', + field=models.BigIntegerField(), + ), + ] diff --git a/shiptrader/migrations/0003_auto_20191002_1541.py b/shiptrader/migrations/0003_auto_20191002_1541.py new file mode 100644 index 0000000..3d6842c --- /dev/null +++ b/shiptrader/migrations/0003_auto_20191002_1541.py @@ -0,0 +1,25 @@ +# Generated by Django 2.2.6 on 2019-10-02 15:41 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('shiptrader', '0002_auto_20191002_1519'), + ] + + operations = [ + migrations.AddField( + model_name='listing', + name='created_at', + field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now), + preserve_default=False, + ), + migrations.AddField( + model_name='listing', + name='updated_at', + field=models.DateTimeField(auto_now=True), + ), + ] diff --git a/shiptrader/migrations/0004_listing_is_active.py b/shiptrader/migrations/0004_listing_is_active.py new file mode 100644 index 0000000..0c18472 --- /dev/null +++ b/shiptrader/migrations/0004_listing_is_active.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.6 on 2019-10-02 15:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('shiptrader', '0003_auto_20191002_1541'), + ] + + operations = [ + migrations.AddField( + model_name='listing', + name='is_active', + field=models.BooleanField(blank=True, default=True), + ), + ] diff --git a/shiptrader/migrations/__init__.py b/shiptrader/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/shiptrader/models.py b/shiptrader/models.py new file mode 100644 index 0000000..41d2c78 --- /dev/null +++ b/shiptrader/models.py @@ -0,0 +1,57 @@ +from django.db import models + + +class Starship(models.Model): + starship_class = models.CharField(max_length=255) + manufacturer = models.CharField(max_length=255) + + length = models.FloatField() + hyperdrive_rating = models.FloatField() + cargo_capacity = models.BigIntegerField() + + crew = models.IntegerField() + passengers = models.IntegerField() + + def __str__(self): + return '{self.manufacturer} {self.starship_class}'.format(self=self) + + +class ListingManager(models.QuerySet): + def active_only(self): + return self.filter(is_active=True) + + def inactive_only(self): + return self.filter(is_active=False) + + def filter_by_starship_class(self, ship_class): + ship_class = str(ship_class or '').strip() + if not ship_class: + return self + + return self.filter(ship_type__starship_class__icontains=ship_class) + + def sort(self, value, available=None): + value = list(filter(lambda x: len(x), str(value or '').strip().split(','))) + if not value: + return self + + if available: + value = filter(lambda x: x.strip('-') in available, value) + + return self.order_by(*list(value)) + + +class Listing(models.Model): + objects = ListingManager.as_manager() + + name = models.CharField(max_length=255) + ship_type = models.ForeignKey(Starship, related_name='listings', on_delete=models.CASCADE) + price = models.BigIntegerField() + + created_at = models.DateTimeField(auto_now_add=True) + updated_at = models.DateTimeField(auto_now=True) + + is_active = models.BooleanField(default=True, null=False, blank=True) + + def __str__(self): + return '{self.name} {self.ship_type} ({self.price:,} CR)'.format(self=self) diff --git a/shiptrader/serializers.py b/shiptrader/serializers.py new file mode 100644 index 0000000..353cbe7 --- /dev/null +++ b/shiptrader/serializers.py @@ -0,0 +1,19 @@ +from rest_framework import serializers + +from shiptrader import models as shiptrader_models + + +class StarshipSerializer(serializers.ModelSerializer): + class Meta: + model = shiptrader_models.Starship + fields = '__all__' + + +class ListingSerializer(serializers.ModelSerializer): + ship_type = serializers.PrimaryKeyRelatedField( + queryset=shiptrader_models.Starship.objects.all() + ) + + class Meta: + model = shiptrader_models.Listing + fields = '__all__' diff --git a/testsite/stream/tests.py b/shiptrader/tests.py similarity index 100% rename from testsite/stream/tests.py rename to shiptrader/tests.py diff --git a/testsite/items/views.py b/shiptrader/views.py similarity index 100% rename from testsite/items/views.py rename to shiptrader/views.py diff --git a/testsite/__init__.py b/testsite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testsite/items/admin.py b/testsite/items/admin.py deleted file mode 100644 index debc267..0000000 --- a/testsite/items/admin.py +++ /dev/null @@ -1,6 +0,0 @@ -from django.contrib import admin - -from models import PhotoItem, TweetItem - -admin.site.register(PhotoItem) -admin.site.register(TweetItem) diff --git a/testsite/items/migrations/0001_initial.py b/testsite/items/migrations/0001_initial.py deleted file mode 100644 index 9392339..0000000 --- a/testsite/items/migrations/0001_initial.py +++ /dev/null @@ -1,41 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import models, migrations -from django.conf import settings - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ] - - operations = [ - migrations.CreateModel( - name='PhotoItem', - fields=[ - ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), - ('created_at', models.DateTimeField()), - ('image', models.ImageField(upload_to=b'')), - ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)), - ], - options={ - 'abstract': False, - }, - bases=(models.Model,), - ), - migrations.CreateModel( - name='TweetItem', - fields=[ - ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), - ('created_at', models.DateTimeField()), - ('text', models.CharField(max_length=150)), - ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)), - ], - options={ - 'abstract': False, - }, - bases=(models.Model,), - ), - ] diff --git a/testsite/items/models.py b/testsite/items/models.py deleted file mode 100644 index 7d7ebe0..0000000 --- a/testsite/items/models.py +++ /dev/null @@ -1,18 +0,0 @@ -from django.db import models -from django.contrib.auth.models import User - - -class ItemAbstract(models.Model): - user = models.ForeignKey(User) - created_at = models.DateTimeField() - - class Meta: - abstract = True - - -class PhotoItem(ItemAbstract): - image = models.ImageField() - - -class TweetItem(ItemAbstract): - text = models.CharField(max_length=150) diff --git a/testsite/manage.py b/testsite/manage.py deleted file mode 100755 index 4671d39..0000000 --- a/testsite/manage.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env python -import os -import sys - -if __name__ == "__main__": - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings") - - from django.core.management import execute_from_command_line - - execute_from_command_line(sys.argv) diff --git a/testsite/settings.py b/testsite/settings.py new file mode 100644 index 0000000..6f91262 --- /dev/null +++ b/testsite/settings.py @@ -0,0 +1,149 @@ +""" +Django settings for testsite project. + +Generated by 'django-admin startproject' using Django 1.11.8. + +For more information on this file, see +https://docs.djangoproject.com/en/1.11/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/1.11/ref/settings/ +""" + +import os + +IS_DOCKER = os.getenv('USER') == 'ubuntu' +IS_VAGRANT = os.getenv('USER') == 'vagrant' + +# Build paths inside the project like this: os.path.join(BASE_DIR, ...) +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = 'a-z$8$66fyjy01^328r$5nmj=bzz2a8m%^-kf403!(ohrg5k_b' + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'rest_framework', + 'rest_framework_swagger', + + 'shiptrader', + 'api_v1', +] + +MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', +] + +ROOT_URLCONF = 'testsite.urls' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + +WSGI_APPLICATION = 'testsite.wsgi.application' + + +# Database +# https://docs.djangoproject.com/en/1.11/ref/settings/#databases +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'shiptrader', + } +} + +if IS_VAGRANT: + DATABASES['default']['NAME'] = 'shiptrader' + DATABASES['default']['USER'] = 'vagrant' +elif IS_DOCKER: + DATABASES['default']['NAME'] = 'postgres' + DATABASES['default']['HOST'] = 'postgresql' + DATABASES['default']['USER'] = 'postgres' + DATABASES['default']['PASSWORD'] = 'postgres' + +# Password validation +# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Internationalization +# https://docs.djangoproject.com/en/1.11/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/1.11/howto/static-files/ + +STATIC_URL = '/static/' + + +# Swagger settings +REST_FRAMEWORK = { + 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema', + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', + 'PAGE_SIZE': 10 +} + + +try: + from testsite.local_settings import * +except ImportError: + pass diff --git a/testsite/stream/admin.py b/testsite/stream/admin.py deleted file mode 100644 index 54a2562..0000000 --- a/testsite/stream/admin.py +++ /dev/null @@ -1,5 +0,0 @@ -from django.contrib import admin - -from models import Stream - -admin.site.register(Stream) diff --git a/testsite/stream/migrations/0001_initial.py b/testsite/stream/migrations/0001_initial.py deleted file mode 100644 index 30d7667..0000000 --- a/testsite/stream/migrations/0001_initial.py +++ /dev/null @@ -1,26 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import models, migrations -from django.conf import settings - - -class Migration(migrations.Migration): - - dependencies = [ - migrations.swappable_dependency(settings.AUTH_USER_MODEL), - ] - - operations = [ - migrations.CreateModel( - name='Stream', - fields=[ - ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), - ('created_at', models.DateTimeField()), - ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)), - ], - options={ - }, - bases=(models.Model,), - ), - ] diff --git a/testsite/stream/models.py b/testsite/stream/models.py deleted file mode 100644 index e10fd24..0000000 --- a/testsite/stream/models.py +++ /dev/null @@ -1,7 +0,0 @@ -from django.db import models -from django.contrib.auth.models import User - - -class Stream(models.Model): - user = models.ForeignKey(User) - created_at = models.DateTimeField() \ No newline at end of file diff --git a/testsite/stream/views.py b/testsite/stream/views.py deleted file mode 100644 index 91ea44a..0000000 --- a/testsite/stream/views.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.shortcuts import render - -# Create your views here. diff --git a/testsite/testsite/settings.py b/testsite/testsite/settings.py deleted file mode 100644 index 2821860..0000000 --- a/testsite/testsite/settings.py +++ /dev/null @@ -1,86 +0,0 @@ -""" -Django settings for testsite project. - -For more information on this file, see -https://docs.djangoproject.com/en/1.7/topics/settings/ - -For the full list of settings and their values, see -https://docs.djangoproject.com/en/1.7/ref/settings/ -""" - -# Build paths inside the project like this: os.path.join(BASE_DIR, ...) -import os -BASE_DIR = os.path.dirname(os.path.dirname(__file__)) - - -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = '!4oojc^tmd%+*c_^(n)7^p^6-0f8td(_o&(2bj*e7demr&jtvy' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -TEMPLATE_DEBUG = True - -ALLOWED_HOSTS = [] - -INSTALLED_APPS = ( - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'stream', - 'items', -) - -MIDDLEWARE_CLASSES = ( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -) - -ROOT_URLCONF = 'testsite.urls' - -WSGI_APPLICATION = 'testsite.wsgi.application' - - -# Database -# https://docs.djangoproject.com/en/1.7/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': 'lecodetest', - 'USER': 'devuser', - 'PASSWORD': 'devpass', - 'HOST': '127.0.0.1', - 'PORT': '', - } -} - -# Internationalization -# https://docs.djangoproject.com/en/1.7/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = True - -USE_TZ = True - - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.7/howto/static-files/ - -STATIC_URL = '/static/' diff --git a/testsite/testsite/urls.py b/testsite/testsite/urls.py deleted file mode 100644 index bcd73c7..0000000 --- a/testsite/testsite/urls.py +++ /dev/null @@ -1,10 +0,0 @@ -from django.conf.urls import patterns, include, url -from django.contrib import admin - -urlpatterns = patterns('', - # Examples: - # url(r'^$', 'testsite.views.home', name='home'), - # url(r'^blog/', include('blog.urls')), - - url(r'^admin/', include(admin.site.urls)), -) diff --git a/testsite/urls.py b/testsite/urls.py new file mode 100644 index 0000000..2d4ef50 --- /dev/null +++ b/testsite/urls.py @@ -0,0 +1,14 @@ +from django.contrib import admin +from django.conf.urls import url, include +from rest_framework_swagger.views import get_swagger_view + + +api_v1_docs = get_swagger_view('Shiptrader API', url='/api/v1/', urlconf='api_v1.urls') + + +urlpatterns = [ + url(r'^admin/', admin.site.urls), + + url(r'^api/v1/docs/$', api_v1_docs), + url(r'^api/v1/', include('api_v1.urls')), +] diff --git a/testsite/testsite/wsgi.py b/testsite/wsgi.py similarity index 83% rename from testsite/testsite/wsgi.py rename to testsite/wsgi.py index 2091485..a65c11b 100644 --- a/testsite/testsite/wsgi.py +++ b/testsite/wsgi.py @@ -4,11 +4,13 @@ It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see -https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/ +https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ """ import os -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings") from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings") + application = get_wsgi_application()