Skip to content

Relationships

These relationships are defined separately to the object/table declarations to allow them to reference each other without circular imports.

setup_relationships() ΒΆ

Defines relationships/joins between the new models in ckanext-attribution.

Source code in ckanext/attribution/model/relationships.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def setup_relationships():
    """
    Defines relationships/joins between the new models in ckanext-attribution.
    """

    # Agent
    manager = attributes.manager_of_class(Agent)
    if not manager or not manager.is_mapped:
        meta.mapper(
            Agent,
            agent_table,
            properties={
                'user': relationship(
                    User,
                    backref=backref('agent', cascade='all, delete-orphan'),
                    primaryjoin=agent_table.c.user_id.__eq__(User.id),
                ),
                'contribution_activities': relationship(
                    ContributionActivity, secondary=agent_contribution_activity_table
                ),
            },
        )

    # ContributionActivity
    manager = attributes.manager_of_class(ContributionActivity)
    if not manager or not manager.is_mapped:
        meta.mapper(
            ContributionActivity,
            contribution_activity_table,
            properties={
                'agent': relationship(
                    Agent, secondary=agent_contribution_activity_table, uselist=False
                ),
                'package': relationship(
                    Package,
                    secondary=package_contribution_activity_table,
                    uselist=False,
                ),
            },
        )

    # AgentAffiliation
    manager = attributes.manager_of_class(AgentAffiliation)
    if not manager or not manager.is_mapped:
        meta.mapper(
            AgentAffiliation,
            agent_affiliation_table,
            properties={
                '_agent_backref': relationship(
                    Agent,
                    backref=backref('_affiliations', cascade='all, delete-orphan'),
                    primaryjoin=or_(
                        agent_affiliation_table.c.agent_a_id.__eq__(Agent.id),
                        agent_affiliation_table.c.agent_b_id.__eq__(Agent.id),
                    ),
                ),
                '_agent_a': relationship(
                    Agent,
                    primaryjoin=agent_affiliation_table.c.agent_a_id.__eq__(Agent.id),
                ),
                '_agent_b': relationship(
                    Agent,
                    primaryjoin=agent_affiliation_table.c.agent_b_id.__eq__(Agent.id),
                ),
                'package': relationship(
                    Package,
                    primaryjoin=agent_affiliation_table.c.package_id == Package.id,
                ),
            },
        )

    # AgentContributionActivity
    manager = attributes.manager_of_class(AgentContributionActivity)
    if not manager or not manager.is_mapped:
        meta.mapper(
            AgentContributionActivity,
            agent_contribution_activity_table,
            properties={
                'agent': relationship(
                    Agent,
                    backref=backref(
                        'contribution_activity_link', cascade='all, delete-orphan'
                    ),
                    primaryjoin=agent_contribution_activity_table.c.agent_id.__eq__(
                        Agent.id
                    ),
                ),
                'contribution_activity': relationship(
                    ContributionActivity,
                    backref=backref('agent_link', cascade='all, delete-orphan'),
                    primaryjoin=agent_contribution_activity_table.c.contribution_activity_id.__eq__(
                        ContributionActivity.id
                    ),
                ),
            },
        )

    # PackageContributionActivity
    manager = attributes.manager_of_class(PackageContributionActivity)
    if not manager or not manager.is_mapped:
        meta.mapper(
            PackageContributionActivity,
            package_contribution_activity_table,
            properties={
                'package': relationship(
                    Package,
                    backref=backref(
                        'contribution_activity_link', cascade='all, delete-orphan'
                    ),
                    primaryjoin=package_contribution_activity_table.c.package_id.__eq__(
                        Package.id
                    ),
                ),
                'contribution_activity': relationship(
                    ContributionActivity,
                    backref=backref('package_link', cascade='all, delete-orphan'),
                    primaryjoin=package_contribution_activity_table.c.contribution_activity_id.__eq__(
                        ContributionActivity.id
                    ),
                ),
            },
        )