def put(self, request, pk, format=None):
        print('== request.data: ', request.data)
        talent_item = self.get_object(pk)
        talent_data = request.data
        user_data = {}
        if "user" in talent_data:
            user_data = talent_data['user']
            talent_data.pop('user', None)
            
        serializer = TalentSerializer(talent_item, data=talent_data)
        if serializer.is_valid():
            serializer.save()
            if "talent_position_sub_type" in request.data:
                # Save primary position sub type
                position_sub_type = TalentPositionSubType.objects.get(name=request.data['talent_position_sub_type']['name'])
                if position_sub_type:
                    talent_item.talent_position_sub_type = position_sub_type
                    talent_item.save()

                # Delete all secondary position sub types of talent
                TalentAdditionalPositionSubType.objects.filter(
                    talent = talent_item
                ).delete()

                # Save secondary position sub type
                talent_additional_position_sub_types = request.data['talent_additional_position_sub_types']
                for additional_position_sub_type in talent_additional_position_sub_types:
                    position_sub_type_item = TalentPositionSubType.objects.get(
                            name=additional_position_sub_type['name']
                        )
                    new_additional_position_sub_type_item = TalentAdditionalPositionSubType.objects.create(
                            talent_position_sub_type = position_sub_type_item,
                            talent = talent_item
                        )
                    new_additional_position_sub_type_item.save()

            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
示例#2
0
    def put(self, request, pk, format=None):
        print('== request.data: ', request.data)
        talent_item = self.get_object(pk)
        talent_data = request.data

        # pick out user data
        user_data = self.pickout_data(talent_data, 'user')

        # pick out position sub type data
        talent_position_type_data = self.pickout_data(talent_data,
                                                      'talent_position_type')

        # pick out position sub type data
        talent_position_sub_type_data = self.pickout_data(
            talent_data, 'talent_position_sub_type')

        # pick out multiple position sub types data
        talent_position_sub_types_data = self.pickout_data(
            talent_data, 'talent_position_sub_types')

        # pick out skills data
        talent_skills_data = self.pickout_data(talent_data, 'talent_skills')

        # pick out sub skills data
        talent_sub_skills_data = self.pickout_data(talent_data,
                                                   'talent_sub_skills')

        # pick out visa data
        talent_visas_data = self.pickout_data(talent_data, 'talent_visas')

        print('==== talent_data: ', talent_data)
        print('==== talent_skills_data: ', talent_skills_data)
        print('==== talent_sub_skills_data: ', talent_sub_skills_data)

        serializer = TalentSerializer(talent_item, data=talent_data)
        # Reset tid
        if talent_position_type_data:
            serializer.tid = self.generate_tid_by_one(
                talent_item, talent_position_type_data)

        if serializer.is_valid():
            serializer.save()

            # Check and save position sub type
            if talent_position_type_data:
                self.delete_all_talent_position_and_skill(talent_item)

                self.save_talent_position_type(talent_item,
                                               talent_position_type_data)

                # Check and save position sub type
                if talent_position_sub_type_data:
                    self.save_talent_position_sub_type(
                        talent_item, talent_position_sub_type_data,
                        talent_position_type_data)

                # Check and save multiple position sub types
                if talent_position_sub_types_data:
                    self.save_talent_position_sub_types(
                        talent_item, talent_position_sub_types_data,
                        talent_position_type_data)

            # Check and save skills
            if talent_skills_data:
                self.save_talent_skills(talent_item, talent_skills_data)

            # Check and save skills
            if talent_sub_skills_data:
                self.save_talent_sub_skills(talent_item,
                                            talent_sub_skills_data)

            # Check and save visa types
            if talent_visas_data:
                self.save_talent_visas(talent_item, talent_visas_data)

            return Response(request.data, status=status.HTTP_200_OK)
        return Response({'error': serializer.errors},
                        status=status.HTTP_400_BAD_REQUEST)
示例#3
0
    def put(self, request, pk, format=None):
        talent_item = self.get_object(pk)
        talent_data = request.data

        # pick out user data
        user_data = self.pickout_data(talent_data, 'user')

        # pick out position sub type data
        talent_position_type_data = self.pickout_data(talent_data,
                                                      'talent_position_type')

        # pick out position sub type data
        talent_position_sub_type_data = self.pickout_data(
            talent_data, 'talent_position_sub_type')

        # pick out multiple position sub types data
        talent_position_sub_types_data = self.pickout_data(
            talent_data, 'talent_position_sub_types')

        # pick out skills data
        talent_skills_data = self.pickout_data(talent_data, 'talent_skills')

        # pick out sub skills data
        talent_sub_skills_data = self.pickout_data(talent_data,
                                                   'talent_sub_skills')

        # pick out visa data
        talent_visas_data = self.pickout_data(talent_data, 'talent_visas')

        # pick out approved data
        # approved_data = self.pickout_data(talent_data, 'approved')

        # pick out locked_dance_combination
        # locked_dance_combination_data = self.pickout_data(talent_data, 'locked_dance_combination')

        print('==== talent_data: ', talent_data)
        print('==== talent_skills_data: ', talent_skills_data)
        print('==== talent_sub_skills_data: ', talent_sub_skills_data)

        serializer = TalentSerializer(talent_item, data=talent_data)
        # Reset tid
        if talent_position_type_data:
            serializer.tid = self.generate_tid_by_one(
                talent_item, talent_position_type_data)

        if serializer.is_valid():
            serializer.save()

            # In the case approved or locked_dance_combination, logging
            agency = request.user
            if 'approved' in talent_data:
                approved = talent_data['approved']
                note = 'TALENT {approve_status} by {admin}'.format(
                    approve_status='Approved' if approved else 'Rejected',
                    admin=agency.first_name)
                UserNoteManager.profile_logger(agency, None, talent_item.user,
                                               note, talent_item.user)

            if 'locked_dance_combination' in talent_data:
                locked_dance_combination = talent_data[
                    'locked_dance_combination']
                note = 'Lockout Reset by Agent ({admin})'.format(
                    admin=agency.first_name)
                if locked_dance_combination:
                    note = 'TALENT LOCKED OUT FOR MULTIPLE DOWNLOADS'

                UserNoteManager.dance_combination_logger(
                    None, None, talent_item.user, note, talent_item.user)

            if 'tid' in talent_data:
                tid = talent_data['tid']
                note = 'Changed to {tid} by {admin}'.format(
                    tid=tid, admin=agency.first_name)
                UserNoteManager.tid_logger(None, None, talent_item.user, note,
                                           talent_item.user)

            # Check and save position sub type
            if talent_position_type_data:
                self.delete_all_talent_position_and_skill(talent_item)

                self.save_talent_position_type(talent_item,
                                               talent_position_type_data)

                # Check and save position sub type
                if talent_position_sub_type_data:
                    self.save_talent_position_sub_type(
                        talent_item, talent_position_sub_type_data,
                        talent_position_type_data)

                # Check and save multiple position sub types
                if talent_position_sub_types_data:
                    self.save_talent_position_sub_types(
                        talent_item, talent_position_sub_types_data,
                        talent_position_type_data)

            # Check and save skills
            if talent_skills_data:
                self.save_talent_skills(talent_item, talent_skills_data)

            # Check and save skills
            if talent_sub_skills_data:
                self.save_talent_sub_skills(talent_item,
                                            talent_sub_skills_data)

            # Check and save visa types
            if talent_visas_data:
                self.save_talent_visas(talent_item, talent_visas_data)

            return Response(request.data, status=status.HTTP_200_OK)

        return Response({'error': serializer.errors},
                        status=status.HTTP_400_BAD_REQUEST)