Exemple #1
0
    def update(self, instance, validated_data):
        data = validated_data.copy()
        interests_data = data.pop('interests')
        user = instance
        tag_ids=[]

        # update profile except interests
        s = ProfileSerializerBase(instance, data=data, context=self.context, partial=True)
        if s.is_valid():
            s.update(instance, s.validated_data)

        # update interests
        for interest in interests_data:
            tag = Tag.objects.filter(label=interest["label"])
            if tag.exists():
                tag = tag.first()
                t = user.interests.filter(id=tag.id)
                if t.exists():
                    tag_ids.append(t.first().id)
                    continue
            else:
                serializer = TagSerializer(data=interest)
                if serializer.is_valid():
                    tag = serializer.create(serializer.validated_data)
            tag_ids.append(tag.id)
            user.interests.add(tag)

        for tag in user.interests.all():
            if tag.id not in tag_ids:
                user.interests.remove(tag)

        user.save()
        return user
Exemple #2
0
    def create(self, validated_data):
        data = validated_data.copy()
        tags_data = data.pop("tags")
        content = Content.objects.create(
            owner=self.context["request"].user,
            content_type_id=data["content_type_id"])
        for comp_data in data["components"]:
            serializer = ComponentSerializer2(data=comp_data,
                                              context=self.context)
            if serializer.is_valid():
                comp = serializer.create(serializer.validated_data)
                comp.save()
                content.components.add(comp)
        content.save()

        for tag_data in tags_data:
            tag = Tag.objects.filter(label=tag_data["label"])
            if tag.exists():
                tag = tag.first()
            else:
                serializer = TagSerializer(data=tag_data)
                if serializer.is_valid():
                    tag = serializer.create(serializer.validated_data)
            content.tags.add(tag)
        content.save()
        return content
Exemple #3
0
 def create(self, validated_data):
     data = validated_data.copy()
     interests_data = data.pop('interests')
     user = UserProfile.objects.create(**data)
     for interest in interests_data:
         tag = Tag.objects.filter(label=interest["label"])
         if tag.exists():
             tag = tag.first()
         else:
             serializer = TagSerializer(data=interest)
             if serializer.is_valid():
                 tag = serializer.create(serializer.validated_data)
         user.interests.add(tag)
     user.save()
     return user
Exemple #4
0
    def update(self, instance, validated_data):
        print("---update---")
        data = validated_data.copy()

        instance.owner_id = data.get("owner_id", instance.owner_id)
        instance.content_type_id = data.get("content_type_id",
                                            instance.content_type_id)
        instance.save()

        if "components" in data:
            for comp_data in data["components"]:
                comp = instance.components.filter(
                    order=comp_data["order"]).first()
                serializer = ComponentSerializer2(comp,
                                                  many=False,
                                                  data=comp_data,
                                                  context=self.context)
                if serializer.is_valid():
                    comp = serializer.update(comp, serializer.validated_data)
        instance.save()

        print(data)

        if "tags" in data:
            tag_ids = []
            print("we have some tags")
            tags_data = data["tags"]
            for tag_data in tags_data:
                tag = Tag.objects.filter(label=tag_data["label"])
                if tag.exists():
                    tag = tag.first()
                    t = instance.tags.filter(id=tag.id)
                    if t.exists():
                        tag_ids.append(tag.id)
                        continue
                else:
                    serializer = TagSerializer(data=tag_data)
                    if serializer.is_valid():
                        tag = serializer.create(serializer.validated_data)
                tag_ids.append(tag.id)
                instance.tags.add(tag)
            for tag in instance.tags.all():
                if tag.id not in tag_ids:
                    instance.tags.remove(tag)
        instance.save()
        return instance
Exemple #5
0
 def update(self, instance, validated_data):
     data = validated_data.copy()
     interests_data = data.pop('interests')
     print(interests_data)
     user = instance
     for interest in interests_data:
         tag = Tag.objects.filter(label=interest["label"])
         if tag.exists():
             tag = tag.first()
             t = user.interests.filter(id=tag.id)
             if t.exists():
                 continue
         else:
             serializer = TagSerializer(data=interest)
             if serializer.is_valid():
                 tag = serializer.create(serializer.validated_data)
         user.interests.add(tag)
     user.save()
     return user