Basic setup and ajax structure

This commit is contained in:
Josh Stewart 2013-05-21 21:23:53 +10:00
parent 9f1e9fa099
commit a05d6d8b5c
15 changed files with 182 additions and 25 deletions

View File

@ -14,9 +14,9 @@
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="sprinklers">Sprinklers</a></li>
<li><a href="weather">Weather</a></li>
<li><a href="schedule">Schedule</a></li>
<li><a href="setup">Setup</a></li>
<li><a href="/sprinklers">Sprinklers</a></li>
<li><a href="/weather">Weather</a></li>
<li><a href="/schedule">Schedule</a></li>
<li><a href="/setup">Setup</a></li>
</ul>
</nav>

Binary file not shown.

0
setup/__init__.py Normal file
View File

12
setup/models.py Normal file
View File

@ -0,0 +1,12 @@
from django.db import models
class Setup(models.Model):
latitude = models.CharField(max_length=10)
longitude = models.CharField(max_length=10)
cityID = models.IntegerField()
googleID = models.CharField(max_length=100)
calendarName = models.CharField(max_length=100)
precipitationLimit = models.IntegerField()
precipitationTime = models.IntegerField()

View File

@ -0,0 +1,61 @@
{% include "templates/header.html" %}
<section id="intro">
<header>
<h2>Sprinklers!</h2>
</header>
<p>Blah blah blah watering. Blah blah rain.</p>
<img src="/static/images/introimage.png" alt="lime" />
</section>
<div id="content">
<article class="articlecontent">
<p id="demo">Finding location...</p>
</article>
<article class="articlecontent">
<p id="demo2"></p>
</article>
</div>
<script>
var x=document.getElementById("demo");
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("demo2").innerHTML=xmlhttp.responseText;
}
}
document.getElementById("demo2").innerHTML= "Retrieving weather...";
xmlhttp.open("GET","/weather/ajax?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude,true);
xmlhttp.send();
}
</script>
</body>
</html>

16
setup/tests.py Normal file
View File

@ -0,0 +1,16 @@
"""
This file demonstrates writing tests using the unittest module. These will pass
when you run "manage.py test".
Replace this with more appropriate tests for your application.
"""
from django.test import TestCase
class SimpleTest(TestCase):
def test_basic_addition(self):
"""
Tests that 1 + 1 always equals 2.
"""
self.assertEqual(1 + 1, 2)

1
setup/views.py Normal file
View File

@ -0,0 +1 @@
# Create your views here.

View File

@ -19,7 +19,16 @@ def currentTemp():
response = urllib2.urlopen(queryURL)
decoder = json.JSONDecoder()
data = decoder.decode(response.read())["main"]["temp"]
temperature = decoder.decode(response.read())["main"]["temp"]
return (str(data) + " Degrees")
return (str(temperature) + " Degrees")
def currentTempByLatLon(latitude, longitude):
queryURL = weatherURL + "lat=" + str(latitude) + "&lon=" + str(longitude)
response = urllib2.urlopen(queryURL)
decoder = json.JSONDecoder()
temperature = decoder.decode(response.read())["main"]["temp"]
return (str(temperature) + " Degrees")

Binary file not shown.

View File

@ -1,17 +0,0 @@
{% include "templates/header.html" %}
{% if sprinkler %}
sprinkler ID: {{sprinkler.id}}<br/>
sprinkler Name: {{sprinkler.name}}<br/>
sprinkler On? {{sprinkler.status}}<br/>
{% for entry in sprinkler_log %}
Log ID: {{entry.id}}<br/>
Time Started: {{entry.timeOn}}<br/>
Time Ended: {{entry.timeOff}}<br/>
{% endfor %}
{% else %}
<p>No sprinklers were found with that ID.</p>
{% endif %}
</body>
</html>

View File

@ -0,0 +1,61 @@
{% include "templates/header.html" %}
<section id="intro">
<header>
<h2>Sprinklers!</h2>
</header>
<p>Blah blah blah watering. Blah blah rain.</p>
<img src="/static/images/introimage.png" alt="lime" />
</section>
<div id="content">
<article class="articlecontent">
<p id="demo">Finding location...</p>
</article>
<article class="articlecontent">
<p id="demo2"></p>
</article>
</div>
<script>
var x=document.getElementById("demo");
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("demo2").innerHTML=xmlhttp.responseText;
}
}
document.getElementById("demo2").innerHTML= "Retrieving weather...";
xmlhttp.open("GET","/weather/ajax?lat=" + position.coords.latitude + "&lon=" + position.coords.longitude,true);
xmlhttp.send();
}
</script>
</body>
</html>

View File

@ -3,5 +3,6 @@ from django.conf.urls import patterns, url
from weather import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index')
url(r'^$', views.index, name='index'),
url(r'^ajax$', views.ajax, name='ajax')
)

Binary file not shown.

View File

@ -4,6 +4,8 @@ import urllib2
from django.core.cache import cache
from django.http import HttpResponse
from weather.models import Location
from django.template import Context, loader
from weather import WeatherUtils
weatherURL = "http://api.openweathermap.org/data/2.5/weather?"
@ -18,5 +20,16 @@ def index(request):
decoder = json.JSONDecoder()
data = decoder.decode(response.read())
return HttpResponse("Latitude: " + str(data))
template = loader.get_template('weather/index.html')
context = Context({
'latitude': latitude,
})
return HttpResponse(template.render(context))
def ajax(request):
latitude = request.GET['lat'] # request.POST.get('lat', False)
longitude = request.GET['lon'] # request.POST.get('lat', False)
temperature = WeatherUtils.currentTempByLatLon(latitude, longitude)
return HttpResponse(temperature)

Binary file not shown.