Permalink
Browse files

Updated code to use POST.value for the value. Removed use of getParts…

…, removed all mention (and tests of it). Since POST.value is a clean string, I removed the prepare and inheritance of StringSummary
  • Loading branch information...
1 parent bc47b73 commit 860a54c81c1643f401462f252db28adbe201b3da @ibolmo committed May 15, 2009
Showing with 15 additions and 42 deletions.
  1. +3 −12 myapp/stat.py
  2. +12 −13 myapp/views.py
  3. +0 −17 test/test_util.py
View
@@ -50,7 +50,7 @@ class NumberSummary(Summary):
def prepare(cls, value):
"""docstring for prepare"""
try:
- return float(value)
+ return float(value or 1)
except:
logging.critical('Could not convert %s into a float' % value)
return None
@@ -72,17 +72,8 @@ def calculate(cls, stats, datum):
class StringSummary(Summary):
match_type = ['str', 'string', 'text']
-
- @classmethod
- def prepare(self, value):
- """docstring for prepare"""
- try:
- return urllib.unquote_plus(value)
- except:
- logging.critical('Could not unquote_plus(%s)' % value)
- return None
-
-class DatetimeSummary(StringSummary):
+
+class DatetimeSummary(Summary):
match_type = ['date', 'datetime', 'timestamp']
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
View
@@ -63,35 +63,34 @@ def measurements(request, key, path):
if (not key):
return HttpResponse('Invalid service usage')
- ns, value = util.getParts(path)
- format = request.REQUEST.get('format', 'json')
campaign = Campaign.get(key)
if (not campaign):
return HttpResponse('Campaign not found')
+ ns = path.strip('/').replace('/', '.')
+
if request.method == 'GET':
- ns += value and ('.%s' % value) or ''
- logging.info('get: %s, %s' % (ns, value))
+ format = request.GET.get('format', 'json')
data = Storage.all().filter('campaign = ', campaign).filter('namespace = ', ns).fetch(1000) # todo, paginator
stats = [Statistics.get_by_campaign_and_namespace(campaign, ns)]
renderer = globals().get('render_%s' % format)
return renderer and render_to_response(request, 'myapp/get_data.%s' % format, {'data': renderer(data, stats)}, mimetype = mimetypes.get(format, 'text/plain')) \
or HttpResponse('Unsupported format: %s' % format, status = 500)
elif request.method == 'POST':
- v_type = request.POST.get('type', 'number')
- if (v_type is 'number' and not value):
- value = 1
- new_value = stat.get(v_type).prepare(value)
+ value = request.POST.get('value')
+ kind = request.POST.get('type', 'number')
+ new_value = stat.get(kind).prepare(value)
if (new_value is None):
return HttpResponse('Incorrect value (%s) passed' % value, status = 404)
- logging.info('post: %s, %s, %s' % (ns, new_value, v_type))
- datum = Storage(namespace = ns, value = new_value, type = v_type, campaign = campaign)
+ datum = Storage(namespace = ns, value = new_value, type = kind, campaign = campaign)
if (not datum.put()):
- logging.error('Datum not saved. Campaign: %s %s %s %s' % (campaign, ns, value, v_type))
+ logging.error('Datum not saved. Campaign: %s %s %s %s' % (campaign, ns, value, kind))
return HttpResponse('Internal error when saving measurement', status = 500)
- return HttpResponse('Ok')
-
+ return HttpResponse('Ok', status = 201)
+ elif request.method == 'DELETE':
+ return HttpResponse('Not yet supported, please contact admin.', status = 304)
+
return HttpResponse('Internal Error', status = 500)
@login_required
View
@@ -1,17 +0,0 @@
-import unittest, logging, util
-
-class UtilTest (unittest.TestCase):
- def setUp(self):
- self.test_paths = [
- ('', (None, None)),
- ('visitor', ('visitor', None)),
- ('visitor/olmo', ('visitor', 'olmo')),
- ('visitor/count/1', ('visitor.count', '1')),
- ('visitor/agent/ie', ('visitor.agent', 'ie')),
- ('visitor/agent/ie/1', ('visitor.agent.ie', '1')),
- #('visitor.joined=2009-05-04%2015:11:32.239000', ('visitor.joined', '2009-05-04%2015:11:32.239000'))
- ]
-
- def test_get_parts(self):
- for test in self.test_paths:
- self.failUnlessEqual(util.getParts(test[0]), test[1])

0 comments on commit 860a54c

Please sign in to comment.