This commit is contained in:
2019-10-27 02:18:42 +01:00
commit 134a3fa598
32 changed files with 1056 additions and 0 deletions
View File
+3
View File
@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.
+5
View File
@@ -0,0 +1,5 @@
from django.apps import AppConfig
class MobileConfig(AppConfig):
name = 'mobile'
+3
View File
@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.
+18
View File
@@ -0,0 +1,18 @@
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
</head>
<body>
<div id=content>
{% block content %}
{% endblock %}
</div>
</body>
</html>
+10
View File
@@ -0,0 +1,10 @@
{% extends "mobile/base.html" %}
{% block content %}
<body>
<h1>Bloomhunt</h1>
<p>Hunt blooming trees in your area.</p>
<a class="btn btn-default" id="btn_scan" href="/mobile/scan">Scan a blooming tree</a>
{% endblock %}
+19
View File
@@ -0,0 +1,19 @@
{% extends "mobile/base.html" %}
{% block content %}
<h1>Scan a blooming tree!</h1>
<p>We will help you identifying the tree you are hunting.</p>
<p>{{ question.text }}</p>
{% for option in question.options %}
<form action="" method="post">
{% csrf_token %}
<input name="question" type="hidden" value="{{ question.key }}">
<input name="chosen_option" class="btn btn-default" type="submit" value="{{ option.text }}">
</form>
{% endfor %}
{% endblock %}
+28
View File
@@ -0,0 +1,28 @@
{% extends "mobile/base.html" %}
{% block content %}
<p>Congratulions, you are the first to report that this <b>{{ tree.name }}</b>
tree is blooming.<p>
<p>You may give it a name now</p>
<form action="/mobile/addtree/" method="post">
{% csrf_token %}
<input type="text" name="name">
<input type="hidden" name="tree" value="{{ tree.name }}">
<input id="in_long" type="text" name="longitude">
<input id="in_lat" type="text" name="latitude">
<input type="submit" value="Name your tree">
</form>
<script>
navigator.geolocation.getCurrentPosition(function(position) {
var in_long = document.getElementById("in_long");
var in_lat = document.getElementById("in_lat");
in_long.value = position.coords.longitude;
in_lat.value = position.coords.latitude;
});
</script>
{% endblock %}
+12
View File
@@ -0,0 +1,12 @@
{% extends "mobile/base.html" %}
{% block content %}
<h1>You are now one of the glorious Bloom Hunters!</h1>
<p>Does that feel great? It should! You really achieved something in
your life!</p>
<a class="btn btn-default" href="/mobile">Back to hunting</a>
{% endblock %}
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+9
View File
@@ -0,0 +1,9 @@
from django.urls import path
from . import views
urlpatterns = [
path('addtree/', views.addtree, name='addtree'),
path('scan/', views.scan , name='scan'),
path('', views.index, name='index')
]
+88
View File
@@ -0,0 +1,88 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from datetime import datetime
import json
import os
def index(request):
return render(request, 'mobile/index.html')
def scan(request):
question = q1
if request.POST:
data = request.POST
print("data")
if data["question"]:
print("response to question", data["question"])
for q in questions:
if not q.key == data["question"]:
continue
for opt in q.options:
if not opt.text == data["chosen_option"]:
continue
if opt.question:
question = opt.question
break
else:
return scanned(request, opt.result)
# first question
context = {'question': question}
return render(request, 'mobile/scan.html', context)
def scanned(request, tree):
context = {'tree': {'name': tree}}
return render(request, 'mobile/scanned.html', context)
def addtree(request):
if not request.POST:
return HttpResponseRedirect("/mobile/scan")
name = request.POST["name"]
tree = request.POST["name"]
longitude = request.POST["longitude"]
latitude = request.POST["latitude"]
bloom_start = datetime.now().timetuple().tm_yday
if not os.path.isfile('/tmp/data.json'):
data = {'trees': []}
else:
with open('/tmp/data.json', 'r') as f:
data = json.load(f)
with open('/tmp/data.json', 'w') as f:
data["trees"].append(
{'name': name, 'species': tree,
'longitude': longitude, 'latitude': latitude,
'bloom_start': bloom_start }
)
json.dump(data, f, indent=4)
return render(request, "mobile/thankyou.html")
class Option:
def __init__(self, text, question=None, result=None):
self.text = text
self.question = question
self.result = result
class Question:
def __init__(self, key, text, options):
self.key = key
self.text = text
self.options = options
q3 = Question("alive", "Is your tree alive?",
[Option("yes", result="Poor Apple Tree"),
Option("no", result="Dead Apple Tree")])
q2 = Question("red", "Is your tree red?",
[Option("yes", result="Red Apple Tree"),
Option("no", result="Green Apple Tree")])
q1 = Question("leaves", "Does your tree have leaves?",
[Option("yes", question=q2), Option("no", question=q3)])
questions = [q1, q2, q3]