def post(self, request, job_id=None): user = get_user(request) user_company = user.companies.first() vacancy = None if job_id: vacancy = self.get_vacancy(request, job_id) form = VacancyForm(request.POST) if not form.is_valid(): return render(request, 'vacancies/user/vacancy_edit.html', {'form': form}) if vacancy: vacancy.title = form.cleaned_data['title'] vacancy.specialty = form.cleaned_data['specialty'] vacancy.skills = form.cleaned_data['skills'] vacancy.description = form.cleaned_data['description'] vacancy.salary_min = form.cleaned_data['salary_min'] vacancy.salary_max = form.cleaned_data['salary_max'] else: vacancy = Vacancy( title=form.cleaned_data['title'], specialty=form.cleaned_data['specialty'], skills=form.cleaned_data['skills'], description=form.cleaned_data['description'], salary_min=form.cleaned_data['salary_min'], salary_max=form.cleaned_data['salary_max'], company=user_company, ) vacancy.save() return redirect(reverse('my_vacancy_form', args=[vacancy.id]) + '?submitted=True')
def _fill_vacancies(): vacancy_titles: List[str] = [ 'Разработчик на Python', 'Разработчик в проект на Django', 'Разработчик на Swift в аутсорс компанию', 'Мидл программист на Python', 'Питонист в стартап', ] for company in Company.objects.all(): specialty_list = Specialty.objects.all() number_of_specialties: int = len(specialty_list) number_of_vacancies: int = choice(range(1, 4)) for index in range(number_of_vacancies): specialty: Specialty = specialty_list[choice( range(number_of_specialties))] salary_min = choice(range(10, 20)) * 10000 vacancy_instance = Vacancy( title=choice(vacancy_titles), specialty=specialty, company=company, description= f'Описание вакансии №{index} для компании {company.name}', salary_min=salary_min, salary_max=salary_min + choice(range(1, 10)) * 10000, skills='Бэкенд, Старший (Senior), C#, ASP.NET MVC, MySQL, Git', ) vacancy_instance.save()
def main(): # companies Company.objects.all().delete() for company in data.companies: new_company = Company( id=company['id'], name=company['title'], location=company['location'], logo=company['logo'], description=company['description'], employee_count=company['employee_count'], ) new_company.save() # specialties Specialty.objects.all().delete() for specialty in data.specialties: new_specialty = Specialty( code=specialty['code'], title=specialty['title'], picture='specty_' + specialty['code'] + '.png', ) new_specialty.save() # vacancies Vacancy.objects.all().delete() for job in data.jobs: new_vacancy = Vacancy( id=job['id'], title=job['title'], specialty=Specialty.objects.get(code=job['specialty']), company=Company.objects.get(id=job['company']), skills=job['skills'], description=job['description'], salary_min=job['salary_from'], salary_max=job['salary_to'], published_at=job['posted'], ) new_vacancy.save() # settings Settings.objects.all().delete() new_setting = Settings(name='site_title', setval='Джуманджи') new_setting.save() new_setting = Settings(name='site_description', setval='Вакансии для <br>Junior-разработчиков') new_setting.save()
def get(self, request, vacancy_id): user = request.user if user.is_anonymous: return redirect(reverse('login')) company = get_object_or_404(Company, owner=user) vacancy = Vacancy.objects.filter(id=vacancy_id).annotate(number_of_responses=Count('applications')).first() if vacancy is None: vacancy = Vacancy(company=company, specialty=None) applications = Application.objects.filter(vacancy=vacancy).all() form = VacancyEditForm({'title': vacancy.title, 'skills': vacancy.skills, # возникает ошибка ValueError # Exception Value: # Cannot assign "'backend'": "Vacancy.specialty" must be a "Specialty" instance. # 'specialty': vacancy.specialty, # поэтому создаём свой элемент в форме и используем его 'form_specialty': str(vacancy.specialty), 'description': vacancy.description, 'salary_min': vacancy.salary_min, 'salary_max': vacancy.salary_max, }) return render(request, self.template_name, {'form': form, 'vacancy': vacancy, 'company': company, 'applications': applications, 'title_left': 'Моя компания | Вакансия'})
def catch_previous_vacancy_status(sender, instance: Vacancy, **kwargs): try: old_data: Vacancy = Vacancy.objects.get(id=instance.pk) except Vacancy.DoesNotExist: pass else: instance.context[ 'is_status_changed'] = old_data.status != instance.status
def post_job(request): success = False ctx = None logged_user_id = request.session.get('logged_user', False) if request.method == 'POST': companyObj = Companies.objects.get(auth_id_id=logged_user_id) job_form = JobPostingForm(request.POST) if job_form.is_valid(): success = True title = job_form.cleaned_data['title'] description = job_form.cleaned_data['description'] district = job_form.cleaned_data['district'] experience = job_form.cleaned_data['experience'] personality = job_form.cleaned_data['personality'] vacancy_type = job_form.cleaned_data['vacancy_type'] salary_ranges = job_form.cleaned_data['salary_ranges'] new_job = Vacancy( company_id=companyObj, name=title, description=description, district=district, experience=experience, vacancy_type=vacancy_type, salary_ranges=salary_ranges, updated=timezone.now(), created=timezone.now(), ) new_job.save() personality = request.POST.getlist('personality') for item in personality: Vacancy_personality(vacancy_id_id=new_job.pk, personality_id=item.encode('utf8')).save() skills = request.POST.getlist('skills') for skill in skills: Vacancy_skills(vacancy_id_id=new_job.pk, skill_id_id=skill.encode('utf8')).save() new_job_form = JobPostingForm() ctx = {'success':success, 'job_form':new_job_form} return render_to_response('post_job.html', ctx , context_instance=RequestContext(request)) else: job_form = JobPostingForm() ctx = {'job_form':job_form} return render_to_response('post_job.html', ctx, context_instance=RequestContext(request))
def handle(self, *args, **options): for specialty in specialties: sp = Specialty(code=specialty['code'], title=specialty['title'], picture='https://place-hold.it/100x60') sp.save() for company in companies: cm = Company(name=company['title'], logo='https://place-hold.it/100x60') cm.save() for job in jobs: vc = Vacancy( title=job['title'], specialty=Specialty.objects.filter(code=job['cat']).first(), company=Company.objects.filter(name=job['company']).first(), salary_min=job['salary_from'], salary_max=job['salary_to'], published_at=job['posted'], description=job['desc']) vc.save()
def create_user_vacancy(request): user = request.user if user.is_anonymous: return redirect(reverse('login')) # company = get_object_or_404(Company, owner=user) company = Company.objects.filter(owner=user).first() if company is None: raise Http404 vacancy = Vacancy(company=company) return render(request, 'vacancies/vacancy-edit.html', {'form': VacancyEditForm(), 'vacancy': vacancy, 'company': company, 'title_left': 'Моя компания | Вакансия'})
def run(): cache = { 'companies': {}, 'specialties': {}, } for _specialty in specialties: picture = _specialty.get('logo', '') if picture: picture = f'{settings.MEDIA_SPECIALITY_IMAGE_DIR}/{picture}' else: picture = f'{settings.MEDIA_SPECIALITY_IMAGE_DIR}/{settings.NO_LOGO_PIC}' specialty = Specialty.objects.create(code=_specialty['code'], title=_specialty['title'], picture=picture) cache['specialties'][specialty.code] = specialty for company_no, _company in enumerate(companies): logo = _company.get('logo', '') if logo: logo = f'{settings.MEDIA_COMPANY_IMAGE_DIR}/{logo}' else: logo = f'{settings.MEDIA_COMPANY_IMAGE_DIR}/no_logo.png' company = Company.objects.create(name=_company['title'], location=_company['location'], employee_count=randint(1, 10), logo=logo, description=_company.get( 'description', ''), owner=create_user(company_no)) cache['companies'][company.name] = company for job in jobs: vacancy = Vacancy( title=job['title'], published_at=job['posted'], salary_min=job.get('salary_from'), salary_max=job.get('salary_to'), description=job['desc'], specialty=cache['specialties'][job['cat']], company=cache['companies'][job['company']], is_remote=job.get('is_remote', False), ) vacancy.save() for _skill in job.get('skills', []): skill, _ = Skill.objects.get_or_create(title=_skill) vacancy.skills.add(skill) vacancy.save()
def handle(self, *args, **kwargs): for direction in specialties: item = DotMap(direction) new_item = Specialty( code=item.code, title=item.title, picture=item.picture ) new_item.save() for company in companies: item = DotMap(company) new_item = Company( id=item.id, title=item.title, location=item.location, logo=item.logo, description=item.description, employee_count=item.employee_count ) new_item.save() for job in jobs: item = DotMap(job) direction = Specialty.objects.filter(code=item.specialty).first() company = Company.objects.get(id=item.company) new_item = Vacancy( id=item.id, title=item.title, specialty=direction, company=company, skills=item.skills, description=item.description, salary_min=item.salary_from, salary_max=item.salary_to, posted=item.posted ) new_item.save()
def test_saving_and_retrieving_vacancies(self): username1 = '*****@*****.**' password = '******' user1 = User.objects.create_user(username1, password) first_vacancy = Vacancy() first_vacancy.user = user1 first_vacancy.employer = self.employer first_vacancy.title = 'Welder TIG' first_vacancy.salary_min = 50000 first_vacancy.save() second_vacancy = Vacancy() username2 = '*****@*****.**' password = '******' user2 = User.objects.create_user(username2, password) second_vacancy.user = user2 second_vacancy.employer = self.employer second_vacancy.title = "Welder MIG-MAG" second_vacancy.salary_min = 60000 second_vacancy.save() saved_vacancies = Vacancy.objects.all() self.assertEqual(saved_vacancies.count(), 2) first_saved_vacancy = saved_vacancies[0] second_saved_vacancy = saved_vacancies[1] self.assertEqual(first_saved_vacancy.title, 'Welder TIG') self.assertEqual(second_saved_vacancy.title, 'Welder MIG-MAG')
def post(self, request, vacancy_id): user = request.user if user.is_anonymous: return redirect(reverse('login')) company = get_object_or_404(Company, owner=user) vacancy = Vacancy.objects.filter(id=vacancy_id).annotate(number_of_responses=Count('applications')).first() if vacancy is None: vacancy = Vacancy(company=company) applications = Application.objects.filter(vacancy=vacancy).all() form = VacancyEditForm(request.POST) context = {'form': form, 'company': company, 'vacancy': vacancy, 'applications': applications, 'title_left': 'Моя компания | Вакансия'} if form.is_valid(): data = form.cleaned_data vacancy.title = data['title'] specialty = Specialty.objects.filter(code=data['form_specialty']).first() if specialty is not None: vacancy.specialty = specialty vacancy.company = company vacancy.salary_min = data['salary_min'] vacancy.salary_max = data['salary_max'] vacancy.skills = data['skills'] vacancy.description = data['description'] vacancy.save() context['vacancy_specialty'] = data['specialty'] context['info_updated'] = True return render(request, 'vacancies/vacancy-edit.html', context)
def main(): # users if not User.objects.all().count(): last_login = '******' User.objects.create_superuser(username='******', email='admin@stepik_vacancies.com', password='******', last_login=last_login, first_name='Иван', last_name='Петров') User.objects.create_user(username='******', email='user1@stepik_vacancies.com', password='******', last_login=last_login, first_name='Петр', last_name='Сергеев') User.objects.create_user(username='******', email='user2@stepik_vacancies.com', password='******', last_login=last_login, first_name='Сергей', last_name='Иванов') # companies users = User.objects.all() Company.objects.all().delete() for company in data.companies: user = users[randint(0, 2)] new_company = Company( id=company['id'], name=company['title'], location=company['location'], description=company['description'], employee_count=company['employee_count'], owner=user, ) new_company.save() # specialties Specialty.objects.all().delete() for specialty in data.specialties: new_specialty = Specialty( code=specialty['code'], title=specialty['title'], ) new_specialty.save() # vacancies Vacancy.objects.all().delete() for job in data.jobs: new_vacancy = Vacancy( id=job['id'], title=job['title'], specialty=Specialty.objects.get(code=job['specialty']), company=Company.objects.get(id=job['company']), skills=job['skills'], description=job['description'], salary_min=job['salary_from'], salary_max=job['salary_to'], published_at=job['posted'], ) new_vacancy.save()
def test_get_vacancy_count(self): username1 = '*****@*****.**' password = '******' user1 = User.objects.create_user(username1, password) first_vacancy = Vacancy() first_vacancy.user = user1 first_vacancy.employer = self.employer_1 first_vacancy.title = 'Welder TIG' first_vacancy.salary_min = 50000 first_vacancy.save() second_vacancy = Vacancy() username2 = '*****@*****.**' password = '******' user2 = User.objects.create_user(username2, password) second_vacancy.user = user2 second_vacancy.employer = self.employer_1 second_vacancy.title = "Welder MIG-MAG" second_vacancy.salary_min = 60000 second_vacancy.save() self.assertEqual(2, self.employer_1.get_vacancy_count())
if __name__ == '__main__': for spec in data.specialties: specialty = Specialty(code=spec['code'], title=spec['title'], picture=f'{settings.MEDIA_SPECIALITY_IMAGE_DIR}/specty_{spec["code"]}.png') specialty.save() loc = ['Москва', 'Калуга', 'п. Марс', 'Брянск', 'Прага', 'Лос-Анджелес', 'Сухиничи'] for index, company in enumerate(data.companies, start=1): company = Company(name=company['title'], location=random.choice(loc), logo=f'{settings.MEDIA_COMPANY_IMAGE_DIR}/logo{index}.png', description='', employee_count=random.randint(1, 1000)) company.save() skills = ['Python', 'Django', 'Git', 'Linux', 'CSS', 'Excel', 'Photoshop', 'ICQ', 'C++', 'Java', 'C#'] for vacancy in data.jobs: specialty = Specialty.objects.get(code=vacancy['cat']) company = Company.objects.get(name=vacancy['company']) _vacancy = Vacancy(title=vacancy['title'], specialty=specialty, company=company, skills=', '.join(random.sample(skills, 4)), description=vacancy['desc'], salary_min=int(vacancy['salary_from']), salary_max=int(vacancy['salary_to']), published_at=vacancy['posted']) _vacancy.save()
specialties = [ {"code": "frontend", "title": "Фронтенд"}, {"code": "backend", "title": "Бэкенд"}, {"code": "gamedev", "title": "Геймдев"}, {"code": "devops", "title": "Девопс"}, {"code": "design", "title": "Дизайн"}, {"code": "products", "title": "Продукты"}, {"code": "management", "title": "Менеджмент"}, {"code": "testing", "title": "Тестирование"} ] # Добавление данных в базу """ Категории """ for specialty in specialties: new_specialty = Specialty(code=specialty["code"], title=specialty["title"], picture="https://place-hold.it/100x60") # new_specialty.save() """ Компании """ for company in companies: new_company = Company(name=company["title"], logo="https://place-hold.it/100x60") # new_company.save() """ Вакансии """ for job in jobs: new_job = Vacancy(title=job["title"], specialty=Specialty.objects.get(code=job["cat"]), salary_min=job["salary_from"], company=Company.objects.get(name=job["company"]), salary_max=job["salary_to"], published_at=job["posted"], description=job["desc"]) # new_job.save()
def main_parse(search, page, quantity, vacancy_on_page=10, location=None, country=None): country_site = IndeedCountrySites.objects.get(code_iso=country) start_at = page * vacancy_on_page debug_log(f'{"#"* 21}\nParse starts at: {start_at}') total_company_add = 0 total_vacancies_add = 0 for start in range(start_at, quantity, vacancy_on_page): page_num = int(start / vacancy_on_page) debug_log(f"Parse Page: {page_num}") params = {'start': start, 'sort': settings.DEFAULT_SORT} if search: params.update({'q': search}) if location: params.update({'l': location}) debug_log(f'Params: {params}') try: vacancies = get_vacancies_on_page(country=country, session=session, params=params) if vacancies is None: continue company_add = 0 vacancies_add = 0 for vacancy_data in vacancies: company_uid = vacancy_data.get('cmpid', None) company_link = vacancy_data.get('company_link', None) if company_uid: company = Company.objects.filter(uid=company_uid).first() if not company and company_link: _counry = IndeedCountrySites.objects.get( code_iso=country) company_link = normalize_url(_counry.site, company_link) company_data = get_company_info(session, company_link) if company_data: company = Company( uid=company_uid, name=company_data.get('title'), link=company_link, about=company_data.get('about'), headquarters=company_data.get('headquarters'), employees=company_data.get('employees'), industry=company_data.get('industry'), website=company_data.get('website'), revenue=company_data.get('revenue'), country_site=country_site) company.save() company_add += 1 else: company = None else: company = None job_key = vacancy_data.get('jk') if not job_key: continue exist_vacancy = Vacancy.objects.filter(job_key=job_key).first() print(exist_vacancy) if exist_vacancy: continue vacancy_url = vacancy_data.get('link') vacancy = Vacancy( job_key=job_key, title=vacancy_data.get('title'), link=normalize_url(country_site.site, vacancy_url), description_text=vacancy_data.get('description', {}).get('text'), description_html=vacancy_data.get('description', {}).get('html'), location=vacancy_data.get('loc'), location_uid=vacancy_data.get('locid'), country=vacancy_data.get('country'), city=vacancy_data.get('city'), zip=vacancy_data.get('zip'), company_name=url_parse.unquote( vacancy_data.get('srcname', '')), company_uid=vacancy_data.get('cmpid'), company_link=normalize_url(country_site.site, company_link) if company_link else None, company_id=company.id if company else None, raw_data=vacancy_data, country_site=country_site) _data = { 'title': vacancy.title, 'link': vacancy.link, 'description_text': vacancy.description_text, 'description_html': vacancy.description_html, 'location': vacancy.location, 'location_uid': vacancy.location_uid, 'country': vacancy.company, 'city': vacancy.city, 'zip': vacancy.zip, 'company_name': vacancy.company_name, 'company_uid': vacancy.company_uid, 'company_link': vacancy.company_link, 'company_id': company.id if company else None, 'raw_data': vacancy.raw_data, 'country_site': country_site } Vacancy.objects.get_or_create(job_key=job_key, defaults=_data) vacancies_add += 1 total_company_add += company_add total_vacancies_add += vacancies_add debug_log( f"TC: {total_company_add}, TV: {total_vacancies_add}, C: {company_add}, V: {vacancies_add}" ) except Exception as e: tb = traceback.format_exc() error_log(f"###\nPage {page}\n{tb}") raise