prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Cannot use the same shortcut and label name in different projects
  The issue is the following:
  
  1. I create Project A.
  2. Import some annotated data to the project - set of labels is automatically created as a result (let's say, Label 1, Label 2, Label 3)
  3. I edit these labels - provide a shortcut and color
  4. I create Project B and import another portion of dataset (with the same set of labels)
  5. I try to replicate setting of Project A (assign the same shortcuts, colors and label names to labels), which gives me an error:
  `You cannot use same label name or shortcut key.`
  
  It seems not very convenient that we cannot create labels with the same shortcut and label name in different project. I wonder if it was made intentionally or it is a bug.
  
  Thank you very much for a great tool.
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `app/server/serializers.py`
  Content:
  ```
  1 from django.contrib.auth import get_user_model
  2 from rest_framework import serializers
  3 from rest_polymorphic.serializers import PolymorphicSerializer
  4 from rest_framework.exceptions import ValidationError
  5 
  6 
  7 from .models import Label, Project, Document
  8 from .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject
  9 from .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation
  10 
  11 
  12 class UserSerializer(serializers.ModelSerializer):
  13 
  14     class Meta:
  15         model = get_user_model()
  16         fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser')
  17 
  18 
  19 class LabelSerializer(serializers.ModelSerializer):
  20 
  21     def validate(self, attrs):
  22         if 'prefix_key' not in attrs and 'suffix_key' not in attrs:
  23             return super().validate(attrs)
  24 
  25         prefix_key = attrs['prefix_key']
  26         suffix_key = attrs['suffix_key']
  27 
  28         # In the case of user don't set any shortcut key.
  29         if prefix_key is None and suffix_key is None:
  30             return super().validate(attrs)
  31 
  32         # Don't allow shortcut key not to have a suffix key.
  33         if prefix_key and not suffix_key:
  34             raise ValidationError('Shortcut key may not have a suffix key.')
  35 
  36         # Don't allow to save same shortcut key when prefix_key is null.
  37         if Label.objects.filter(suffix_key=suffix_key,
  38                                 prefix_key__isnull=True).exists():
  39             raise ValidationError('Duplicate key.')
  40         return super().validate(attrs)
  41 
  42     class Meta:
  43         model = Label
  44         fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')
  45 
  46 
  47 class DocumentSerializer(serializers.ModelSerializer):
  48     annotations = serializers.SerializerMethodField()
  49 
  50     def get_annotations(self, instance):
  51         request = self.context.get('request')
  52         project = instance.project
  53         model = project.get_annotation_class()
  54         serializer = project.get_annotation_serializer()
  55         annotations = model.objects.filter(document=instance.id)
  56         if request:
  57             annotations = annotations.filter(user=request.user)
  58         serializer = serializer(annotations, many=True)
  59         return serializer.data
  60 
  61     class Meta:
  62         model = Document
  63         fields = ('id', 'text', 'annotations', 'meta')
  64 
  65 
  66 class ProjectSerializer(serializers.ModelSerializer):
  67 
  68     class Meta:
  69         model = Project
  70         fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  71         read_only_fields = ('image', 'updated_at')
  72 
  73 
  74 class TextClassificationProjectSerializer(serializers.ModelSerializer):
  75 
  76     class Meta:
  77         model = TextClassificationProject
  78         fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  79         read_only_fields = ('image', 'updated_at', 'users')
  80 
  81 
  82 class SequenceLabelingProjectSerializer(serializers.ModelSerializer):
  83 
  84     class Meta:
  85         model = SequenceLabelingProject
  86         fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  87         read_only_fields = ('image', 'updated_at', 'users')
  88 
  89 
  90 class Seq2seqProjectSerializer(serializers.ModelSerializer):
  91 
  92     class Meta:
  93         model = Seq2seqProject
  94         fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')
  95         read_only_fields = ('image', 'updated_at', 'users')
  96 
  97 
  98 class ProjectPolymorphicSerializer(PolymorphicSerializer):
  99     model_serializer_mapping = {
  100         Project: ProjectSerializer,
  101         TextClassificationProject: TextClassificationProjectSerializer,
  102         SequenceLabelingProject: SequenceLabelingProjectSerializer,
  103         Seq2seqProject: Seq2seqProjectSerializer
  104     }
  105 
  106 
  107 class ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):
  108 
  109     def get_queryset(self):
  110         view = self.context.get('view', None)
  111         request = self.context.get('request', None)
  112         queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()
  113         if not request or not queryset or not view:
  114             return None
  115         return queryset.filter(project=view.kwargs['project_id'])
  116 
  117 
  118 class DocumentAnnotationSerializer(serializers.ModelSerializer):
  119     # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  120     label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  121     document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  122 
  123     class Meta:
  124         model = DocumentAnnotation
  125         fields = ('id', 'prob', 'label', 'user', 'document')
  126         read_only_fields = ('user', )
  127 
  128 
  129 class SequenceAnnotationSerializer(serializers.ModelSerializer):
  130     #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())
  131     label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())
  132     document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  133 
  134     class Meta:
  135         model = SequenceAnnotation
  136         fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')
  137         read_only_fields = ('user',)
  138 
  139 
  140 class Seq2seqAnnotationSerializer(serializers.ModelSerializer):
  141     document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())
  142 
  143     class Meta:
  144         model = Seq2seqAnnotation
  145         fields = ('id', 'text', 'user', 'document')
  146         read_only_fields = ('user',)
  147
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/app/server/serializers.py b/app/server/serializers.py
  --- a/app/server/serializers.py
  +++ b/app/server/serializers.py
  @@ -34,8 +34,11 @@
               raise ValidationError('Shortcut key may not have a suffix key.')
   
           # Don't allow to save same shortcut key when prefix_key is null.
  +        context = self.context['request'].parser_context
  +        project_id = context['kwargs'].get('project_id')
           if Label.objects.filter(suffix_key=suffix_key,
  -                                prefix_key__isnull=True).exists():
  +                                prefix_key__isnull=True,
  +                                project=project_id).exists():
               raise ValidationError('Duplicate key.')
           return super().validate(attrs)
  
verification_info: {"golden_diff": "diff --git a/app/server/serializers.py b/app/server/serializers.py\n--- a/app/server/serializers.py\n+++ b/app/server/serializers.py\n@@ -34,8 +34,11 @@\n             raise ValidationError('Shortcut key may not have a suffix key.')\n \n         # Don't allow to save same shortcut key when prefix_key is null.\n+        context = self.context['request'].parser_context\n+        project_id = context['kwargs'].get('project_id')\n         if Label.objects.filter(suffix_key=suffix_key,\n-                                prefix_key__isnull=True).exists():\n+                                prefix_key__isnull=True,\n+                                project=project_id).exists():\n             raise ValidationError('Duplicate key.')\n         return super().validate(attrs)\n", "issue": "Cannot use the same shortcut and label name in different projects\nThe issue is the following:\r\n\r\n1. I create Project A.\r\n2. Import some annotated data to the project - set of labels is automatically created as a result (let's say, Label 1, Label 2, Label 3)\r\n3. I edit these labels - provide a shortcut and color\r\n4. I create Project B and import another portion of dataset (with the same set of labels)\r\n5. I try to replicate setting of Project A (assign the same shortcuts, colors and label names to labels), which gives me an error:\r\n`You cannot use same label name or shortcut key.`\r\n\r\nIt seems not very convenient that we cannot create labels with the same shortcut and label name in different project. I wonder if it was made intentionally or it is a bug.\r\n\r\nThank you very much for a great tool.\n", "before_files": [{"content": "from django.contrib.auth import get_user_model\nfrom rest_framework import serializers\nfrom rest_polymorphic.serializers import PolymorphicSerializer\nfrom rest_framework.exceptions import ValidationError\n\n\nfrom .models import Label, Project, Document\nfrom .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject\nfrom .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation\n\n\nclass UserSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = get_user_model()\n        fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser')\n\n\nclass LabelSerializer(serializers.ModelSerializer):\n\n    def validate(self, attrs):\n        if 'prefix_key' not in attrs and 'suffix_key' not in attrs:\n            return super().validate(attrs)\n\n        prefix_key = attrs['prefix_key']\n        suffix_key = attrs['suffix_key']\n\n        # In the case of user don't set any shortcut key.\n        if prefix_key is None and suffix_key is None:\n            return super().validate(attrs)\n\n        # Don't allow shortcut key not to have a suffix key.\n        if prefix_key and not suffix_key:\n            raise ValidationError('Shortcut key may not have a suffix key.')\n\n        # Don't allow to save same shortcut key when prefix_key is null.\n        if Label.objects.filter(suffix_key=suffix_key,\n                                prefix_key__isnull=True).exists():\n            raise ValidationError('Duplicate key.')\n        return super().validate(attrs)\n\n    class Meta:\n        model = Label\n        fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')\n\n\nclass DocumentSerializer(serializers.ModelSerializer):\n    annotations = serializers.SerializerMethodField()\n\n    def get_annotations(self, instance):\n        request = self.context.get('request')\n        project = instance.project\n        model = project.get_annotation_class()\n        serializer = project.get_annotation_serializer()\n        annotations = model.objects.filter(document=instance.id)\n        if request:\n            annotations = annotations.filter(user=request.user)\n        serializer = serializer(annotations, many=True)\n        return serializer.data\n\n    class Meta:\n        model = Document\n        fields = ('id', 'text', 'annotations', 'meta')\n\n\nclass ProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = Project\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at')\n\n\nclass TextClassificationProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = TextClassificationProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at', 'users')\n\n\nclass SequenceLabelingProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = SequenceLabelingProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at', 'users')\n\n\nclass Seq2seqProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = Seq2seqProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at', 'users')\n\n\nclass ProjectPolymorphicSerializer(PolymorphicSerializer):\n    model_serializer_mapping = {\n        Project: ProjectSerializer,\n        TextClassificationProject: TextClassificationProjectSerializer,\n        SequenceLabelingProject: SequenceLabelingProjectSerializer,\n        Seq2seqProject: Seq2seqProjectSerializer\n    }\n\n\nclass ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):\n\n    def get_queryset(self):\n        view = self.context.get('view', None)\n        request = self.context.get('request', None)\n        queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()\n        if not request or not queryset or not view:\n            return None\n        return queryset.filter(project=view.kwargs['project_id'])\n\n\nclass DocumentAnnotationSerializer(serializers.ModelSerializer):\n    # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = DocumentAnnotation\n        fields = ('id', 'prob', 'label', 'user', 'document')\n        read_only_fields = ('user', )\n\n\nclass SequenceAnnotationSerializer(serializers.ModelSerializer):\n    #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = SequenceAnnotation\n        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')\n        read_only_fields = ('user',)\n\n\nclass Seq2seqAnnotationSerializer(serializers.ModelSerializer):\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = Seq2seqAnnotation\n        fields = ('id', 'text', 'user', 'document')\n        read_only_fields = ('user',)\n", "path": "app/server/serializers.py"}], "after_files": [{"content": "from django.contrib.auth import get_user_model\nfrom rest_framework import serializers\nfrom rest_polymorphic.serializers import PolymorphicSerializer\nfrom rest_framework.exceptions import ValidationError\n\n\nfrom .models import Label, Project, Document\nfrom .models import TextClassificationProject, SequenceLabelingProject, Seq2seqProject\nfrom .models import DocumentAnnotation, SequenceAnnotation, Seq2seqAnnotation\n\n\nclass UserSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = get_user_model()\n        fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_superuser')\n\n\nclass LabelSerializer(serializers.ModelSerializer):\n\n    def validate(self, attrs):\n        if 'prefix_key' not in attrs and 'suffix_key' not in attrs:\n            return super().validate(attrs)\n\n        prefix_key = attrs['prefix_key']\n        suffix_key = attrs['suffix_key']\n\n        # In the case of user don't set any shortcut key.\n        if prefix_key is None and suffix_key is None:\n            return super().validate(attrs)\n\n        # Don't allow shortcut key not to have a suffix key.\n        if prefix_key and not suffix_key:\n            raise ValidationError('Shortcut key may not have a suffix key.')\n\n        # Don't allow to save same shortcut key when prefix_key is null.\n        context = self.context['request'].parser_context\n        project_id = context['kwargs'].get('project_id')\n        if Label.objects.filter(suffix_key=suffix_key,\n                                prefix_key__isnull=True,\n                                project=project_id).exists():\n            raise ValidationError('Duplicate key.')\n        return super().validate(attrs)\n\n    class Meta:\n        model = Label\n        fields = ('id', 'text', 'prefix_key', 'suffix_key', 'background_color', 'text_color')\n\n\nclass DocumentSerializer(serializers.ModelSerializer):\n    annotations = serializers.SerializerMethodField()\n\n    def get_annotations(self, instance):\n        request = self.context.get('request')\n        project = instance.project\n        model = project.get_annotation_class()\n        serializer = project.get_annotation_serializer()\n        annotations = model.objects.filter(document=instance.id)\n        if request:\n            annotations = annotations.filter(user=request.user)\n        serializer = serializer(annotations, many=True)\n        return serializer.data\n\n    class Meta:\n        model = Document\n        fields = ('id', 'text', 'annotations', 'meta')\n\n\nclass ProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = Project\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at')\n\n\nclass TextClassificationProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = TextClassificationProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at', 'users')\n\n\nclass SequenceLabelingProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = SequenceLabelingProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at', 'users')\n\n\nclass Seq2seqProjectSerializer(serializers.ModelSerializer):\n\n    class Meta:\n        model = Seq2seqProject\n        fields = ('id', 'name', 'description', 'guideline', 'users', 'project_type', 'image', 'updated_at')\n        read_only_fields = ('image', 'updated_at', 'users')\n\n\nclass ProjectPolymorphicSerializer(PolymorphicSerializer):\n    model_serializer_mapping = {\n        Project: ProjectSerializer,\n        TextClassificationProject: TextClassificationProjectSerializer,\n        SequenceLabelingProject: SequenceLabelingProjectSerializer,\n        Seq2seqProject: Seq2seqProjectSerializer\n    }\n\n\nclass ProjectFilteredPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField):\n\n    def get_queryset(self):\n        view = self.context.get('view', None)\n        request = self.context.get('request', None)\n        queryset = super(ProjectFilteredPrimaryKeyRelatedField, self).get_queryset()\n        if not request or not queryset or not view:\n            return None\n        return queryset.filter(project=view.kwargs['project_id'])\n\n\nclass DocumentAnnotationSerializer(serializers.ModelSerializer):\n    # label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = DocumentAnnotation\n        fields = ('id', 'prob', 'label', 'user', 'document')\n        read_only_fields = ('user', )\n\n\nclass SequenceAnnotationSerializer(serializers.ModelSerializer):\n    #label = ProjectFilteredPrimaryKeyRelatedField(queryset=Label.objects.all())\n    label = serializers.PrimaryKeyRelatedField(queryset=Label.objects.all())\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = SequenceAnnotation\n        fields = ('id', 'prob', 'label', 'start_offset', 'end_offset', 'user', 'document')\n        read_only_fields = ('user',)\n\n\nclass Seq2seqAnnotationSerializer(serializers.ModelSerializer):\n    document = serializers.PrimaryKeyRelatedField(queryset=Document.objects.all())\n\n    class Meta:\n        model = Seq2seqAnnotation\n        fields = ('id', 'text', 'user', 'document')\n        read_only_fields = ('user',)\n", "path": "app/server/serializers.py"}]}