Skip to content

Commit

Permalink
schemas: add SimplifiedSchemaSerializer
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Trzcinska <[email protected]>
  • Loading branch information
annatrz committed Nov 18, 2019
1 parent b371189 commit d2b2a25
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
24 changes: 24 additions & 0 deletions cap/modules/records/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,27 @@ def api_url_for(endpoint, pid, **kwargs):
**kwargs)

return url_to_api_url(url)


def reindex_by_schema_url(schema_url, pid_type):
"""Reindex all records of given pid_type belonging to that schema."""
def _get_json_type():
"""If postgres db return JSONB, else JSON."""
return JSONB if db.session.bind.dialect.name == 'postgresql' else JSON

indexer = RecordIndexer()

ids = (x[0] for x in RecordMetadata.query.filter(
RecordMetadata.json['$schema'] == cast(
schema_url, _get_json_type())).values(RecordMetadata.id))

filtered_by_pid_type = (x[0] for x in PersistentIdentifier.query.filter(
PersistentIdentifier.status == PIDStatus.REGISTERED,
PersistentIdentifier.object_type == 'rec', PersistentIdentifier.
pid_type == pid_type, PersistentIdentifier.object_uuid.in_(
ids)).values(PersistentIdentifier.object_uuid))

print('{} records will be reindexed...'.format(schema_url))

indexer.bulk_index(filtered_by_pid_type)
indexer.process_bulk_queue(es_bulk_kwargs={'raise_on_error': True})
14 changes: 12 additions & 2 deletions cap/modules/schemas/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
import copy

from flask import url_for
from invenio_jsonschemas.proxies import current_jsonschemas
from marshmallow import Schema, ValidationError, fields, pre_load, validate

from cap.utils import url_to_api_url
from invenio_jsonschemas.proxies import current_jsonschemas
from marshmallow import Schema, ValidationError, fields, pre_load, validate

from .validators import JSONSchemaValidator

Expand Down Expand Up @@ -102,6 +102,15 @@ def filter_out_fields_that_cannot_be_updated(self, data, **kwargs):
return data


class SimplifiedSchemaSerializer(Schema):
"""Simplified serializer for schema."""

name = fields.Str(dump_only=True, required=True)
version = fields.Str(dump_only=True, required=True,
validate=validate.Regexp(regex=r"(\d+).(\d+).(\d+)"))
fullname = fields.Str(dump_only=True)


class ResolvedSchemaSerializer(SchemaSerializer):
"""Schema serializer with resolved jsonschemas."""

Expand All @@ -125,5 +134,6 @@ def get_resolved_record_schema(self, obj):


schema_serializer = SchemaSerializer()
simplified_schema_serializer = SimplifiedSchemaSerializer()
update_schema_serializer = UpdateSchemaSerializer()
resolved_schemas_serializer = ResolvedSchemaSerializer()

0 comments on commit d2b2a25

Please sign in to comment.