Exemplo n.º 1
0
 def createJob(self):
     job = job_record(
         job_id=100,
         agency="test_agency",
         posting_type="test_posting_type",
         num_positions=10,
         business_title="test_business_title",
         civil_service_title="test_civil_service_title",
         title_classification="test_title_classification",
         title_code_no="test_title_code_no",
         level="test_level",
         job_category="test_job_category",
         full_time_part_time_indicator="test_full_time",
         career_level="test_career_level",
         salary_range_from=100000,
         salary_range_to=120000,
         salary_frequency="test_salary_frequency",
         work_location="test_work_location",
         division_work_unit="test_division_work_unit",
         job_description="test_job_description",
         minimum_qual_requirements="test_minimum_qual_requirements",
         preferred_skills="test_preferred_skills",
         additional_information="test_additional_information",
         to_apply="test_to_apply",
         hours_shift="test_hours_shift",
         work_location_1="test_work_location_1",
         recruitment_contact="test_recruitment_contact",
         residency_requirement="test_residency_requirement",
         posting_date=timezone.now(),
         post_until=None,
         posting_updated=None,
         process_date=timezone.now(),
     )
     job.save()
    def test_recommended_jobs_view(self):

        # set up fake jobs
        for i in range(15):
            job = job_record(
                job_id=i,
                agency="test_agency",
                posting_type="External",
                num_positions=10,
                business_title="test_business_title",
                civil_service_title="title_description_" + str(i + 1),
                title_classification="test_title_classification",
                title_code_no="test_title_code_no",
                level="test_level",
                job_category="test_job_category",
                full_time_part_time_indicator="test_full_time",
                career_level="test_career_level",
                salary_range_from=100000,
                salary_range_to=120000,
                salary_frequency="test_salary_frequency",
                work_location="test_work_location",
                division_work_unit="test_division_work_unit",
                job_description="test_job_description",
                minimum_qual_requirements="test_minimum_qual_requirements",
                preferred_skills="test_preferred_skills",
                additional_information="test_additional_information",
                to_apply="test_to_apply",
                hours_shift="test_hours_shift",
                work_location_1="test_work_location_1",
                recruitment_contact="test_recruitment_contact",
                residency_requirement="test_residency_requirement",
                posting_date=timezone.now(),
                post_until=None,
                posting_updated=None,
                process_date=timezone.now(),
            )
            job.save()

        # Test-Check if user is authenticated
        user_login = self.client.login(username=self.test_user.username,
                                       password="******")
        self.assertTrue(user_login)
        """ Expectation is  1 because saved Jobs  executes since preference is null(
         not yet set) """
        # response = self.client.post(
        #     reverse("jobs:saveJob", kwargs={"pk": job_record.objects.get(id=5).id})
        # )
        # self.assertEqual(json.loads(response.content)["response_data"], "Job Saved")
        # saved_job = (
        #     job_record.objects.filter(civil_service_title="title_description_4")
        #     .filter(
        #         Q(post_until__gte=datetime.date.today()) | Q(post_until__isnull=True)
        #     )
        #     .filter(posting_type__iexact="External")
        #     .distinct()
        #     .order_by("-posting_date")[:10]
        # )
        # print(saved_job.count())
        # response = self.client.get(reverse("dashboard:recommendedjobs"))
        # self.assertEqual(response.context["jobs"].count(), 1)

        # Test-Pass Title ID 1 as the one user is interested and
        # ID 2 as one user currently holds
        response = self.client.post(
            reverse("signin:SaveCivilServiceTitleView"),
            data={"user_int_cst[]": [1]},
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            json.loads(response.content)["response_data"], "CST_SAVED")

        response = self.client.get(reverse("dashboard:recommendedjobs"))

        self.assertEqual(response.context["jobs"].count(), 1)

        # # Test - If user can save a job and save job 3 as saved job for the user

        response = self.client.post(
            reverse("signin:SaveCivilServiceTitleView"),
            data={
                "user_int_cst[]": [1],
                "user_curr_cst[]": [2]
            },
        )

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            json.loads(response.content)["response_data"], "CST_SAVED")

        response = self.client.get(reverse("dashboard:recommendedjobs"))

        # Expectation is 2 because we have 2 CSTs saved in preferences and
        # savedjobs CST will not be executed
        self.assertEqual(response.context["jobs"].count(), 2)

        # Testing when there are more than 10 Jobs matching the CSTs in preferences
        response = self.client.post(
            reverse("signin:SaveCivilServiceTitleView"),
            data={
                "user_int_cst[]": [1, 3, 4, 5, 6, 7, 8, 9],
                "user_curr_cst[]": [2, 11, 13, 12],
            },
        )
        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            json.loads(response.content)["response_data"], "CST_SAVED")

        response = self.client.get(reverse("dashboard:recommendedjobs"))
        self.assertEqual(response.context["jobs"].count(), 10)
    def setUp(self):
        self.test_user = User.objects.create_user(
            is_hiring_manager="False",
            username="******",
            first_name="Jane",
            last_name="Doe",
            dob="1994-10-02",
            email="*****@*****.**",
            password="******",
        )

        # set up fake jobs
        for i in range(10):
            job = job_record(
                job_id=i,
                agency="test_agency",
                posting_type="test_posting_type",
                num_positions=10,
                business_title="test_business_title",
                civil_service_title="test_civil_service_title",
                title_classification="test_title_classification",
                title_code_no="test_title_code_no",
                level="test_level",
                job_category="test_job_category",
                full_time_part_time_indicator="test_full_time",
                career_level="test_career_level",
                salary_range_from=100000,
                salary_range_to=120000,
                salary_frequency="test_salary_frequency",
                work_location="test_work_location",
                division_work_unit="test_division_work_unit",
                job_description="test_job_description",
                minimum_qual_requirements="test_minimum_qual_requirements",
                preferred_skills="test_preferred_skills",
                additional_information="test_additional_information",
                to_apply="test_to_apply",
                hours_shift="test_hours_shift",
                work_location_1="test_work_location_1",
                recruitment_contact="test_recruitment_contact",
                residency_requirement="test_residency_requirement",
                posting_date=timezone.now(),
                post_until=None,
                posting_updated=None,
                process_date=timezone.now(),
            )
            job.save()

        # set up fake Civil Service title
        for i in range(15):
            civil_service_title = CivilServicesTitle(
                title_code=i + 1,
                title_description="title_description_" + str(i + 1))
            civil_service_title.save()

        # set up fake Exam Reuslt Service title
        exam = ExamResultsActive(
            exam_number=1,
            list_number=1,
            first_name="first_name",
            last_name="last_name",
            middle_initial="middle",
            adjust_final_average=100.0,
            list_title_code="1234",
            list_title_desc="test_desc",
        )
        exam.save()
def update_Jobs():
    columns = [
        "job_id",
        "agency",
        "posting_type",
        "num_positions",
        "business_title",
        "civil_service_title",
        "title_classification",
        "title_code_no",
        "level",
        "job_category",
        "full_time_part_time_indicator",
        "career_level",
        "salary_range_from",
        "salary_range_to",
        "salary_frequency",
        "work_location",
        "division_work_unit",
        "job_description",
        "minimum_qual_requirements",
        "preferred_skills",
        "additional_information",
        "to_apply",
        "hours_shift",
        "work_location_1",
        "recruitment_contact",
        "residency_requirement",
        "posting_date",
        "post_until",
        "posting_updated",
        "process_date",
    ]

    limit = 10000
    record_count = int(client.get("kpav-sd4t", select="COUNT(*)")[0]["COUNT"])
    print("Total Records Before Preprocessing: ", record_count)
    offset = 0
    jobrecord = []
    while offset < record_count:
        try:
            jobs_list = client.get("kpav-sd4t", offset=offset, limit=limit)
        except Exception as e:
            print("API Errors", e)
        else:

            jobs_list_df = pd.DataFrame.from_records(jobs_list)
            jobs_list_df = jobs_list_df.replace(r"^\s*$", None, regex=True)
            jobs_list_df = jobs_list_df.where(jobs_list_df.notnull(), None)
            jobs_list_df = jobs_list_df[jobs_list_df["job_id"].notnull()]

            for c in columns:
                if c not in jobs_list_df.columns:
                    jobs_list_df[c] = None

            for index, row in jobs_list_df.iterrows():
                try:
                    # print(row["job_id"])
                    val_in_db = job_record.objects.filter(
                        job_id=row["job_id"],
                        agency=row["agency"],
                        posting_type=row["posting_type"],
                        title_code_no=row["title_code_no"],
                    )
                    if not val_in_db.exists():
                        jobrecord.append(
                            job_record(
                                job_id=row["job_id"],
                                agency=row["agency"],
                                posting_type=row["posting_type"],
                                num_positions=row["number_of_positions"],
                                business_title=fix_text_encoding(
                                    row["business_title"]),
                                civil_service_title=civil_service_title_cleanup(
                                    row["civil_service_title"]),
                                title_classification=
                                title_classification_cleanup(
                                    row["title_classification"]),
                                title_code_no=row["title_code_no"],
                                level=row["level"],
                                job_category=row["job_category"],
                                full_time_part_time_indicator=
                                full_time_part_time_indicator_cleanup(
                                    row["full_time_part_time_indicator"]
                                ),  # Full-time/Part-time
                                career_level=row["career_level"],
                                salary_range_from=row["salary_range_from"],
                                salary_range_to=row["salary_range_to"],
                                salary_frequency=row["salary_frequency"],
                                work_location=fix_text_encoding(
                                    row["work_location"]),
                                division_work_unit=row["division_work_unit"],
                                job_description=fix_text_encoding(
                                    row["job_description"]),
                                minimum_qual_requirements=fix_text_encoding(
                                    row["minimum_qual_requirements"]),
                                preferred_skills=fix_text_encoding(
                                    row["preferred_skills"]),
                                additional_information=fix_text_encoding(
                                    row["additional_information"]),
                                to_apply=fix_text_encoding(row["to_apply"]),
                                hours_shift=row["hours_shift"],
                                work_location_1=fix_text_encoding(
                                    row["work_location_1"]),
                                recruitment_contact=fix_text_encoding(
                                    row["recruitment_contact"]),
                                residency_requirement=fix_text_encoding(
                                    row["residency_requirement"]),
                                posting_date=getAwareDate(row["posting_date"]),
                                post_until=convertDateFormat(
                                    row["post_until"]
                                ),  # if not is_nan(row['post_until']) else None,
                                posting_updated=getAwareDate(
                                    row["posting_updated"]),
                                process_date=getAwareDate(row["process_date"]),
                            ))
                except Exception as e:
                    print("Error", e)

            offset += limit

    print("Found", len(jobrecord), "new entries for Jobs")
    if len(jobrecord) > 0:
        job_record.objects.bulk_create(jobrecord, ignore_conflicts=True)
        print("Inserted New Job records")
    else:
        print("No new Job records to insert")