Skip to content

Commit

Permalink
Merge pull request #16 from yoyo930021/backendgeo-django-setup
Browse files Browse the repository at this point in the history
[backend] GeoDjango setting
  • Loading branch information
stegben committed Oct 6, 2019
2 parents 2a5ef50 + 15ecfb7 commit 3ab5003
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 9 deletions.
3 changes: 3 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /Disfactory

RUN apt-get update
RUN apt-get install -y binutils libproj-dev gdal-bin

# Install dependencies
COPY Pipfile Pipfile.lock /Disfactory/
RUN pip install pipenv && pipenv install --system
Expand Down
30 changes: 28 additions & 2 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,37 @@

### 1. Pre-install
- Python 3.7
- PostgreSQL 11
- PostgreSQL > 9.0
- PostGIS > 2.0

### 2. DB settings
用 superuser 進 PostgreSQL
```
sudo su postgres
psql
```

建立一個可以開 DB 的 user 給 server
```
CREATE USER "disfactory" CREATEDB;
\password disfactory
```

開一個 DB
```
CREATE DATABASE disfactory_data OWNER "disfactory";
```

打開 PostGIS extention,但要先[安裝](https://postgis.net/install/)在系統上。
```
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
```

設定好之後,記得也要改環境變數,或是直接改動 `.env` 裡面的值。

### 3. Environment variables
使用環境變數來設定,使用 `dotenv` 來方便修改。請參考 `.env.sample` 並複製一份 `.env`
使用環境變數來設定專案,用 `python-dotenv` 來讀取。請參考 `.env.sample` 並複製一份 `.env`

### 4. Install python packages
`pipenv install --dev`
Expand Down
34 changes: 34 additions & 0 deletions backend/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by Django 2.2.3 on 2019-10-04 03:19

import django.contrib.gis.db.models.fields
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Factory',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('point', django.contrib.gis.db.models.fields.PointField(srid=3857)),
],
),
migrations.CreateModel(
name='Image',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='ReportRecord',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
11 changes: 6 additions & 5 deletions backend/api/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from django.db import models
from django.contrib.gis.db import models
from django.conf import settings

# TODO: Design and implement these models

class Factory:
class Factory(models.Model):
"""Factories that are potential to be illegal.
This table should store information of a factory,
Expand All @@ -17,10 +18,10 @@ class Factory:
"""
# TODO: write a migration for data initialization, ref: https://docs.djangoproject.com/en/2.2/howto/initial-data/
pass
point = models.PointField(srid=settings.POSTGIS_SRID)


class ReportRecord:
class ReportRecord(models.Model):
"""Report records send by users.
This table should contain the report record after calling these API:
Expand All @@ -39,7 +40,7 @@ class ReportRecord:
pass


class Image:
class Image(models.Model):
"""Images of factories that are uploaded by user
We store the actual image files on Imgur, so this table
Expand Down
11 changes: 10 additions & 1 deletion backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ services:
- 8000:8000
depends_on:
- db
environment:
DISFACTORY_BACKEND_DEFAULT_DB_NAME: disfactory_dev
DISFACTORY_BACKEND_DEFAULT_DB_USER: postgres
DISFACTORY_BACKEND_DEFAULT_DB_PASSWORD: cop
DISFACTORY_BACKEND_DEFAULT_DB_HOST: db
DISFACTORY_BACKEND_DEFAULT_DB_PORT: 5432

db:
image: postgres:11
image: mdillon/postgis:11-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
POSTGRES_DB: disfactory_dev
POSTGRES_PASSWORD: cop

volumes:
postgres_data:
8 changes: 7 additions & 1 deletion backend/gis_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",

# for gis
"django.contrib.gis",
# 3rd party
"rest_framework",
# Local
Expand Down Expand Up @@ -80,7 +83,7 @@

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"ENGINE": "django.contrib.gis.db.backends.postgis",
"NAME": os.environ.get("DISFACTORY_BACKEND_DEFAULT_DB_NAME", "postgres"),
"USER": os.environ.get("DISFACTORY_BACKEND_DEFAULT_DB_USER", "postgres"),
"PASSWORD": os.environ.get("DISFACTORY_BACKEND_DEFAULT_DB_PASSWORD", "postgres"),
Expand Down Expand Up @@ -120,3 +123,6 @@

STATIC_URL = "/static/"
AUTH_USER_MODEL = "users.CustomUser"

POSTGIS_SRID = 3857
# ref: https://epsg.io/3857

0 comments on commit 3ab5003

Please sign in to comment.