|
| 1 | +from enum import Enum |
| 2 | +from typing import List, Optional |
| 3 | +from labelbox.orm.db_object import DbObject, Updateable |
| 4 | +from labelbox.orm.model import Field |
| 5 | +from pydantic import BaseModel |
| 6 | + |
| 7 | + |
| 8 | +class ResourceTagType(Enum): |
| 9 | + """Enum for resource tag types.""" |
| 10 | + Default = "Default" |
| 11 | + System = "System" |
| 12 | + Request = "Request" |
| 13 | + Migration = "Migration" |
| 14 | + Billing = "Billing" |
| 15 | + |
| 16 | + |
| 17 | +class CreateResourceTagInput(BaseModel): |
| 18 | + """Input for creating a new resource tag.""" |
| 19 | + |
| 20 | + text: str |
| 21 | + color: str |
| 22 | + type: Optional[str] = None |
| 23 | + |
| 24 | + |
| 25 | +class UpdateResourceTagInput(BaseModel): |
| 26 | + """Input for updating a resource tag.""" |
| 27 | + |
| 28 | + id: str |
| 29 | + text: str |
| 30 | + color: str |
| 31 | + type: Optional[str] = None |
| 32 | + |
| 33 | + |
| 34 | +class DeleteResourceTagInput(BaseModel): |
| 35 | + """Input for deleting a resource tag.""" |
| 36 | + |
| 37 | + id: str |
| 38 | + type: Optional[str] = None |
| 39 | + |
| 40 | + |
| 41 | +class ResourceTagsInput(BaseModel): |
| 42 | + """Input for querying resource tags.""" |
| 43 | + |
| 44 | + type: str |
| 45 | + |
| 46 | + |
| 47 | +class EnhancedResourceTag(DbObject, Updateable): |
| 48 | + """Enhanced resource tag with additional functionality and type support.""" |
| 49 | + |
| 50 | + # Fields matching the DDL schema |
| 51 | + id = Field.String("id") |
| 52 | + createdAt = Field.DateTime("createdAt") |
| 53 | + updatedAt = Field.DateTime("updatedAt") |
| 54 | + organizationId = Field.String("organizationId") |
| 55 | + text = Field.String("text") |
| 56 | + color = Field.String("color") |
| 57 | + createdById = Field.String("createdById") |
| 58 | + type = Field.String("type") |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def create( |
| 62 | + cls, client, text: str, color: str, tag_type: Optional[ResourceTagType] = None |
| 63 | + ) -> "EnhancedResourceTag": |
| 64 | + """Create a new enhanced resource tag. |
| 65 | +
|
| 66 | + Args: |
| 67 | + client: Labelbox client instance |
| 68 | + text: Text content of the resource tag |
| 69 | + color: Color of the resource tag |
| 70 | + tag_type: Optional type of the resource tag |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Created EnhancedResourceTag instance |
| 74 | + """ |
| 75 | + # Use the existing organization create_resource_tag method |
| 76 | + # Get the organization |
| 77 | + org = client.get_organization() |
| 78 | + |
| 79 | + # Create the tag using existing API |
| 80 | + tag_data = {"text": text, "color": color} |
| 81 | + created_tag = org.create_resource_tag(tag_data) |
| 82 | + |
| 83 | + # Create EnhancedResourceTag with the same data plus defaults for missing fields |
| 84 | + enhanced_tag = cls(client, { |
| 85 | + "id": created_tag.uid, |
| 86 | + "text": created_tag.text, |
| 87 | + "color": created_tag.color, |
| 88 | + "createdAt": None, |
| 89 | + "updatedAt": None, |
| 90 | + "organizationId": None, |
| 91 | + "createdById": None, |
| 92 | + "type": tag_type.value if tag_type else None |
| 93 | + }) |
| 94 | + |
| 95 | + return enhanced_tag |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + @classmethod |
| 100 | + def search_by_text( |
| 101 | + cls, client, search_text: str, tag_type: ResourceTagType |
| 102 | + ) -> List["EnhancedResourceTag"]: |
| 103 | + """Search resource tags by text content. |
| 104 | +
|
| 105 | + Args: |
| 106 | + client: Labelbox client instance |
| 107 | + search_text: Text to search for |
| 108 | + tag_type: Type filter |
| 109 | +
|
| 110 | + Returns: |
| 111 | + List of matching EnhancedResourceTag instances |
| 112 | + """ |
| 113 | + # Use the existing organization get_resource_tags method |
| 114 | + # Get the organization |
| 115 | + org = client.get_organization() |
| 116 | + |
| 117 | + # Get all resource tags |
| 118 | + regular_tags = org.get_resource_tags() |
| 119 | + |
| 120 | + # Convert to EnhancedResourceTag instances and filter by search text and type |
| 121 | + matching_tags = [] |
| 122 | + for tag in regular_tags: |
| 123 | + if search_text.lower() in tag.text.lower(): |
| 124 | + enhanced_tag = cls(client, { |
| 125 | + "id": tag.uid, |
| 126 | + "text": tag.text, |
| 127 | + "color": tag.color, |
| 128 | + "createdAt": None, |
| 129 | + "updatedAt": None, |
| 130 | + "organizationId": None, |
| 131 | + "createdById": None, |
| 132 | + "type": tag_type.value |
| 133 | + }) |
| 134 | + |
| 135 | + # Apply type filter |
| 136 | + if enhanced_tag.type == tag_type.value: |
| 137 | + matching_tags.append(enhanced_tag) |
| 138 | + |
| 139 | + return matching_tags |
0 commit comments