Example #1
0
class ActivitySerializer(serializers.ModelSerializer):
    actor = GenericRelatedField({
        User: UserSerializer(),
        Post: PostSerializer(),
        Destination: DestinationSerializer(),
        Address: AddressSerializer()
    })
    target = GenericRelatedField({
        User: UserSerializer(),
        Post: PostSerializer(),
        Destination: DestinationSerializer(),
        Address: AddressSerializer()
    })
    action_object = GenericRelatedField({
        User: UserSerializer(),
        Post: PostSerializer(),
        Destination: DestinationSerializer(),
        Address: AddressSerializer()
    })

    class Meta:
        model = Activity
        fields = [
            'id', 'verb',
            'created', 'address',
            'actor', 'actor_content_type',
            'target',
            'action_object', 'action_object_content_type'
        ]

    def to_representation(self, instance):
        self.fields['address'] = DAddressSerializer(read_only=True)
        return super(ActivitySerializer, self).to_representation(instance)
Example #2
0
class CandidacyMessageJobListSerializer(serializers.ModelSerializer):
    applicant = UserSerializer(source='candidacy.applicant.user', read_only=True)
    emmiter = UserSerializer(read_only=True)
    job = serializers.PrimaryKeyRelatedField(source='candidacy.job.id', read_only=True)
    is_read = serializers.SerializerMethodField()

    class Meta:
        model = CandidacyMessage
        fields = ('id', 'candidacy', 'applicant', 'job', 'emmiter', 'message', 'created', 'is_read')

    def get_is_read(self, obj):
        is_read = self.context.get('is_reads').get(obj.candidacy_id)
        if is_read:
            return {'is_read': is_read[0], 'date': is_read[1]}
        return {'is_read': False, 'date': None}
Example #3
0
class CandidacyMessageSerializer(serializers.ModelSerializer):
    emmiter = serializers.PrimaryKeyRelatedField(read_only=True, default=serializers.CurrentUserDefault())
    emmiter_extra = UserSerializer(source='emmiter', read_only=True)

    class Meta:
        model = CandidacyMessage
        fields = ('id', 'candidacy', 'emmiter', 'emmiter_extra', 'message', 'created')
        read_only_fields = ('id', 'created')

    def validate_candidacy(self, value):
        request = self.context.get('request')
        if request.user.is_pro and value.job.pro != request.user.pro:
            raise serializers.ValidationError(_('La candidature ne correspond pas à une offre de votre structure'))
        elif request.user.is_applicant and value.applicant != request.user.applicant:
            raise serializers.ValidationError(_('La candidature ne vous est pas liée'))
        return value

    def create(self, validated_data):
        request = self.context.get('request')
        candidacy = validated_data.get('candidacy')
        CandidacyMessageRead.objects.filter(Q(candidacy=candidacy) & ~Q(user=request.user)).update(is_read=False)
        return super(CandidacyMessageSerializer, self).create(validated_data)

    def update(self, instance, validated_data):
        del validated_data['candidacy']  # Remove candidacy from validated_data to avoid updating it
        return super(CandidacyMessageSerializer, self).update(instance, validated_data)
Example #4
0
def home(request, app_id):
    if request.method == "GET":
        key = "pub_home_" + str(app_id)
        force = request.GET.get("force", False)
        if key in cache and force is False:
            data = cache.get(key)
        else:
            pub = None
            if app_id != "0":
                if app_id.isdigit():
                    pub = Publication.objects.get(pk=app_id)
                else:
                    pub = Publication.objects.get(slug=app_id)
            terms = Term.objects.filter()[:5]
            posts = Post.objects.filter(post_type="article")[:11]
            users = User.objects.filter()[:16]
            publications = Publication.objects.filter()[:12]
            data = {
                "terms":
                serializers.TermSerializer(terms, many=True).data,
                "posts":
                serializers.PostSerializer(posts, many=True).data,
                "users":
                UserSerializer(users, many=True).data,
                "publications":
                serializers.PublicationSerializer(publications,
                                                  many=True).data,
                "pub":
                serializers.PublicationSerializer(pub).data
            }
            cache.set(key, data, timeout=CACHE_TTL)
        return Response(data)
    def get_action_object(self, obj):
        action_object = obj.action_object

        if isinstance(action_object, User):
            serializer = UserSerializer(action_object)
        elif isinstance(action_object, Job):
            serializer = JobSerializer(action_object)
        else:
            raise Exception('Unexpected type action_object')
        return serializer.data
class ApplicantFullSerializer(serializers.ModelSerializer):
    user = UserSerializer()
    experiences = ApplicantExperienceSerializer(many=True, read_only=True)
    educations = ApplicantEducationSerializer(many=True, read_only=True)
    skills = ApplicantSkillSerializer(many=True, read_only=True)
    languages = ApplicantLanguageSerializer(many=True, read_only=True)
    interests = ApplicantInterestSerializer(many=True, read_only=True)

    class Meta:
        model = Applicant
        fields = '__all__'
class NotificationSerializer(serializers.ModelSerializer):
    emmiter = UserSerializer(read_only=True)
    action_object = serializers.SerializerMethodField()

    class Meta:
        model = Notification
        fields = ('id', 'type_name', 'emmiter', 'action_object', 'created',
                  'is_unread')
        read_only_fields = ('id', 'type_name', 'emmiter', 'action_object',
                            'created')

    def get_action_object(self, obj):
        action_object = obj.action_object

        if isinstance(action_object, User):
            serializer = UserSerializer(action_object)
        elif isinstance(action_object, Job):
            serializer = JobSerializer(action_object)
        else:
            raise Exception('Unexpected type action_object')
        return serializer.data
Example #8
0
 def to_representation(self, instance):
     self.fields['user'] = UserSerializer(read_only=True)
     return super(MessageSerializer, self).to_representation(instance)
Example #9
0
 def get_request_user(request, format=None):
     return Response(UserSerializer(request.user).data)
Example #10
0
 def to_representation(self, instance):
     self.fields['user_mention'] = UserSerializer(read_only=True, many=True)
     return super(ActivitySerializer, self).to_representation(instance)