Skip to content

Commit

Permalink
implemented feature: Removes the tasks belonging to a deactivated par…
Browse files Browse the repository at this point in the history
…ents
  • Loading branch information
Andreu Vallbona committed Feb 23, 2018
1 parent 2f6b1df commit e35e4ad
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ Update the number of words to translate in every task. It counts the number of w

:code:`python manage.py update_number_of_words`


Delete disabled parent tasks
----------------------------
This commands deletes the tasks that no longer have a parent enabled object. It is a command that
can be executed from time to time or it can be added to the *cron*.

:code:`python manage.py remove_tasks_for_disbled_parents`
2 changes: 1 addition & 1 deletion transmanager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

__author__ = 'Andreu Vallbona'
__email__ = '[email protected]'
__version__ = '0.3.3'
__version__ = '0.3.4'

default_app_config = 'transmanager.apps.TransManagerConfig'
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# -*- encoding: utf-8 -*-

from django.core.management.base import BaseCommand
from transmanager.models import TransModelLanguage
from transmanager.settings import TM_DEFAULT_ENABLED_ATTRIBUTE_NAME
from transmanager.tasks.tasks import delete_translations_for_item_and_its_children
from transmanager.utils import has_field


class Command(BaseCommand):
help = "Removes the tasks belonging to a deactivated parents."

def handle(self, *args, **options):

filter_by = {
TM_DEFAULT_ENABLED_ATTRIBUTE_NAME: False
}

whole_ids = []

self.stdout.write('Start')
# generate the translations tasks for every record on the translatable models
for model in TransModelLanguage.objects.all():
mc = model.get_model_class()
if not has_field(mc, TM_DEFAULT_ENABLED_ATTRIBUTE_NAME):
continue

# if the model has the enabled attr, the we search the disabled items ids
disabled_ids = list(mc.objects.filter(**filter_by).values_list('id', flat=True))
whole_ids += disabled_ids
self.stdout.write('Model: {} has {} ids'.format(mc.__name__, len(disabled_ids)))
if disabled_ids:
for item_id in disabled_ids:
delete_translations_for_item_and_its_children.delay(mc, item_id)

self.stdout.write('{} ids will be processed'.format(len(whole_ids)))

self.stdout.write('End')
1 change: 0 additions & 1 deletion transmanager/tasks/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.contrib.auth.models import User

from django.utils.translation import ugettext_lazy as _
from django.core.files import File
from django.core.files.base import ContentFile
from rq.decorators import job

Expand Down
16 changes: 16 additions & 0 deletions transmanager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import re
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import FieldDoesNotExist
from hvad.models import TranslatableModel


Expand Down Expand Up @@ -55,3 +56,18 @@ def get_num_words(text):
return len(num_words)
except TypeError:
return 0


def has_field(mc, field_name):
"""
detect if a model has a given field has
:param field_name:
:param mc:
:return:
"""
try:
mc._meta.get_field(field_name)
except FieldDoesNotExist:
return False
return True

0 comments on commit e35e4ad

Please sign in to comment.