Skip to content

Commit

Permalink
Cleanup code, added ERROR_STATUS
Browse files Browse the repository at this point in the history
  • Loading branch information
kroman0 committed Jan 21, 2015
1 parent ddf072a commit f355f13
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 55 deletions.
49 changes: 2 additions & 47 deletions src/openprocurement/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
gevent.monkey.patch_all()
import os
import pkg_resources
from logging import getLogger
from pyramid.config import Configurator
from openprocurement.api.auth import AuthenticationPolicy
from pyramid.authorization import ACLAuthorizationPolicy as AuthorizationPolicy
Expand All @@ -16,55 +15,18 @@
from openprocurement.api.migration import migrate_data
from boto.s3.connection import S3Connection, Location
from openprocurement.api.traversal import factory
from openprocurement.api.models import get_now
from openprocurement.api.utils import error_handler
from openprocurement.api.utils import forbidden, set_journal_handler, cleanup_journal_handler, update_journal_handler_role

try:
from systemd.journal import JournalHandler
except ImportError:
JournalHandler = False

LOGGER = getLogger(__name__)
#VERSION = int(pkg_resources.get_distribution(__package__).parsed_version[0])
VERSION = pkg_resources.get_distribution(__package__).version
ROUTE_PREFIX = '/api/{}'.format(VERSION)


def set_journal_handler(event):
request = event.request
params = {
'TAGS': 'python,api',
'USER_ID': str(request.authenticated_userid or ''),
#'ROLE': str(request.authenticated_role),
'CURRENT_URL': request.url,
'CURRENT_PATH': request.path_info,
'REMOTE_ADDR': request.remote_addr or '',
'USER_AGENT': request.user_agent or '',
'AWARD_ID': '',
'BID_ID': '',
'COMPLAINT_ID': '',
'CONTRACT_ID': '',
'DOCUMENT_ID': '',
'QUESTION_ID': '',
'TENDER_ID': '',
'TIMESTAMP': get_now().isoformat(),
}
if request.params:
params['PARAMS'] = str(dict(request.params))
if request.matchdict:
for i, j in request.matchdict.items():
params[i.upper()] = j
for i in LOGGER.handlers:
LOGGER.removeHandler(i)
LOGGER.addHandler(JournalHandler(**params))


def update_journal_handler_role(event):
for i in LOGGER.handlers:
if isinstance(i, JournalHandler):
i._extra.update({'ROLE': str(event.request.authenticated_role)})


def set_renderer(event):
request = event.request
try:
Expand Down Expand Up @@ -129,18 +91,10 @@ def fix_url(item, app_url):


def beforerender(event):
for i in LOGGER.handlers:
LOGGER.removeHandler(i)
if event.rendering_val and 'data' in event.rendering_val:
fix_url(event.rendering_val['data'], event['request'].application_url)


def forbidden(request):
request.errors.add('url', 'permission', 'Forbidden')
request.errors.status = 403
return error_handler(request.errors)


def main(global_config, **settings):
config = Configurator(
settings=settings,
Expand All @@ -157,6 +111,7 @@ def main(global_config, **settings):
if JournalHandler:
config.add_subscriber(set_journal_handler, NewRequest)
config.add_subscriber(update_journal_handler_role, ContextFound)
config.add_subscriber(cleanup_journal_handler, BeforeRender)
config.add_subscriber(set_renderer, NewRequest)
config.add_subscriber(beforerender, BeforeRender)
config.include('pyramid_exclog')
Expand Down
4 changes: 2 additions & 2 deletions src/openprocurement/api/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
}
],
"enquiryPeriod": {
"endDate": (now + timedelta(days=7)).isoformat() #"2014-10-31T00:00:00"
"endDate": (now + timedelta(days=7)).isoformat()
},
"tenderPeriod": {
"endDate": (now + timedelta(days=14)).isoformat() #u"2014-11-06T10:00:00"
"endDate": (now + timedelta(days=14)).isoformat()
}
}

Expand Down
1 change: 0 additions & 1 deletion src/openprocurement/api/tests/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ def test_migrate_from12to13(self):
'submissionMethod': 'Electronic Auction',
}
_id, _rev = self.db.save(data)
item = self.db.get(_id)
migrate_data(self.db, 13)
migrated_item = self.db.get(_id)
self.assertEqual('open', migrated_item['procurementMethod'])
Expand Down
6 changes: 3 additions & 3 deletions src/openprocurement/api/tests/tender.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,16 +703,16 @@ def test_first_bid_tender(self):
for i in auction_bids_data
]
}
})
})
# view bid participationUrl
self.app.authorization = ('Basic', ('broker', ''))
response = self.app.get('/tenders/{}/bids/{}?acc_token={}'.format(tender_id, bid_id, bid_token))
self.assertEqual(response.json['data']['participationUrl'], 'https://tender.auction.url/for_bid/{}'.format(bid_id))

# posting auction results
self.app.authorization = ('Basic', ('auction', ''))
response = self.app.post_json('/tenders/{}/auction'.format(tender_id),
{'data': {'bids': auction_bids_data}})
{'data': {'bids': auction_bids_data}})
# get awards
self.app.authorization = ('Basic', ('broker', ''))
response = self.app.get('/tenders/{}/awards?acc_token={}'.format(tender_id, owner_token))
Expand Down
1 change: 0 additions & 1 deletion src/openprocurement/api/traversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
Deny,
Everyone,
)
from pyramid.httpexceptions import HTTPNotFound
from openprocurement.api.utils import error_handler


Expand Down
58 changes: 57 additions & 1 deletion src/openprocurement/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
from cornice.util import json_error
from json import dumps

try:
from systemd.journal import JournalHandler
except ImportError:
JournalHandler = False


LOGGER = getLogger('openprocurement.api')


def generate_id():
return uuid4().hex
Expand Down Expand Up @@ -192,8 +200,56 @@ def add_next_award(request):


def error_handler(errors):
LOGGER = getLogger('openprocurement.api')
for i in LOGGER.handlers:
if isinstance(i, JournalHandler):
i._extra['ERROR_STATUS'] = errors.status
LOGGER.info('Error on processing request "{}"'.format(dumps(errors, indent=4)), extra={'MESSAGE_ID': 'error_handler'})
for i in LOGGER.handlers:
LOGGER.removeHandler(i)
return json_error(errors)


def forbidden(request):
request.errors.add('url', 'permission', 'Forbidden')
request.errors.status = 403
return error_handler(request.errors)


def set_journal_handler(event):
request = event.request
params = {
'TAGS': 'python,api',
'USER_ID': str(request.authenticated_userid or ''),
#'ROLE': str(request.authenticated_role),
'CURRENT_URL': request.url,
'CURRENT_PATH': request.path_info,
'REMOTE_ADDR': request.remote_addr or '',
'USER_AGENT': request.user_agent or '',
'AWARD_ID': '',
'BID_ID': '',
'COMPLAINT_ID': '',
'CONTRACT_ID': '',
'DOCUMENT_ID': '',
'QUESTION_ID': '',
'TENDER_ID': '',
'TIMESTAMP': get_now().isoformat(),
}
if request.params:
params['PARAMS'] = str(dict(request.params))
if request.matchdict:
for i, j in request.matchdict.items():
params[i.upper()] = j
for i in LOGGER.handlers:
LOGGER.removeHandler(i)
LOGGER.addHandler(JournalHandler(**params))


def update_journal_handler_role(event):
for i in LOGGER.handlers:
if isinstance(i, JournalHandler):
i._extra['ROLE'] = str(event.request.authenticated_role)


def cleanup_journal_handler(event):
for i in LOGGER.handlers:
LOGGER.removeHandler(i)

0 comments on commit f355f13

Please sign in to comment.