-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathlegacy_mapping.py
139 lines (115 loc) · 3.78 KB
/
legacy_mapping.py
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
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
# This file is part of Indico.
# Copyright (C) 2002 - 2024 CERN
#
# Indico is free software; you can redistribute it and/or
# modify it under the terms of the MIT License; see the
# LICENSE file for more details.
from sqlalchemy.ext.declarative import declared_attr
from indico.core.db import db
from indico.core.db.sqlalchemy.util.models import auto_table_args
class _LegacyLinkMixin:
events_backref_name = None
@declared_attr
def event_id(cls):
return db.Column(
db.Integer,
db.ForeignKey('events.events.id'),
nullable=False,
index=True
)
@declared_attr
def session_id(cls):
return db.Column(
db.String,
nullable=True
)
@declared_attr
def contribution_id(cls):
return db.Column(
db.String,
nullable=True
)
@declared_attr
def subcontribution_id(cls):
return db.Column(
db.String,
nullable=True
)
@declared_attr
def event(cls):
return db.relationship(
'Event',
lazy=True,
backref=db.backref(
cls.events_backref_name,
lazy='dynamic'
)
)
@property
def link_repr(self):
"""A kwargs-style string suitable for the object's repr."""
_all_columns = {'event_id', 'contribution_id', 'subcontribution_id', 'session_id'}
info = [(key, getattr(self, key)) for key in _all_columns if getattr(self, key) is not None]
return ', '.join(f'{key}={value}' for key, value in info)
class LegacyAttachmentFolderMapping(_LegacyLinkMixin, db.Model):
"""Legacy attachmentfolder id mapping.
Legacy folders ("materials") had ids unique only within their
linked object. This table maps those ids for a specific object
to the new globally unique folder id.
"""
__tablename__ = 'legacy_folder_id_map'
events_backref_name = 'all_legacy_attachment_folder_mappings'
@declared_attr
def __table_args__(cls):
return auto_table_args(cls, schema='attachments')
material_id = db.Column(
db.String,
nullable=False
)
folder_id = db.Column(
db.Integer,
db.ForeignKey('attachments.folders.id'),
primary_key=True,
autoincrement=False
)
folder = db.relationship(
'AttachmentFolder',
lazy=False,
backref=db.backref('legacy_mapping', uselist=False, lazy=True)
)
def __repr__(self):
return f'<LegacyAttachmentFolderMapping({self.folder}, material_id={self.material_id}, {self.link_repr})>'
class LegacyAttachmentMapping(_LegacyLinkMixin, db.Model):
"""Legacy attachment id mapping.
Legacy attachments ("resources") had ids unique only within their
folder and its linked object. This table maps those ids for a
specific object to the new globally unique attachment id.
"""
__tablename__ = 'legacy_attachment_id_map'
events_backref_name = 'all_legacy_attachment_mappings'
@declared_attr
def __table_args__(cls):
return auto_table_args(cls, schema='attachments')
material_id = db.Column(
db.String,
nullable=False
)
resource_id = db.Column(
db.String,
nullable=False
)
attachment_id = db.Column(
db.Integer,
db.ForeignKey('attachments.attachments.id'),
primary_key=True,
autoincrement=False
)
attachment = db.relationship(
'Attachment',
lazy=False,
backref=db.backref('legacy_mapping', uselist=False, lazy=True)
)
def __repr__(self):
return '<LegacyAttachmentMapping({}, material_id={}, resource_id={}, {})>'.format(
self.attachment, self.material_id, self.resource_id, self.link_repr
)