-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement advertisement, advertisement image, attribute, adv attribute value models
- Loading branch information
Showing
10 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class AdvertisementsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "advertisements" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.core.validators import FileExtensionValidator | ||
from django.db import models | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from categories.models import Category | ||
from locations.models import Location | ||
from utilities.base_model import BaseModel | ||
from utilities.username import generate_random_string | ||
|
||
# To get user from settings | ||
User = get_user_model() | ||
|
||
|
||
class Advertisement(BaseModel): | ||
""" | ||
This class represents Advertisement model. | ||
Each user can one or more advertisement to publish 📢 | ||
""" | ||
|
||
user = models.ForeignKey( | ||
User, | ||
on_delete=models.CASCADE, | ||
verbose_name=_("user"), | ||
related_name="advertisements", | ||
) | ||
title = models.CharField(max_length=50, verbose_name=_("title")) | ||
description = models.TextField(blank=True, verbose_name=_("description")) | ||
price = models.PositiveIntegerField(default=0, verbose_name=_("description")) | ||
location = models.ForeignKey( | ||
Location, | ||
on_delete=models.CASCADE, | ||
related_name="advertisements", | ||
verbose_name=_("location"), | ||
) | ||
category = models.ForeignKey( | ||
Category, | ||
on_delete=models.CASCADE, | ||
related_name="advertisements", | ||
verbose_name=_("category"), | ||
) | ||
|
||
def __str__(self): | ||
return f"{self.title} > {self.location.city.name}" | ||
|
||
class Meta: | ||
verbose_name = _("advertisement") | ||
verbose_name_plural = _("advertisements") | ||
|
||
@classmethod | ||
def add(cls, user, title, description, price, location, category, images): | ||
""" | ||
Get data an Advertisement and Save it in Database 💾 | ||
""" | ||
adv = cls.objects.create( | ||
user=user, | ||
title=title, | ||
description=description, | ||
price=price, | ||
location=location, | ||
category=category, | ||
) | ||
for file in images: | ||
adv.images.create(name=generate_random_string(), image_file=file) | ||
adv.save() | ||
|
||
def get_absolute_url(self): | ||
from django.urls import reverse | ||
|
||
return reverse("advertisement-detail", args=[str(self.pk)]) | ||
|
||
@classmethod | ||
def is_belong_user(cls, user, advertisement_pk): | ||
"""To check this advertisement posted by this user or not""" | ||
advertisement = cls.objects.select_related("user").get(pk=advertisement_pk) | ||
return user == advertisement.user | ||
|
||
|
||
class AdvertisementImage(BaseModel): | ||
""" | ||
This class represents Image model. | ||
Each advertisement has one or many images. 🖼 | ||
""" | ||
|
||
name = models.CharField(max_length=50, verbose_name=_("name")) | ||
advertisement = models.ForeignKey( | ||
Advertisement, | ||
on_delete=models.CASCADE, | ||
related_name="images", | ||
verbose_name=_("advertisement"), | ||
) | ||
image_file = models.FileField( | ||
upload_to="images/advertisement/", | ||
validators=[FileExtensionValidator(allowed_extensions=("jpg", "png", "jpeg"))], | ||
verbose_name=_("images"), | ||
) | ||
|
||
|
||
class Attribute(models.Model): | ||
name = models.CharField(max_length=50, verbose_name=_("attribute")) | ||
|
||
|
||
class AdvAttrValue(models.Model): | ||
advertisement = models.ForeignKey( | ||
Advertisement, | ||
on_delete=models.CASCADE, | ||
related_name="attributes", | ||
verbose_name=_("advertisement"), | ||
) | ||
attribute = models.ForeignKey( | ||
Attribute, | ||
on_delete=models.CASCADE, | ||
related_name="attributes", | ||
verbose_name=_("attribute"), | ||
) | ||
value = models.CharField(max_length=50, verbose_name=_("value")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.shortcuts import render | ||
|
||
# Create your views here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters