Models

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
2
Indexable
Never
from django.db import models
from django.conf import settings
from django.core.validators import RegexValidator
from taggit.managers import TaggableManager
from django.contrib.auth.models import User


class Contact(models.Model):
    email = models.EmailField()
    phone_number = models.CharField(
        max_length=10,
        validators=[RegexValidator(r"^[6-9]\d{9}$", ("Invalid Phone number"))],
    )


class Category(models.Model):
    name = models.CharField(max_length=256, db_index=True)
    slug = models.TextField(max_length=256, db_index=True, blank=True, unique=True)


class Post(models.Model):
    TYPES = (
        ("job", "Job"),
        ("gig", "Gig"),
        ("for sale", "For sale"),
        ("service", "Service"),
        ("event", "Event"),
        ("class", "Class"),
    )
    title = models.CharField(max_length=256)
    slug = models.TextField(max_length=256, blank=True, unique=True)
    owner = models.OneToOneField(
        User, on_delete=models.CASCADE
    )
    tags = TaggableManager(blank=True)
    postal_code = models.CharField(
        max_length=6,
        validators=[RegexValidator("^[0-9]{6}$", ("Invalid postal code"))],
    )
    location = models.CharField(max_length=200)
    description = models.TextField(max_length=256)
    contact_info = models.OneToOneField(Contact, on_delete=models.CASCADE)
    show_contact_info = models.BooleanField(default=True)
    price = models.IntegerField()
    date = models.DateTimeField(blank=True, null=True)
    type = models.CharField(max_length=12, choices=TYPES)
    category = models.ForeignKey(
        Category, on_delete=models.DO_NOTHING, related_name="posts"
    )


class Image(models.Model):
    title = models.CharField(max_length=200, db_index=True)
    slug = models.SlugField(max_length=200, db_index=True, blank=True)
    post = models.ForeignKey(Post)
    url = models.URLField()
    image = models.ImageField(upload_to="images/%Y/%m/%d/")


class Like(models.Model):
    liked_by = models.ManyToManyField(
        User, on_delete=models.CASCADE, related_name="liked_by"
    )
    liked_post = models.ManyToManyField(
        Post, on_delete=models.CASCADE, related_name="liked_posts"
    )