Açıklama Yok

models.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from django.db import models
  2. from django_google_maps import fields as map_fields
  3. from colorfield.fields import ColorField
  4. from smart_selects.db_fields import (
  5. ChainedForeignKey,
  6. ChainedManyToManyField,
  7. GroupedForeignKey,
  8. )
  9. from django.db.models import Q
  10. import googlemaps
  11. from django.contrib.gis.geos import fromstr
  12. from django.conf import settings
  13. from mptt.models import MPTTModel, TreeForeignKey
  14. from django.contrib.auth.models import User
  15. from django.db.models.signals import post_save
  16. from django.dispatch import receiver
  17. from shaqfindbed.utils import get_current_user
  18. from django_quill.fields import QuillField
  19. from django.utils.html import escape, format_html
  20. from taggit.managers import TaggableManager
  21. from fruit.models import Store, Product
  22. # Create your models here.
  23. class GenericModel(models.Model):
  24. created_at = models.DateTimeField(auto_now_add=True, null=True)
  25. updated_at = models.DateTimeField(auto_now=True)
  26. created_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_created', on_delete=models.SET_NULL)
  27. modified_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_modified', on_delete=models.SET_NULL)
  28. class Meta:
  29. abstract = True
  30. class PostCat(GenericModel, MPTTModel):
  31. name = models.CharField(max_length=50, unique=True)
  32. parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
  33. class MPTTMeta:
  34. order_insertion_by = ['name']
  35. def __str__(self):
  36. return f"{self.name}"
  37. class Post(GenericModel, models.Model):
  38. feature_image = models.ImageField(upload_to="uploads/%Y/%m/%d/", blank=True, null=True)
  39. title = models.CharField(max_length=200)
  40. body = QuillField()
  41. #body = models.TextField(blank=True, null=True)
  42. cat = TreeForeignKey('PostCat', on_delete=models.SET_NULL, null=True)
  43. top_store = models.ForeignKey(Store, on_delete=models.SET_NULL, null=True, blank=True)
  44. products1 = ChainedForeignKey(
  45. Product,
  46. chained_field="top_store",
  47. chained_model_field="store",
  48. show_all=False,
  49. auto_choose=True,
  50. null=True,
  51. blank=True,
  52. related_name="product1"
  53. )
  54. products2 = ChainedForeignKey(
  55. Product,
  56. chained_field="top_store",
  57. chained_model_field="store",
  58. show_all=False,
  59. auto_choose=True,
  60. null=True,
  61. blank=True,
  62. related_name="product2"
  63. )
  64. products3 = ChainedForeignKey(
  65. Product,
  66. chained_field="top_store",
  67. chained_model_field="store",
  68. show_all=False,
  69. auto_choose=True,
  70. blank=True,
  71. null=True,
  72. related_name="product3"
  73. )
  74. tags = TaggableManager(blank=True)
  75. status = models.CharField(
  76. max_length=30,
  77. choices=(("draft", "Draft"), ("publish", "Publish")),
  78. default="draft",
  79. null=True,
  80. )
  81. def image_tag(self):
  82. return format_html('<img src="%s" width="300px"/>' % escape(self.feature_image.url))
  83. image_tag.short_description = 'Image'
  84. image_tag.allow_tags = True
  85. def __str__(self):
  86. return f"{self.title}"
  87. class PostPhoto(GenericModel, models.Model):
  88. name = models.CharField(max_length=200, unique=True, blank=True, null=True)
  89. photo = models.ImageField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Photo")
  90. post = models.ForeignKey('Post', on_delete=models.CASCADE, null=True)
  91. def image_tag(self):
  92. return format_html('<img src="%s" width="300px"/>' % escape(self.photo.url))