Skip to content

Commit

Permalink
global: clean up missed steps from restructering
Browse files Browse the repository at this point in the history
* remove dependency from invenio-records-lom. leaf package should not
  depend on data-models

* remove files where the code has been moved to invenio-workflows-tugraz

* add test dependencies

* fix tests, mainly about the schema changes
  • Loading branch information
utnapischtim committed Jul 4, 2024
1 parent 915413b commit 4a95772
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 173 deletions.
95 changes: 0 additions & 95 deletions invenio_moodle/files.py

This file was deleted.

34 changes: 31 additions & 3 deletions invenio_moodle/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,42 @@
"""Schemas for validating input from moodle."""

from collections import Counter, defaultdict
from types import MappingProxyType

from invenio_records_lom.services.schemas.fields import ControlledVocabularyField
from marshmallow import Schema, ValidationError, validates_schema
from marshmallow.fields import Constant, Dict, List, Nested, Number, String

from .utils import extract_moodle_records


class ControlledVocabularyField(String):
"""A controlled vocabulary field."""

default_error_messages = MappingProxyType(
{
"not_in_vocabulary": "Value {string!r} not in controlled vocabulary {vocabulary!r}.",
},
)

def __init__(self, *, vocabulary: list | None = None, **kwargs: dict) -> None:
"""Initialize self."""
self.vocabulary = vocabulary
super().__init__(**kwargs)

def _deserialize(
self,
value: str | None,
attr: str | None,
data: dict | None,
**kwargs: dict,
) -> str:
string = super()._deserialize(value, attr, data, **kwargs)
if string not in self.vocabulary:
msg = "not_in_vocabulary"
raise self.make_error(msg, vocabulary=self.vocabulary, string=string)
return string


class ClassificationValuesSchema(Schema):
"""Moodle classification-values schema."""

Expand All @@ -26,7 +54,7 @@ class ClassificationValuesSchema(Schema):
class ClassificationSchema(Schema):
"""Moodle classification schema."""

type = Constant("oefos", required=True) # noqa: A003
type = Constant("oefos", required=True)
url = Constant(
"https://www.data.gv.at/katalog/dataset/stat_ofos-2012",
required=True,
Expand Down Expand Up @@ -90,7 +118,7 @@ class FileSchema(Schema):
fileurl = String() # application profile 1.0
source = String() # application profile 2.0
language = String(required=True)
license = Nested(LicenseSchema) # noqa: A003
license = Nested(LicenseSchema)
mimetype = String(required=True)
persons = List(Nested(PersonSchema), required=True)
resourcetype = String(required=True)
Expand Down
35 changes: 19 additions & 16 deletions invenio_moodle/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,31 @@ def is_course_root(sourceid: str) -> bool:


def extract_moodle_records(moodle_data: dict) -> list[dict]:
"""Create moodle file jsons."""

"""Create moodle file jsons.
application profile 2.0
{
"elements": [
{METADATA goes here}
]
}
application profile 1.0
{
"moodlecourses":{
"1": {
"files OR elements":[
{METADATA goes here}
]
}
"""
# application profile 2.0 uses elements and a flat structure to serve the metadata.
# {
# "elements": [
# {METADATA goes here}
# ]
# }

if "elements" in moodle_data:
return moodle_data["elements"]

# application profile 1.0 uses a nested structure with
# {
# "moodlecourses":{
# "1": {
# "files OR elements":[
# {METADATA goes here}
# ]
# }
elements = []
for _, moodle_course in moodle_data["moodlecourses"].items():
for moodle_course in moodle_data["moodlecourses"].values():
if "files" in moodle_course:
elements.extend(file_json for file_json in moodle_course["files"])
if "elements" in moodle_course:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ select = ["ALL"]
ignore = [
"ANN101", "ANN102",
"D203", "D211", "D212", "D213",
"E501",
"FA102",
"INP001",
"PERF203",
Expand Down
4 changes: 2 additions & 2 deletions run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ set -o errexit
# Quit on unbound symbols
set -o nounset

ruff .
ruff check .

python -m check_manifest
python -m sphinx.cmd.build -qnNW docs docs/_build/html
python -m pytest -s
python -m pytest
6 changes: 6 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ install_requires =

[options.extras_require]
tests =
flask-principal>=0.4.0
invenio-access>=2.0.0
invenio-i18n>=2.1.0
invenio-records-resources>=5.0.0
invenio-search[opensearch2]>=2.3.0
marshmallow>=3.0.0
pytest-black-ng>=0.4.0
pytest-invenio>=1.4.3
ruff>=0.4.10
Expand Down
24 changes: 0 additions & 24 deletions tests/test_convert.py

This file was deleted.

6 changes: 3 additions & 3 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2023 Graz University of Technology.
# Copyright (C) 2023-2024 Graz University of Technology.
#
# invenio-moodle is free software; you can redistribute it and/or modify
# it under the terms of the MIT License; see LICENSE file for more details.

"""Module test convert."""

from invenio_moodle.schemas import MoodleSchema
from invenio_moodle.schemas import MoodleSchemaApplicationProfile1


def test_simple(minimal_record: dict) -> None:
"""Test Simple."""
errors = MoodleSchema().validate(
errors = MoodleSchemaApplicationProfile1().validate(
{
"applicationprofile": "1.0",
"moodlecourses": {
Expand Down
30 changes: 0 additions & 30 deletions tests/test_types.py

This file was deleted.

0 comments on commit 4a95772

Please sign in to comment.