Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

# -*- coding: UTF-8 -*- 

# Copyright 2015 by Luc Saffre. 

# License: BSD, see LICENSE for more details. 

"""Defines the :manage:`checkdata` management command: 

 

.. management_command:: checkdata 

 

.. py2rst:: 

 

  from lino.modlib.plausibility.management.commands.checkdata \ 

      import Command 

  print(Command.help) 

 

In other words, this command does the same as if a user would click on 

the button with the bell ("Check plausibility") on each database 

object for which there are data checkers. 

 

""" 

 

from __future__ import unicode_literals, print_function 

 

from optparse import make_option 

from clint.textui import puts, progress 

 

from django.utils import translation 

from django.core.management.base import BaseCommand 

 

from lino.modlib.plausibility.choicelists import Checkers 

from lino.modlib.plausibility.models import get_checkable_models 

 

from lino.api import rt 

 

 

def check_plausibility(args=[], fix=True): 

    """Called by :manage:`check_plausibility`. See there.""" 

    Problem = rt.modules.plausibility.Problem 

    mc = get_checkable_models(*args) 

    with translation.override('en'): 

        for m, checkers in mc.items(): 

            ct = rt.modules.contenttypes.ContentType.objects.get_for_model(m) 

            Problem.objects.filter(owner_type=ct).delete() 

            name = unicode(m._meta.verbose_name_plural) 

            qs = m.objects.all() 

            msg = "Running {0} plausibility checkers on {1} {2}...".format( 

                len(checkers), qs.count(), name) 

            puts(msg) 

            sums = [0, 0, name] 

            for obj in progress.bar(qs): 

                for chk in checkers: 

                    todo, done = chk.update_problems(obj, False, fix) 

                    sums[0] += len(todo) 

                    sums[1] += len(done) 

            if sums[0] or sums[1]: 

                msg = "Found {0} and fixed {1} plausibility problems in {2}." 

                puts(msg.format(*sums)) 

            else: 

                puts("No plausibility problems found in {0}.".format(name)) 

 

 

class Command(BaseCommand): 

    args = "[app1.Model1.Checker1] [app2.Model2.Checker2] ..." 

    help = """ 

 

    Update the table of plausibility problems. 

 

    If no arguments are given, run it on all plausibility checkers. 

    Otherwise every positional argument is expected to be a model name in 

    the form `app_label.ModelName`, and only these models are being 

    updated. 

 

    """ 

 

    option_list = BaseCommand.option_list + ( 

        make_option( 

            '-l', '--list', action='store_true', dest='list', 

            default=False, 

            help="Don't check, just show a list of available checkers."), 

        make_option( 

            '-f', '--fix', action='store_true', dest='fix', 

            default=False, 

            help="Fix any repairable problems."), 

    ) 

 

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

        if options['list']: 

            Checkers.show() 

        else: 

            rt.startup() 

            check_plausibility(args=args, fix=options['fix'])