class BidSerializer(PrefetchedSerializer): bidcycle = StaticRepresentationField(read_only=True) user = StaticRepresentationField(read_only=True) position = StaticRepresentationField(read_only=True) waivers = StaticRepresentationField(read_only=True, many=True) is_paneling_today = serializers.BooleanField(read_only=True) class Meta: model = Bid fields = "__all__" nested = { "position": { "class": PositionSerializer, "field": "position", "kwargs": { "override_fields": [ "id", "position_number", "bureau", "title", "skill", "grade", "post__id", "post__location", "update_date", "create_date" ], "read_only": True } }, "reviewer": { "class": "talentmap_api.user_profile.serializers.UserProfileShortSerializer", "field": "reviewer", "kwargs": { "read_only": True } } }
class UserBidStatisticsSerializer(PrefetchedSerializer): bidcycle = StaticRepresentationField(read_only=True) user = StaticRepresentationField(read_only=True) class Meta: model = UserBidStatistics fields = "__all__"
class OrganizationSerializer(PrefetchedSerializer): bureau_organization = serializers.SerializerMethodField() parent_organization = serializers.SerializerMethodField() highlighted_positions = StaticRepresentationField(read_only=True, many=True) location = StaticRepresentationField(read_only=True) # This method returns the string representation of the bureau, or the code # if it doesn't currently exist in the database def get_bureau_organization(self, obj): if obj.bureau_organization: return obj.bureau_organization._string_representation else: return obj._parent_bureau_code # This method returns the string representation of the parent org, or the code # if it doesn't currently exist in the database def get_parent_organization(self, obj): if obj.parent_organization: return obj.parent_organization._string_representation else: return obj._parent_organization_code class Meta: model = Organization fields = "__all__"
class CurrentAssignmentSerializer(PrefetchedSerializer): user = StaticRepresentationField(read_only=True) tour_of_duty = StaticRepresentationField(read_only=True) class Meta: model = Assignment exclude = ("position", )
class PostSerializer(PrefetchedSerializer): code = serializers.CharField(source="_location_code", read_only=True) location = StaticRepresentationField(read_only=True) tour_of_duty = StaticRepresentationField(read_only=True) class Meta: model = Post fields = "__all__"
class WaiverSerializer(PrefetchedSerializer): ''' For read-only usages ''' bid = StaticRepresentationField(read_only=True) user = StaticRepresentationField(read_only=True) reviewer = StaticRepresentationField(read_only=True) position = StaticRepresentationField(read_only=True) class Meta: model = Waiver fields = "__all__"
class ClientSerializer(PrefetchedSerializer): current_assignment = serializers.SerializerMethodField() grade = StaticRepresentationField(read_only=True) is_cdo = serializers.ReadOnlyField() primary_nationality = StaticRepresentationField(read_only=True) secondary_nationality = StaticRepresentationField(read_only=True) def get_current_assignment(self, obj): if obj.assignments.count() > 0: return str(obj.assignments.latest('start_date')) else: return None class Meta: model = UserProfile fields = [ "id", "current_assignment", "skills", "grade", "is_cdo", "primary_nationality", "secondary_nationality", "bid_statistics", "user", "language_qualifications" ] nested = { "user": { "class": UserSerializer, "kwargs": { "read_only": True } }, "language_qualifications": { "class": LanguageQualificationSerializer, "kwargs": { "override_fields": ["id", "representation"], "many": True, "read_only": True, } }, "bid_statistics": { "class": UserBidStatisticsSerializer, "kwargs": { "many": True, "read_only": True } }, "skills": { "class": SkillSerializer, "kwargs": { "many": True, "read_only": True } } }
class PermissionGroupMembersSerializer(PrefetchedSerializer): permissions = PermissionSerializer(many=True) user_set = StaticRepresentationField(read_only=True, many=True) class Meta: model = Group fields = "__all__"
class NotificationSerializer(PrefetchedSerializer): owner = StaticRepresentationField(read_only=True) class Meta: model = Notification fields = "__all__" writable_fields = ("is_read")
class GlossaryEntrySerializer(PrefetchedSerializer): last_editing_user = StaticRepresentationField(read_only=True) class Meta: model = GlossaryEntry fields = "__all__" writable_fields = ('title', 'definition', 'link', 'is_archived')
class HistoricalSerializer(serializer): history_user = StaticRepresentationField(read_only=True) class Meta(serializer.Meta): model = model_class.history.model fields = "__all__" nested = {} # No nesting serializers here - the FK traversal doesn't cascade (yet)
class CapsuleDescriptionSerializer(PrefetchedSerializer): last_editing_user = StaticRepresentationField(read_only=True) # This is a dynamic flag used by the front end to simplify checking if the current user has permissions is_editable_by_user = serializers.SerializerMethodField() date_created = serializers.DateTimeField(read_only=True) date_updated = serializers.DateTimeField(read_only=True) def get_is_editable_by_user(self, obj): try: return self.context.get("request").user.has_perm( f"position.{obj.position.post.permission_edit_post_capsule_description_codename}" ) except AttributeError: # The position doesn't have a post, or otherwise return False class Meta: model = CapsuleDescription fields = "__all__" writable_fields = ( "content", "point_of_contact", "website", )
class TaskSerializer(PrefetchedSerializer): owner = StaticRepresentationField(read_only=True) class Meta: model = Task fields = "__all__" writable_fields = ("date_due", "date_completed", "title", "content", "priority", "tags")
class SharableSerializer(PrefetchedSerializer): sharing_user = StaticRepresentationField(read_only=True) receiving_user = StaticRepresentationField(read_only=True) content = serializers.SerializerMethodField() def get_content(self, obj): model = apps.get_model(obj.sharable_model) instance = model.objects.get(id=obj.sharable_id) return { "representation": f"{instance}", "url": reverse(f'{obj.sharable_model}-detail', kwargs={"pk": obj.sharable_id}, request=self.context.get("request")) } class Meta: model = Sharable fields = ["id", "sharing_user", "receiving_user", "content", "is_read"] writable_fields = ("is_read", )
class UserProfileSerializer(PrefetchedSerializer): current_assignment = serializers.SerializerMethodField() skills = StaticRepresentationField(read_only=True, many=True) grade = StaticRepresentationField(read_only=True) cdo = StaticRepresentationField(read_only=True) is_cdo = serializers.ReadOnlyField() primary_nationality = StaticRepresentationField(read_only=True) secondary_nationality = StaticRepresentationField(read_only=True) def get_current_assignment(self, obj): if obj.assignments.count() > 0: return str(obj.assignments.latest('start_date')) else: return None class Meta: model = UserProfile fields = "__all__" nested = { "user": { "class": UserSerializer, "kwargs": { "read_only": True } }, "cdo": { "class": UserProfileShortSerializer, "kwargs": { "read_only": True } }, "language_qualifications": { "class": LanguageQualificationSerializer, "kwargs": { "override_fields": ["id", "representation"], "many": True, "read_only": True, } }, "favorite_positions": { "class": PositionSerializer, "kwargs": { "override_fields": ["id", "representation"], "many": True, "read_only": True } }, "received_shares": { "class": SharableSerializer, "kwargs": { "many": True, "read_only": True } }, "skills": { "class": SkillSerializer, "kwargs": { "many": True, "read_only": True } } }
class PrePanelSerializer(PrefetchedSerializer): ''' This is a bit of a bespoke serializer which takes the bid and constructs pre-panel aggregation of statuses and information ''' bidcycle = StaticRepresentationField(read_only=True) user = StaticRepresentationField(read_only=True) position = StaticRepresentationField(read_only=True) prepanel = serializers.SerializerMethodField() def get_prepanel(self, obj): prepanel = {} user = UserProfileSerializer.prefetch_model( UserProfile, UserProfile.objects.all()).get(id=obj.user.id) position = PositionSerializer.prefetch_model( Position, Position.objects.all()).get(id=obj.position.id) waivers = obj.waivers sii = user.status_surveys.filter(bidcycle=obj.bidcycle).first() if not sii: return "Bidder has not submitted a self-identification survey for this bidcycle" prepanel['fairshare'] = self.generate_prepanel_fairshare( user, position, waivers, sii) prepanel['six_eight'] = self.generate_prepanel_six_eight( user, position, waivers, sii) prepanel['language'] = self.generate_prepanel_language( user, position, waivers) prepanel['skill'] = self.generate_prepanel_skill( user, position, waivers) return prepanel def generate_prepanel_fairshare(self, user, position, waivers, sii): return { "calculated": user.is_fairshare, "self_identified": sii.is_fairshare, "waivers": [ str(x) for x in list( waivers.filter(category=Waiver.Category.fairshare)) ] } def generate_prepanel_six_eight(self, user, position, waivers, sii): return { "calculated": user.is_six_eight, "self_identified": sii.is_six_eight, "post_location": str(safe_navigation(position, 'post.location')), "waivers": [ str(x) for x in list( waivers.filter(category=Waiver.Category.six_eight)) ] } def generate_prepanel_language(self, user, position, waivers): ''' For matching the language, we consider a language_match to be true if they possess any proficiency in the required language. ''' language_match = True reading_proficiency_match = True spoken_proficiency_match = True for language in list(position.languages.all()): user_language = user.language_qualifications.filter( language=language.language).first() if user_language: reading_proficiency_match = language.reading_proficiency > user_language.reading_proficiency spoken_proficiency_match = language.spoken_proficiency > user_language.spoken_proficiency else: # If we're missing even one language, fail all cases and break language_match = False reading_proficiency_match = False spoken_proficiency_match = False break return { "language_match": language_match, "reading_proficiency_match": reading_proficiency_match, "spoken_proficiency_match": spoken_proficiency_match, "position_languages": [str(x) for x in list(position.languages.all())], "user_languages": [str(x) for x in list(user.language_qualifications.all())], "waivers": [ str(x) for x in list(waivers.filter( category=Waiver.Category.language)) ] } def generate_prepanel_skill(self, user, position, waivers): return { "skill_match": user.skills.filter( code=safe_navigation(position, 'skill.code')).exists(), "position_skill": str(safe_navigation(position, 'skill')), "user_skills": [str(x) for x in list(user.skills.all())], "waivers": [ str(x) for x in list(waivers.filter(category=Waiver.Category.skill)) ] } class Meta: model = Bid fields = ("bidcycle", "user", "position", "prepanel")
class LocationSerializer(PrefetchedSerializer): country = StaticRepresentationField(read_only=True) class Meta: model = Location fields = "__all__"
class SkillSerializer(PrefetchedSerializer): cone = StaticRepresentationField(read_only=True) class Meta: model = Skill fields = "__all__"
class SkillConeSerializer(PrefetchedSerializer): skills = StaticRepresentationField(read_only=True, many=True) class Meta: model = SkillCone fields = "__all__"
class PositionSerializer(PrefetchedSerializer): grade = StaticRepresentationField(read_only=True) skill = StaticRepresentationField(read_only=True) bureau = serializers.SerializerMethodField() organization = serializers.SerializerMethodField() classifications = StaticRepresentationField(read_only=True, many=True) representation = serializers.SerializerMethodField() # This method returns the string representation of the bureau, or the code # if it doesn't currently exist in the database def get_bureau(self, obj): if obj.bureau: return obj.bureau._string_representation else: return obj._bureau_code # This method returns the string representation of the parent org, or the code # if it doesn't currently exist in the database def get_organization(self, obj): if obj.organization: return obj.organization._string_representation else: return obj._org_code class Meta: model = Position fields = "__all__" nested = { "bid_statistics": { "class": PositionBidStatisticsSerializer, "kwargs": { "many": True, "read_only": True } }, "languages": { "class": LanguageQualificationSerializer, "kwargs": { "many": True, "read_only": True } }, "post": { "class": PostSerializer, "field": "post", "kwargs": { "many": False, "read_only": True } }, "description": { "class": CapsuleDescriptionSerializer, "field": "description", "kwargs": { "read_only": True } }, "current_assignment": { "class": CurrentAssignmentSerializer, "field": "current_assignment", "kwargs": { "override_fields": [ "user", "status", "start_date", "tour_of_duty", "estimated_end_date" ], "read_only": True } } }
class PositionBidStatisticsSerializer(PrefetchedSerializer): bidcycle = StaticRepresentationField(read_only=True) class Meta: model = PositionBidStatistics exclude = ("position", )