Skip to content

Instantly share code, notes, and snippets.

@olivx
Created April 10, 2018 18:40
Show Gist options
  • Save olivx/fc3bcabaa92800e3582a06da25aae347 to your computer and use it in GitHub Desktop.
Save olivx/fc3bcabaa92800e3582a06da25aae347 to your computer and use it in GitHub Desktop.
def attach_to_job(request, items, job_id, company, _candi_type='candidate'):
if items:
if not job_id:
messages.error(request, _('Selecione ao menos uma vaga.'))
else:
job = Job.objects.get(id=job_id)
job_stage = JobStages.objects.get(
job=job,
order=1
)
if _candi_type == 'candidate':
user_list = Candidate.objects.filter(pk__in=items).values_list('candidate', flat=True)
candidate_in_job = Candidate.objects.filter(
candidate__in=user_list, job=job).values_list('candidate', flat=True)
else:
user_list = items
company_candidate_dict = {}
for candidate in CompanyCandidate.objects.filter(candidate__in=user_list, company=company).values('candidate', 'source', 'not_hire'):
company_candidate_dict[candidate['candidate']] = {
'source':candidate['source'], 'not_hire':candidate['not_hire']
}
candidate_to_atttach = list()
stage_to_atttacch = list()
users = User.objects.filter(pk__in=user_list)
for user in users:
if _candi_type == 'candidate':
if user.pk not in candidate_in_job:
source = company_candidate_dict[user.pk]['source']
not_hire = company_candidate_dict[user.pk]['not_hire']
candidate = Candidate(
job=job,
candidate=user,
status='1',
status_date=timezone.now(),
apply_date=timezone.now(),
stage_status=job_stage,
source=source,
not_hire=not_hire
)
candidate_stage_trail = CandidateStageTrail(
job=job,
candidate=user,
stage_status=job_stage,
status_date=timezone.now()
)
else:
source = company_candidate_dict[user.pk]['source']
not_hire = company_candidate_dict[user.pk]['not_hire']
candidate = Candidate(
job=job,
candidate=user,
status='1',
status_date=timezone.now(),
apply_date=timezone.now(),
stage_status=job_stage,
source=source,
not_hire=not_hire
)
candidate_stage_trail = CandidateStageTrail(
job=job,
candidate=user,
stage_status=job_stage,
status_date=timezone.now()
)
candidate_to_atttach.append(candidate)
stage_to_atttacch.append(candidate_stage_trail)
Candidate.objects.bulk_create(candidate_to_atttach)
CandidateStageTrail.objects.bulk_create(stage_to_atttacch)
messages.success(request, _(
'Sucesso! Candidatos inseridos na Vaga %s') % (job.title))
else:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment