Commit 3b7c35da118ffbf3d8de8dff1af84ff0222c1e71
1 parent
c452bde3
Exists in
master
change db url to ini file
Showing
3 changed files
with
58 additions
and
65 deletions
Show diff stats
wscserver/__init__.py
| 1 | -from pyramid.config import Configurator | |
| 2 | -from pyramid_restler import includeme | |
| 3 | 1 | |
| 4 | -from sqlalchemy import engine_from_config, create_engine | |
| 5 | -from sqlalchemy.orm import sessionmaker | |
| 6 | 2 | |
| 7 | -from wscserver.config.routing import make_routes | |
| 8 | -from wscserver.model import initialize_sql, sqlalchemy_url | |
| 3 | +from sqlalchemy import create_engine | |
| 4 | +from pyramid_restler import includeme | |
| 5 | +from pyramid.config import Configurator | |
| 6 | +from sqlalchemy.orm import sessionmaker | |
| 7 | +from sqlalchemy import engine_from_config | |
| 9 | 8 | |
| 10 | 9 | |
| 11 | 10 | def main(global_config, **settings): |
| 12 | 11 | """ |
| 13 | 12 | This function returns a Pyramid WSGI application. |
| 14 | 13 | """ |
| 14 | + | |
| 15 | + from wscserver.model import initialize_sql | |
| 16 | + | |
| 17 | + initialize_sql(settings) | |
| 15 | 18 | config = Configurator(settings=settings) |
| 16 | 19 | config.scan('wscserver.model') # the "important" line |
| 17 | - engine = create_engine(sqlalchemy_url) | |
| 18 | - initialize_sql(engine) | |
| 19 | - includeme(config) | |
| 20 | 20 | |
| 21 | + from wscserver.config.routing import make_routes | |
| 22 | + | |
| 23 | + includeme(config) | |
| 21 | 24 | make_routes(config) |
| 22 | 25 | config.scan('wscserver') |
| 26 | + | |
| 23 | 27 | return config.make_wsgi_app() | ... | ... |
wscserver/model/__init__.py
| 1 | 1 | # from sqlalchemy import engine_from_config |
| 2 | -from sqlalchemy.ext.declarative import declarative_base | |
| 3 | -from sqlalchemy.orm import scoped_session, sessionmaker | |
| 4 | -from sqlalchemy.engine import create_engine | |
| 2 | + | |
| 3 | + | |
| 5 | 4 | from pyramid import config |
| 5 | +from sqlalchemy.engine import create_engine | |
| 6 | 6 | from paste.deploy.loadwsgi import appconfig |
| 7 | +from sqlalchemy.ext.declarative import declarative_base | |
| 8 | +from sqlalchemy.orm import scoped_session, sessionmaker | |
| 9 | + | |
| 10 | +def initialize_sql(settings): | |
| 7 | 11 | |
| 8 | -sqlalchemy_url = 'postgresql://cacic:cacic@localhost/cacic' | |
| 9 | -DBSession = scoped_session(sessionmaker()) | |
| 10 | -Base = declarative_base() | |
| 11 | -engine = create_engine(sqlalchemy_url) | |
| 12 | -Session = sessionmaker(bind=engine) | |
| 13 | -session = Session() | |
| 12 | + global DBSession | |
| 13 | + global sqlalchemy_url | |
| 14 | + global DBSession | |
| 15 | + global Base | |
| 16 | + global engine | |
| 17 | + global Session | |
| 18 | + global session | |
| 14 | 19 | |
| 20 | + sqlalchemy_url = settings['sqlalchemy.url'] | |
| 21 | + DBSession = scoped_session(sessionmaker()) | |
| 22 | + Base = declarative_base() | |
| 23 | + engine = create_engine(sqlalchemy_url) | |
| 24 | + Session = sessionmaker(bind=engine) | |
| 25 | + session = Session() | |
| 15 | 26 | |
| 16 | -def initialize_sql(engine): | |
| 17 | 27 | DBSession.configure(bind=engine) |
| 18 | 28 | Base.metadata.bind = engine |
| 19 | 29 | # Base.metadata.create_all(engine) |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | ... | ... |
wscserver/view/restfulview.py
| 1 | 1 | import json |
| 2 | -import hashlib | |
| 3 | - | |
| 4 | 2 | from pyramid.response import Response |
| 5 | 3 | from wscserver.model import session |
| 6 | - | |
| 4 | +import zipfile | |
| 7 | 5 | |
| 8 | 6 | FILE_BEGIN = '{"results":[' |
| 9 | 7 | FILE_END = '], "result_count": %s}' |
| ... | ... | @@ -33,21 +31,20 @@ FILTER_KEYS = str(tuple(COMPUTER_FILTER.keys())) |
| 33 | 31 | |
| 34 | 32 | def viewcoleta(request): |
| 35 | 33 | |
| 36 | - #create index idx_id_computador on computador_coleta(id_computador); | |
| 34 | + # Please ensure this index exists on database | |
| 35 | + # CREATE INDEX idx_id_computador ON computador_coleta(id_computador); | |
| 37 | 36 | |
| 38 | 37 | limit = request.params.get('limit', 'NULL') |
| 39 | 38 | |
| 40 | 39 | stmt1 = """ |
| 41 | - SELECT | |
| 42 | - id_computador | |
| 40 | + SELECT id_computador | |
| 43 | 41 | FROM computador_coleta |
| 44 | 42 | GROUP BY id_computador |
| 45 | 43 | LIMIT {}; |
| 46 | - """.format(limit) | |
| 44 | + """.format(limit) | |
| 47 | 45 | |
| 48 | 46 | computer_ids = session.execute(stmt1) |
| 49 | 47 | |
| 50 | - | |
| 51 | 48 | stmt2 = """ |
| 52 | 49 | SELECT classe.nm_class_name, |
| 53 | 50 | cp.nm_property_name, |
| ... | ... | @@ -56,7 +53,7 @@ def viewcoleta(request): |
| 56 | 53 | INNER JOIN class_property as cp ON (cc.id_class_property = |
| 57 | 54 | cp.id_class_property) |
| 58 | 55 | INNER JOIN classe ON (classe.id_class = cp.id_class) |
| 59 | - WHERE cc.id_computador = {} AND | |
| 56 | + WHERE cc.id_computador = {} AND | |
| 60 | 57 | classe.nm_class_name IN {}; |
| 61 | 58 | """ |
| 62 | 59 | |
| ... | ... | @@ -80,13 +77,22 @@ def viewcoleta(request): |
| 80 | 77 | |
| 81 | 78 | f.write(FILE_END % computer_ids.rowcount) |
| 82 | 79 | |
| 83 | - return Response('ok') | |
| 84 | - #return Response( | |
| 85 | - # content_type='application/json', | |
| 86 | - # content_disposition='filename=coleta.json', | |
| 87 | - # app_iter=[open('/var/antony/coleta.json', 'rb').read()] | |
| 88 | - #) | |
| 80 | + if '1' in tuple(request.params.get('zip')): | |
| 89 | 81 | |
| 82 | + with zipfile.ZipFile('/tmp/coleta.zip', 'w') as myzip: | |
| 83 | + myzip.write('/tmp/coleta.json') | |
| 84 | + | |
| 85 | + return Response( | |
| 86 | + content_type='application/zip', | |
| 87 | + content_disposition='filename=coleta.zip', | |
| 88 | + body_file=open('/tmp/coleta.zip', 'rb')) | |
| 89 | + else: | |
| 90 | + return Response( | |
| 91 | + content_type='application/json', | |
| 92 | + content_disposition='filename=coleta.json', | |
| 93 | + body_file=open('/tmp/coleta.json')) | |
| 94 | + | |
| 95 | + return Response('ok') | |
| 90 | 96 | |
| 91 | 97 | |
| 92 | 98 | def build_computer_json(computer_group): |
| ... | ... | @@ -116,34 +122,3 @@ def build_computer_json(computer_group): |
| 116 | 122 | |
| 117 | 123 | return json.dumps(computer) |
| 118 | 124 | |
| 119 | -def zipcoleta(): | |
| 120 | - """Recebe o json da coleta em um arquivo e zipa""" | |
| 121 | - coleta = requests.get('http://localhost/wscserver/rest/coleta') | |
| 122 | - | |
| 123 | - tmpdir = tempfile.gettempdir() | |
| 124 | - os.chdir(tmpdir) | |
| 125 | - f = open('coleta.json','r') | |
| 126 | - arquivozip = 'coleta.zip' | |
| 127 | - with zipfile.ZipFile(arquivozip, 'w') as myzip: | |
| 128 | - myzip.write(f.name) | |
| 129 | - f.close() | |
| 130 | - os.remove('coleta.json') | |
| 131 | - filepath = os.path.abspath(arquivozip) | |
| 132 | - return filepath | |
| 133 | -''' | |
| 134 | -def zipcoleta(): | |
| 135 | - """Recebe o json da coleta em um arquivo e zipa""" | |
| 136 | - coleta = requests.get('http://localhost/wscserver/rest/coleta') | |
| 137 | - | |
| 138 | - path = tempfile.gettempdir() | |
| 139 | - os.chdir(path) | |
| 140 | - f = open('coleta.json','w') | |
| 141 | - f.write(coleta.text) | |
| 142 | - arquivozip = 'coleta.zip' | |
| 143 | - with zipfile.ZipFile(arquivozip, 'w') as myzip: | |
| 144 | - myzip.write(f.name) | |
| 145 | - f.close() | |
| 146 | - os.remove('coleta.json') | |
| 147 | - filepath = os.path.abspath(arquivozip) | |
| 148 | - return filepath | |
| 149 | -''' | ... | ... |