Skip to content

Commit 58356ca

Browse files
germanpskalanuxIanPuchetti
authored
Migrar las ofertas de la aplicación de jobs vieja a la nueva (#544)
* Added pytest as a runner * Added pytest as a runner on Makefile * Added joboffers app * Some fixes to the JobOffer model * Added initial joboffer detail * Added initial job offer add form * Changed joboffer's urls to use their namespace * Added some basic pytest's fixtures * Updated JobOffer's factory to include all the needed fields * Added joboffer creation test * Added joboffer admin page * Moved add button to bottom of the page (joboffers) * Added search button in admin page (joboffers) * Added joboffer edit view * Changed joboffer's choices to their long version * Added migration for company owner to UserCompanyProfile * Added migration for Job model to JobOffer * Adjusted pytest to don't execute migrations * Added short description generation for jobs migration * Added migrations for old jobs tags * Added migration for joboffer's comments * Changed job migration to mark also rejected job offers * Added search field for UserCompanyProfile * Added empty message for JobOfferHistoryView * Changed joboffer model to accept NULL companies and the corresponding migrations * Renamed migrations to avoid conflicts with previous branch Co-authored-by: Juan Manuel Schillaci <[email protected]> Co-authored-by: Ian Puchetti <[email protected]>
1 parent 415c336 commit 58356ca

File tree

10 files changed

+228
-6
lines changed

10 files changed

+228
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 3.2.12 on 2022-06-13 17:25
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('pycompanies', '0005_company_owner_to_usercompanyprofile'),
11+
('joboffers', '0014_alter_jobofferaccesslog_options'),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name='joboffer',
17+
name='company',
18+
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='pycompanies.company', verbose_name='Empresa'),
19+
),
20+
]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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)]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Generated by Django 3.2.12 on 2022-06-13 17:30
2+
3+
from django.db import migrations
4+
5+
6+
def forward(apps, schema_editor):
7+
ContentType = apps.get_model('contenttypes', 'ContentType')
8+
TaggedItem = apps.get_model('taggit', 'TaggedItem')
9+
10+
job_content_type = ContentType.objects.get(app_label='jobs', model='job')
11+
joboffer_content_type = ContentType.objects.get(app_label='joboffers',
12+
model='joboffer')
13+
14+
tagged_items = TaggedItem.objects.filter(content_type=job_content_type)
15+
16+
new_tags = [
17+
TaggedItem(content_type=joboffer_content_type,
18+
object_id=tagged_item.object_id,
19+
tag=tagged_item.tag) for tagged_item in tagged_items
20+
]
21+
for new_tag in new_tags:
22+
new_tag.save()
23+
24+
25+
class Migration(migrations.Migration):
26+
27+
dependencies = [
28+
('joboffers', '0016_migrate_old_jobs'),
29+
]
30+
31+
operations = [migrations.RunPython(forward, migrations.RunPython.noop)]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Generated by Django 3.2.12 on 2022-06-13 17:30
2+
3+
from django.db import migrations
4+
5+
6+
def forward(apps, schema_editor):
7+
JobInactivated = apps.get_model('jobs', 'jobinactivated')
8+
JobOfferComment = apps.get_model('joboffers', 'joboffercomment')
9+
10+
User = apps.get_model('auth', 'User')
11+
12+
superuser = User.objects.filter(is_superuser=True).first()
13+
14+
for old_comment in JobInactivated.objects.filter(
15+
job__company__isnull=False):
16+
new_comment = JobOfferComment(
17+
text=f"{old_comment.reason}: {old_comment.comment}",
18+
comment_type='MODERATION',
19+
created_at=old_comment.created,
20+
created_by=superuser,
21+
joboffer_id=old_comment.job_id)
22+
23+
new_comment.save()
24+
25+
26+
class Migration(migrations.Migration):
27+
28+
dependencies = [
29+
('joboffers', '0017_migrate_old_jobs_tags'),
30+
]
31+
32+
operations = [migrations.RunPython(forward, migrations.RunPython.noop)]

joboffers/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class JobOffer(models.Model):
8888
'pycompanies.Company',
8989
verbose_name=_('Empresa'),
9090
on_delete=models.CASCADE,
91+
null=True
9192
)
9293
location = models.CharField(max_length=100, blank=True, null=True, verbose_name=_('Lugar'))
9394
contact_mail = models.EmailField(

joboffers/templates/joboffers/joboffer_history.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ <h2>Historial de «{{ object.title }}» </h2>
3535
</div>
3636
</div>
3737
</div>
38+
{% empty %}
39+
<p>No hay registros históricos para esta oferta.</p>
3840
{% endfor %}
3941
</div>
4042
{% include 'joboffers/_paginator.html' with page_obj=page_obj %}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 3.2.12 on 2022-06-07 19:19
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
('pycompanies', '0003_usercompanyprofile'),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name='usercompanyprofile',
18+
name='user',
19+
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='company', to=settings.AUTH_USER_MODEL),
20+
),
21+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by Django 3.2.12 on 2022-06-07 13:57
2+
3+
from django.db import migrations
4+
5+
6+
def forward(apps, schema_editor):
7+
Company = apps.get_model('pycompanies', 'Company')
8+
UserCompanyProfile = apps.get_model('pycompanies', 'UserCompanyProfile')
9+
10+
user_profiles = [
11+
UserCompanyProfile(company_id=company_id, user_id=user_id)
12+
for company_id, user_id in Company.objects.values_list('id', 'owner')
13+
]
14+
15+
UserCompanyProfile.objects.bulk_create(user_profiles)
16+
17+
18+
def backward(apps, schema_editor):
19+
UserCompanyProfile = apps.get_model('pycompanies', 'UserCompanyProfile')
20+
UserCompanyProfile.objects.all().delete()
21+
22+
23+
class Migration(migrations.Migration):
24+
25+
dependencies = [
26+
('pycompanies', '0004_alter_usercompanyprofile_user'),
27+
]
28+
29+
operations = [migrations.RunPython(forward, backward)]

pycompanies/models.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ class Company(TimeStampedModel):
1717
name = models.CharField('Nombre', max_length=255, unique=True)
1818
description = models.TextField('Descripción')
1919
photo = models.ImageField('Logo', upload_to='pycompanies/logos')
20-
link = models.URLField('URL',
21-
help_text=_('Dirección web de la empresa')
22-
)
20+
link = models.URLField('URL', help_text=_('Dirección web de la empresa'))
2321
rank = models.PositiveIntegerField(default=0)
2422

2523
def get_absolute_url(self):
@@ -47,9 +45,9 @@ class UserCompanyProfile(models.Model):
4745
"""Company data for a User."""
4846
objects = UserCompanyProfileManager()
4947

50-
user = models.OneToOneField(settings.AUTH_USER_MODEL,
51-
related_name='company',
52-
on_delete=models.CASCADE)
48+
user = models.ForeignKey(settings.AUTH_USER_MODEL,
49+
related_name='company',
50+
on_delete=models.CASCADE)
5351
company = models.ForeignKey(Company,
5452
related_name='users',
5553
on_delete=models.CASCADE)
@@ -60,6 +58,7 @@ def __str__(self):
6058

6159
# SIGNALS
6260

61+
6362
@receiver(post_delete, sender=Company)
6463
def post_delete_user(sender, instance, *args, **kwargs):
6564
"Delete logo image after delete company"

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[pytest]
22
requests_mock_case_sensitive = True
3+
addopts = --no-migrations --reuse-db --create-db

0 commit comments

Comments
 (0)