Project_Camper/vagus/camper/models.py
2021-02-06 12:31:59 +01:00

120 lines
3.8 KiB
Python
Executable File

from django.db import models
from django.utils.translation import gettext_lazy as _
from django.urls import reverse
from users.models import Base_User
import users
from django.conf import settings
from django.template.defaultfilters import slugify
from django import template
register = template.Library()
STATUS = (
('Active',_('Active offer')),
('Inactive',_('Inactive offer')),
('Verification',_('Waiting for verication')),
('Blocked',_('Blocked Offer')),
)
DMC_Value = (
('B',_('Do 3.5 tony.')),
('C',_('Między 3.5 a 7.5 tony')),
('C1',_('Powyzej 7.5 tony')),
)
COOKER_Option = (
('Elecric',_('Kuchenka elektryczna')),
('Gas',_('Kuchenka gazowa')),
('None',_('Brak')),
)
GEARBOX_Option = (
('Manual',_('Skrzynia manualna')),
('Automat',_('Skrzynia automatyczna')),
)
class Offer_Category(models.Model):
id = models.AutoField(primary_key= True)
name = models.CharField(max_length=50)
class Meta:
verbose_name = 'Category'
verbose_name_plural = 'Categories'
def __str__(self):
return self.name
class Offer(models.Model):
id = models.AutoField(primary_key= True)
created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
date_added = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=32, choices=STATUS, default='Inactive')
title = models.CharField(_("Title"), max_length=50,default='Tytuł oferty')
slug = models.SlugField(max_length=70, editable = False,default='' , unique=True,)
description = models.TextField(max_length = 1000,default = '')
category = models.ForeignKey(Offer_Category ,default= None,on_delete= models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
DMC = models.CharField(max_length=32, choices=DMC_Value, default='B')
Places_to_sleep = models.IntegerField(default=2)
cooker = models.CharField(max_length = 32, choices=COOKER_Option, default='None')
fridge = models.BooleanField(default=False)
microwave = models.BooleanField(default= False)
Webasto = models.BooleanField(default=False)
auto_temp_control = models.BooleanField(default= False)
water_level_indicator = models.BooleanField(default=False)
battery_level = models.BooleanField(default=False)
photovoltaic_panels = models.BooleanField(default=False)
gearbox = models.CharField(max_length=32, choices=GEARBOX_Option,default='Manual')
tempomat = models.BooleanField(default=False)
class Meta:
verbose_name = 'Offer'
verbose_name_plural = 'Offers'
def __str__(self):
return self.title
def save(self,*args,**kwargs):
super(Offer, self).save(*args,**kwargs)
self.slug = slugify(f'{self.title}-{self.id}')
return super(Offer, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('offer_detail', kwargs = {'slug': self.slug })
def _upload_path(instance,filename):
return instance.get_upload_path(filename)
class Image_offer(models.Model):
offer = models.ForeignKey("Offer", verbose_name="", on_delete = models.CASCADE, default=None, related_name='images')
image = models.ImageField(upload_to=_upload_path, null = False, blank = False)
main_image = models.BooleanField(default= False)
def get_upload_path(self,filename):
return "uploads/offer-images"+ "/" + 'id-'+str(self.offer.id) + "/" +filename
def delete(self,*args,**kwargs):
self.image.delete()
return super(Image_offer,self).save(*args,**kwargs)
class Favourite_Offers(models.Model):
user = models.ForeignKey('users.Base_User', on_delete=models.CASCADE,)
offer = models.ForeignKey('Offer', on_delete=models.SET_NULL, null = True)
timestap = models.DateTimeField(auto_now_add=True, db_index=True)