Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Integrate aldryn-categories into aldryn-jobs. #44

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: python
python: 2.7
env:
- TOX_ENV=py27-dj16
- TOX_ENV=py27-dj17
- TOX_ENV=py27-django16
- TOX_ENV=py27-django17

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not relevant anymore

- TOX_ENV=pep8

matrix:
Expand Down
68 changes: 44 additions & 24 deletions aldryn_jobs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

import cms
from aldryn_apphooks_config.admin import BaseAppHookConfig
from aldryn_categories.admin import CategoryAdmin
from aldryn_categories.models import Category
from cms.admin.placeholderadmin import PlaceholderAdmin
from cms.admin.placeholderadmin import FrontendEditableAdmin
from distutils.version import LooseVersion
from emailit.api import send_mail
from parler.admin import TranslatableAdmin

from .forms import JobCategoryAdminForm, JobOfferAdminForm
from .forms import JobOfferAdminForm
from .models import (
JobApplication, JobCategory, JobOffer,
JobsConfig, NewsletterSignup
JobApplication, JobOffer,
JobsConfig, NewsletterSignup, JobCategoryOpts
)


Expand Down Expand Up @@ -123,28 +125,45 @@ def get_attachment_address(self, instance):
get_attachment_address.short_description = _('Attachments')


class JobCategoryAdmin(TranslatableAdmin):
form = JobCategoryAdminForm
list_display = ['__unicode__', 'language_column', 'ordering']
list_editable = ['ordering']
filter_horizontal = ['supervisors']
# class JobCategoryAdmin(TranslatableAdmin):
# form = JobCategoryAdminForm
# list_display = ['__unicode__', 'language_column', 'ordering']
# list_editable = ['ordering']
# filter_horizontal = ['supervisors']

# def get_fieldsets(self, request, obj=None):
# fieldsets = [
# (_('Translatable fields'), {
# 'fields': ['name', 'slug']
# }),
# (_('Supervisors'), {
# 'fields': ['supervisors']
# }),
# (_('Options'), {
# 'fields': ['app_config']
# })
# ]
# return fieldsets


class JobCategoryOptsInline(admin.StackedInline):
extra = 1
max_num = 1
model = JobCategoryOpts
verbose_name = _("Aldryn Jobs option")
verbose_name_plural = _("Aldryn Jobs options")

def has_delete_permission(self, *args, **kwargs):
return False

def get_fieldsets(self, request, obj=None):
fieldsets = [
(_('Translatable fields'), {
'fields': ['name', 'slug']
}),
(_('Supervisors'), {
'fields': ['supervisors']
}),
(_('Options'), {
'fields': ['app_config']
})
]
return fieldsets

class JobCategoryAdmin(CategoryAdmin):
list_display = ['__unicode__', 'language_column']
inlines = [JobCategoryOptsInline]


class JobOfferAdmin(FrontendEditableAdmin, TranslatableAdmin, PlaceholderAdmin):
class JobOfferAdmin(FrontendEditableAdmin, TranslatableAdmin,
PlaceholderAdmin):
form = JobOfferAdminForm
list_display = ['__unicode__', 'language_column']
frontend_editable_fields = ('title', 'lead_in')
Expand All @@ -156,7 +175,7 @@ def get_fieldsets(self, request, obj=None):
'fields': ['title', 'slug', 'lead_in']
}),
(_('Options'), {
'fields': ['category', 'is_active', 'can_apply', 'app_config']
'fields': ['category', 'category_new', 'is_active', 'can_apply', 'app_config']
}),
(_('Publication period'), {
'fields': ['publication_start', 'publication_end']
Expand Down Expand Up @@ -207,7 +226,8 @@ class JobsConfigAdmin(BaseAppHookConfig):


admin.site.register(JobApplication, JobApplicationAdmin)
admin.site.register(JobCategory, JobCategoryAdmin)
admin.site.unregister(Category)
admin.site.register(Category, JobCategoryAdmin)
admin.site.register(JobOffer, JobOfferAdmin)
admin.site.register(JobsConfig, JobsConfigAdmin)
admin.site.register(NewsletterSignup, JobNewsletterSignupAdmin)
13 changes: 2 additions & 11 deletions aldryn_jobs/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
from emailit.api import send_mail
from multiupload.fields import MultiFileField
from parler.forms import TranslatableModelForm
from treebeard.forms import MoveNodeForm
from unidecode import unidecode

from .models import (
JobApplication, JobApplicationAttachment, JobCategory, JobOffer,
JobApplication, JobApplicationAttachment, JobOffer,
NewsletterSignup, JobsConfig,
JobListPlugin)

Expand Down Expand Up @@ -106,16 +107,6 @@ def is_edit_action(self):
return self.instance.pk is not None


class JobCategoryAdminForm(AutoSlugForm):

slugified_field = 'name'

class Meta:
model = JobCategory
fields = ['name', 'slug', 'supervisors', 'app_config']



class JobOfferAdminForm(AutoSlugForm):

slugified_field = 'title'
Expand Down
21 changes: 16 additions & 5 deletions aldryn_jobs/managers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
# -*- coding: utf-8 -*-
from aldryn_apphooks_config.managers.parler import \
AppHookConfigTranslatableManager, AppHookConfigTranslatableQueryset

from django.contrib.sites.models import get_current_site
from django.core.urlresolvers import reverse
from django.db import models
from django.db.models import Q
from django.utils import timezone
from django.db import models
from django.core.urlresolvers import reverse
from django.utils.crypto import get_random_string
from django.contrib.sites.models import get_current_site

from aldryn_apphooks_config.managers.parler import (
AppHookConfigTranslatableManager, AppHookConfigTranslatableQueryset
)
from aldryn_categories.models import CategoryManager, CategoryQuerySet
from emailit.api import send_mail


class JobCategoryQuerySet(AppHookConfigTranslatableQueryset, CategoryQuerySet):
pass


class JobCategoryManager(AppHookConfigTranslatableManager, CategoryManager):
queryset_class = JobCategoryQuerySet


class JobOffersQuerySet(AppHookConfigTranslatableQueryset):

def active(self):
Expand Down
11 changes: 6 additions & 5 deletions aldryn_jobs/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from menus.base import NavigationNode
from menus.menu_pool import menu_pool

from aldryn_jobs.models import JobCategory
from aldryn_categories.models import Category
from aldryn_jobs.models import JobOffer


Expand All @@ -22,14 +22,15 @@ def get_nodes(self, request):
language = get_language_from_request(request)
nodes = []
categories = (
JobCategory.objects.namespace(app_namespace)
.language(language)
.translated(language)
Category.objects
.language(language)
.translated(language)
.filter(jobs_opts__app_config__namespace=app_namespace)
)
for category in categories:
try:
node = NavigationNode(category.name,
category.get_absolute_url(),
category.jobs_opts.get_absolute_url(),
category.slug)
nodes.append(node)
except NoReverseMatch:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Migration(migrations.Migration):

dependencies = [
('auth', '0001_initial'),
('cms', '0004_auto_20150309_1408'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('aldryn_jobs', '0004_data_create_default_namespace_20150210_1211'),
]
Expand Down
1 change: 0 additions & 1 deletion aldryn_jobs/migrations/0007_auto_20150330_0545.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
class Migration(migrations.Migration):

dependencies = [
('cms', '0004_auto_20150309_1408'),
('aldryn_jobs', '0006_auto_20150330_0111'),
]

Expand Down
44 changes: 44 additions & 0 deletions aldryn_jobs/migrations/0008_auto_20150403_0611.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
import aldryn_categories.fields


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('aldryn_categories', '0003_auto_20150128_1359'),
('aldryn_jobs', '0007_auto_20150330_0545'),
]

operations = [
migrations.CreateModel(
name='JobCategoryOpts',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('app_config', models.ForeignKey(verbose_name='app_config', to='aldryn_jobs.JobsConfig', null=True)),
('category', aldryn_categories.fields.CategoryOneToOneField(related_name='jobs_opts', to='aldryn_categories.Category')),
('supervisors', models.ManyToManyField(help_text='Those people will be notified via e-mail when new application arrives.', related_name='job_categories_opts', verbose_name='Supervisors', to=settings.AUTH_USER_MODEL, blank=True)),
],
options={
'verbose_name': 'Job category opts',
'verbose_name_plural': 'Job categories opts',
},
bases=(models.Model,),
),
migrations.AddField(
model_name='joboffer',
name='category_new',
field=aldryn_categories.fields.CategoryForeignKey(related_name='job_offers', default=None, to='aldryn_categories.Category', null=True),
preserve_default=True,
),
migrations.AlterField(
model_name='joboffer',
name='category',
field=models.ForeignKey(related_name='jobs', default=None, verbose_name='Category', to='aldryn_jobs.JobCategory', null=True),
preserve_default=True,
),
]
106 changes: 106 additions & 0 deletions aldryn_jobs/migrations/0009_auto_20150403_0611.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from aldryn_categories.models import Category as NoFrozenCategory


def forwards(apps, schema_editor):
"Write your forwards methods here."
# Note: Don't use "from appname.models import ModelName".
# Use orm.ModelName to refer to models in this application,
# and orm['appname.ModelName'] for models in other applications.
Category = apps.get_model('aldryn_categories.Category')
JobCategory = apps.get_model('aldryn_jobs.JobCategory')
JobCategoryOpts = apps.get_model('aldryn_jobs.JobCategoryOpts')
for old_category in JobCategory.objects.all():
# This is maybe wrong but using the model out of 'orm' (not frozen)
# we can access add_root. Copy all the treebeard category creation
# into migration is a big overhead, but would be the perfect thing
# to do.
en_tr = old_category.translations.get(language_code='en')
new_category = NoFrozenCategory.add_root(
name=en_tr.name,
slug=en_tr.slug,
)
new_category = Category.objects.get(pk=new_category.pk)

# When creating default translation parler use as default 'en-us',
# so we fix it here because we use only 'en'.
en_tr = new_category.translations.get()
en_tr.language_code = 'en'
en_tr.save()

# add other translations, we can't use create_translations
# in migrations.
for tr in old_category.translations.exclude(language_code='en'):
new_category.translations.create(
language_code=tr.language_code,
name=tr.name,
slug=tr.slug,
)

for job in old_category.jobs.all():
job.category_new = new_category
job.category = None
job.save()

job_opts = JobCategoryOpts.objects.create(
category_id=new_category.pk,
app_config=old_category.app_config,
# ordering isn't migrated since aldryn-categories provide a way
# to order categories already
)
job_opts.supervisors = old_category.supervisors.all()
job_opts.save()

old_category.delete()


def backwards(apps, schema_editor):
"Write your backwards methods here."
Category = apps.get_model('aldryn_categories.Category')
JobCategory = apps.get_model('aldryn_jobs.JobCategory')
JobCategoryOpts = apps.get_model('aldryn_jobs.JobCategoryOpts')

for new_category in Category.objects.all():
job_opt = JobCategoryOpts.objects.get(category=new_category)
# create the old category
old_category = JobCategory.objects.create(
app_config=job_opt.app_config
)
old_category.supervisors = job_opt.supervisors.all()

# create default translation
en_tr = new_category.translations.get(language_code='en')
old_category.translations.create(
language_code='en',
name=en_tr.name,
slug=en_tr.slug
)
for tr in new_category.translations.exclude(language_code='en'):
# create another translations
old_category.translations.create(
language_code=tr.language_code,
name=tr.name,
slug=tr.slug,
)

for job in new_category.job_offers.all():
job.category = old_category
job.category_new = None
job.save()

old_category.save()
new_category.delete()


class Migration(migrations.Migration):

dependencies = [
('aldryn_jobs', '0008_auto_20150403_0611'),
]

operations = [
migrations.RunPython(forwards, backwards)
]
Loading