Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new sample application #76

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
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
6 changes: 4 additions & 2 deletions examples/explore/exploreproj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='', cast=Csv())

# Application definition
# NB new apps need to be placed first for their templates
# to override subsequent apps
INSTALLED_APPS = [
'exploreapp',
'multigtfs',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.gis',
'exploreapp',
'multigtfs',
]

LOCAL_INSTALLED_APPS = list(config('LOCAL_INSTALLED_APPS',
Expand Down
7 changes: 7 additions & 0 deletions examples/gtfsproj/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
SECRET_KEY=l$hc*-biy7*#7cic5q5r^mtf-2&l34pq4k7znn%)si+$h(i%e&

DEBUG=True
ALLOWED_HOSTS=*

DATABASE_URL=spatialite:///db.sqlite3
#DATABASE_URL=postgis://USER:PASSWORD@HOST:PORT/DBNAME
28 changes: 28 additions & 0 deletions examples/gtfsproj/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM python:3

# Install system dependencies
RUN apt-get update && apt-get install -y \
binutils \
gdal-bin \
libproj-dev \
postgresql-client

# Copy project code (current folder)
COPY . /code

# Install project dependencies, please note uses
# pypi version of gtfsmulti not local version.
# NB: To use local you will need to copy multigtfs
# code to image and update requirements.txt
WORKDIR /code
RUN pip install -r requirements.txt

# Create volume for feeds
VOLUME /gtfs/feeds

# run_project script passes all arguments except
# bash to django manage.py script for project
# runs development server by default
RUN chmod +x /code/run_project.sh
ENTRYPOINT ["/code/run_project.sh"]
CMD ["./manage.py", "runserver", "0.0.0.0:8000"]
127 changes: 127 additions & 0 deletions examples/gtfsproj/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<h1>Multi GTFS project</h1>

This is an alternative example project. It is largely based on the
"explore" example project but has been recreated with slightly different
structure.

The intention is that this example will ultimately provide a
slightly different experience. This will include more mapping (possibly
using leaflet), new template (e.g. bootstrap 4) and different navigation.

<h2>Run with Docker</h2>

<h3>Build and run</h3>

Build docker image. This will use the settings in teh .env.example
file, which can be replaced by creating a ".env" file before building
the image (eg. cp env.example .env).

# Get source code
git clone https://github.com/alaw005/django-multi-gtfs.git
cd django-multi-gtfs/examples/gtfsproj

# Build image
sudo docker build -t multiproj .

Run docker container as background (to run in forground change
"-d" to "-it". This will automatically run the development server
using the settings in the .env.example file.

# Create container
sudo docker run -d -p 8000:8000 --name multiproj1 multiproj

Now setup database and superuser, refer management commands below.
This is only required if database does not already exist or multgtfs
has been updated (in which case need to migrate only)

# Setup database (if required)
sudo docker exec -it multiproj1 ./manage.py migrate
sudo docker exec -it multiproj1 ./manage.py createsuperuser

Now you can access from browser using host IP address and port 8000.

<h3>Management commands</h3>

You can run django management commands as follows:

sudo docker exec -it multiproj1 ./manage.py help

For example, to load a new feed you can do the following (assuming
container name is "multiproj1"). NB: The first command copies a
file from host system to the container.

cat /host/feeds/feed.zip | sudo docker exec -i multiproj1 sh -c 'cat > /gtfs/feeds/feed.zip'
sudo docker exec -it multiproj1 ./manage.py importgtfs /gtfs/feeds/feed.zip

To specify a feed name rather than the defaul use the following.

sudo docker exec -it multiproj1 ./manage.py importgtfs --name "Feed name" /feeds/feed.zip

<h3>Creating postgres database (assumes server already running)</h3>

At the command line (as long as postgres is installed):

# Create database
createdb -h HOST -p 5432 -U USERNAME multigtfs

# Install postgis extension
psql -h HOST -p 5432 -U USERNAME -d multigtfs -c "CREATE EXTENSION postgis;"

Now initiatise the database using multigtfs:

# Configure database
sudo docker exec -it multigtfs ./manage.py migrate

# Create superuser
sudo docker exec -it multigtfs ./manage.py createsuperuser

That's it.

<h2>Run without Docker</h2>

The following applies to Ubunutu

sudo apt-get install -y git python-pip python-virtualenv spatialite-bin gdal-bin

cd ~/
git clone https://github.com/alaw005/django-multi-gtfs.git

# Navigate to project folder
cd django-multi-gtfs/examples/multiproj

# Create and activate new virtual environment (myenv).
# Use "deactivate" to deactivate virtualenv.
virtualenv --always-copy myenv
source myenv/bin/activate

pip install -r requirements.txt

# Create multigtfs tables in database
./manage.py migrate

# Create db.sqlite3, superuser. Enter "yes" to create superuser and
# then enter username and password when prompted (you can leave email address blank)
./manage.py createsuperuser

# Run
./manage.py runserver 0.0.0.0:8000

Now you can access from browser using host IP address and port 8000.

<h2>Environment variables</h2>

You can either set environment variables in the .env file as per above or directly
at command line when you create the container. For example, to use existing postgis database:

sudo docker run -d -p 8000:8000 --name multiproj1 -e DATABASE_URL=postgis://USER:PASSWORD@HOST:PORT/DBNAME multiproj

The environment variables you can change are identified in the following file:

django-multi-gtfs/examples/gtfsproj/gtfsproj/settings.py

They all use the config() function e.g. SECRET_KEY is set in the following example,
indicated by the "config('SECRET_KEY'" section.

SECRET_KEY = config('SECRET_KEY', default='+$8pgzf)luxklr(rhzg4!$6+b^2hbw*-frvh_2-7an9-_==n_u', cast=str)


Empty file.
6 changes: 6 additions & 0 deletions examples/gtfsproj/gtfsapp/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin

# Register your models here.
8 changes: 8 additions & 0 deletions examples/gtfsproj/gtfsapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.apps import AppConfig


class GtfsappConfig(AppConfig):
name = 'gtfsapp'
Empty file.
6 changes: 6 additions & 0 deletions examples/gtfsproj/gtfsapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models

# Create your models here.
8 changes: 8 additions & 0 deletions examples/gtfsproj/gtfsapp/static/css/explore.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
body {
padding-top: 20px;
}

.body-top {
padding: 40px 15px;
text-align: center;
}
44 changes: 44 additions & 0 deletions examples/gtfsproj/gtfsapp/static/js/explore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
explore = {
mapPoint: function(map_id, x, y, zoom) {
var map = new OpenLayers.Map(map_id);
var osm = new OpenLayers.Layer.OSM("OpenStreetMap Map");
var fromProjection = new OpenLayers.Projection("EPSG:4326");
var toProjection = new OpenLayers.Projection("EPSG:900913");
var position = new OpenLayers.LonLat(x, y).transform(fromProjection, toProjection);
map.addLayer(osm);

var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
markers.addMarker(new OpenLayers.Marker(position));

map.setCenter(position, zoom);
},
mapLine: function(map_id, line_wkt) {
var map = new OpenLayers.Map(map_id);
var osm = new OpenLayers.Layer.OSM("OpenStreetMap Map");
var fromProjection = new OpenLayers.Projection("EPSG:4326");
var toProjection = new OpenLayers.Projection("EPSG:900913");
map.addLayer(osm);

var style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
style.strokeColor = "red";
style.strokeColor = "red";
style.fillColor = "red";
style.pointRadius = 10;
style.strokeWidth = 3;
style.rotation = 45;
style.strokeLinecap = "butt";
var vectorLayer = new OpenLayers.Layer.Vector("Line", {'style': style});
var wkt = new OpenLayers.Format.WKT({
'internalProjection': toProjection,
'externalProjection': fromProjection,
'style': style });
var lineVector = wkt.read(line_wkt);
var bounds = lineVector.geometry.getBounds();

map.addLayer(vectorLayer);
vectorLayer.addFeatures([lineVector]);

map.zoomToExtent(bounds);
}
}
21 changes: 21 additions & 0 deletions examples/gtfsproj/gtfsapp/templates/multigtfs/agency_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "multigtfs/base.html" %}
{% block head_title %}{{ block.super }} - Agency {{object}}{% endblock %}
{% block page_title %}Agency {{object}}{% endblock %}
{% block page_top_content_elem %}
An <strong>Agency</strong> represents a transit agency.
{% endblock %}
{% block page_middle_content %}
<h2>Attributes</h2>
<dl>
<dt>feed</dt><dd><a href="{% url 'feed_detail' pk=object.feed_id %}">Feed {{object.feed}}</a></dd>
<dt>id</dt><dd>{{object.id}}</dd>
<dt>agency_id</dt><dd>{{object.agency_id|default:'<em>Not set</em>'}}</dd>
<dt>name</dt><dd>{{object.name}}</dd>
<dt>url</dt><dd>{% if object.url %}<a href="{{object.url}}">{{object.url}}</a>{% else %}<em>Not set</em>{% endif %}</dd>
<dt>timezone</dt><dd>{{object.timezone}}</dd>
<dt>lang</dt><dd>{{object.lang|default:'<em>Not set</em>'}}</dd>
<dt>phone</dt><dd>{{object.phone|default:'<em>Not set</em>'}}</dd>
<dt>fare_url</dt><dd>{% if object.fare_url %}<a href="{{object.fare_url}}">{{object.fare_url}}</a>{% else %}<em>Not set</em>{% endif %}</dd>
</dl>
{% include "multigtfs/extra_data.html" %}
{% endblock %}
15 changes: 15 additions & 0 deletions examples/gtfsproj/gtfsapp/templates/multigtfs/agency_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "multigtfs/base.html" %}
{% block head_title %}{{ block.super }} - Agencies{% endblock %}
{% block page_title %}Agencies{% endblock %}
{% block page_top_content_elem %}
An <strong>Agency</strong> represents a transit agency.
{% endblock %}
{% block page_middle_content %}
{% for agency in object_list %}
{% if forloop.first %}<ul>{% endif %}
<li><a href="{% url 'agency_detail' feed_id=agency.feed_id pk=agency.pk %}">Agency {{ agency }}</a></li>
{% if forloop.last %}</ul>{% endif %}
{% empty %}
<p><em>No agencies yet.</em></p>
{% endfor %}
{% endblock %}
53 changes: 53 additions & 0 deletions examples/gtfsproj/gtfsapp/templates/multigtfs/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block head_title %}Multi GTFS - django-multi-gtfs{% endblock %}</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="{% static 'css/explore.css' %}">
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
{% block head_extra %}{% endblock %}
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{% url 'feed_list' %}">Multi GTFS</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li{% block nav_feed_active_class %}{% endblock %}><a href="{% url 'feed_list' %}">Home</a></li>
<li><a href="{% url 'admin:index' %}">Admin</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="body-top">
<h1>{% block page_title %}TODO: set page_title{% endblock %}</h1>
{% block page_top_content_elem %}
<p class="lead">
{% block page_top_content %}TODO: set page_top_content{% endblock %}
<p>
{% endblock %}
</div>
<div class="body-middle">{% block page_middle_content %}{% endblock %}</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
{% block body_script %}{% endblock %}
</body>
</html>
19 changes: 19 additions & 0 deletions examples/gtfsproj/gtfsapp/templates/multigtfs/block_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends "multigtfs/base.html" %}
{% block head_title %}{{ block.super }} - Block {{object}}{% endblock %}
{% block page_title %}Block {{object}}{% endblock %}
{% block page_top_content_elem %}
A <strong>Block</strong> represents a fare zone for a trip.
{% endblock %}
{% block page_middle_content %}
<h2>Attributes</h2>
<dl>
<dt>feed</dt><dd><a href="{% url 'feed_detail' pk=object.feed_id %}">Feed {{object.feed}}</a></dd>
<dt>id</dt><dd>{{object.id}}</dd>
<dt>block_id</dt><dd>{{object.block_id}}</dd>
</dl>
{% include "multigtfs/extra_data.html" %}
<h2>Related Objects</h2>
<ul>
<li><a href="{% url 'trip_by_block_list' feed_id=object.id block_id=object.id %}">{{object.trip_set.count}} Trip{{ object.trip_set.count|pluralize }}</a></li>
</ul>
{% endblock %}
15 changes: 15 additions & 0 deletions examples/gtfsproj/gtfsapp/templates/multigtfs/block_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "multigtfs/base.html" %}
{% block head_title %}{{ block.super }} - Blocks{% endblock %}
{% block page_title %}Blocks{% endblock %}
{% block page_top_content_elem %}
A <strong>Block</strong> represents a fare zone for a trip.
{% endblock %}
{% block page_middle_content %}
{% for blocks in object_list %}
{% if forloop.first %}<ul>{% endif %}
<li><a href="{% url 'blocks_detail' feed_id=blocks.feed_id pk=blocks.pk %}">Block {{ blocks }}</a></li>
{% if forloop.last %}</ul>{% endif %}
{% empty %}
<p><em>No blockss yet.</em></p>
{% endfor %}
{% endblock %}
8 changes: 8 additions & 0 deletions examples/gtfsproj/gtfsapp/templates/multigtfs/extra_data.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% if object.extra_data %}
<h2>Extra Attributes</h2>
<dl>
{% for key, val in object.extra_data.iteritems %}
<dt>{{ key }}</dt><dd>{{ val }}</dd>
{% endfor %}
</dl>
{% endif %}
Loading