Skip to content

Commit 1cef417

Browse files
adding the division pattern model
1 parent 126148c commit 1cef417

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

sde_collections/models/pattern.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
parse_title,
1111
resolve_title,
1212
)
13-
from .collection_choice_fields import DocumentTypes
13+
from .collection_choice_fields import Divisions, DocumentTypes
1414

1515

1616
class BaseMatchPattern(models.Model):
@@ -250,6 +250,29 @@ class Meta:
250250
unique_together = ("collection", "match_pattern")
251251

252252

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+
253276
# @receiver(post_save, sender=TitlePattern)
254277
# def post_save_handler(sender, instance, created, **kwargs):
255278
# if created:

0 commit comments

Comments
 (0)