Skip to content

Commit

Permalink
global: adds user_id as Event model column
Browse files Browse the repository at this point in the history
* Allows for tracing the event's origin to the user.

Signed-off-by: Krzysztof Nowak <[email protected]>
  • Loading branch information
Krzysztof Nowak committed May 3, 2018
1 parent ea95659 commit f0481aa
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 77 deletions.
1 change: 1 addition & 0 deletions asclepias_broker/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class EventModelView(ModelView):

column_list = (
'id',
'user_id',
'status',
'payload',
)
Expand Down
5 changes: 3 additions & 2 deletions asclepias_broker/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EventAPI:
"""Event API."""

@classmethod
def handle_event(cls, event: dict, no_index=False):
def handle_event(cls, event: dict, no_index=False, user_id=None):
"""Handle an event payload."""
# Raises JSONSchema ValidationError
jsonschema.validate(event, EVENT_SCHEMA)
Expand All @@ -34,7 +34,8 @@ def handle_event(cls, event: dict, no_index=False):
if errors:
raise MarshmallowValidationError(errors)

event_obj = Event(payload=event, status=EventStatus.New)
event_obj = Event(payload=event, status=EventStatus.New,
user_id=user_id)
db.session.add(event_obj)
db.session.commit()
event_uuid = str(event_obj.id)
Expand Down
3 changes: 0 additions & 3 deletions asclepias_broker/jsonschemas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@

_CUR_DIR = os.path.dirname(__file__)

with open(os.path.join(_CUR_DIR, 'definitions.json'), 'r') as fp:
DEFINITIONS_SCHEMA = json.load(fp)

with open(os.path.join(_CUR_DIR, 'scholix-v3.json'), 'r') as fp:
SCHOLIX_SCHEMA = json.load(fp)

Expand Down
9 changes: 7 additions & 2 deletions asclepias_broker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from copy import deepcopy

import jsonschema
from invenio_accounts.models import User
from invenio_db import db
from sqlalchemy import JSON, Boolean, Column, Enum, ForeignKey, Integer, String
from sqlalchemy.dialects import postgresql
Expand Down Expand Up @@ -229,8 +230,12 @@ class Event(db.Model, Timestamp):
payload = Column(JSONType)

status = Column(Enum(EventStatus), nullable=False)
# TODO:
# add FK to user = ...
user_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=True)

#
# Relationships
#
user = db.relationship(User)

@classmethod
def get(cls, id=None, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion asclepias_broker/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json

from flask import Blueprint, abort, request
from flask_login import current_user
from flask.views import MethodView
from invenio_oauth2server import require_api_auth
from jsonschema.exceptions import ValidationError as JSONValidationError
Expand Down Expand Up @@ -44,7 +45,8 @@ def post(self):
"""Submit an event."""
try:
no_index = bool(request.args.get('noindex', False))
EventAPI.handle_event(request.json, no_index=no_index)
EventAPI.handle_event(request.json, user_id=current_user.id,
no_index=no_index)
except JSONValidationError as e:
raise PayloadValidationRESTError(e.message, code=422)
except MarshmallowValidationError as e:
Expand Down
1 change: 0 additions & 1 deletion tests/schemas/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from asclepias_broker.models import Event, Identifier, Relation, Relationship
from asclepias_broker.schemas.loaders import IdentifierSchema, \
RelationshipSchema
from asclepias_broker.schemas.scholix import SCHOLIX_RELATIONS


def compare_identifiers(a, b):
Expand Down
68 changes: 0 additions & 68 deletions tests/schemas/test_scholix.py

This file was deleted.

0 comments on commit f0481aa

Please sign in to comment.