Skip to content

Commit 60a504a

Browse files
committed
Initial commit
0 parents  commit 60a504a

34 files changed

+522
-0
lines changed

.env-sample

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
DEBUG = True
2+
RUN_LOCAL_DB = True
3+
SECRET_KEY = 'super_s3cret_key'
4+
5+
# Local database
6+
LOCAL_DB_NAME='local_db'
7+
LOCAL_DB_USER='your_username'
8+
9+
# Heroku database
10+
DB_NAME=''
11+
DB_USER=''
12+
DB_PASSWORD=''
13+
DB_HOST='ec2-xx-xx-xxx-xx.eu-west-1.compute.amazonaws.com'

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.venv
2+
venv
3+
*.pyc
4+
staticfiles
5+
.env
6+
db.sqlite3
7+
getting-started/*

Procfile

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
release: python manage.py migrate
2+
web: gunicorn core.wsgi

Procfile.windows

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: python manage.py runserver 0.0.0.0:5000

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# BT5110 - Your first Django app
2+
3+
A barebones Django app used for the [BT5110](https://nusmods.com/modules/BT5110/data-management-and-warehousing) module at NUS. Shows how to create a web app that
4+
5+
1. Interacts with a database
6+
2. Is deployed to the internet (for free!)
7+
8+
This template starter code is heavily based on [Python: Getting Started](https://github.com/heroku/python-getting-started.git).
9+
10+
## Running Locally
11+
12+
Make sure you have [Python 3.9](https://docs.python-guide.org/starting/installation/) and [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed. Also make sure you have PostgreSQL running and create a new database for local development. Then run the following in a terminal:
13+
14+
```sh
15+
$ git clone https://github.com/BT5110/demo.git
16+
$ cd demo
17+
```
18+
19+
This will download the code to your computer. One of the files will be called [`.env-sample`](/.env-sample). Make a new file called `.env` where you copy the contents over, and then you can set the values as you desire (namely, make sure `LOCAL_DB_NAME` and `LOCAL_DB_USER` are correctly configured).
20+
21+
Now run the following to finish the setup:
22+
23+
```sh
24+
$ python3 -m venv .venv
25+
$ source .venv/bin/activate
26+
$ pip install -r requirements.txt
27+
28+
$ python manage.py migrate
29+
$ python manage.py collectstatic
30+
$ python manage.py runserver
31+
```
32+
33+
Your app should now be running on [localhost:8000](http://localhost:8000/).
34+
35+
## Deploying to Heroku
36+
37+
Make sure you have followed the steps above to run it locally. Then:
38+
39+
1. Create a new private repository on GitHub
40+
* Go to <https://github.com/new>.
41+
* Name your repository whatever you like. **NB**: Make sure to choose "Private" - this is a requirement to avoid that other students plagiarise your code for the assignment. Also make sure to **not** check any of the boxes under "Initialize this repository with". When you are done, click *Create repository*.
42+
![new repo](./images/new-repo.png)
43+
* Copy the commands under *"...or push an existing repository from the command line"* to push the code you cloned for running locally.
44+
![push repo](./images/push-repo.png)
45+
2. Create the deployment on Heroku
46+
* Go to <https://dashboard.heroku.com/new-app> and click *Create new app*
47+
![new app](./images/heroku-new.png)
48+
* Name your app whatever you like. For region, Heroku sadly doesn't support Asia. You can choose either Europe or United States. Click *Create app*.
49+
* Go to *Resources*, Under *Add-ons* search for "postgres" and select it as shown here (you might need to click "Submit Order Form", just make sure you choose the free hobby dev):
50+
![res](./images/heroku-res.png)
51+
* Go to "Deploy", choose GitHub, connect your GitHub account, search for the repo you created previously to connect to, click "Connect".
52+
![deploy](./images/heroku-connect.png)
53+
* Deploy your app in its current state, and do such that Heroku automatically deploys changes whenever you push your code to GitHub.
54+
![deploy](./images/heroku-deploy.png)
55+
3. After 1-2 min, you should be able to see your page at `your-app-name.herokuapp.com`!
56+
57+

app.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "Start on Heroku: Python",
3+
"description": "A barebones Python app, which can easily be deployed to Heroku.",
4+
"image": "heroku/python",
5+
"repository": "https://github.com/heroku/python-getting-started",
6+
"keywords": ["python", "django" ],
7+
"addons": [ "heroku-postgresql" ],
8+
"environments": {
9+
"test": {
10+
"scripts": {
11+
"test-setup": "python manage.py collectstatic --noinput",
12+
"test": "python manage.py test"
13+
}
14+
}
15+
}
16+
}

app/__init__.py

Whitespace-only changes.

app/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

app/migrations/0001_initial.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 1.9.1 on 2016-01-27 21:54
3+
from __future__ import unicode_literals
4+
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='Greeting',
18+
fields=[
19+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('when', models.DateTimeField(auto_now_add=True, verbose_name=b'date created')),
21+
],
22+
),
23+
]
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.2.24 on 2021-09-03 07:51
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('app', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='greeting',
15+
name='when',
16+
field=models.DateTimeField(auto_now_add=True, verbose_name='date created'),
17+
),
18+
]

app/migrations/__init__.py

Whitespace-only changes.

app/models.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.db import models
2+
3+
4+
# Create your models here.
5+
class Greeting(models.Model):
6+
id = models.AutoField(primary_key=True)
7+
when = models.DateTimeField("date created", auto_now_add=True)

app/static/lang-logo.png

2.17 KB
Loading

app/templates/base.html

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>
5+
{% block title %}{% endblock %} | BT5110
6+
</title>
7+
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" />
8+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
9+
<script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
10+
<style type="text/css">
11+
.jumbotron {
12+
background: #532f8c;
13+
color: white;
14+
padding-bottom: 80px
15+
}
16+
.jumbotron .btn-primary {
17+
background: #845ac7;
18+
border-color: #845ac7
19+
}
20+
.jumbotron .btn-primary:hover {
21+
background: #7646c1
22+
}
23+
.jumbotron p {
24+
color: #d9ccee;
25+
max-width: 75%;
26+
margin: 1em auto 2em
27+
}
28+
.navbar+.jumbotron {
29+
margin-top: -20px
30+
}
31+
.jumbotron .lang-logo {
32+
display: block;
33+
background: #b01302;
34+
border-radius: 50%;
35+
overflow: hidden;
36+
width: 100px;
37+
height: 100px;
38+
margin: auto;
39+
border: 2px solid white
40+
}
41+
.jumbotron .lang-logo img {
42+
max-width: 100%
43+
}
44+
45+
46+
</style>
47+
</head>
48+
<body>
49+
<nav class="navbar navbar-default navbar-static-top navbar-inverse">
50+
<div class="container">
51+
<ul class="nav navbar-nav">
52+
<li class="{% if nbar == 'home' %}active{% endif %}">
53+
<a href="/"><span class="glyphicon glyphicon-home"></span> Home</a>
54+
</li>
55+
<li class="{% if nbar == 'db' %}active{% endif %}">
56+
<a href="/db"><span class="glyphicon glyphicon-list-alt"></span> DB</a>
57+
</li>
58+
<li class="{% if nbar == 'tpc-c' %}active{% endif %}">
59+
<a href="/tpc-c"><span class="glyphicon glyphicon-list-alt"></span> TPC-C</a>
60+
</li>
61+
<li class="{% if nbar == 'project' %}active{% endif %}">
62+
<a href="/project"><span class="glyphicon glyphicon-wrench"></span> Project</a>
63+
</li>
64+
</ul>
65+
</div>
66+
</nav>
67+
68+
{% block content %}{% endblock %}
69+
</body>
70+
</html>

app/templates/db.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{% extends "base.html" %}
2+
{% block title %} DB {% endblock %}
3+
{% load static %}
4+
5+
{% block content %}
6+
<div class="container">
7+
<h2>Page View Report</h2>
8+
<p>
9+
This is a very simple page that, each time there is made a request, it will
10+
save one row in the database with the current datetime and then it will
11+
show all the datetimes of all the requests made.
12+
</p>
13+
<p>
14+
You may find it interesting to check out <code>app/views.py</code>,
15+
<code>app/models.py</code> and <code>app/templates/db.html</code>
16+
(the latter being the file where the text you're reading now is written)!
17+
</p>
18+
<ul>
19+
{% for greeting in greetings %}
20+
<li>{{ greeting.when }}</li>
21+
{% endfor %}
22+
</ul>
23+
</div>
24+
{% endblock %}

app/templates/index.html

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{% extends "base.html" %}
2+
{% block title %} Home {% endblock %}
3+
{% load static %}
4+
5+
{% block content %}
6+
7+
<div class="jumbotron text-center">
8+
<div class="container">
9+
<a href="/" class="lang-logo">
10+
<img src="{% static 'lang-logo.png'%}">
11+
</a>
12+
<h1>
13+
Welcome to the BT5110<br/>
14+
Django tutorial
15+
</h1>
16+
<p>
17+
This is a sample Python application deployed to Heroku. It's a reasonably simple app - but a good foundation for understanding how to work with Django and how to expose your apps to the web.
18+
</p>
19+
<a type="button" class="btn btn-lg btn-default" href="https://github.com/BT5110/demo">
20+
<span class="glyphicon glyphicon-download"></span> Source on GitHub
21+
</a>
22+
</div>
23+
</div>
24+
<div class="container">
25+
<div class="row">
26+
<div class="col-md-6">
27+
<h3><span class="glyphicon glyphicon-info-sign"></span> How this sample app works</h3>
28+
<ul>
29+
<li>This app was deployed to Heroku using GitHub.</li>
30+
<li>It is a fork from <a href="https://github.com/heroku/python-getting-started">Python: Getting Started</a> tailored to our class.</li>
31+
<li>When Heroku received the source code, it fetched all the dependencies in <a href="https://github.com/BT5110/demo/blob/main/requirements.txt">requirements.txt</a>, creating a deployable slug.</li>
32+
<li>The platform then spins up a dyno, a lightweight container that provides an isolated environment in which the slug can be mounted and executed.</li>
33+
</ul>
34+
</div>
35+
<div class="col-md-6">
36+
<h3><span class="glyphicon glyphicon-link"></span> Next Steps</h3>
37+
<ul>
38+
<li>Set up an account on <a href="https://github.com">GitHub</a> and <a href="https://heroku.com/">Heroku</a>, respectively.</li>
39+
<li>Follow the steps outlined at the README of the <a href="https://github.com/BT5110/demo">source of this app</a>.</li>
40+
<li>Within a few minutes, you should be able to see your changes deployed to the web!</li>
41+
</ul>
42+
</div>
43+
</div> <!-- row -->
44+
<br/>
45+
<div class="alert alert-info text-center" role="alert">
46+
We will not cover how the the starter app was set up, but if you want to do so, you can follow <a href="https://github.com/heroku/python-getting-started">Python: Getting Started</a>. Furthermore, while you in principle can look at the final source for TPC-C, please try to follow the steps to build it yourself. This will greatly enhance your understanding and thus make it easier once you get to the project.
47+
</div>
48+
</div>
49+
{% endblock %}

app/templates/project.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "base.html" %}
2+
{% block title %} Project {% endblock %}
3+
{% load static %}
4+
5+
{% block content %}
6+
<div class="container">
7+
<h2>Project</h2>
8+
<p>To be implemented by you...</p>
9+
</div>
10+
{% endblock %}

app/templates/tpc-c.html

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{% extends "base.html" %}
2+
{% block title %} TPC-C {% endblock %}
3+
{% load static %}
4+
5+
{% block content %}
6+
<div class="container">
7+
<h2>TPC-C</h2>
8+
<p>To be implemented during class...</p>
9+
</div>
10+
{% endblock %}

app/tests.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.contrib.auth.models import AnonymousUser, User
2+
from django.test import TestCase, RequestFactory
3+
4+
from .views import index
5+
6+
7+
class SimpleTest(TestCase):
8+
def setUp(self):
9+
# Every test needs access to the request factory.
10+
self.factory = RequestFactory()
11+
12+
def test_details(self):
13+
# Create an instance of a GET request.
14+
request = self.factory.get("/")
15+
request.user = AnonymousUser()
16+
17+
# Test my_view() as if it were deployed at /customer/details
18+
response = index(request)
19+
self.assertEqual(response.status_code, 200)

app/views.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.shortcuts import render
2+
3+
from .models import Greeting
4+
5+
6+
def index(request):
7+
context = {'nbar': 'home'}
8+
return render(request, 'index.html', context)
9+
10+
11+
def db(request):
12+
greeting = Greeting()
13+
greeting.save()
14+
15+
greetings = Greeting.objects.all()
16+
context = {'greetings': greetings, 'nbar': 'db'}
17+
return render(request, 'db.html', context)
18+
19+
20+
def tpc_c(request):
21+
context = {'nbar': 'tpc-c'}
22+
return render(request, 'tpc-c.html', context)
23+
24+
25+
def project(request):
26+
context = {'nbar': 'project'}
27+
return render(request, 'project.html', context)

core/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)