Completed
Push — master ( d3bf11...c24f20 )
by Bart
01:45
created

audit()   C

Complexity

Conditions 7

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 45
rs 5.5
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B advice() 0 29 6
1
# -*- coding: utf-8 -*-
2
from atramhasis.data.models import (
3
    ConceptschemeVisitLog,
4
    ConceptVisitLog
5
)
6
from pyramid.response import Response
7
import logging
8
9
log = logging.getLogger(__name__)
10
11
12
def _origin_from_request(request):
13
    if '.csv' in request.url:
14
        return 'CSV'
15
    elif 'text/html' in request.accept:
16
        return 'HTML'
17
    elif 'application/json' in request.accept:
18
        return 'REST'
19
    else:
20
        return None
21
22
23
def _origin_from_response(response):
24
    if response.content_type == 'application/rdf+xml' \
25
            or response.content_type == 'text/turtle' \
26
            or response.content_type == 'application/x-turtle':
27
        return 'RDF'
28
    else:
29
        return None
30
31
32
def audit(fn):
33
    """
34
    use this decorator to audit an operation and to log the visit
35
    external conceptschemes won't be logged
36
37
    * CSV routes with .csv extensions accept all mime types,
38
      the response is not of the `pyramid.response.Response` type,
39
      the origin is derived from the `pyramid.request.Request.url` extension.
40
    * RDF routes with .rdf, .ttl extensions accept all mime types,
41
      the origin is derived form the response content type.
42
    * REST and HTML the view results are not of the `pyramid.response.Response` type,
43
      the origin is derived from the accept header.
44
    """
45
46
    def advice(parent_object, *args, **kw):
47
        request = parent_object.request
48
        audit_manager = request.data_managers['audit_manager']
49
50
        if 'scheme_id' not in request.matchdict.keys():
51
            log.error('Misuse of the audit decorator. The url must at least contain a {scheme_id} parameter')
52
            return fn(parent_object, *args, **kw)
53
54
        provider = request.skos_registry.get_provider(request.matchdict['scheme_id'])
55
        if not provider or 'external' in provider.get_metadata()['subject']:
56
            return fn(parent_object, *args, **kw)
57
        else:
58
            if 'c_id' in request.matchdict.keys():
59
                visit_log = ConceptVisitLog(
60
                    concept_id=request.matchdict['c_id'],
61
                    conceptscheme_id=request.matchdict['scheme_id']
62
                )
63
            else:
64
                visit_log = ConceptschemeVisitLog(conceptscheme_id=request.matchdict['scheme_id'])
65
            response = fn(parent_object, *args, **kw)
66
67
            if isinstance(response, Response):
68
                visit_log.origin = _origin_from_response(response)
69
            else:
70
                visit_log.origin = _origin_from_request(request)
71
72
            audit_manager.save(visit_log)
73
74
        return response
75
76
    return advice
77