Ejemplo n.º 1
0
 def _annotation(patron, pool, content, motivation=Annotation.IDLING,
                 timestamp=very_old):
     annotation, ignore = Annotation.get_one_or_create(
         self._db,
         patron=patron,
         identifier=pool.identifier,
         motivation=motivation,
     )
     annotation.timestamp = timestamp
     annotation.content = content
     return annotation
Ejemplo n.º 2
0
 def _annotation(patron, pool, content, motivation=Annotation.IDLING,
                 timestamp=very_old):
     annotation, ignore = Annotation.get_one_or_create(
         self._db,
         patron=patron,
         identifier=pool.identifier,
         motivation=motivation,
     )
     annotation.timestamp = timestamp
     annotation.content = content
     return annotation
Ejemplo n.º 3
0
            return INVALID_ANNOTATION_TARGET

        content = data.get("http://www.w3.org/ns/oa#hasBody")
        if content and len(content) == 1:
            content = content[0]
        else:
            content = None

        target = json.dumps(target)
        extra_kwargs = {}
        if motivation == Annotation.IDLING:
            # A given book can only have one 'idling' annotation.
            pass
        elif motivation == Annotation.BOOKMARKING:
            # A given book can only have one 'bookmarking' annotation
            # per target.
            extra_kwargs['target'] = target

        annotation, ignore = Annotation.get_one_or_create(
            _db, patron=patron, identifier=identifier,
            motivation=motivation, on_multiple='interchangeable',
            **extra_kwargs
        )
        annotation.target = target
        if content:
            annotation.content = json.dumps(content)
        annotation.active = True
        annotation.timestamp = datetime.now()

        return annotation
Ejemplo n.º 4
0
        content = data.get("http://www.w3.org/ns/oa#hasBody")
        if content and len(content) == 1:
            content = content[0]
        else:
            content = None

        target = json.dumps(target)
        extra_kwargs = {}
        if motivation == Annotation.IDLING:
            # A given book can only have one 'idling' annotation.
            pass
        elif motivation == Annotation.BOOKMARKING:
            # A given book can only have one 'bookmarking' annotation
            # per target.
            extra_kwargs['target'] = target

        annotation, ignore = Annotation.get_one_or_create(
            _db,
            patron=patron,
            identifier=identifier,
            motivation=motivation,
            on_multiple='interchangeable',
            **extra_kwargs)
        annotation.target = target
        if content:
            annotation.content = json.dumps(content)
        annotation.active = True
        annotation.timestamp = datetime.now()

        return annotation
Ejemplo n.º 5
0
class AnnotationParser(object):
    @classmethod
    def parse(cls, _db, data, patron):
        if patron.synchronize_annotations != True:
            return PATRON_NOT_OPTED_IN_TO_ANNOTATION_SYNC

        try:
            data = json.loads(data)
            data = jsonld.expand(data)
        except ValueError, e:
            return INVALID_ANNOTATION_FORMAT

        if not data or not len(data) == 1:
            return INVALID_ANNOTATION_TARGET
        data = data[0]

        target = data.get("http://www.w3.org/ns/oa#hasTarget")
        if not target or not len(target) == 1:
            return INVALID_ANNOTATION_TARGET
        target = target[0]

        source = target.get("http://www.w3.org/ns/oa#hasSource")

        if not source or not len(source) == 1:
            return INVALID_ANNOTATION_TARGET
        source = source[0].get('@id')

        identifier, ignore = Identifier.parse_urn(_db, source)

        motivation = data.get("http://www.w3.org/ns/oa#motivatedBy")
        if not motivation or not len(motivation) == 1:
            return INVALID_ANNOTATION_MOTIVATION
        motivation = motivation[0].get('@id')
        if motivation not in Annotation.MOTIVATIONS:
            return INVALID_ANNOTATION_MOTIVATION

        loans = patron.loans
        loan_identifiers = [loan.license_pool.identifier for loan in loans]
        if identifier not in loan_identifiers:
            return INVALID_ANNOTATION_TARGET

        content = data.get("http://www.w3.org/ns/oa#hasBody")
        if content and len(content) == 1:
            content = content[0]
        else:
            content = None

        target = json.dumps(target)
        extra_kwargs = {}
        if motivation == Annotation.IDLING:
            # A given book can only have one 'idling' annotation.
            pass
        elif motivation == Annotation.BOOKMARKING:
            # A given book can only have one 'bookmarking' annotation
            # per target.
            extra_kwargs['target'] = target

        annotation, ignore = Annotation.get_one_or_create(
            _db,
            patron=patron,
            identifier=identifier,
            motivation=motivation,
            **extra_kwargs)
        annotation.target = target
        if content:
            annotation.content = json.dumps(content)
        annotation.active = True
        annotation.timestamp = datetime.now()

        return annotation