Adds custom JSON renderer for non-native objects.
All urls can have “/json” added to the beginning of the request string to return only the json for that page
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
from pyramid.config import Configurator
|
||||
from datetime import datetime
|
||||
from datetime import datetime, date
|
||||
from pyramid.renderers import JSON
|
||||
from bson.objectid import ObjectId
|
||||
from .pagination import Pagination
|
||||
|
||||
try:
|
||||
# for python 2
|
||||
@@ -46,6 +49,27 @@ def roundDigits(inpValue, digits=2):
|
||||
return round(inpValue, digits)
|
||||
|
||||
|
||||
# JSON RENDERERS
|
||||
def datetime_adapter(obj, request):
|
||||
return obj.strftime("%Y-%m-%d %H:%M:%S.%fZ")
|
||||
|
||||
|
||||
def objectId_adapter(obj, request):
|
||||
return str(obj)
|
||||
|
||||
|
||||
def date_adapter(obj, request):
|
||||
return obj.strftime("%Y-%m-%d")
|
||||
|
||||
|
||||
def pagination_adapter(obj, request):
|
||||
p = {
|
||||
'page': obj.page,
|
||||
'per_page': obj.per_page,
|
||||
'total_count': obj.total_count,
|
||||
}
|
||||
return p
|
||||
|
||||
def main(global_config, **settings):
|
||||
""" This function returns a Pyramid WSGI application.
|
||||
"""
|
||||
@@ -72,22 +96,39 @@ def main(global_config, **settings):
|
||||
config.add_request_method(add_db, 'db', reify=True)
|
||||
config.add_static_view('static', 'static', cache_max_age=3600)
|
||||
|
||||
# CUSTOM JSON RENDERER
|
||||
prettyjson = JSON(indent=4)
|
||||
prettyjson.add_adapter(datetime, datetime_adapter)
|
||||
prettyjson.add_adapter(date, date_adapter)
|
||||
prettyjson.add_adapter(ObjectId, objectId_adapter)
|
||||
prettyjson.add_adapter(Pagination, pagination_adapter)
|
||||
config.add_renderer('prettyjson', prettyjson)
|
||||
|
||||
# SHARED ROUTES
|
||||
config.add_route('home', '/')
|
||||
config.add_route('json_snapshot', '/json')
|
||||
|
||||
config.add_route('cards_page', '/cards/{cards_date}/{page_num}')
|
||||
config.add_route('json_cards_page', '/json/cards/{cards_date}/{page_num}')
|
||||
config.add_route('cards_date', '/cards/{cards_date}')
|
||||
config.add_route('json_cards_date', '/json/cards/{cards_date}')
|
||||
|
||||
config.add_route('cards', '/cards')
|
||||
config.add_route('json_cards', '/json/cards')
|
||||
|
||||
config.add_route('card_single', "/card/view/{stroke_number}")
|
||||
config.add_route('json_card_single', "/json/card/view/{stroke_number}")
|
||||
|
||||
config.add_route('values_all', "/values")
|
||||
config.add_route('json_values_all', "/json/values")
|
||||
|
||||
config.add_route('values_tag', "/values/tag/{tagname}")
|
||||
config.add_route('json_values_tag', "/json/values/tag/{tagname}")
|
||||
|
||||
config.add_route('values_time', '/values/time/{time}')
|
||||
config.add_route('json_values_time', '/json/values/time/{time}')
|
||||
|
||||
# JSON ROUTES
|
||||
# JSON-ONLY ROUTES
|
||||
config.add_route('json_lastcard', "/json/lastcard")
|
||||
|
||||
config.scan()
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from pyramid.view import view_config
|
||||
from .view_helpers import *
|
||||
from bson import json_util
|
||||
|
||||
|
||||
# JSON
|
||||
@view_config(route_name="json_lastcard", renderer="json")
|
||||
@view_config(route_name="json_lastcard", renderer="prettyjson")
|
||||
def json_lastcard(request):
|
||||
pass
|
||||
return get_latest_card(request)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from datetime import datetime, timedelta
|
||||
from math import ceil
|
||||
from .pagination import Pagination
|
||||
|
||||
|
||||
|
||||
def get_lastest_tag_values(request):
|
||||
latest_tag_values = []
|
||||
latesttag_agg = request.db['wellData'].aggregate([
|
||||
@@ -26,7 +29,10 @@ def get_lastest_tag_values(request):
|
||||
|
||||
def get_latest_card(request):
|
||||
try:
|
||||
return list(request.db['cards'].find().sort("timestamp", -1).limit(1))[0]
|
||||
latest_card = list(request.db['cards'].find().sort("timestamp", -1).limit(1))[0]
|
||||
for x in latest_card:
|
||||
print("{} = {}".format(x, type(latest_card[x])))
|
||||
return latest_card
|
||||
except IndexError:
|
||||
return []
|
||||
|
||||
|
||||
@@ -4,11 +4,13 @@ from .view_helpers import *
|
||||
|
||||
|
||||
@view_config(route_name='home', renderer='templates/dashboard.jinja2')
|
||||
@view_config(route_name="json_snapshot", renderer="prettyjson")
|
||||
def my_view(request):
|
||||
return {'project': 'POC Web Interface', 'navgroup': 'dashboard', 'tag_values': get_lastest_tag_values(request), 'latest_card': get_latest_card(request)}
|
||||
|
||||
|
||||
@view_config(route_name='cards', renderer='templates/datelist.jinja2')
|
||||
@view_config(route_name='json_cards', renderer='prettyjson')
|
||||
def cards(request):
|
||||
datelist = []
|
||||
dateagg = request.db['cards'].aggregate([
|
||||
@@ -31,11 +33,14 @@ def cards(request):
|
||||
|
||||
@view_config(route_name='cards_date', renderer='templates/cardlist.jinja2')
|
||||
@view_config(route_name='cards_page', renderer='templates/cardlist.jinja2')
|
||||
@view_config(route_name='json_cards_date', renderer='prettyjson')
|
||||
@view_config(route_name='json_cards_page', renderer='prettyjson')
|
||||
def cards_page(request):
|
||||
return card_page(request)
|
||||
|
||||
|
||||
@view_config(route_name='card_single', renderer='templates/cardsingle.jinja2')
|
||||
@view_config(route_name='json_card_single', renderer='prettyjson')
|
||||
def card_single(request):
|
||||
card = {}
|
||||
try:
|
||||
@@ -47,6 +52,7 @@ def card_single(request):
|
||||
|
||||
|
||||
@view_config(route_name='values_all', renderer="templates/valuesall.jinja2")
|
||||
@view_config(route_name='json_values_all', renderer='prettyjson')
|
||||
def values_all(request):
|
||||
# all_dates = list(request.db['wellData'].distinct('timestamp'))
|
||||
all_dates = request.db['wellData'].aggregate([
|
||||
@@ -56,11 +62,12 @@ def values_all(request):
|
||||
}},
|
||||
{"$sort": {"_id": -1}}
|
||||
])
|
||||
all_dates_formatted = map(lambda x: {'count': x['count'], 'timestamp': datetime.strptime(x['_id'], "%Y-%m-%d %H:%M:%S.000Z")}, all_dates)
|
||||
all_dates_formatted = list(map(lambda x: {'count': x['count'], 'timestamp': datetime.strptime(x['_id'], "%Y-%m-%d %H:%M:%S.000Z")}, all_dates))
|
||||
return {'navgroup': 'values', 'current_tag_values': get_lastest_tag_values(request), 'all_dates': all_dates_formatted, 'tag_history': get_all_tags_between(request)}
|
||||
|
||||
|
||||
@view_config(route_name="values_tag", renderer="templates/values_single.jinja2")
|
||||
@view_config(route_name='json_values_tag', renderer='prettyjson')
|
||||
def values_tag(request):
|
||||
tagname = request.matchdict['tagname']
|
||||
single_tag = list(request.db['wellData'].find({"tagname": tagname}))
|
||||
|
||||
Reference in New Issue
Block a user