Skip to content

Commit

Permalink
Merge pull request #22 from healthy-food-and-dietary-products/fix/use…
Browse files Browse the repository at this point in the history
…rs_and_address

Fix/users and address
  • Loading branch information
earlinn authored Oct 20, 2023
2 parents 0d079ac + b88b3c1 commit 2486eb7
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 51 deletions.
24 changes: 19 additions & 5 deletions backend/orders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@ class ShoppingCartAdmin(admin.ModelAdmin):

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
list_display = ("id", "user", "goods",
"date", "status", "payment_method", "is_paid",
"delivery_method", "comment", "total_price")
list_editable = ("status", "payment_method",
"is_paid", "comment", "delivery_method")
list_display = (
"id",
"user",
"goods",
"date",
"status",
"payment_method",
"is_paid",
"delivery_method",
"comment",
"total_price",
)
list_editable = (
"status",
"payment_method",
"is_paid",
"comment",
"delivery_method",
)
4 changes: 2 additions & 2 deletions backend/orders/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Generated by Django 4.2.6 on 2023-10-19 14:43

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
Expand Down
5 changes: 3 additions & 2 deletions backend/products/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Generated by Django 4.2.6 on 2023-10-19 12:24

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

import products.models


Expand Down
1 change: 0 additions & 1 deletion backend/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class AddressAdmin(admin.ModelAdmin):
"city",
"street",
"house",
"building",
"apartment",
]

Expand Down
57 changes: 46 additions & 11 deletions backend/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.6 on 2023-10-19 04:01
# Generated by Django 4.2.6 on 2023-10-19 19:54

import django.contrib.auth.models
import django.core.validators
Expand Down Expand Up @@ -103,7 +103,7 @@ class Migration(migrations.Migration):
),
(
"city",
models.CharField(blank=True, max_length=30, verbose_name="City"),
models.CharField(blank=True, max_length=50, verbose_name="City"),
),
(
"birth_date",
Expand All @@ -114,7 +114,7 @@ class Migration(migrations.Migration):
models.IntegerField(
default=0,
validators=[django.core.validators.MaxValueValidator(5)],
verbose_name="number_of_cities",
verbose_name="number_of_adresses",
),
),
(
Expand Down Expand Up @@ -156,16 +156,49 @@ class Migration(migrations.Migration):
),
),
("country", models.CharField(max_length=100, verbose_name="Country")),
("region", models.CharField(max_length=200, verbose_name="Region")),
(
"city_type",
models.CharField(max_length=100, verbose_name="City_type"),
),
("city", models.CharField(max_length=100, verbose_name="City")),
("street", models.CharField(max_length=100, verbose_name="Street")),
("house", models.IntegerField(verbose_name="House_number")),
(
"building",
"microdistrict",
models.CharField(
blank=True,
max_length=150,
null=True,
verbose_name="Microdistrict",
),
),
(
"street_type",
models.CharField(
blank=True,
max_length=100,
null=True,
verbose_name="Street_type",
),
),
(
"street",
models.CharField(
blank=True, max_length=100, null=True, verbose_name="Street"
),
),
("house", models.CharField(max_length=40, verbose_name="House_number")),
(
"apartment",
models.IntegerField(
blank=True, null=True, verbose_name="Buiding_number"
blank=True, null=True, verbose_name="Apartment_number"
),
),
(
"postal_code",
models.CharField(
blank=True, max_length=6, null=True, verbose_name="Postal_code"
),
),
("apartment", models.IntegerField(verbose_name="Apartment_number")),
],
options={
"verbose_name": "Address",
Expand All @@ -175,15 +208,17 @@ class Migration(migrations.Migration):
migrations.AddConstraint(
model_name="address",
constraint=models.UniqueConstraint(
fields=("city", "street", "house", "building", "apartment"),
name="unique address",
fields=("city", "street", "house", "apartment"), name="unique address"
),
),
migrations.AddField(
model_name="user",
name="address",
field=models.ManyToManyField(
related_name="users", to="users.address", verbose_name="Addresses"
blank=True,
related_name="users",
to="users.address",
verbose_name="Addresses",
),
),
migrations.AddField(
Expand Down
81 changes: 51 additions & 30 deletions backend/users/models.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,83 @@
from django.contrib.auth.models import AbstractUser
from django.core import validators
from django.core.exceptions import ValidationError
from django.core.validators import MaxValueValidator
from django.db import models
from django.utils import timezone
from phonenumber_field.modelfields import PhoneNumberField


class Address(models.Model):
"""Describes address of user."""

country = models.CharField(
"Country",
null=False,
blank=False,
max_length=100,
)
region = models.CharField(
"Region",
max_length=200,
)
city_type = models.CharField(
"City_type",
max_length=100,
)
city = models.CharField(
"City",
null=False,
blank=False,
max_length=100,
)
microdistrict = models.CharField(
"Microdistrict",
null=True,
blank=True,
max_length=150,
)
street_type = models.CharField(
"Street_type",
max_length=100,
null=True,
blank=True,
)
street = models.CharField(
"Street",
null=False,
blank=False,
max_length=100,
null=True,
blank=True,
)
house = models.IntegerField(
house = models.CharField(
"House_number",
null=False,
blank=False,
max_length=40,
)
building = models.IntegerField(
"Buiding_number",
apartment = models.IntegerField(
"Apartment_number",
null=True,
blank=True,
)
apartment = models.IntegerField(
"Apartment_number",
null=False,
blank=False,
postal_code = models.CharField(
"Postal_code",
max_length=6,
null=True,
blank=True,
)

def __str__(self):
return (
f"{self.country}, {self.city}, {self.street}, "
f"{self.house}{self.building}, {self.apartment}"
f"{self.house}, {self.apartment}"
)

class Meta:
verbose_name = "Address"
verbose_name_plural = "Addresses"
constraints = [
models.UniqueConstraint(
fields=["city", "street", "house", "building", "apartment"],
fields=["city", "street", "house", "apartment"],
name="unique address",
)
]


class User(AbstractUser):
"""
Расширение встроенной модели User."""
"""Extending the Built-in Model User."""

USER = "user"
MODERATOR = "moderator"
Expand All @@ -76,25 +93,24 @@ def user_directory_path(self, filename):
"""Constructs the path which the users photo will be saved."""
return f"images/{self.username}"

username: str = models.CharField(
username = models.CharField(
"Username",
unique=True,
max_length=150,
)
email: str = models.EmailField(
email = models.EmailField(
"E-mail address",
unique=True,
blank=False,
max_length=254,
)
role: str = models.CharField(
role = models.CharField(
max_length=9,
choices=CHOISES,
default="user",
)
city: str = models.CharField(
city = models.CharField(
"City",
max_length=30,
max_length=50,
blank=True,
)
birth_date = models.DateField(
Expand All @@ -104,13 +120,14 @@ def user_directory_path(self, filename):
)
address = models.ManyToManyField(
Address,
blank=True,
related_name="users",
verbose_name="Addresses",
)
address_quantity = models.IntegerField(
"number_of_cities",
"number_of_adresses",
default=0,
validators=[MaxValueValidator(5)],
validators=[validators.MaxValueValidator(5)],
)
phone_number = PhoneNumberField(
"Phone_number",
Expand All @@ -133,8 +150,12 @@ def clean_fields(self, exclude=None):
super().clean_fields(exclude=exclude)

now = timezone.now()
if self.birth_date > now or (now.year - self.birth_date.year) > 120:
raise ValidationError("Введен неверный возраст.")
if self.birth_date:
if (
self.birth_date.year > now.year
or (now.year - self.birth_date.year) > 120
):
raise ValidationError("Указана неверная дата рождения.")

@property
def is_moderator(self):
Expand Down

0 comments on commit 2486eb7

Please sign in to comment.