src/hdalab/management/commands/calculate_preview.py
author ymh <ymh.work@gmail.com>
Wed, 11 Apr 2018 12:19:47 +0200
branchdocumentation
changeset 693 09e00f38d177
parent 492 19220d52bce7
permissions -rw-r--r--
Add hdabo/hdalab documentations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
     3
Commande permettant la création des miniatures renkan.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
     4
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
     5
Par défaut, seul les renkan pour lesquel c'est nécessaire sont traités (ceux qui sont publié)
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
     6
Les miniatures sont calculées immédiatement de façons synchrone.
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
     8
**Usage**: ``django-admin calculate_preview [options]``
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
     9
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    10
**Options spécifiques:**
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    11
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    12
    - *\-\-all*: Capture les miniatures pours tous les renkan publié. Remplace les miniatures existantes.
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    13
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
'''
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
import logging
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
from optparse import make_option
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
from django.conf import settings
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
from django.core.management.base import NoArgsCommand
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
from django.core.management.color import no_style
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
from hdabo.utils import show_progress
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
from hdalab.models.renkan import HdalabRenkan
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
from hdalab.services import renkan_capture_preview
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
logger = logging.getLogger(__name__)
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
class Command(NoArgsCommand):
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
    '''
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
    calculate renkan preview.
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
    '''
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
    options = ''
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
    help = """calculate renkan preview."""
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    36
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    option_list = NoArgsCommand.option_list + (
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
        make_option('--all',
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
            action='store_true',
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
            dest='all',
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
            default=False,
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
            help='force all tags to be updated, not only those not yet processed'),
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
    )
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    44
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
    def handle_noargs(self, **options):
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    47
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
        self.style = no_style()
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    49
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        self.all = options.get('all', False)
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        queryset = HdalabRenkan.objects.filter(state=HdalabRenkan.PUBLISHED)
693
09e00f38d177 Add hdabo/hdalab documentations
ymh <ymh.work@gmail.com>
parents: 492
diff changeset
    53
492
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
        if not self.all:
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            queryset = queryset.filter(renkan__image = settings.DEFAULT_RENKAN_ICON)
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        count = queryset.count()
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
        writer = None
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
        for i,hdalab_renkan in enumerate(queryset.select_related()):
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            writer = show_progress(i+1, count, hdalab_renkan.renkan.title, 50, writer)
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            if hdalab_renkan.renkan.image and hdalab_renkan.renkan.image != settings.DEFAULT_RENKAN_ICON:
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
                hdalab_renkan.renkan.image.delete(False)
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
                hdalab_renkan.renkan.image.delete_thumbnails()
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            renkan_capture_preview(hdalab_renkan)
19220d52bce7 add preview calculation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67