Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CustomUser(AbstractBaseUser, PermissionsMixin):
- """Расширенный пользователь"""
- phone_regex = RegexValidator(
- regex=r'^\+?1?\d{9,15}$',
- message="Phone number must be entered in the format."
- )
- email = models.EmailField(verbose_name='Email', unique=True)
- date_of_birth = models.DateField(verbose_name='Дата рождения', blank=True, null=True)
- first_name = models.CharField(verbose_name='Имя', max_length=25, unique=True)
- second_name = models.CharField(verbose_name='Фамилия', max_length=25, unique=True)
- patronymic = models.CharField(verbose_name='Отчество', max_length=25, unique=True)
- username = models.CharField(verbose_name='Пользователь', max_length=25, unique=True)
- date_joined = models.DateTimeField(verbose_name='Дата регистрации', auto_now_add=True)
- phone = models.CharField(verbose_name='Мобильный номер телефона', validators=[phone_regex], max_length=17, blank=True)
- is_active = models.BooleanField(verbose_name='Статус активности', default=True)
- is_staff = models.BooleanField(verbose_name='Является ли сотрудником', default=False)
- hide_personal_data = models.BooleanField(verbose_name='Скрыть персональные данные:', default=False, blank=True)
- user_photo = models.ImageField(verbose_name='Фото профиля', upload_to='users/personal-img/', null=True, blank=True)
- gallery_collections = models.ManyToManyField('GalleryRef', blank=True, related_name='user_gallery_collections')
- objects = UserManager()
- USERNAME_FIELD = 'username'
- REQUIRED_FIELDS = []
- def __str__(self):
- return self.username
- def to_json(self):
- return model_to_dict(self)
- __repr__ = __str__
- class Meta:
- db_table = 'user'
- verbose_name = 'Пользователь'
- verbose_name_plural = 'Пользователи'
- class Notifications(models.Model):
- """Уведомления пользователя"""
- user = models.ForeignKey(CustomUser, verbose_name='Пользователь', on_delete=models.CASCADE)
- pub_date = models.DateTimeField(verbose_name='Время уведомления', auto_now_add=True)
- text = models.TextField(verbose_name='Уведомление:')
- title = models.CharField(verbose_name='Заголовок уведомления: ', max_length=255, blank=True)
- is_read = models.BooleanField(verbose_name='Прочитано', default=False)
- def __str__(self):
- return self.title
- def to_json(self):
- return model_to_dict(self)
- __repr__ = __str__
- class Meta:
- db_table = 'notifications'
- class ResourceRating(models.Model):
- """"""
- class QualityСontrol(models.TextChoices):
- LIKE = 'like', 'Нравиться'
- DISLIKE = 'dislike', 'Не нравится'
- user = models.ForeignKey(CustomUser, verbose_name='Пользователь', on_delete=models.CASCADE )
- quality_control = models.CharField(verbose_name='Оценка качества', max_length=12, choices=QualityСontrol.choices, null=False)
- def to_json(self):
- return model_to_dict(self)
- class Meta:
- db_table = 'resource_rating'
- class Analytics(models.Model):
- content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
- object_id = models.PositiveIntegerField()
- content_object = GenericForeignKey('content_type', 'object_id')
- resource_rating = models.ManyToManyField(ResourceRating)
- class Meta:
- db_table = 'analytics'
- def to_json(self):
- return model_to_dict(self)
- class UserSourceAnalytics(models.Model):
- """"""
- views = models.BigIntegerField(verbose_name='Просмотры ресурса', default=0)
- tags = GenericRelation('Analytics')
- def __str__(self):
- return self.project.project_name
- def to_json(self):
- return model_to_dict(self)
- __repr__ = __str__
- class Meta:
- abstract = True
- class GalleryCollection(models.Model):
- """"""
- gallery = models.ImageField(verbose_name='Картина', upload_to='users/gallery-img/', null=True, blank=True)
- def to_json(self):
- return model_to_dict(self)
- class Meta:
- db_table = 'gallery_collection'
- verbose_name = 'Галерея'
- verbose_name_plural = 'Галерея'
- class GalleryRef(models.Model):
- """"""
- gallery_collection = models.ForeignKey(GalleryCollection, on_delete=models.CASCADE,verbose_name='Галерея', related_name='user_gallery_collection')
- user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, verbose_name='Пользователь')
- description = models.TextField('Описание', null=True, blank=True)
- public_date = models.DateTimeField(verbose_name='Дата публикации', auto_now_add=True)
- draft = models.BooleanField(verbose_name='Черновик', default=True)
- def __str__(self):
- return f'{self.user}'
- def to_json(self):
- return model_to_dict(self)
- __repr__ = __str__
- class Meta:
- db_table = 'gallery_ref'
- class GalleryCollectionSerializer(serializers.ModelSerializer):
- class Meta:
- model = GalleryCollection
- fields = '__all__'
- class GalleryRefSerializer(serializers.ModelSerializer):
- gallery_collection = GalleryCollectionSerializer(
- read_only=True,
- )
- class Meta:
- model = GalleryRef
- fields = (
- 'id', 'public_date', 'description', 'draft', 'gallery_collection',
- )
- class CustomUserSerializer(serializers.ModelSerializer):
- gallery_collections = GalleryRefSerializer(
- read_only=True, many=False
- )
- class Meta:
- model = CustomUser
- fields = (
- 'id', 'email', 'date_of_birth', 'first_name',
- 'second_name', 'patronymic', 'username',
- 'date_joined', 'phone', 'is_active',
- 'is_staff', 'hide_personal_data', 'user_photo',
- 'gallery_collections',
- )
- class GalleryCollectionSerializerViewSet(viewsets.ViewSet):
- def list(self, request):
- queryset = GalleryCollection.objects.all()
- serializer = GalleryCollectionSerializer(queryset, many=True)
- print(serializer.data)
- return Response(serializer.data)
- class GalleryRefSerializerViewSet(viewsets.ViewSet):
- def list(self, request):
- queryset = GalleryRef.objects.all()
- serializer = GalleryRefSerializer(queryset, many=True)
- print(serializer.data)
- return Response(serializer.data)
- class CustomUserSerializerViewSet(viewsets.ViewSet):
- def list(self, request):
- queryset = CustomUser.objects.all()
- serializer = CustomUserSerializer(queryset, many=True)
- print(serializer.data)
- return Response(serializer.data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement