2 Commits

Author SHA1 Message Date
Patrick McDonagh
3e37d88d6e Adds PEP formatting 2017-05-25 15:01:59 -05:00
Patrick McDonagh
7f0aff3c9c Adds basic mongo and pyramid framework 2017-05-16 21:51:04 -05:00
19 changed files with 675 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
[run]
source = web_db
omit = web_db/test*

21
web_db/web_db/.gitignore vendored Normal file
View File

@@ -0,0 +1,21 @@
*.egg
*.egg-info
*.pyc
*$py.class
*~
.coverage
coverage.xml
build/
dist/
.tox/
nosetests.xml
env*/
tmp/
Data.fs*
*.sublime-project
*.sublime-workspace
.*.sw?
.sw?
.DS_Store
coverage
test

View File

@@ -0,0 +1,4 @@
0.0
---
- Initial version.

View File

@@ -0,0 +1,2 @@
include *.txt *.ini *.cfg *.rst
recursive-include web_db *.ico *.png *.css *.gif *.jpg *.pt *.txt *.mak *.mako *.js *.html *.xml *.jinja2

29
web_db/web_db/README.txt Normal file
View File

@@ -0,0 +1,29 @@
Henry Pump DataLogger Database
==============================
Getting Started
---------------
- Change directory into your newly created project.
cd web_db
- Create a Python virtual environment.
python3 -m venv env
- Upgrade packaging tools.
env/bin/pip install --upgrade pip setuptools
- Install the project in editable mode with its testing requirements.
env/bin/pip install -e ".[testing]"
- Run your project's tests.
env/bin/pytest
- Run your project.
env/bin/pserve development.ini

View File

@@ -0,0 +1,61 @@
###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###
[app:main]
use = egg:web_db
pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
pyramid.includes =
pyramid_debugtoolbar
# By default, the toolbar only appears for clients from IP addresses
# '127.0.0.1' and '::1'.
# debugtoolbar.hosts = 127.0.0.1 ::1
mongo_uri = mongodb://poc_www:HenryPump1903@localhost:27017/poc
###
# wsgi server configuration
###
[server:main]
use = egg:waitress#main
listen = *:6543
###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###
[loggers]
keys = root, web_db
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = INFO
handlers = console
[logger_web_db]
level = DEBUG
handlers =
qualname = web_db
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s

View File

@@ -0,0 +1,53 @@
###
# app configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/environment.html
###
[app:main]
use = egg:web_db
pyramid.reload_templates = false
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = en
###
# wsgi server configuration
###
[server:main]
use = egg:waitress#main
listen = *:6543
###
# logging configuration
# http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/logging.html
###
[loggers]
keys = root, web_db
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
[logger_web_db]
level = WARN
handlers =
qualname = web_db
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(asctime)s %(levelname)-5.5s [%(name)s:%(lineno)s][%(threadName)s] %(message)s

3
web_db/web_db/pytest.ini Normal file
View File

@@ -0,0 +1,3 @@
[pytest]
testpaths = web_db
python_files = *.py

53
web_db/web_db/setup.py Normal file
View File

@@ -0,0 +1,53 @@
"""Set up the package."""
import os
from setuptools import setup, find_packages
here = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(here, 'README.txt')) as f:
README = f.read()
with open(os.path.join(here, 'CHANGES.txt')) as f:
CHANGES = f.read()
requires = [
'pyramid',
'pyramid_jinja2',
'pyramid_debugtoolbar',
'waitress',
'pymongo',
]
tests_require = [
'WebTest >= 1.3.1', # py3 compat
'pytest',
'pytest-cov',
]
setup(
name='web_db',
version='0.0',
description='Henry Pump DataLogger Database',
long_description=README + '\n\n' + CHANGES,
classifiers=[
'Programming Language :: Python',
'Framework :: Pyramid',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
],
author='',
author_email='',
url='',
keywords='web pyramid pylons',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
extras_require={
'testing': tests_require,
},
install_requires=requires,
entry_points={
'paste.app_factory': [
'main = web_db:main',
],
},
)

View File

@@ -0,0 +1,79 @@
"""Configure everything at once."""
from pyramid.config import Configurator
from pymongo import MongoClient
from pyramid.renderers import JSON
from datetime import datetime, date
from bson.objectid import ObjectId
try:
# for python 2
from urlparse import urlparse
except ImportError:
# for python 3
from urllib.parse import urlparse
# JSON RENDERERS
def datetime_adapter(obj, request):
"""Adapt the datetime for json."""
return obj.strftime("%Y-%m-%d %H:%M:%S.%fZ")
def objectId_adapter(obj, request):
"""Adapt the objectId for json."""
return str(obj)
def date_adapter(obj, request):
"""Adapt the date for json."""
return obj.strftime("%Y-%m-%d")
def pagination_adapter(obj, request):
"""Adapt the pagination for json."""
p = {
'page': obj.page,
'per_page': obj.per_page,
'total_count': obj.total_count,
}
return p
def main(global_config, **settings):
"""Make a Pyramid WSGI application."""
config = Configurator(settings=settings)
config.include('pyramid_jinja2')
# Database Configuration
db_url = urlparse(settings['mongo_uri'])
config.registry.db = MongoClient(
host=db_url.hostname,
port=db_url.port,
)
def add_db(request):
db = config.registry.db[db_url.path[1:]]
if db_url.username and db_url.password:
db.authenticate(db_url.username, db_url.password)
return db
config.add_request_method(add_db, 'db', reify=True)
# 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)
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
# config.include(devices_include, route_prefix="/devices")
config.add_route("devices.collection", "/devices")
config.add_route("devices.single", "/devices/{address}")
config.add_route("devices.single.tags.collection", "/devices/{address}/tags")
config.add_route("devices.single.tags.single", "/devices/{address}/tags/{tag_name}")
config.scan()
return config.make_wsgi_app()

View File

@@ -0,0 +1,98 @@
"""Handle all device-relation functions."""
from pyramid.view import view_config
@view_config(route_name="devices.collection", renderer="prettyjson", request_method="GET")
def get_all(request):
"""Retrieve all devices."""
devices = request.db['data'].aggregate([
{"$project": {
"address": 1,
"device_type": 1,
"tag_types": 1
}}
])
return {"devices": list(devices)}
@view_config(route_name="devices.collection", renderer="prettyjson", request_method="POST")
def post_one(request):
"""Insert one new device."""
new_device = {"$set": {"address": request.json_body['address'], "device_type": request.json_body['device_type'], "tag_types": {"handshake": [], "normal": []}}}
print(new_device)
request.db['data'].update_one({'address': request.json_body['address']}, new_device, upsert=True)
devices = request.db['data'].find_one({'address': request.json_body['address']}, {"address": 1, "device_type": 1})
return {"device": devices}
@view_config(route_name="devices.single", renderer="prettyjson", request_method="GET")
def get_single(request):
"""Retrieve a single device by its address."""
devices = request.db['data'].find({'address': request.matchdict['address']}, {"address": 1, "device_type": 1})
return {"devices": list(devices)}
@view_config(route_name="devices.single", renderer="prettyjson", request_method="DELETE")
def delete_single(request):
"""Delete a single device by its address."""
result = request.db['data'].delete_one({'address': request.matchdict['address']})
return {"deleted": result.deleted_count}
@view_config(route_name="devices.single", renderer="prettyjson", request_method="PUT")
def update_one(request):
"""Update a single device by its address."""
upd_device = {"address": request.json_body['address'], "device_type": request.matchdict['address']}
request.db['data'].update_one({'address': request.matchdict['address']}, {"$set": upd_device}, upsert=True)
devices = request.db['data'].find({'address': request.json_body['address']}, {"address": 1, "device_type": 1})
return {"device": list(devices)[0]}
enable_tag = {
"tag_types.normal.name": 1,
"tag_types.normal.tag": 1,
"tag_types.normal.description": 1,
"tag_types.normal.data_type": 1,
"tag_types.normal.change_threshold": 1,
"tag_types.normal.guarantee_sec": 1,
"tag_types.normal.units": 1,
"tag_types.normal.min_expected": 1,
"tag_types.normal.max_expected": 1
}
@view_config(route_name="devices.single.tags.collection", renderer="prettyjson", request_method="GET")
def get_devicetags(request):
"""Retrieve a single device by its address."""
devices = request.db['data'].find({'address': request.matchdict['address']}, {"address": 1, "device_type": 1, "tag_types": 1})
return {"devices": list(devices)}
@view_config(route_name="devices.single.tags.collection", renderer="prettyjson", request_method="POST")
def add_normaltag(request):
"""Retrieve a single device by its address."""
jsb = request.json_body
new_tag = {
"name": jsb['name'],
"tag": jsb['tag'],
"description": jsb['description'],
"data_type": jsb['data_type'],
"change_threshold": jsb['change_threshold'],
"guarantee_sec": jsb['guarantee_sec'],
"units": jsb['units'],
"min_expected": jsb['min_expected'],
"max_expected": jsb['max_expected']
}
request.db['data'].update_one({'address': request.matchdict['address']}, {"$push": {"tag_types.normal": new_tag}})
find_tag = request.db['data'].find_one({"$and": [{'address': request.matchdict['address']}, {"tag_types.normal.tag": new_tag['tag']}]}, enable_tag)
# find_tag = request.db['data'].find_one({"tag_types.normal.tag": new_tag['tag']}, enable_tag)
# find_tag = list(request.db['data'].find({'{}.tag_types.normal.tag'.format(request.matchdict['address']): new_tag['tag']}, enable_tag))
return {"tag": find_tag}
@view_config(route_name="devices.single.tags.single", renderer="prettyjson", request_method="GET")
def get_devicesingletag(request):
"""Retrieve a single tag by its address and tag_name."""
find_tag = request.db['data'].find_one({"$and": [{'address': request.matchdict['address']}, {"tag_types.normal.tag": request.matchdict['tag_name']}]}, enable_tag)
return {"tag": find_tag}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@@ -0,0 +1,154 @@
@import url(//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700);
body {
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 300;
color: #ffffff;
background: #bc2131;
}
h1,
h2,
h3,
h4,
h5,
h6 {
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-weight: 300;
}
p {
font-weight: 300;
}
.font-normal {
font-weight: 400;
}
.font-semi-bold {
font-weight: 600;
}
.font-bold {
font-weight: 700;
}
.starter-template {
margin-top: 250px;
}
.starter-template .content {
margin-left: 10px;
}
.starter-template .content h1 {
margin-top: 10px;
font-size: 60px;
}
.starter-template .content h1 .smaller {
font-size: 40px;
color: #f2b7bd;
}
.starter-template .content .lead {
font-size: 25px;
color: #f2b7bd;
}
.starter-template .content .lead .font-normal {
color: #ffffff;
}
.starter-template .links {
float: right;
right: 0;
margin-top: 125px;
}
.starter-template .links ul {
display: block;
padding: 0;
margin: 0;
}
.starter-template .links ul li {
list-style: none;
display: inline;
margin: 0 10px;
}
.starter-template .links ul li:first-child {
margin-left: 0;
}
.starter-template .links ul li:last-child {
margin-right: 0;
}
.starter-template .links ul li.current-version {
color: #f2b7bd;
font-weight: 400;
}
.starter-template .links ul li a, a {
color: #f2b7bd;
text-decoration: underline;
}
.starter-template .links ul li a:hover, a:hover {
color: #ffffff;
text-decoration: underline;
}
.starter-template .links ul li .icon-muted {
color: #eb8b95;
margin-right: 5px;
}
.starter-template .links ul li:hover .icon-muted {
color: #ffffff;
}
.starter-template .copyright {
margin-top: 10px;
font-size: 0.9em;
color: #f2b7bd;
text-transform: lowercase;
float: right;
right: 0;
}
@media (max-width: 1199px) {
.starter-template .content h1 {
font-size: 45px;
}
.starter-template .content h1 .smaller {
font-size: 30px;
}
.starter-template .content .lead {
font-size: 20px;
}
}
@media (max-width: 991px) {
.starter-template {
margin-top: 0;
}
.starter-template .logo {
margin: 40px auto;
}
.starter-template .content {
margin-left: 0;
text-align: center;
}
.starter-template .content h1 {
margin-bottom: 20px;
}
.starter-template .links {
float: none;
text-align: center;
margin-top: 60px;
}
.starter-template .copyright {
float: none;
text-align: center;
}
}
@media (max-width: 767px) {
.starter-template .content h1 .smaller {
font-size: 25px;
display: block;
}
.starter-template .content .lead {
font-size: 16px;
}
.starter-template .links {
margin-top: 40px;
}
.starter-template .links ul li {
display: block;
margin: 0;
}
.starter-template .links ul li .icon-muted {
display: none;
}
.starter-template .copyright {
margin-top: 20px;
}
}

View File

@@ -0,0 +1,8 @@
"""Handle all tag-relation functions."""
from pyramid.view import view_config
@view_config(route_name="values.collection", renderer="prettyjson", request_method="GET")
def get_valuescollection(request):
"""Get tag values for all tags for the given device."""
tag = request.db['data'].find({'address': request.matchdict['address']}, {"address": 1, "device_type": 1, "tag_types": 1})

View File

@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="{{request.locale_name}}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="pyramid web application">
<meta name="author" content="Pylons Project">
<link rel="shortcut icon" href="{{request.static_url('web_db:static/pyramid-16x16.png')}}">
<title>Cookiecutter Starter project for the Pyramid Web Framework</title>
<!-- Bootstrap core CSS -->
<link href="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this scaffold -->
<link href="{{request.static_url('web_db:static/theme.css')}}" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="//oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="//oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="starter-template">
<div class="container">
<div class="row">
<div class="col-md-2">
<img class="logo img-responsive" src="{{request.static_url('web_db:static/pyramid.png') }}" alt="pyramid web framework">
</div>
<div class="col-md-10">
{% block content %}
<p>No content</p>
{% endblock content %}
</div>
</div>
<div class="row">
<div class="links">
<ul>
<li><i class="glyphicon glyphicon-cog icon-muted"></i><a href="https://github.com/Pylons/pyramid">Github Project</a></li>
<li><i class="glyphicon glyphicon-globe icon-muted"></i><a href="https://webchat.freenode.net/?channels=pyramid">IRC Channel</a></li>
<li><i class="glyphicon glyphicon-home icon-muted"></i><a href="http://pylonsproject.org">Pylons Project</a></li>
</ul>
</div>
</div>
<div class="row">
<div class="copyright">
Copyright &copy; Pylons Project
</div>
</div>
</div>
</div>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="//oss.maxcdn.com/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//oss.maxcdn.com/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
</body>
</html>

View File

@@ -0,0 +1,8 @@
{% extends "layout.jinja2" %}
{% block content %}
<div class="content">
<h1><span class="font-semi-bold">Pyramid</span> <span class="smaller">Starter project</span></h1>
<p class="lead">Welcome to <span class="font-normal">Henry Pump DataLogger Database</span>, a&nbsp;Pyramid application generated&nbsp;by<br><span class="font-normal">Cookiecutter</span>.</p>
</div>
{% endblock content %}

View File

@@ -0,0 +1,29 @@
import unittest
from pyramid import testing
class ViewTests(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
def tearDown(self):
testing.tearDown()
def test_my_view(self):
from .views import my_view
request = testing.DummyRequest()
info = my_view(request)
self.assertEqual(info['project'], 'Henry Pump DataLogger Database')
class FunctionalTests(unittest.TestCase):
def setUp(self):
from web_db import main
app = main({})
from webtest import TestApp
self.testapp = TestApp(app)
def test_root(self):
res = self.testapp.get('/', status=200)
self.assertTrue(b'Pyramid' in res.body)

View File

@@ -0,0 +1,6 @@
from pyramid.view import view_config
@view_config(route_name='home', renderer='templates/mytemplate.jinja2')
def my_view(request):
return {'project': 'Henry Pump DataLogger Database'}