models.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. from django.db import models
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.contrib.contenttypes.generic import GenericForeignKey
  4. from django.db import models, IntegrityError, transaction
  5. from django.template.defaultfilters import slugify
  6. from django.utils.translation import ugettext_lazy as _, ugettext
  7. from directory.models import Place
  8. from taggit.managers import TaggableManager
  9. from django_extensions.db.models import TimeStampedModel
  10. from documents.managers import PublishedManager
  11. class Document(TimeStampedModel):
  12. ACCESS_CHOICES = (
  13. ("public", "Public"),
  14. ("private", "Private"),
  15. ("organization", "Organization"),
  16. )
  17. title = models.CharField(verbose_name=_("Title"), max_length=255)
  18. slug = models.SlugField(verbose_name=_("Slug"), unique=True, max_length=200)
  19. description = models.TextField(_("Description"), blank=True, null=True)
  20. source = models.CharField(_("Source"), max_length=100)
  21. access = models.CharField(
  22. _("Access"), max_length=12, choices=ACCESS_CHOICES, default="public"
  23. )
  24. published = models.BooleanField(_("published"), default=False)
  25. published_on = models.DateTimeField(_("published on"))
  26. preview = models.ImageField(
  27. _("preview"), upload_to="documents/previews", blank=True, null=True
  28. )
  29. doc_cloud_id = models.SlugField(
  30. max_length=80, editable=False, null=True, blank=True
  31. )
  32. tags = TaggableManager()
  33. objects = models.Manager()
  34. published_objects = PublishedManager()
  35. def __unicode__(self):
  36. return self.title
  37. class Meta:
  38. verbose_name = _("Document")
  39. verbose_name_plural = _("Documents")
  40. @models.permalink
  41. def get_absolute_url(self):
  42. return (
  43. "dc-document-detail",
  44. (),
  45. {
  46. "year": self.published_on.year,
  47. "month": self.published_on.strftime("%b").lower(),
  48. "day": self.published_on.day,
  49. "slug": self.slug,
  50. },
  51. )
  52. @property
  53. def display(self):
  54. return True
  55. class TextDocument(Document):
  56. content = models.TextField(
  57. _("content"),
  58. blank=True,
  59. null=True,
  60. help_text="If document is plaintext, insert it here.",
  61. )
  62. class Meta:
  63. verbose_name = _("Text Document")
  64. verbose_name_plural = _("Text Documents")
  65. @models.permalink
  66. def get_absolute_url(self):
  67. return (
  68. "dc-text-document-detail",
  69. (),
  70. {
  71. "year": self.published_on.year,
  72. "month": self.published_on.strftime("%b").lower(),
  73. "day": self.published_on.day,
  74. "slug": self.slug,
  75. },
  76. )
  77. class PDFDocument(Document):
  78. file = models.FileField(_("file"), upload_to="documents/pdf/%Y/%b/%d")
  79. class Meta:
  80. verbose_name = _("PDF Document")
  81. verbose_name_plural = _("PDF Documents")
  82. @models.permalink
  83. def get_absolute_url(self):
  84. return (
  85. "dc-pdf-document-detail",
  86. (),
  87. {
  88. "year": self.published_on.year,
  89. "month": self.published_on.strftime("%b").lower(),
  90. "day": self.published_on.day,
  91. "slug": self.slug,
  92. },
  93. )
  94. class ExcelDocument(Document):
  95. file = models.FileField(_("file"), upload_to="documents/excel/%Y/%b/%d")
  96. class Meta:
  97. verbose_name = _("Excel Document")
  98. verbose_name_plural = _("Excel Documents")
  99. @models.permalink
  100. def get_absolute_url(self):
  101. return (
  102. "dc-xcl-document-detail",
  103. (),
  104. {
  105. "year": self.published_on.year,
  106. "month": self.published_on.strftime("%b").lower(),
  107. "day": self.published_on.day,
  108. "slug": self.slug,
  109. },
  110. )
  111. class WordDocument(Document):
  112. file = models.FileField(_("file"), upload_to="documents/word/%Y/%b/%d")
  113. class Meta:
  114. verbose_name = _("Word Document")
  115. verbose_name_plural = _("Word Documents")
  116. @models.permalink
  117. def get_absolute_url(self):
  118. return (
  119. "dc-word-document-detail",
  120. (),
  121. {
  122. "year": self.published_on.year,
  123. "month": self.published_on.strftime("%b").lower(),
  124. "day": self.published_on.day,
  125. "slug": self.slug,
  126. },
  127. )