~homeworkprod/byceps

ref: 4237b3ec9496efe95dcce82bea3207ab9de4d520 byceps/byceps/services/terms/consent_service.py -rw-r--r-- 914 bytes
4237b3ec — Jochen Kupperschmidt Move ticketing blueprint into `site` subpackage 1 year, 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
byceps.services.terms.consent_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from typing import Dict

from ...database import db

from ..consent.models.consent import Consent
from ..consent.models.subject import Subject

from .models.version import Version
from .transfer.models import DocumentID, VersionID


def count_consents_for_document_versions(
    document_id: DocumentID,
) -> Dict[VersionID, int]:
    """Return the number of consents for each version of the document."""
    rows = db.session \
        .query(
            Version.id,
            db.func.count(Consent.subject_id)
        ) \
        .outerjoin(Subject, Version.consent_subject) \
        .outerjoin(Consent) \
        .group_by(Version.id) \
        .filter(Version.document_id == document_id) \
        .all()

    return dict(rows)