events.py
62 lines
| 2.1 KiB
| text/x-python
|
PythonLexer
r0 | # -*- coding: utf-8 -*- | |||
r112 | # Copyright 2010 - 2017 RhodeCode GmbH and the AppEnlight project authors | |||
r0 | # | |||
r112 | # Licensed under the Apache License, Version 2.0 (the "License"); | |||
# you may not use this file except in compliance with the License. | ||||
# You may obtain a copy of the License at | ||||
r0 | # | |||
r112 | # http://www.apache.org/licenses/LICENSE-2.0 | |||
r0 | # | |||
r112 | # Unless required by applicable law or agreed to in writing, software | |||
# distributed under the License is distributed on an "AS IS" BASIS, | ||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
# See the License for the specific language governing permissions and | ||||
# limitations under the License. | ||||
r0 | ||||
from appenlight.lib.helpers import gen_pagination_headers | ||||
from appenlight.models.services.event import EventService | ||||
from pyramid.view import view_config | ||||
from pyramid.httpexceptions import HTTPBadRequest, HTTPNotFound | ||||
r135 | from ziggurat_foundations.models.services.user import UserService | |||
r0 | ||||
r153 | @view_config(route_name="events_no_id", renderer="json", permission="authenticated") | |||
r0 | def fetch_events(request): | |||
""" | ||||
Returns list of log entries from Elasticsearch | ||||
""" | ||||
event_paginator = EventService.get_paginator( | ||||
r153 | user=request.user, page=1, items_per_page=100 | |||
r0 | ) | |||
headers = gen_pagination_headers(request, event_paginator) | ||||
request.response.headers.update(headers) | ||||
return [ev.get_dict() for ev in event_paginator.items] | ||||
r153 | @view_config( | |||
route_name="events", | ||||
renderer="json", | ||||
request_method="PATCH", | ||||
permission="authenticated", | ||||
) | ||||
r0 | def event_PATCH(request): | |||
r135 | resources = UserService.resources_with_perms( | |||
r153 | request.user, ["view"], resource_types=request.registry.resource_types | |||
) | ||||
r0 | event = EventService.for_resource( | |||
r153 | [r.resource_id for r in resources], event_id=request.matchdict["event_id"] | |||
).first() | ||||
r0 | if not event: | |||
return HTTPNotFound() | ||||
r153 | allowed_keys = ["status"] | |||
r0 | for k, v in request.unsafe_json_body.items(): | |||
if k in allowed_keys: | ||||
r153 | if k == "status": | |||
r0 | event.close() | |||
else: | ||||
setattr(event, k, v) | ||||
else: | ||||
return HTTPBadRequest() | ||||
return event.get_dict() | ||||