Skip to content

Commit

Permalink
Add slug and pet_key field to Pet table
Browse files Browse the repository at this point in the history
  • Loading branch information
riadelimemmedov committed Feb 17, 2024
1 parent 22b24f1 commit a877890
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 11 deletions.
32 changes: 32 additions & 0 deletions backend/apps/pet/migrations/0003_pet_pet_key_pet_slug.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Generated by Django 5.0.1 on 2024-02-17 21:14

import django_extensions.db.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("pet", "0002_pet_city"),
]

operations = [
migrations.AddField(
model_name="pet",
name="pet_key",
field=django_extensions.db.fields.RandomCharField(
blank=True,
editable=False,
length=12,
unique=True,
verbose_name="Pet key",
),
),
migrations.AddField(
model_name="pet",
name="slug",
field=models.SlugField(
blank=True, editable=False, unique=True, verbose_name="Slug"
),
),
]
22 changes: 22 additions & 0 deletions backend/apps/pet/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from colorfield.fields import ColorField
from django.core.validators import FileExtensionValidator
from django.db import models
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django_extensions.db.fields import RandomCharField
from django_extensions.db.models import TimeStampedModel

from abstract.constants import GendersPet
from config.helpers import setPetName


# *ActiveStatusQueryset
Expand Down Expand Up @@ -40,6 +43,12 @@ class Pet(TimeStampedModel):
name = models.CharField(_("Name"), max_length=50)
age = models.IntegerField(_("Age"))
breed = models.CharField(_("Breed"), max_length=50)
slug = models.SlugField(
_("Slug"), unique=True, db_index=True, editable=False, blank=True
)
pet_key = RandomCharField(
_("Pet key"), length=12, unique=True, blank=True, include_alpha=True
)
color = ColorField()
weight = models.FloatField(_("Weight"))
gender = models.CharField(
Expand All @@ -63,5 +72,18 @@ class Meta:
verbose_name_plural = _("Pets")
ordering = ["-created"]

def save(self, *args, **kwargs):
if not self.name:
name = setPetName(
self.name if self.name is not None else "",
)
is_exists = __class__.objects.filter(name=name).exists()
if is_exists:
self.name = f"{name}_{self.pet_key}"
else:
self.name = f"{name}"
self.slug = slugify(self.name)
super(Pet, self).save(*args, **kwargs)

def __str__(self):
return f"{self.name} - {self.age}"
4 changes: 2 additions & 2 deletions backend/apps/users/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.urls import path

from .views import hello_world
# from .views import hello_world

app_name = "users"
urlpatterns = [path("sayhello/", hello_world, name="hello_world")]
# urlpatterns = [path("sayhello/", hello_world, name="hello_world")]
10 changes: 5 additions & 5 deletions backend/apps/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# Create your views here.


@api_view(["GET", "POST"])
def hello_world(request):
if request.method == "POST":
return Response({"message": "Got some data!", "data": request.data})
return {"message": "Hello, world!"}
# @api_view(["GET", "POST"])
# def hello_world(request):
# if request.method == "POST":
# return Response({"message": "Got some data!", "data": request.data})
# return {"message": "Hello, world!"}
7 changes: 6 additions & 1 deletion backend/config/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@


# ? setFullName
def setFullName(name, surname):
def setFullName(name: str, surname: str) -> str:
return f"{name} {surname}"


# ? setPetName
def setPetName(name: str) -> str:
return f"{name}"


# ? is_valid_wallet_address
def is_valid_wallet_address(wallet_address: str):
pattern = r"^0x[a-fA-F0-9]{40}$"
Expand Down
1 change: 1 addition & 0 deletions backend/config/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Meta:
name = faker.name()
age = faker.random_int(min=1, max=15)
breed = faker.name()
slug = faker.random_letters(length=12)
color = faker.hex_color()
weight = faker.random_int(min=1, max=50)
gender = faker.random_element(["MALE", "FEMALE"])
Expand Down
3 changes: 3 additions & 0 deletions backend/config/tests/pet/test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def create_mock_pet(self, pet_factory):
name="Max",
age=3,
breed="Labrador",
slug="yxxa77",
color="#ff00ff",
weight=25.5,
gender="Male",
Expand All @@ -32,6 +33,7 @@ def create_mock_pet(self, pet_factory):
pet_factory(
age=2,
breed="Golden Retriever",
slug="yxghjk88pa",
color="#f9a602",
weight=28.7,
gender="Female",
Expand All @@ -46,6 +48,7 @@ def create_mock_pet(self, pet_factory):
name="Charlie",
age=4,
breed="Beagle",
slug="ghjkjdha5577uj",
color="#aabbcc",
weight=15.2,
gender="Male",
Expand Down
2 changes: 1 addition & 1 deletion backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
),
]
urlpatterns += i18n_patterns(path("admin/", admin.site.urls))
urlpatterns += [path("users/", include("apps.users.urls", namespace="users"))]
# urlpatterns += [path("users/", include("apps.users.urls", namespace="users"))]
urlpatterns += [path("upload/", include("apps.upload.urls", namespace="upload"))]


Expand Down
58 changes: 56 additions & 2 deletions backend/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,59 @@ info:
title: Django DRF Ecommerce
version: 1.0.0
description: This project purpose creating ecommerce api for business company
paths: {}
components: {}
paths:
/upload/image/:
post:
operationId: upload_image_create
tags:
- upload
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/Upload'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/Upload'
multipart/form-data:
schema:
$ref: '#/components/schemas/Upload'
required: true
security:
- cookieAuth: []
- basicAuth: []
- {}
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Upload'
description: ''
components:
schemas:
Upload:
type: object
properties:
id:
type: integer
readOnly: true
upload_at:
type: string
format: date-time
readOnly: true
file:
type: string
format: uri
required:
- file
- id
- upload_at
securitySchemes:
basicAuth:
type: http
scheme: basic
cookieAuth:
type: apiKey
in: cookie
name: sessionid

0 comments on commit a877890

Please sign in to comment.