Skip to content
Browse files

Updated with an untested / non-debugged POST method for Readings (R.v…

…iew) in the post file. (hasty "on train" edits)

Fixed some debugging aids I left in the code previously.
  • Loading branch information...
1 parent 63a2a7c commit 6333ef3560d6554b2cf6862b2d4ad4c4bd0c9e98 Jawaad Mahmood @ Neways Desktop committed
Showing with 53 additions and 13 deletions.
  1. +49 −2 tickety/REST/post.py
  2. +2 −9 tickety/REST/views.py
  3. +2 −2 tickety/geigercounter/models.py
View
51 tickety/REST/post.py
@@ -200,6 +200,7 @@ def post(request):
return success(request, detector)
@staticmethod
+ @csrf_exempt
def recalibrate(request):
def valid_request(request):
try:
@@ -216,7 +217,7 @@ def valid_request(request):
user = authenticate(username=request.POST['nickname'],password=request.POST['password'])
if not user:
- return HttpResponse(repr(user), mimetype="text/plain", status=401)
+ return HttpResponse("Invalid username/Password", mimetype="text/plain", status=401)
try:
if float(request.POST['countPerMicrosievert']) < 0:
return HttpResponse("Invalid countPerMicrosievert", mimetype="text/plain", status=400)
@@ -246,11 +247,57 @@ def valid_request(request):
def disable(request):
pass
+# Reading "View" class
class R:
@staticmethod
+ @csrf_exempt
def new(request):
- pass
+ def valid_request(request):
+ try:
+ nickname = request.POST['nickname']
+ password = request.POST['password']
+ cpm = request.POST['cpm']
+ return True
+ except:
+ return False
+ def valid_cpm(request):
+ try:
+ cpm = int(cpm)
+ return cpm >= 0
+ except:
+ return False
+
+ bad_request_type = fail_on_get(request)
+ if bad_request_type:
+ return bad_request_type
+
+ if not valid_request(request):
+ return HttpResponse("Invalid request", mimetype="text/plain", status=400)
+
+ user = authenticate(username=request.POST['nickname'],password=request.POST['password'])
+
+ if not user:
+ return HttpResponse("Invalid username/password", mimetype="text/plain", status=401)
+
+ if not valid_cpm(request):
+ return HttpResponse("CPM must be an integer greater than or equal to 0.", mimetype="text/plain", status=400)
+
+ reading = Radiation()
+ reading.cpm = request.POST['cpm']
+ if 'particle' in request.POST and request.POST['particle'] in ['alpha','beta','gamma','all']:
+ reading.particle = request.POST['particle']
+ if 'taken' in request.POST and request.POST['particle'] in ['alpha','beta','gamma','all']:
+ reading.particle = request.POST['particle']
+ reading.detector = user.detector
+ try:
+ calibration = detector.calibration.filter(enabled=True).get()
+ reading.detector_calibration = calibration
+ except:
+ pass
+
+ reading.save()
+ return HttpResponse("Your content has been saved\n%s" % reading.__unicode() , mimetype="text/plain", status=200)
@staticmethod
def revise(request):
View
11 tickety/REST/views.py
@@ -1,12 +1,5 @@
-#
-"""
- (r'^city/([a-zA-Z.-\s]+)/detector/', 'REST.views.city_detectors'),
- (r'^city/([a-zA-Z.-\s]+)/(.*)/', 'REST.views.city'),
- (r'^city/([a-zA-Z.-\s]+)/', 'REST.views.city'),
- (r'^detector/([a-zA-Z.-\s]+)/(.*)/', 'REST.views.detector'),
- (r'^detector/(\d+)/(.*)/', 'REST.views.detector_by_id'),
-"""
-
+# GET commands are handled in here.
+# (I will probably be renaming this to get.py shortly.)
from django.db.models import Avg, Max, Min, Count
from django.http import HttpResponse
View
4 tickety/geigercounter/models.py
@@ -57,9 +57,9 @@ class Meta:
RADIOACTIVE_PARTICLES = ( ('alpha','Alpha Particles'), ('beta','Beta Particles'), ('gamma','Gamma Particles'), ('all','All'))
cpm = models.IntegerField(verbose_name="Counts per Minute")
added = models.DateTimeField(auto_now_add=True)
- taken = models.DateTimeField(verbose_name="Date/Time the readings are taken")
+ taken = models.DateTimeField(verbose_name="Date/Time the readings are taken", blank=True, null=True)
detector = models.ForeignKey(Detector, related_name="detected")
- detector_callibration = models.ForeignKey(DetectorCalibration, verbose_name="Detector Calibration when readings were taken")
+ detector_calibration = models.ForeignKey(DetectorCalibration, verbose_name="Detector Calibration when readings were taken", blank=True, null=True)
particle = models.CharField(max_length=10, choices=RADIOACTIVE_PARTICLES, default='all')
def __unicode__(self):

0 comments on commit 6333ef3

Please sign in to comment.
Something went wrong with that request. Please try again.