def get(self, request, pk, format=None):
        try:
            talent = Talent.objects.get(pk=pk)
        except Talent.DoesNotExist:
            raise Http404

        serializer = TalentSerializer(talent)

        # In the case client require this talent info, save this history in Favorite table.
        user = request.user
        client = Client.objects.filter(user_id=user.id).first()
        if client:
            # Logging
            UserNoteManager.search_logger(
                None, user, talent.user,
                'TALENT VIEWED BY {finder}'.format(finder=user), user)

            # Favoriting
            favorite = Favorite.objects.filter(client=client,
                                               talent=talent).first()
            if favorite:
                favorite.save()
            else:
                new_favorite = Favorite.objects.create(client=client,
                                                       talent=talent)
                new_favorite.save()

            # Logging
            UserNoteManager.favorite_logger(
                None, user, talent.user,
                'TALENT FAVORITED BY {finder}'.format(finder=user), user)

        return Response(serializer.data)
Exemple #2
0
    def post(self, request, format=None):
        """
        Get talents matching to search condition
        """
        # Filter talents according to search condition
        search_conditions = request.data
        hasAnyConditions = len(search_conditions.values()) > 0
        talent_name = self.pickout_data(search_conditions, 'talent_name')
        talent_tid = self.pickout_data(search_conditions, 'talent_tid')
        casting_request_id = self.pickout_data(search_conditions,
                                               'casting_request_id')
        talent_name_or_tid = self.pickout_data(search_conditions,
                                               'talent_name_or_tid')
        ages = self.pickout_data(search_conditions, 'ages')
        availability = self.pickout_data(search_conditions, 'availability')
        heights = self.pickout_data(search_conditions, 'heights')
        languages = self.pickout_data(search_conditions, 'languages')
        position_ids = self.pickout_data(search_conditions, 'position_ids')
        position_sub_type_ids = self.pickout_data(search_conditions,
                                                  'position_sub_type_ids')
        ratings = self.pickout_data(search_conditions, 'ratings')
        sexes = self.pickout_data(search_conditions, 'sexes')
        skill_ids = self.pickout_data(search_conditions, 'skill_ids')
        sub_skill_ids = self.pickout_data(search_conditions, 'sub_skill_ids')
        approved = self.pickout_data(search_conditions, 'approved')
        locked_dance_combination = self.pickout_data(
            search_conditions, 'locked_dance_combination')

        talents = Talent.objects.all()

        # Check talent_tid
        if talent_tid:
            try:
                talents = talents.filter(tid__icontains=talent_tid)
            except Talent.DoesNotExist:
                raise Http404

        # Check talent_name
        if talent_name:
            talents = talents.filter(
                Q(user__first_name__icontains=talent_name)
                | Q(user__last_name__icontains=talent_name))

        # Check casting_request_id
        if casting_request_id:
            casting_request = CastingRequest.objects.get(pk=casting_request_id)
            # casting_request_talent_ids
            talent_ids = CastingRequestTalent.objects\
                .filter(casting_request_id=casting_request.id)\
                .values_list('talent', flat=True)\
                .order_by('talent')\
                .distinct()
            talents = talents.filter(~Q(id__in=talent_ids))

        # Check talent_name_or_tid
        if talent_name_or_tid:
            talents = talents.filter(
                Q(user__first_name__icontains=talent_name_or_tid)
                | Q(user__last_name__icontains=talent_name_or_tid)
                | Q(tid__icontains=talent_name_or_tid))

        # Check sexes
        if sexes:
            talents = talents.filter(Q(sex__in=sexes))

        # Check position_ids
        if position_ids:
            talent_position_talent_ids = TalentPositionType.objects.filter(
                    Q(position_type_id__in=position_ids)
                )\
                .values_list('talent', flat=True)\
                .order_by('talent')\
                .distinct()
            talents = talents.filter(Q(id__in=talent_position_talent_ids))

        # Check position_sub_type_ids
        if position_sub_type_ids:
            talent_position_sub_type_talent_ids = TalentPositionSubType.objects.filter(
                    Q(position_sub_type_id__in=position_sub_type_ids)
                )\
                .values_list('talent', flat=True)\
                .order_by('talent')\
                .distinct()
            talents = talents.filter(
                Q(id__in=talent_position_sub_type_talent_ids))

        # Check skill_ids
        if skill_ids:
            talent_skill_talent_ids = TalentSkill.objects.filter(
                    reduce(or_, (Q(skill_id=skill_id) for skill_id in skill_ids))
                )\
                .values_list('talent', flat=True)\
                .order_by('talent')\
                .distinct()
            talents = talents.filter(Q(id__in=talent_skill_talent_ids))

        # Check sub_skill_ids
        if sub_skill_ids:
            talent_sub_skill_talent_ids = TalentSubSkill.objects.filter(Q(sub_skill_id__in=sub_skill_ids))\
                .values_list('talent', flat=True)\
                .order_by('talent')\
                .distinct()
            talents = talents.filter(Q(id__in=talent_sub_skill_talent_ids))

        # Check availability
        if availability and (availability['start_date']
                             or availability['end_date']):
            queries = Q()
            if availability['end_date']:
                queries = Q(start_date__lte=parse(availability['end_date']))
            if availability['start_date']:
                queries &= Q(end_date__gte=parse(availability['start_date']))

            talent_availabilities_talent_ids = TalentAvailability.objects.filter(queries)\
                .values_list('talent', flat=True)\
                .order_by('talent')\
                .distinct()

            talents = talents.filter(
                Q(id__in=talent_availabilities_talent_ids))

        # Check ages
        if ages:
            talents = talents.filter(Q(age_range__in=ages))

        # Check heights
        if heights:
            queries = Q()
            for height_range in heights:
                start_height = height_range['start_height']
                end_height = height_range['end_height']
                if end_height == 0:
                    end_height = start_height + 1

                queries |= Q(height__contained_by=NumericRange(
                    start_height, end_height))

            talents = talents.filter(queries)

        # Check languages
        if languages:
            talent_languages_talent_ids = TalentLanguage.objects.filter(language__in=languages)\
                .values_list('talent', flat=True)\
                .order_by('talent')\
                .distinct()
            talents = talents.filter(Q(id__in=talent_languages_talent_ids))

        # Check ratings
        if ratings:
            rated_talent_ids = []
            for talent in talents:
                talent_average_rating = TalentRating.objects.filter(talent=talent)\
                    .aggregate(Avg('rating'))['rating__avg']
                if talent_average_rating:
                    for rating_range in ratings:
                        if (talent_average_rating >=
                                rating_range['start_rating']) and (
                                    talent_average_rating <=
                                    rating_range['end_rating']):
                            rated_talent_ids.append(talent.id)

            talents = talents.filter(Q(id__in=rated_talent_ids))

        # Check approved
        if approved is not None:
            talents = talents.filter(approved=approved)

        if locked_dance_combination:
            talents = talents.filter(
                locked_dance_combination=locked_dance_combination)

        talents = self.check_sort_condition(talents, search_conditions)

        # Logging
        user = request.user
        if user and user.type != 'agency' and len(
                talents) > 0 and hasAnyConditions:
            for talent in talents:
                UserNoteManager.search_logger(
                    None, user, talent.user,
                    'TALENT APPEARED INSEARCH BY {finder}'.format(finder=user),
                    user)

        serializer = TalentSerializer(talents, many=True)
        return Response(serializer.data)