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

web

parent 59608255
......@@ -2,8 +2,21 @@ from django.contrib.auth.models import AbstractUser
from django.db import models
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):
pass
objects = UserManager()
class Profile(models.Model):
......
......@@ -12,6 +12,8 @@ https://docs.djangoproject.com/en/3.2/ref/settings/
import os
from pathlib import Path
from faker import Faker
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
......@@ -145,3 +147,5 @@ AUTH_USER_MODEL = 'accounts.User'
LOGIN_URL = 'login'
# LOGIN_REDIRECT_URL = 'main_page'
# LOGOUT_REDIRECT_URL = 'main_page'
faker = Faker()
import random
from django.db import models
from django.db.models import TextChoices
from django.core.validators import MinLengthValidator
from core.settings import faker
class StatusChoices(TextChoices):
ACCEPTED = 'accepted', 'Разрешено'
......@@ -9,9 +13,41 @@ class StatusChoices(TextChoices):
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):
# CharField = varchar
# title varchar(200) not null
objects = ArticlesManager()
title = models.CharField(
max_length=200,
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