Commit db6fb61e authored by Давид Ли's avatar Давид Ли

web

parent 59608255
...@@ -2,8 +2,21 @@ from django.contrib.auth.models import AbstractUser ...@@ -2,8 +2,21 @@ from django.contrib.auth.models import AbstractUser
from django.db import models from django.db import models
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
class UserManager(models.Manager):
def all(self):
return super().all().filter(is_active=True)
def restore(self):
return self.get_queryset().update(is_active=True)
def delete(self):
# Article.objects.delete()
return self.get_queryset().update(is_active=False)
class User(AbstractUser): class User(AbstractUser):
pass objects = UserManager()
class Profile(models.Model): class Profile(models.Model):
......
...@@ -12,6 +12,8 @@ https://docs.djangoproject.com/en/3.2/ref/settings/ ...@@ -12,6 +12,8 @@ https://docs.djangoproject.com/en/3.2/ref/settings/
import os import os
from pathlib import Path from pathlib import Path
from faker import Faker
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
...@@ -145,3 +147,5 @@ AUTH_USER_MODEL = 'accounts.User' ...@@ -145,3 +147,5 @@ AUTH_USER_MODEL = 'accounts.User'
LOGIN_URL = 'login' LOGIN_URL = 'login'
# LOGIN_REDIRECT_URL = 'main_page' # LOGIN_REDIRECT_URL = 'main_page'
# LOGOUT_REDIRECT_URL = 'main_page' # LOGOUT_REDIRECT_URL = 'main_page'
faker = Faker()
import random
from django.db import models from django.db import models
from django.db.models import TextChoices from django.db.models import TextChoices
from django.core.validators import MinLengthValidator from django.core.validators import MinLengthValidator
from core.settings import faker
class StatusChoices(TextChoices): class StatusChoices(TextChoices):
ACCEPTED = 'accepted', 'Разрешено' ACCEPTED = 'accepted', 'Разрешено'
...@@ -9,9 +13,41 @@ class StatusChoices(TextChoices): ...@@ -9,9 +13,41 @@ class StatusChoices(TextChoices):
CHECK = 'check', 'На проверке' CHECK = 'check', 'На проверке'
class ArticlesManager(models.Manager):
def all(self, *args, **kwargs):
# Article.objects.all() -> Article.objects.filter(status='accepted')
return super().all().filter(status='accepted')
# Article.objects.all().filter(status='accepted')
def create_random_tags(self):
from web.models import Tag
random_count = random.randint(5, 15)
for obj in self.get_queryset():
obj.tags.add(
*(
Tag(name=faker.company()).save()
for _ in range(random_count)
)
)
def create_random_comments(self):
from web.models import Comment
random_count = random.randint(5, 15)
for obj in self.get_queryset():
obj.comments.bulk_create(
(
Comment(text=faker.text(), article_id=obj.id)
for _ in range(random_count)
)
)
class Article(models.Model): class Article(models.Model):
# CharField = varchar objects = ArticlesManager()
# title varchar(200) not null
title = models.CharField( title = models.CharField(
max_length=200, max_length=200,
null=False, # <null> null=False, # <null>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment