|
| 1 | +# Generated by Django 3.2.12 on 2022-06-13 17:30 |
| 2 | +import html |
| 3 | +import re |
| 4 | + |
| 5 | +from datetime import timedelta |
| 6 | + |
| 7 | +from django.db import migrations |
| 8 | +from django.utils.timezone import now |
| 9 | + |
| 10 | + |
| 11 | +def get_short_description(description): |
| 12 | + """ |
| 13 | + Deduce the short_description from a given html description string |
| 14 | + """ |
| 15 | + description_stripped_tags = re.sub(r'<[^>]*>', ' ', description) |
| 16 | + description_without_spaces = re.sub(r'\s+', ' ', |
| 17 | + description_stripped_tags).strip() |
| 18 | + description_unescaped = html.unescape(description_without_spaces) |
| 19 | + return description_unescaped[:512] |
| 20 | + |
| 21 | + |
| 22 | +def forward(apps, schema_editor): |
| 23 | + Job = apps.get_model('jobs', 'Job') |
| 24 | + JobOffer = apps.get_model('joboffers', 'JobOffer') |
| 25 | + UserCompanyProfile = apps.get_model('pycompanies', 'UserCompanyProfile') |
| 26 | + User = apps.get_model('auth', 'User') |
| 27 | + |
| 28 | + superuser = User.objects.filter(is_superuser=True).first() |
| 29 | + |
| 30 | + for job in Job.objects.all(): |
| 31 | + if not job.email: |
| 32 | + email = UserCompanyProfile.objects.filter( |
| 33 | + company=job.company).first().user.email |
| 34 | + else: |
| 35 | + email = job.email |
| 36 | + |
| 37 | + SENIORITY_TO_EXPERIENCE_MAP = { |
| 38 | + '': '0', # Tomo por defecto 0 a falta de otro valor mejor |
| 39 | + 'Trainee': '0', |
| 40 | + 'Junior': '2+', |
| 41 | + 'Semi Senior': '5+', |
| 42 | + 'Senior': '10+' |
| 43 | + } |
| 44 | + |
| 45 | + experience = SENIORITY_TO_EXPERIENCE_MAP[job.seniority] |
| 46 | + remoteness = 'REMOTE' if job.remote_work else 'IN_OFFICE' |
| 47 | + |
| 48 | + if job.is_active: |
| 49 | + thirty_days_ago = now() - timedelta(days=30) |
| 50 | + |
| 51 | + if job.modified < thirty_days_ago: |
| 52 | + state = 'EXPIRED' |
| 53 | + else: |
| 54 | + state = 'ACTIVE' |
| 55 | + elif job.jobinactivated_set.exists(): |
| 56 | + state = 'REJECTED' |
| 57 | + else: |
| 58 | + state = 'DEACTIVATED' |
| 59 | + |
| 60 | + joboffer = JobOffer(id=job.id, |
| 61 | + title=job.title, |
| 62 | + company=job.company, |
| 63 | + location=job.location, |
| 64 | + contact_mail=email, |
| 65 | + experience=experience, |
| 66 | + remoteness=remoteness, |
| 67 | + hiring_type='OTHER', |
| 68 | + description=job.description, |
| 69 | + created_at=job.created, |
| 70 | + created_by=job.owner, |
| 71 | + modified_at=job.modified, |
| 72 | + modified_by=superuser, |
| 73 | + short_description=get_short_description( |
| 74 | + job.description), |
| 75 | + state=state) |
| 76 | + |
| 77 | + joboffer.save() |
| 78 | + |
| 79 | + |
| 80 | +class Migration(migrations.Migration): |
| 81 | + |
| 82 | + dependencies = [ |
| 83 | + ('joboffers', '0015_alter_joboffer_company'), |
| 84 | + ] |
| 85 | + |
| 86 | + operations = [migrations.RunPython(forward, migrations.RunPython.noop)] |
0 commit comments