From 06a700925c0655b9643b0a6929e9aba7d8bbd20e Mon Sep 17 00:00:00 2001 From: Robin Schroer Date: Thu, 27 Oct 2016 15:46:55 +0100 Subject: [PATCH 1/2] Code test --- .dockerignore | 2 + .gitignore | 3 + Dockerfile | 9 ++ README.md | 59 ++++++++++++ Vagrantfile | 18 ++++ docker-compose.yml | 18 ++++ manage.py | 22 +++++ manapy | 3 + requirements.in | 3 + scripts/runserver | 10 ++ scripts/setup-server-user.sh | 33 +++++++ scripts/setup-server.sh | 15 +++ shiptrader/__init__.py | 0 shiptrader/admin.py | 3 + shiptrader/migrations/0001_initial.py | 43 +++++++++ shiptrader/migrations/__init__.py | 0 shiptrader/models.py | 19 ++++ shiptrader/tests.py | 3 + shiptrader/views.py | 3 + testsite/__init__.py | 0 testsite/settings.py | 129 ++++++++++++++++++++++++++ testsite/urls.py | 21 +++++ testsite/wsgi.py | 16 ++++ 23 files changed, 432 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 Vagrantfile create mode 100644 docker-compose.yml create mode 100755 manage.py create mode 100755 manapy create mode 100644 requirements.in create mode 100755 scripts/runserver create mode 100755 scripts/setup-server-user.sh create mode 100755 scripts/setup-server.sh create mode 100644 shiptrader/__init__.py create mode 100644 shiptrader/admin.py create mode 100644 shiptrader/migrations/0001_initial.py create mode 100644 shiptrader/migrations/__init__.py create mode 100644 shiptrader/models.py create mode 100644 shiptrader/tests.py create mode 100644 shiptrader/views.py create mode 100644 testsite/__init__.py create mode 100644 testsite/settings.py create mode 100644 testsite/urls.py create mode 100644 testsite/wsgi.py 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 new file mode 100644 index 0000000..b3f6532 --- /dev/null +++ b/.gitignore @@ -0,0 +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..a83e86e --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +# Ostmodern Python Code Test + +The goal of this exercise is to test that you know your way around Django and +REST APIs. Approach it the way you would an actual long-term project. + +The idea is to build a platform on which your users can buy and sell Starships. +To make this process more transparent, it has been decided to source some +technical information about the Starships on sale from the [Starship +API](https://swapi.co/documentation#starships). + +A Django project some initial data models have been created already. You may need +to do some additional data modelling to satify the requirements. + +## Getting started + +* This test works with either + [Docker](https://docs.docker.com/compose/install/#install-compose) or + [Vagrant](https://www.vagrantup.com/downloads.html) +* Get the code from `https://github.com/ostmodern/python-code-test` +* Do all your work in your own `develop` branch +* Once you have downloaded the code the following commands will get the site up + and running + +```shell +# For Docker +docker-compose up +# You can run `manage.py` commands using the `./manapy` wrapper + +# For Vagrant +vagrant up +vagrant ssh +# Inside the box +./manage.py runserver 0.0.0.0:8008 +``` +* The default Django "It worked!" page should now be available at + http://localhost:8008/ + +## Tasks + +Your task is to build a JSON-based REST API for your frontend developers to +consume. You have built a list of user stories with your colleagues, but you get +to decide how to design the API. Remember that the frontend developers will need +some documentation of your API to understand how to use it. + +We do not need you to implement users or authentication, to reduce the amount of +time this exercise will take to complete. You may use any external libraries you +require. + +* We need to be able to import all existing + [Starships](https://swapi.co/documentation#starships) to the provided Starship + Model +* 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 + +After you are done, create a release branch in your repo and send us the link. diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..6fab5ed --- /dev/null +++ b/Vagrantfile @@ -0,0 +1,18 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +# Vagrantfile API/syntax version. Don't touch unless you know what you're doing! +VAGRANTFILE_API_VERSION = "2" + +Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| + config.vm.box = "ubuntu/xenial64" + + # Django runserver networking + config.vm.network "forwarded_port", guest: 8008, host: 8008 + + config.vm.provision "shell", path:"./scripts/setup-server.sh" + + config.vm.provider :virtualbox do |vb, override| + vb.memory = 512 + end +end 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/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..13869b0 --- /dev/null +++ b/requirements.in @@ -0,0 +1,3 @@ +django>=1.11,<2.0 +Pillow>=3.4.1,<3.5 +psycopg2>=2.6.2,<2.7 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/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/shiptrader/__init__.py b/shiptrader/__init__.py new file mode 100644 index 0000000..e69de29 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/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/__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..7fc3377 --- /dev/null +++ b/shiptrader/models.py @@ -0,0 +1,19 @@ +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() + + +class Listing(models.Model): + name = models.CharField(max_length=255) + ship_type = models.ForeignKey(Starship, related_name='listings') + price = models.IntegerField() diff --git a/shiptrader/tests.py b/shiptrader/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/shiptrader/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/shiptrader/views.py b/shiptrader/views.py new file mode 100644 index 0000000..91ea44a --- /dev/null +++ b/shiptrader/views.py @@ -0,0 +1,3 @@ +from django.shortcuts import render + +# Create your views here. diff --git a/testsite/__init__.py b/testsite/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testsite/settings.py b/testsite/settings.py new file mode 100644 index 0000000..549b890 --- /dev/null +++ b/testsite/settings.py @@ -0,0 +1,129 @@ +""" +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 + +DOCKER = os.getenv('USER') != 'ubuntu' + +# 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', + 'shiptrader', +] + +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': 'postgres', + } +} + +if 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/' diff --git a/testsite/urls.py b/testsite/urls.py new file mode 100644 index 0000000..0bd1a31 --- /dev/null +++ b/testsite/urls.py @@ -0,0 +1,21 @@ +"""testsite URL Configuration + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/1.11/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.conf.urls import url, include + 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls')) +""" +from django.conf.urls import url +from django.contrib import admin + +urlpatterns = [ + url(r'^admin/', admin.site.urls), +] diff --git a/testsite/wsgi.py b/testsite/wsgi.py new file mode 100644 index 0000000..a65c11b --- /dev/null +++ b/testsite/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for testsite project. + +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.11/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testsite.settings") + +application = get_wsgi_application() From 994afec0efef42f7beed2f374252a5c07014ad0f Mon Sep 17 00:00:00 2001 From: Murphy Meng Date: Sun, 19 May 2019 18:12:36 +0100 Subject: [PATCH 2/2] update redme --- README.md | 59 +++++++------------------------------------------------ 1 file changed, 7 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index a83e86e..8885218 100644 --- a/README.md +++ b/README.md @@ -1,59 +1,14 @@ -# Ostmodern Python Code Test +This project is for implementation to https://github.com/ostmodern/python-code-test -The goal of this exercise is to test that you know your way around Django and -REST APIs. Approach it the way you would an actual long-term project. - -The idea is to build a platform on which your users can buy and sell Starships. -To make this process more transparent, it has been decided to source some -technical information about the Starships on sale from the [Starship -API](https://swapi.co/documentation#starships). - -A Django project some initial data models have been created already. You may need -to do some additional data modelling to satify the requirements. - -## Getting started - -* This test works with either - [Docker](https://docs.docker.com/compose/install/#install-compose) or - [Vagrant](https://www.vagrantup.com/downloads.html) -* Get the code from `https://github.com/ostmodern/python-code-test` -* Do all your work in your own `develop` branch -* Once you have downloaded the code the following commands will get the site up - and running +It provide API documented in [here](./shiptrader/doc/_build/html/index.html) +To start: ```shell -# For Docker docker-compose up -# You can run `manage.py` commands using the `./manapy` wrapper - -# For Vagrant -vagrant up -vagrant ssh -# Inside the box -./manage.py runserver 0.0.0.0:8008 ``` -* The default Django "It worked!" page should now be available at - http://localhost:8008/ - -## Tasks - -Your task is to build a JSON-based REST API for your frontend developers to -consume. You have built a list of user stories with your colleagues, but you get -to decide how to design the API. Remember that the frontend developers will need -some documentation of your API to understand how to use it. - -We do not need you to implement users or authentication, to reduce the amount of -time this exercise will take to complete. You may use any external libraries you -require. +and ship a POST requst to -* We need to be able to import all existing - [Starships](https://swapi.co/documentation#starships) to the provided Starship - Model -* 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 + http://localhost:8008/shiptrader/ships/ +It will load all starships information from [Starship API](https://swapi.co/documentation#starships) to local database, so you can browse and create listing against. -After you are done, create a release branch in your repo and send us the link. +Have fun with all the APIs!