Exemplo n.º 1
0
class CompanySerializer(NestedModelSerializer,
                        ContentTypeAnnotatedModelSerializer):
    user = SimplestUserSerializer(required=False, read_only=False)
    city = serializers.CharField(required=False,
                                 allow_blank=True,
                                 allow_null=True)
    skills = SimpleSkillSerializer(required=False, many=True)
    country = CountryField(required=False)
    country_name = serializers.CharField(required=False, read_only=True)

    class Meta:
        model = Company
        fields = '__all__'

    def save_nested_user(self, data, instance, created=False):
        user = instance.user
        if user:
            user.first_name = data.get('first_name', user.first_name)
            user.last_name = data.get('last_name', user.last_name)
            image = data.get('image', None)
            if image:
                user.image = image
            user.save()

    def save_nested_skills(self, data, instance, created=False):
        if data is not None:
            instance.skills = ', '.join(
                [skill.get('name', '') for skill in data])
            instance.save()

    def save_nested_city(self, data, instance, created=False):
        if data:
            instance.city = data
            instance.save()
Exemplo n.º 2
0
class SimpleProjectSerializer(SimpleModelSerializer):
    skills = SimpleSkillSerializer(required=False, many=True)

    class Meta:
        model = Project
        fields = ('id', 'title', 'description', 'type', 'budget', 'currency',
                  'closed', 'start_date', 'deadline', 'archived', 'skills')
Exemplo n.º 3
0
class ProfileSerializer(NestedModelSerializer, ContentTypeAnnotatedModelSerializer):
    user = SimplestUserSerializer(required=False)
    city = serializers.CharField(required=False, allow_blank=True, allow_null=True)
    skills = SimpleSkillSerializer(required=False, many=True)
    skills_details = SkillsDetailsSerializer(required=False, read_only=True)
    country = CountryField(required=False)
    country_name = serializers.CharField(required=False, read_only=True)

    class Meta:
        model = UserProfile
        fields = '__all__'

    def nested_save_override(self, validated_data, instance=None):
        initial_bio = None
        initial_location = None
        initial_skills = None

        if instance:
            initial_bio = instance.bio
            initial_location = instance.location
            initial_skills = [skill.name for skill in instance.skills.all()]
            list.sort(initial_skills)

        instance = super(ProfileSerializer, self).nested_save_override(validated_data, instance=instance)

        final_skills = [skill.name for skill in instance.skills.all()]
        list.sort(final_skills)
        if not instance or initial_bio != instance.bio or initial_location != instance.location or initial_skills != final_skills:
            user_profile_updated.send(sender=UserProfile, profile=instance)
        return instance

    def save_nested_user(self, data, instance, created=False):
        user = instance.user
        if user:
            user.first_name = data.get('first_name', user.first_name)
            user.last_name = data.get('last_name', user.last_name)
            image = data.get('image', None)
            if image:
                user.image = image
            user.save()

    def save_nested_skills(self, data, instance, created=False):
        if data is not None:
            instance.skills = ', '.join([skill.get('name', '') for skill in data])
            instance.save()

            for skill in data:
                try:
                    category = skill.get('type', None)
                    if category:
                        Skill.objects.filter(name=skill.get('name', ''), type=SKILL_TYPE_OTHER).update(type=category)
                except:
                    pass

    def save_nested_city(self, data, instance, created=False):
        if data:
            instance.city = data
            instance.save()
Exemplo n.º 4
0
class ProjectSerializer(
    NestedModelSerializer, ContentTypeAnnotatedModelSerializer, GetCurrentUserAnnotatedSerializerMixin
):
    user = SimplestUserSerializer(required=False, read_only=True, default=CreateOnlyCurrentUserDefault())
    owner = SimplestUserSerializer(required=False, allow_null=True)
    pm = SimplestUserSerializer(required=False, allow_null=True)
    skills = SimpleSkillSerializer(required=False, many=True)
    participation = SimpleParticipationSerializer(required=False, many=True, source='participation_set')
    documents = SimpleDocumentSerializer(required=False, many=True, source='document_set')
    progress_events = SimpleProgressEventSerializer(required=False, many=True, source='progressevent_set')
    meta = SimpleProjectMetaSerializer(required=False, many=True, source='projectmeta_set')
    change_log = serializers.JSONField(required=False, write_only=True)
    margin = serializers.ReadOnlyField()
    interest_polls = SimpleInterestPollSerializer(required=False, many=True, source='interestpoll_set')
    expected_duration = serializers.CharField(required=True, read_only=False,
                                     allow_null=False, allow_blank=False)
    category = serializers.CharField(required=True, read_only=False,
                                     allow_null=False, allow_blank=False)

    class Meta:
        model = Project
        fields = '__all__'
        read_only_fields = ('created_at', 'updated_at')

    def nested_save_override(self, validated_data, instance=None):
        initial_stage = None
        initial_archived = None

        if instance:
            initial_stage = instance.stage
            initial_archived = instance.archived

        instance = super(ProjectSerializer, self).nested_save_override(validated_data, instance=instance)

        if instance:
            if initial_stage != instance.stage:
                post_field_update.send(sender=Project, instance=instance, field='stage')

            if type(initial_archived) == bool and instance.archived and not initial_archived:
                complete_exact_sync.delay(instance.id)

        return instance

    def save_nested_skills(self, data, instance, created=False):
        if data is not None:
            instance.skills = ', '.join([skill.get('name', '') for skill in data])
            instance.save()

    def save_nested_change_log(self, data, instance, created=False):
        if data is not None:
            for item in data:
                FieldChangeLog.objects.create(content_object=instance, created_by=self.get_current_user(), **item)