def post(self, request): form = CompanyForm(request.POST, request.FILES) if not form.is_valid(): return render(request, 'vacancies/user/company_edit.html', {'form': form}) company = self.get_company(request) if company: company.title = form.cleaned_data['title'] company.location = form.cleaned_data['location'] if form.cleaned_data['logo']: company.logo = form.cleaned_data['logo'] company.description = form.cleaned_data['description'] company.employee_count = form.cleaned_data['employee_count'] company.owner = get_user(request) else: company = Company( title=form.cleaned_data['title'], location=form.cleaned_data['location'], logo=form.cleaned_data['logo'], description=form.cleaned_data['description'], employee_count=form.cleaned_data['employee_count'], owner=get_user(request), ) company.save() return redirect(reverse('my_company_form') + '?submitted=True')
def post(self, request): user = request.user if user.is_anonymous: return redirect(reverse('login')) company = Company.objects.filter(owner=user).first() if company is None: company = Company() form = CompanyForm(request.POST) context = {'form': form, 'company': company} if form.is_valid(): data = form.cleaned_data company.name = data['name'] company.location = data['location'] # Не знаю, как надо загружать logo # в request.POST данные есть, а в cleaned_data - нет # company.logo = data['logo'] # # file_name = request.POST.get('logo') # company.logo.storage.save(file_name) company.description = data['description'] company.employee_count = data['employee_count'] company.owner = user company.save() context['info_updated'] = True return render(self.request, 'vacancies/company-edit.html', context)
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 fill_companies(companies): """Populate company database Args: companies (dict): 'companies' dict from moc-file Returns: dict: mapping dictionary for saving job->company relations """ mapping = {} for company in companies: moc_id = company.pop("id") company["name"] = company["title"] _ = company.pop("title") new_company = Company(**company) new_company.save() mapping.setdefault(moc_id, new_company.pk) return mapping
def create_user_company(request): user = request.user if user.is_anonymous: return redirect(reverse('login')) company = Company.objects.filter(owner=user).first() if company is None: # Создаём временный объект: company = Company(owner=user, name='') return render(request, 'vacancies/company-edit.html', {'form': CompanyForm(), 'company': company, 'title_left': 'Моя компания'})
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 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()
from conf import settings import random import data 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']),
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()
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()