|
10 | 10 | parse_title,
|
11 | 11 | resolve_title,
|
12 | 12 | )
|
13 |
| -from .collection_choice_fields import DocumentTypes |
| 13 | +from .collection_choice_fields import Divisions, DocumentTypes |
14 | 14 |
|
15 | 15 |
|
16 | 16 | class BaseMatchPattern(models.Model):
|
@@ -250,6 +250,29 @@ class Meta:
|
250 | 250 | unique_together = ("collection", "match_pattern")
|
251 | 251 |
|
252 | 252 |
|
| 253 | +class DivisionPattern(BaseMatchPattern): |
| 254 | + division = models.IntegerField(choices=Divisions.choices) |
| 255 | + |
| 256 | + def apply(self) -> None: |
| 257 | + matched_urls = self.matched_urls() |
| 258 | + matched_urls.update(division=self.division) |
| 259 | + candidate_url_ids = list(matched_urls.values_list("id", flat=True)) |
| 260 | + self.candidate_urls.through.objects.bulk_create( |
| 261 | + objs=[ |
| 262 | + DivisionPattern.candidate_urls.through(candidateurl_id=candidate_url_id, divisionpattern_id=self.id) |
| 263 | + for candidate_url_id in candidate_url_ids |
| 264 | + ] |
| 265 | + ) |
| 266 | + |
| 267 | + def unapply(self) -> None: |
| 268 | + self.candidate_urls.update(division=None) |
| 269 | + |
| 270 | + class Meta: |
| 271 | + verbose_name = "Division Pattern" |
| 272 | + verbose_name_plural = "Division Patterns" |
| 273 | + unique_together = ("collection", "match_pattern") |
| 274 | + |
| 275 | + |
253 | 276 | # @receiver(post_save, sender=TitlePattern)
|
254 | 277 | # def post_save_handler(sender, instance, created, **kwargs):
|
255 | 278 | # if created:
|
|
0 commit comments