Keine Beschreibung

models.py 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. # Create your models here.
  22. class GenericModel(models.Model):
  23. created_at = models.DateTimeField(auto_now_add=True, null=True)
  24. updated_at = models.DateTimeField(auto_now=True)
  25. created_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_created', on_delete=models.SET_NULL)
  26. modified_by = models.ForeignKey(User, null=True, editable=False, related_name='%(class)s_modified', on_delete=models.SET_NULL)
  27. class Meta:
  28. abstract = True
  29. class PostCat(GenericModel, MPTTModel):
  30. name = models.CharField(max_length=50, unique=True)
  31. parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
  32. class MPTTMeta:
  33. order_insertion_by = ['name']
  34. def __str__(self):
  35. return f"{self.name}"
  36. class Post(GenericModel, models.Model):
  37. feature_image = models.ImageField(upload_to="uploads/%Y/%m/%d/", blank=True, null=True)
  38. title = models.CharField(max_length=200)
  39. body = QuillField()
  40. #body = models.TextField(blank=True, null=True)
  41. cat = models.ForeignKey('PostCat', on_delete=models.SET_NULL, null=True)
  42. tags = TaggableManager()
  43. status = models.CharField(
  44. max_length=30,
  45. choices=(("draft", "Draft"), ("publish", "Publish")),
  46. default="draft",
  47. null=True,
  48. )
  49. def image_tag(self):
  50. return format_html('<img src="%s" width="300px"/>' % escape(self.feature_image.url))
  51. image_tag.short_description = 'Image'
  52. image_tag.allow_tags = True
  53. def __str__(self):
  54. return f"{self.title}"
  55. class PostPhoto(GenericModel, models.Model):
  56. name = models.CharField(max_length=200, unique=True, blank=True, null=True)
  57. photo = models.ImageField(upload_to="uploads/%Y/%m/%d/", blank=True, verbose_name="Photo")
  58. post = models.ForeignKey('Post', on_delete=models.CASCADE, null=True)
  59. def image_tag(self):
  60. return format_html('<img src="%s" width="300px"/>' % escape(self.photo.url))