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)
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)
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: 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() return Response(serializer.data)
class TalentDetail(APIView): # authentication_classes = (SessionAuthentication, JSONWebTokenAuthentication) # permission_classes = (permissions.IsAuthenticated,) """ Retrieve, update or delete a talent. """ def get_object(self, pk): try: user = User.objects.get(pk=pk) talent = Talent.objects.get(user=user.id) return talent except Talent.DoesNotExist: raise Http404 def save_talent_position_type(self, talent, position_type_name): # delete all position types of talent TalentPositionType.objects.filter(talent=talent).delete() # delete all sub skills of talent TalentPositionSubType.objects.filter(talent=talent).delete() # save position type position_type = PositionType.objects.get(name=position_type_name) if position_type: new_talent_position_type = TalentPositionType.objects.create( talent=talent, position_type=position_type) new_talent_position_type.save() def save_talent_position_sub_type(self, talent, position_sub_type_name, position_type_name): # delete all position sub types of talent TalentPositionSubType.objects.filter(talent=talent).delete() # save position type position_type = PositionType.objects.get(name=position_type_name) position_sub_type = PositionSubType.objects.filter( name=position_sub_type_name, position_type=position_type).first() if position_sub_type: new_talent_position_sub_type = TalentPositionSubType.objects.create( talent=talent, position_sub_type=position_sub_type) new_talent_position_sub_type.save() def save_talent_position_sub_types(self, talent, position_sub_type_names, position_type_name): # delete all position sub types of talent TalentPositionSubType.objects.filter(talent=talent).delete() # save position type position_type = PositionType.objects.get(name=position_type_name) position_sub_types = PositionSubType.objects.filter( name__in=position_sub_type_names, position_type=position_type) if len(position_sub_types) > 0: for position_sub_type in position_sub_types: new_talent_position_sub_type = TalentPositionSubType.objects.create( talent=talent, position_sub_type=position_sub_type) new_talent_position_sub_type.save() def save_talent_skills(self, talent, talent_skills): # delete all skills of talent TalentSkill.objects.filter(talent=talent).delete() # save all talent skills for talent_skill in talent_skills: skill = Skill.objects.get(name=talent_skill['name']) new_talent_skill = TalentSkill.objects.create(talent=talent, skill=skill) new_talent_skill.save() def save_talent_sub_skills(self, talent, talent_sub_skills): print('====== save_talent_sub_skills: ', talent_sub_skills) # delete all sub skills of talent TalentSubSkill.objects.filter(talent=talent).delete() # save all talent sub skills for talent_sub_skill in talent_sub_skills: sub_skill = SubSkill.objects.filter( name=talent_sub_skill['name']).first() new_talent_sub_skill = TalentSubSkill.objects.create( talent=talent, sub_skill=sub_skill) new_talent_sub_skill.save() def save_talent_visas(self, talent, talent_visas): # Delete all visas of talent TalentVisa.objects.filter(talent=talent).delete() # Save visas talent_visas = talent_visas for visa in talent_visas: new_visa = TalentVisa.objects.create( talent=talent, name=visa['name'], expiration_date=visa['expiration_date']) new_visa.save() def add_talent_position_types(self, talent, talent_position_types): for talent_position_type in talent_position_types: position_type = talent_position_type['position_type'] position_sub_types = talent_position_type['position_sub_types'] if position_type: # check position type and skip if exist, else add it if 'id' in position_type: pt = PositionType.objects.get(id=position_type['id']) else: pt = PositionType.objects.filter( name=position_type['name']).first() if pt: res_talent_position_type = TalentPositionType.objects.filter( talent=talent, position_type=pt).first() if not res_talent_position_type: # add this item new_talent_position_type = TalentPositionType.objects.create( talent=talent, position_type=pt) new_talent_position_type.save() # add position sub types if position_sub_types: self.add_talent_position_sub_types(talent, pt, position_sub_types) def add_talent_position_sub_types(self, talent, position_type, position_sub_types): for position_sub_type in position_sub_types: # check position sub types and skill if exist, else add it. if 'id' in position_sub_type: pst = PositionSubType.objects.get(id=position_sub_type['id']) else: pst = PositionSubType.objects.filter( position_type=position_type, name=position_sub_type['name']).first() if pst: res_talent_position_sub_type = TalentPositionSubType.objects.filter( talent=talent, position_sub_type=pst).first() if not res_talent_position_sub_type: # add this item new_talent_position_sub_type = TalentPositionSubType.objects.create( talent=talent, position_sub_type=pst) new_talent_position_sub_type.save() def add_talent_skills(self, talent, talent_skills): for talent_skill in talent_skills: skill = talent_skill['skill'] sub_skills = talent_skill['sub_skills'] if skill: # check skill and skip if exist, else add it if 'id' in skill: sk = Skill.objects.get(id=skill['id']) else: sk = Skill.objects.filter(name=skill['name']).first() if sk: res_talent_skill = TalentSkill.objects.filter( talent=talent, skill=sk).first() if not res_talent_skill: # add this item new_talent_skill = TalentSkill.objects.create( talent=talent, skill=sk) new_talent_skill.save() # add position sub types if sub_skills: self.add_talent_sub_skills(talent, sk, sub_skills) def add_talent_sub_skills(self, talent, skill, sub_skills): for sub_skill in sub_skills: # check position sub types and skill if exist, else add it. if 'id' in sub_skill: ssk = SubSkill.objects.get(id=sub_skill['id']) else: ssk = SubSkill.objects.filter(skill=skill, name=sub_skill['name']).first() if ssk: res_talent_sub_skill = TalentSubSkill.objects.filter( talent=talent, sub_skill=ssk).first() if not res_talent_sub_skill: # add this item new_talent_sub_skill = TalentSubSkill.objects.create( talent=talent, sub_skill=ssk) new_talent_sub_skill.save() def delete_all_talent_position_and_skill(self, talent): # delete all position types of talent TalentPositionType.objects.filter(talent=talent).delete() # delete all position sub types of talent TalentPositionSubType.objects.filter(talent=talent).delete() # delete all skills of talent TalentSkill.objects.filter(talent=talent).delete() # delete all sub skills of talent TalentSubSkill.objects.filter(talent=talent).delete() def pickout_data(self, data, child_name): res = {} if child_name in data: res = data[child_name] data.pop(child_name, None) return res def wrap_generate_tid(self, tid, object_list): new_tid = tid for object in object_list: new_tid = "{tid}{prefix}".format(tid=new_tid, prefix=object.name[0]) return new_tid def generate_tid_by_list(self, talent, positions, sub_positions): tid = self.wrap_generate_tid('', positions) tid = "{tid}{talent_id}".format(tid=tid, talent_id=talent.id) talent.tid = tid talent.save return tid def generate_tid_by_one(self, talent, position): tid = "{prefix}{talent_id}".format(prefix=position[0], talent_id=talent.id) talent.tid = tid talent.save return tid @swagger_auto_schema(responses={200: TalentSerializer(many=False)}) 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: 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() return Response(serializer.data) @swagger_auto_schema(request_body=TalentSerializer, responses={200: TalentSerializer(many=False)}) 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) def delete(self, request, pk, format=None): talent_item = self.get_object(pk) talent_item.delete() return Response(status=status.HTTP_204_NO_CONTENT) @swagger_auto_schema( request_body=WizardTalentInfoSerializer, responses={200: WizardTalentInfoSerializer(many=False)}) def post(self, request, pk, format=None): print('== request.data: ', request.data) talent_item = self.get_object(pk) talent_data = request.data serializer = WizardTalentInfoSerializer(talent_item, data=talent_data) if serializer.is_valid(): # pick out position types talent_position_types_data = self.pickout_data( talent_data, 'talent_position_types') # pick out skills talent_skills_data = self.pickout_data(talent_data, 'talent_skills') # pick out initialize flag need_initialize = self.pickout_data(talent_data, 'need_initialize') if need_initialize: # delete all positions and skills of talent self.delete_all_talent_position_and_skill(talent_item) # add position type and skills from request if talent_position_types_data: self.add_talent_position_types(talent_item, talent_position_types_data) if talent_skills_data: self.add_talent_skills(talent_item, talent_skills_data) self.generate_tid_by_list(talent_item, talent_position_types_data) return Response(request.data, status=status.HTTP_200_OK) return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): talent_item = self.get_object(request.user) serializer = TalentSerializer(talent_item) return Response(serializer.data)
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)
class TalentDetail(APIView): # authentication_classes = (SessionAuthentication, JSONWebTokenAuthentication) # permission_classes = (permissions.IsAuthenticated,) """ Retrieve, update or delete a talent. """ def get_object(self, pk): try: user = User.objects.get(pk=pk) if user.type == 'agency' and user.is_staff: admin = user talent = Talent.objects.filter(user=user.id).first() return talent except Talent.DoesNotExist: raise Http404 def save_talent_position_type(self, talent, position_type_name): # delete all position types of talent TalentPositionType.objects.filter(talent=talent).delete() # delete all sub skills of talent TalentPositionSubType.objects.filter(talent=talent).delete() # save position type position_type = PositionType.objects.get(name=position_type_name) if position_type: new_talent_position_type = TalentPositionType.objects.create( talent=talent, position_type=position_type) new_talent_position_type.save() def save_talent_position_sub_type(self, talent, position_sub_type_name, position_type_name): # delete all position sub types of talent TalentPositionSubType.objects.filter(talent=talent).delete() # save position type position_type = PositionType.objects.get(name=position_type_name) position_sub_type = PositionSubType.objects.filter( name=position_sub_type_name, position_type=position_type).first() if position_sub_type: new_talent_position_sub_type = TalentPositionSubType.objects.create( talent=talent, position_sub_type=position_sub_type) new_talent_position_sub_type.save() def save_talent_position_sub_types(self, talent, position_sub_type_names, position_type_name): # delete all position sub types of talent TalentPositionSubType.objects.filter(talent=talent).delete() # save position type position_type = PositionType.objects.get(name=position_type_name) position_sub_types = PositionSubType.objects.filter( name__in=position_sub_type_names, position_type=position_type) if len(position_sub_types) > 0: for position_sub_type in position_sub_types: new_talent_position_sub_type = TalentPositionSubType.objects.create( talent=talent, position_sub_type=position_sub_type) new_talent_position_sub_type.save() def save_talent_skills(self, talent, talent_skills): # delete all skills of talent TalentSkill.objects.filter(talent=talent).delete() # save all talent skills for talent_skill in talent_skills: skill = Skill.objects.get(name=talent_skill['name']) new_talent_skill = TalentSkill.objects.create(talent=talent, skill=skill) new_talent_skill.save() def save_talent_sub_skills(self, talent, talent_sub_skills): print('====== save_talent_sub_skills: ', talent_sub_skills) # delete all sub skills of talent TalentSubSkill.objects.filter(talent=talent).delete() # save all talent sub skills for talent_sub_skill in talent_sub_skills: sub_skill = SubSkill.objects.filter( name=talent_sub_skill['name']).first() new_talent_sub_skill = TalentSubSkill.objects.create( talent=talent, sub_skill=sub_skill) new_talent_sub_skill.save() def save_talent_visas(self, talent, talent_visas): # Delete all visas of talent TalentVisa.objects.filter(talent=talent).delete() # Save visas talent_visas = talent_visas for visa in talent_visas: new_visa = TalentVisa.objects.create( talent=talent, name=visa['name'], expiration_date=visa['expiration_date']) new_visa.save() def add_talent_position_types(self, talent, talent_position_types): for talent_position_type in talent_position_types: position_type = talent_position_type['position_type'] position_sub_types = talent_position_type['position_sub_types'] if position_type: # check position type and skip if exist, else add it if 'id' in position_type: pt = PositionType.objects.get(id=position_type['id']) else: pt = PositionType.objects.filter( name=position_type['name']).first() if pt: res_talent_position_type = TalentPositionType.objects.filter( talent=talent, position_type=pt).first() if not res_talent_position_type: # add this item new_talent_position_type = TalentPositionType.objects.create( talent=talent, position_type=pt) new_talent_position_type.save() # add position sub types if position_sub_types: self.add_talent_position_sub_types(talent, pt, position_sub_types) def add_talent_position_sub_types(self, talent, position_type, position_sub_types): for position_sub_type in position_sub_types: # check position sub types and skill if exist, else add it. if 'id' in position_sub_type: pst = PositionSubType.objects.get(id=position_sub_type['id']) else: pst = PositionSubType.objects.filter( position_type=position_type, name=position_sub_type['name']).first() if pst: res_talent_position_sub_type = TalentPositionSubType.objects.filter( talent=talent, position_sub_type=pst).first() if not res_talent_position_sub_type: # add this item new_talent_position_sub_type = TalentPositionSubType.objects.create( talent=talent, position_sub_type=pst) new_talent_position_sub_type.save() def add_talent_skills(self, talent, talent_skills): for talent_skill in talent_skills: skill = talent_skill['skill'] sub_skills = talent_skill['sub_skills'] if skill: # check skill and skip if exist, else add it if 'id' in skill: sk = Skill.objects.get(id=skill['id']) else: sk = Skill.objects.filter(name=skill['name']).first() if sk: res_talent_skill = TalentSkill.objects.filter( talent=talent, skill=sk).first() if not res_talent_skill: # add this item new_talent_skill = TalentSkill.objects.create( talent=talent, skill=sk) new_talent_skill.save() # add position sub types if sub_skills: self.add_talent_sub_skills(talent, sk, sub_skills) def add_talent_sub_skills(self, talent, skill, sub_skills): for sub_skill in sub_skills: # check position sub types and skill if exist, else add it. if 'id' in sub_skill: ssk = SubSkill.objects.get(id=sub_skill['id']) else: ssk = SubSkill.objects.filter(skill=skill, name=sub_skill['name']).first() if ssk: res_talent_sub_skill = TalentSubSkill.objects.filter( talent=talent, sub_skill=ssk).first() if not res_talent_sub_skill: # add this item new_talent_sub_skill = TalentSubSkill.objects.create( talent=talent, sub_skill=ssk) new_talent_sub_skill.save() def delete_all_talent_position_and_skill(self, talent): # delete all position types of talent TalentPositionType.objects.filter(talent=talent).delete() # delete all position sub types of talent TalentPositionSubType.objects.filter(talent=talent).delete() # delete all skills of talent TalentSkill.objects.filter(talent=talent).delete() # delete all sub skills of talent TalentSubSkill.objects.filter(talent=talent).delete() def pickout_data(self, data, child_name): res = {} if child_name in data: res = data[child_name] data.pop(child_name, None) return res def wrap_generate_tid(self, tid, object_list): new_tid = tid for object in object_list: print('==== object: ', object) if 'name' in object: new_tid = "{tid}{prefix}".format(tid=new_tid, prefix=object.name[0]) elif 'position_type' in object: new_tid = "{tid}{prefix}".format( tid=new_tid, prefix=object['position_type']['name'][0]) return new_tid def generate_tid_by_list(self, talent, positions): tid = self.wrap_generate_tid('', positions) tid = "{tid}{talent_id}".format(tid=tid, talent_id=talent.id) UserNoteManager.tid_logger( None, None, talent.user, '{action} {tid} by SYS'.format( action='Changed to' if talent.tid else 'Assigned', tid=tid), talent) talent.tid = tid talent.save() return tid def generate_tid_by_one(self, talent, position): tid = "{prefix}{talent_id}".format(prefix=position[0], talent_id=talent.id) UserNoteManager.tid_logger( None, None, talent.user, '{action} {tid} by SYS'.format( action='Changed to' if talent.tid else 'Assigned', tid=tid), talent) talent.tid = tid talent.save() return tid @swagger_auto_schema(responses={200: TalentSerializer(many=False)}) 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) @swagger_auto_schema(request_body=TalentSerializer, responses={200: TalentSerializer(many=False)}) 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) def delete(self, request, pk, format=None): talent_item = self.get_object(pk) talent_item.delete() return Response({'id': int(pk)}, status=status.HTTP_200_OK) @swagger_auto_schema( request_body=WizardTalentInfoSerializer, responses={200: WizardTalentInfoSerializer(many=False)}) def post(self, request, pk, format=None): talent_item = self.get_object(pk) talent_data = request.data serializer = WizardTalentInfoSerializer(talent_item, data=talent_data) if serializer.is_valid(): # pick out position types talent_position_types_data = self.pickout_data( talent_data, 'talent_position_types') # pick out skills talent_skills_data = self.pickout_data(talent_data, 'talent_skills') # pick out initialize flag need_initialize = self.pickout_data(talent_data, 'need_initialize') if need_initialize: # delete all positions and skills of talent self.delete_all_talent_position_and_skill(talent_item) # add position type and skills from request if talent_position_types_data: self.add_talent_position_types(talent_item, talent_position_types_data) if talent_skills_data: self.add_talent_skills(talent_item, talent_skills_data) self.generate_tid_by_list(talent_item, talent_position_types_data) return Response(request.data, status=status.HTTP_200_OK) return Response({'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
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)
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)