Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Detector recalibration now works as laid out in the README file.

- Created tests for detector recalibration.
- Updated detector creation
  • Loading branch information...
commit 63a2a7cb9def9bc63a4ba110bbb851dfc085c8e9 1 parent 423b8be
Jawaad Mahmood authored
View
4 README.mdown
@@ -75,7 +75,7 @@ All of these commands can be __mixed together__ (where it makes sense). For ins
- Used when posting new readings.
- cityName
- The name of the city
- - indoorOutdoor
+ - insideOutside
- Is it an interior/exterior detector?
- Valid values are _indoors_ and _outdoors_
- countPerMicrosievert
@@ -118,7 +118,7 @@ All of these commands can be __mixed together__ (where it makes sense). For ins
-------------------------------------
You will receive a "400 Bad Request" error message.
- __Return on invalid indoorOutdoor value__
+ __Return on invalid insideOutside value__
-------------------------------------
You will receive a "400 Bad Request" error message.
View
41 tickety/REST/post.py
@@ -5,6 +5,8 @@
from geigercounter.models import *
from datetime import datetime, timedelta
from django.views.decorators.csrf import csrf_exempt
+from django.contrib.auth import authenticate, login
+
try:
@@ -34,6 +36,19 @@ def fail_on_get(request):
class D:
@staticmethod
+ def __glogin(request):
+ username = request.POST['nickname']
+ password = request.POST['password']
+ user = authenticate(username=username, password=password)
+ if user is not None:
+ if user.is_active:
+ return user
+ else:
+ return False
+ else:
+ return False
+
+ @staticmethod
def __create(request):
pass
@@ -196,29 +211,33 @@ def valid_request(request):
return True
if not valid_request(request):
- return error400(request)
+ return HttpResponse("Invalid request", mimetype="text/plain", status=400)
- user = __glogin(request)
+ user = authenticate(username=request.POST['nickname'],password=request.POST['password'])
+
if not user:
- return error401(request)
-
+ return HttpResponse(repr(user), mimetype="text/plain", status=401)
try:
if float(request.POST['countPerMicrosievert']) < 0:
- return error401(request)
+ return HttpResponse("Invalid countPerMicrosievert", mimetype="text/plain", status=400)
except:
- return error400(request)
+ return HttpResponse("Non-numeric countPerMicrosievert", mimetype="text/plain", status=400)
- detector = user.detector.get()
- old_calibration = detector.calibration.filter(enabled=False)
- old_calibration.enabled=False
- old_calibration.disabled_on = datetime.now()
+ detector = user.detector
+ try:
+ old_calibration = detector.calibration.filter(enabled=True).get()
+ old_calibration.enabled=False
+ old_calibration.disabled_on = datetime.now()
+ old_calibration.save()
+ except:
+ pass
dc = DetectorCalibration()
dc.countpermicrosievert = request.POST['countPerMicrosievert']
dc.enabled = True
dc.detector = detector
dc.save()
- old_calibration.save()
+ return HttpResponse("Your content has been saved", mimetype="text/plain", status=200)
View
2  tickety/REST/post_urls.py
@@ -1,7 +1,7 @@
from django.conf.urls.defaults import *
# All GET commands get filtered through this.
-from post import D, R
+from post import *
urlpatterns = patterns('',
(r'^detector/new/$', D.new),
View
2  tickety/geigercounter/models.py
@@ -7,7 +7,7 @@ class Location(models.Model):
city = models.CharField( verbose_name = 'City', max_length=45)
district = models.CharField( verbose_name = 'District', max_length=45, blank=True, null=True)
- insideoutside = models.IntegerField( verbose_name='Located Indoors or Outdoors?', choices=INSIDEOUTSIDE)
+ insideoutside = models.IntegerField( verbose_name='Inside or Outside?', choices=INSIDEOUTSIDE)
address = models.CharField( verbose_name='Address', max_length=45, blank=True, null=True)
lat = models.DecimalField( verbose_name="Latitude", max_digits=11, decimal_places=6, blank=True, null=True)
View
61 tickety/geigercounter/tests.py
@@ -7,8 +7,30 @@
from django.test import TestCase
from django.test.client import Client
+from geigercounter.models import *
+from django.contrib.auth.models import User
+from django.contrib.auth import authenticate, login
+
class DetectorPostTest(TestCase):
+ def setUp(self):
+ self.detector = Detector()
+ self.detector.nickname = 'pre-existing'
+ location = Location()
+ location.city='Tokyo'
+ location.insideoutside=1
+ location.save()
+ self.detector.location = location
+
+ user = User()
+ user.username='pre-existing'
+ user.set_password('validpassword')
+ user.save()
+
+ self.detector.owner = user
+ self.detector.enabled=True
+ self.detector.save()
+
def test_get(self):
url = '/post/detector/new/'
c = Client()
@@ -20,3 +42,42 @@ def test_invalid_post(self):
c = Client()
response = c.post(url,{})
self.failUnlessEqual(response.status_code, 400)
+
+ def test_valid_post(self):
+ url = '/post/detector/new/'
+ c = Client()
+ response = c.post(url,{'nickname':'test','password':'test2','cityName':'Tokyo','insideOutside':'inside','countPerMicrosievert':20.0})
+ a = User.objects.get(username='test') # <avu> jbm_tokyo, the tests fail if an exception is raised anyway
+ self.assertEqual(response.status_code, 201)
+ self.assertNotEqual(authenticate(username='test',password='test2'), None)
+
+ def test_valid_recalibration(self):
+ url = '/post/detector/recalibrate/'
+ c = Client()
+ self.assertNotEqual(authenticate(username='pre-existing',password='validpassword'), None)
+ response = c.post(url,{'nickname':'pre-existing','password':'validpassword','countPerMicrosievert':25.0})
+ self.assertEqual(response.status_code, 200)
+
+ def test_invalid_recalibration(self):
+ url = '/post/detector/recalibrate/'
+ c = Client()
+ self.assertEqual(authenticate(username='pre-existing',password='incorrectpassword'), None)
+ response = c.post(url,{'nickname':'pre-existing','password':'incorrectpassword','countPerMicrosievert':25.0})
+ self.assertEqual(response.status_code, 401)
+
+ def test_empty_recalibration(self):
+ url = '/post/detector/recalibrate/'
+ c = Client()
+ response = c.post(url,{})
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.content, 'Invalid request')
+
+ def test_invalid_cpms(self):
+ url = '/post/detector/recalibrate/'
+ c = Client()
+ response = c.post(url,{'nickname':'pre-existing','password':'validpassword','countPerMicrosievert':-25.0})
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.content, 'Invalid countPerMicrosievert')
+ response = c.post(url,{'nickname':'pre-existing','password':'validpassword','countPerMicrosievert':'lol, word'})
+ self.assertEqual(response.status_code, 400)
+ self.assertEqual(response.content, 'Non-numeric countPerMicrosievert')
Please sign in to comment.
Something went wrong with that request. Please try again.