# Path: exampleproject/articles/models.py
# class Article(SingleTenantModelMixin):
#     title = models.CharField(max_length=100)
#     text = models.TextField()
#     tags = models.ManyToManyField('Tag')
#     author = models.ForeignKey(settings.AUTH_USER_MODEL)
# 
#     def __str__(self):
#         return '%s - %s' % (self.title, str(self.author))
# 
# class Tag(MultipleTenantsModelMixin):
#     text = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.text
# 
# Path: exampleproject/articles/serializers.py
# class ArticleSerializer(serializers.ModelSerializer):
#     tags = TagSerializer(many=True, required=False, allow_null=True)
#     author = UserSerializer(read_only=True)
# 
#     class Meta:
#         model = Article
#         fields = ('id', 'title', 'text', 'tags', 'author')
# 
#     def validate_tags(self, tags_dict_list):
#         tags_creators = []
#         has_errors = False
#         errors = []
#         for tag_dict in tags_dict_list:
#             if not tag_dict.get('id'):
#                 serializer = TagSerializer(data=tag_dict)
#             else:
#                 instance = Tag.objects.get(id=tag_dict.get('id'))
#                 serializer = TagSerializer(instance, data=tag_dict)
# 
#             if serializer.is_valid():
#                 tags_creators.append(lambda: serializer.save())
#                 errors.append({})
#             else:
#                 has_errors = True
#                 errors.append(serializer.errors)
# 
#         if has_errors:
#             raise serializers.ValidationError(errors)
# 
#         return tags_creators
# 
#     def create(self, validated_data):
#         tag_creators = validated_data.pop('tags', [])
#         create_data = dict(
#             validated_data,
#             **{'author': self.context['request'].user}
#         )
#         with transaction.atomic():
#             tags = [tag_creator() for tag_creator in tag_creators]
#             article = super(ArticleSerializer, self).create(create_data)
#             article.tags.set(tags)
#             return article
# 
#     def update(self, instance, validated_data):
#         tag_creators = validated_data.pop('tags', [])
#         create_data = dict(
#             validated_data,
#             **{'author': self.context['request'].user}
#         )
# 
#         with transaction.atomic():
#             tags = [tag_creator() for tag_creator in tag_creators]
#             for field_name in Article._meta.get_all_field_names():
#                 setattr(instance, field_name, create_data.get(field_name, getattr(instance, field_name)))
# 
#             instance.tags.set(tags)
#             return instance
# 
# class TagSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = Tag
#         fields = ('id', 'text',)
# 
#     def create(self, validated_data):
#         tag = Tag.objects.filter(**validated_data).first()
# 
#         if tag:
#             return tag
# 
#         return Tag.objects.create(**validated_data)


from rest_framework import viewsets, permissions
from .models import Article, Tag
from .serializers import ArticleSerializer, TagSerializer


class ArticleViewSet(viewsets.ModelViewSet):
    serializer_class = ArticleSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return Article.objects.all()


class TagViewSet(viewsets.ModelViewSet):
    serializer_class = TagSerializer
    permission_classes = [permissions.IsAuthenticated]


# Path: exampleproject/articles/models.py
# class Article(SingleTenantModelMixin):
#     title = models.CharField(max_length=100)
#     text = models.TextField()
#     tags = models.ManyToManyField('Tag')
#     author = models.ForeignKey(settings.AUTH_USER_MODEL)
# 
#     def __str__(self):
#         return '%s - %s' % (self.title, str(self.author))
# 
# class Tag(MultipleTenantsModelMixin):
#     text = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.text
# 
# Path: exampleproject/articles/serializers.py
# class ArticleSerializer(serializers.ModelSerializer):
#     tags = TagSerializer(many=True, required=False, allow_null=True)
#     author = UserSerializer(read_only=True)
# 
#     class Meta:
#         model = Article
#         fields = ('id', 'title', 'text', 'tags', 'author')
# 
#     def validate_tags(self, tags_dict_list):
#         tags_creators = []
#         has_errors = False
#         errors = []
#         for tag_dict in tags_dict_list:
#             if not tag_dict.get('id'):
#                 serializer = TagSerializer(data=tag_dict)
#             else:
#                 instance = Tag.objects.get(id=tag_dict.get('id'))
#                 serializer = TagSerializer(instance, data=tag_dict)
# 
#             if serializer.is_valid():
#                 tags_creators.append(lambda: serializer.save())
#                 errors.append({})
#             else:
#                 has_errors = True
#                 errors.append(serializer.errors)
# 
#         if has_errors:
#             raise serializers.ValidationError(errors)
# 
#         return tags_creators
# 
#     def create(self, validated_data):
#         tag_creators = validated_data.pop('tags', [])
#         create_data = dict(
#             validated_data,
#             **{'author': self.context['request'].user}
#         )
#         with transaction.atomic():
#             tags = [tag_creator() for tag_creator in tag_creators]
#             article = super(ArticleSerializer, self).create(create_data)
#             article.tags.set(tags)
#             return article
# 
#     def update(self, instance, validated_data):
#         tag_creators = validated_data.pop('tags', [])
#         create_data = dict(
#             validated_data,
#             **{'author': self.context['request'].user}
#         )
# 
#         with transaction.atomic():
#             tags = [tag_creator() for tag_creator in tag_creators]
#             for field_name in Article._meta.get_all_field_names():
#                 setattr(instance, field_name, create_data.get(field_name, getattr(instance, field_name)))
# 
#             instance.tags.set(tags)
#             return instance
# 
# class TagSerializer(serializers.ModelSerializer):
# 
#     class Meta:
#         model = Tag
#         fields = ('id', 'text',)
# 
#     def create(self, validated_data):
#         tag = Tag.objects.filter(**validated_data).first()
# 
#         if tag:
#             return tag
# 
#         return Tag.objects.create(**validated_data)


# Path: exampleproject/articles/views.py
from rest_framework import viewsets, permissions
from .models import Article, Tag
from .serializers import ArticleSerializer, TagSerializer



class ArticleViewSet(viewsets.ModelViewSet):
    serializer_class = ArticleSerializer
    permission_classes = [permissions.IsAuthenticated]

    def get_queryset(self):
        return Article.objects.all()


class TagViewSet(viewsets.ModelViewSet):
    serializer_class = TagSerializer
    permission_classes = [permissions.IsAuthenticated]


    def get_queryset(self):