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
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
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
def to_internal_value(self, data): data = data.copy() interests_data = data.pop("interests") validated_data = super(ProfileSerializer,self).to_internal_value(data) validated_data["interests"] = [] for interest in interests_data: t = Tag.objects.filter(label=interest["label"]) if t.exists(): validated_data["interests"].append(interest) else: serializer = TagSerializer(data = interest) if serializer.is_valid(): validated_data["interests"].append(serializer.validated_data) else: ValidationError(serializer.errors) return validated_data
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
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
def to_internal_value(self, data): data = data.copy() validated_data = OrderedDict() print("to_internal_value") try: if not ContentType.objects.filter( id=data['content_type_id']).exists(): raise serializers.ValidationError( "No content type with given content_type_id.") content_type = ContentType.objects.get(pk=data['content_type_id']) validated_data["content_type_id"] = data["content_type_id"] validated_data["components"] = [] if len(data["components"]) != len(content_type.components): raise serializers.ValidationError( "Number of components does not match with content type.") for component in data["components"]: print('comp:', component) serializer = ComponentSerializer2(data=component, context=self.context) if not serializer.is_valid(): print("ehehe") print(serializer.errors) error = serializer.errors raise serializers.ValidationError(error) if component["component_type"] != content_type.components[ component["order"] - 1]: print("ohoho") raise serializers.ValidationError( "Order of the components does not match with content type" ) print("end") validated_data["components"].append(serializer.validated_data) except Exception as e: if not self.partial: raise serializers.ValidationError(str(e)) # print("to_internal_value") try: if "tags" in data: validated_data["tags"] = [] for tag in data["tags"]: t = Tag.objects.filter(label=tag["label"]) if t.exists(): validated_data["tags"].append(tag) else: serializer = TagSerializer(data=tag) if serializer.is_valid(): validated_data["tags"].append( serializer.validated_data) else: ValidationError(serializer.errors) except Exception as e: if not self.partial: raise serializers.ValidationError(str(e)) return validated_data