|
3 | 3 | from circuits.models import Circuit, CircuitTermination, ProviderNetwork |
4 | 4 | from dcim.models import ( |
5 | 5 | Cable, |
| 6 | + CableTermination, |
6 | 7 | ConsolePort, |
7 | 8 | ConsoleServerPort, |
8 | 9 | FrontPort, |
@@ -93,21 +94,26 @@ def cable_in_cables(term_a: tuple, term_b: tuple) -> bool: |
93 | 94 | Each tuple should consist termination object and termination type |
94 | 95 | """ |
95 | 96 |
|
96 | | - cable = Cable.objects.filter( |
97 | | - Q( |
98 | | - termination_a_id=term_a[0].id, |
99 | | - termination_a_type=term_a[1], |
100 | | - termination_b_id=term_b[0].id, |
101 | | - termination_b_type=term_b[1], |
| 97 | + try: |
| 98 | + cable_term_a = CableTermination.objects.get( |
| 99 | + Q( |
| 100 | + termination_id=term_a[0].id, |
| 101 | + termination_type=term_a[1], |
| 102 | + ) |
102 | 103 | ) |
103 | | - | Q( |
104 | | - termination_a_id=term_b[0].id, |
105 | | - termination_a_type=term_b[1], |
106 | | - termination_b_id=term_a[0].id, |
107 | | - termination_b_type=term_a[1], |
| 104 | + cable_term_b = CableTermination.objects.get( |
| 105 | + Q( |
| 106 | + termination_id=term_b[0].id, |
| 107 | + termination_type=term_b[1], |
| 108 | + ) |
108 | 109 | ) |
109 | | - ) |
110 | | - return cable.exists() |
| 110 | + except CableTermination.DoesNotExist: |
| 111 | + return False |
| 112 | + |
| 113 | + cable_a = Cable.objects.get(Q(terminations=cable_term_a)) |
| 114 | + cable_b = Cable.objects.get(Q(terminations=cable_term_b)) |
| 115 | + |
| 116 | + return cable_a.id == cable_b.id |
111 | 117 |
|
112 | 118 |
|
113 | 119 | def check_termination_types(type_a, type_b) -> Tuple[bool, str]: |
@@ -223,13 +229,24 @@ def load_data(self): |
223 | 229 |
|
224 | 230 | check_terminations_are_free(term_a, term_b) |
225 | 231 |
|
226 | | - params["termination_a_id"] = term_a.id |
227 | | - params["termination_b_id"] = term_b.id |
228 | | - params["termination_a_type"] = term_a_ct |
229 | | - params["termination_b_type"] = term_b_ct |
230 | | - |
231 | 232 | cable = Cable.objects.create(**params) |
232 | 233 |
|
| 234 | + params_a_term = { |
| 235 | + "termination_id": term_a.id, |
| 236 | + "termination_type": term_a_ct, |
| 237 | + "cable": cable, |
| 238 | + "cable_end": "A", |
| 239 | + } |
| 240 | + CableTermination.objects.create(**params_a_term) |
| 241 | + |
| 242 | + params_b_term = { |
| 243 | + "termination_id": term_b.id, |
| 244 | + "termination_type": term_b_ct, |
| 245 | + "cable": cable, |
| 246 | + "cable_end": "B", |
| 247 | + } |
| 248 | + CableTermination.objects.create(**params_b_term) |
| 249 | + |
233 | 250 | print(f"🧷 Created cable {cable} {cable_name}") |
234 | 251 |
|
235 | 252 |
|
|
0 commit comments