class Post(models.Model):
title = models.CharField(max_length=100)
content = models.TextField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
price = models.IntegerField(validators=[MinValueValidator(10), MaxValueValidator(10000000)], default=10,
help_text="Enter in Dollars, $.")
featured = models.BooleanField(default=False)
active = models.BooleanField(default=True)
propertyType = models.CharField(max_length=120, choices=TYPE_CHOICES, validators=[
validate_propertyType], default='Villa',)
area = models.DecimalField(decimal_places=2, max_digits=20, null=True, blank=True,
help_text="Enter in Acres. Hint: 100 decimals = 1 acre = 0.405 hectares, 1 Sq. mile = 640 Acres, 1 decimal = 436 sq. feet = 40.5 sq. metres = 0.001 acres.")
bedrooms = models.SmallIntegerField(choices=BEDROOMS_CHOICES, null=True, blank=True)
baths = models.SmallIntegerField(choices=BATHROOMS_CHOICES, null=True, blank=True)
garages = models.SmallIntegerField(choices=GARAGES_CHOICES, null=True, blank=True)
location = models.CharField(
max_length=120, null=True, help_text="Enter a Town/City where your Property is located.")
propertyStatus = models.CharField(
max_length=120, choices=STATUS_CHOICES, validators=[validate_propertyStatus], default='Sale')
amenities = models.TextField(null=True, blank=True,
help_text="Enter separating with commas, e.g: Parking, Balcony, Swimming Pool, Tennis Courts, Outdoor Kitchen, Internet, Cable T.V, Concrete Flooring, ...")
slug = models.SlugField(null=True, blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('post-detail', kwargs={'pk': self.pk})
def get_image_filename(instance, filename):
title = instance.post.title
slug = slugify(title)
return "properties_pics/%s-%s" % (slug, filename)
class Images(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE,default=None)
propertyImages = models.ImageField(
default="default.jpg", upload_to=get_image_filename,
help_text=" We must provide a clear Picture, preferably Landscape, not Portrait!")
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.propertyImages.path)
if img.height > 800 or img.width > 600:
output_size = (600, 800)
img.thumbnail(output_size)
img.save(self.propertyImages.path)