You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
881 B

2 years ago
2 years ago
  1. import abc
  2. from collections import defaultdict
  3. from typing import Dict, List, Tuple
  4. from django.db.models import QuerySet
  5. from data_export.models import ExportedComment, ExportedExample
  6. class Comments(abc.ABC):
  7. comment_class = ExportedComment
  8. column = "Comments"
  9. fields: Tuple[str, ...] = ("example", "user") # To boost performance
  10. def __init__(self, examples: QuerySet[ExportedExample], user=None):
  11. self.comment_groups = defaultdict(list)
  12. comments = self.comment_class.objects.filter(example__in=examples)
  13. if user:
  14. comments = comments.filter(user=user)
  15. for comment in comments.select_related(*self.fields):
  16. self.comment_groups[comment.example.id].append(comment)
  17. def find_by(self, example_id: int) -> Dict[str, List[ExportedComment]]:
  18. return {self.column: self.comment_groups[example_id]}