models.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Custom overrides of Wagtail Document and Image models. All other
  3. models related to website content should most likely go in
  4. ``website.models`` instead.
  5. """
  6. from django.db import models
  7. from wagtail.documents.models import AbstractDocument
  8. from wagtail.documents.models import Document
  9. from wagtail.images.models import AbstractImage
  10. from wagtail.images.models import AbstractRendition
  11. from wagtail.images.models import Image
  12. class CustomDocument(AbstractDocument):
  13. """
  14. A custom Wagtail Document model. Right now it is the same as
  15. the default, but can be easily extended by adding more fields here.
  16. """
  17. admin_form_fields = Document.admin_form_fields
  18. class CustomImage(AbstractImage):
  19. """
  20. A custom Wagtail Image model with fields for alt text and
  21. credit/attribution.
  22. """
  23. alt_text = models.CharField(
  24. max_length=255,
  25. blank=True,
  26. verbose_name="Alt Text",
  27. help_text=(
  28. "A description of this image used by search engines and screen readers."
  29. ),
  30. )
  31. credit = models.CharField(
  32. max_length=255,
  33. blank=True,
  34. verbose_name="Credit",
  35. help_text=(
  36. "Credit or attribute the source of the image. "
  37. "Properly attributing images taken from online sources can "
  38. "reduce your risk of copyright infringement."
  39. ),
  40. )
  41. admin_form_fields = Image.admin_form_fields + (
  42. "alt_text",
  43. "credit",
  44. )
  45. class CustomRendition(AbstractRendition):
  46. """
  47. Image rendition for our CustomImage model.
  48. """
  49. image = models.ForeignKey(
  50. CustomImage, on_delete=models.CASCADE, related_name="renditions"
  51. )
  52. class Meta:
  53. unique_together = (("image", "filter_spec", "focal_point_key"),)