text: from django.db import models
  from django.template.defaultfilters import slugify
  from django_resized import ResizedImageField
  from django.utils import timezone
  from uuid import uuid4
  from django.urls import reverse
  from cloudinary.models import CloudinaryField
  
  
  
  # Create your models here.
  class Category(models.Model):
      title = models.CharField(null=True, blank=True, max_length=200)
  
      #Utility Variable
      uniqueId = models.CharField(null=True, blank=True, max_length=100)
      slug = models.SlugField(max_length=500, unique=True, blank=True, null=True)
      date_created = models.DateTimeField(blank=True, null=True)
  
      @classmethod
      def search_by_category(cls,search_term):
          image = cls.objects.filter(title__icontains=search_term)
          return image
      
  
      def __str__(self):
          return '{} {}'.format(self.title, self.uniqueId)
  
  
      def get_absolute_url(self):
          return reverse('category-detail', kwargs={'slug': self.slug})
  
  
      def save(self, *args, **kwargs):
          if self.date_created is None:
              self.date_created = timezone.localtime(timezone.now())
          if self.uniqueId is None:
              self.uniqueId = str(uuid4()).split('-')[4]
              self.slug = slugify('{} {}'.format(self.title, self.uniqueId))
  
  
          self.slug = slugify('{} {}'.format(self.title, self.uniqueId))
          self.last_updated = timezone.localtime(timezone.now())
          super(Category, self).save(*args, **kwargs)
  
  
  class Location(models.Model):
      name = models.CharField(max_length=60, default='')
  
  
      def __str__(self):
          return self.name
      
      @classmethod
      def get_locations(cls):
          locations = Location.objects.all()
          return locations
      
      @classmethod
      def update_location(cls, id, value):
          cls.objects.filter(id=id).update(image=value)
  
      def save_location(self):
          self.save()
  
      def delete_location(self):
          self.delete()
    
  
  class Image(models.Model):
      description = models.TextField(null=True, blank=True)
      ##ImageFields
      squareImage = CloudinaryField('image')
      ##Related Fiels
      category = models.ForeignKey(Category, null=True, blank=True, on_delete=models.CASCADE)
      location = models.ForeignKey(Location, null=True, on_delete=models.CASCADE)
  
      #Utility Variable
      uniqueId = models.CharField(null=True, blank=True, max_length=100)
      slug = models.SlugField(max_length=500, unique=True, blank=True, null=True)
      date_created = models.DateTimeField(blank=True, null=True)
  
      @classmethod
      def filter_by_location(cls, location):
          image_location = Image.objects.filter(location__name=location).all()
          return image_location
  
      @classmethod
      def update_image(cls, id, value):
          cls.objects.filter(id=id).update(image=value)
  
      @classmethod
      def get_image_by_id(cls, id):
          image = cls.objects.filter(id=id).all()
          return image
      
      @classmethod
      def search_by_category(cls, category):
          images = cls.objects.filter(category__name__icontains=category)
          return images
  
      def __str__(self):
          return self.name
  
      def save_image(self):
          self.save()
  
      def delete_image(self):
          self.delete()
  
      
  
      def __str__(self):
          return '{} {}'.format(self.category.title, self.uniqueId)
  
  
      def get_absolute_url(self):
          return reverse('image-detail', kwargs={'slug': self.slug})
  
  
      def save(self, *args, **kwargs):
          if self.date_created is None:
              self.date_created = timezone.localtime(timezone.now())
          if self.uniqueId is None:
              self.uniqueId = str(uuid4()).split('-')[4]
              self.slug = slugify('{} {}'.format(self.category.title, self.uniqueId))
  
  
          self.slug = slugify('{} {}'.format(self.category.title, self.uniqueId))
          self.last_updated = timezone.localtime(timezone.now())
          super(Image, self).save(*args, **kwargs)
  
  