def remove_image_set_tag(request) -> Response: try: image_set_id = int(request.data['image_set_id']) tag_name = str(request.data['tag_name']).lower() except (KeyError, TypeError, ValueError): raise ParseError image_set = get_object_or_404(ImageSet, pk=image_set_id) tag = get_object_or_404(SetTag, name=tag_name) if not image_set.has_perm('edit_set', request.user): return Response( { 'detail': 'permission for tagging this image set missing.', }, status=HTTP_403_FORBIDDEN) if tag not in image_set.set_tags.all(): return Response({ 'detail': 'tag not in imageset tags', }, status=HTTP_200_OK) tag.imagesets.remove(image_set) serializer = SetTagSerializer(tag) serializer_data = serializer.data if not tag.imagesets.exists() and tag.name != 'test': tag.delete() else: tag.save() return Response({ 'detail': 'removed the tag.', 'tag': serializer_data, }, status=HTTP_201_CREATED)
def tag_image_set(request) -> Response: try: image_set_id = int(request.data['image_set_id']) tag_name = str(request.data['tag_name']).lower() except (KeyError, TypeError, ValueError): raise ParseError image_set = get_object_or_404(ImageSet, pk=image_set_id) char_blacklist = [',', '&', '=', '?'] for char in char_blacklist: tag_name = tag_name.replace(char, '') tag_name = tag_name.replace(' ', '_') if not image_set.has_perm('edit_set', request.user): return Response( { 'detail': 'permission for tagging this image set missing.', }, status=HTTP_403_FORBIDDEN) if image_set.set_tags.filter(name=tag_name).exists(): return Response({ 'detail': 'imageset has the tag already.', }, status=HTTP_200_OK) # TODO: validate the name? # TODO: this in better? if SetTag.objects.filter(name=tag_name).exists(): tag = SetTag.objects.get(name=tag_name) else: tag = SetTag(name=tag_name) # TODO this in better? tag.save() tag.imagesets.add(image_set) tag.save() serializer = SetTagSerializer(tag) return Response( { 'detail': 'tagged the imageset.', 'tag': serializer.data, }, status=HTTP_201_CREATED)