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

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

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

# Copyright 2011-2015 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

""" 

Miscellaneous mixins for `lino.modlib.system`. 

 

""" 

 

from __future__ import unicode_literals 

 

import datetime 

 

from django.utils.translation import ugettext_lazy as _ 

from django.db.models import Q 

 

from lino.core.choicelists import ChoiceList, Choice 

 

from lino.utils import AttrDict 

 

 

class YesNo(ChoiceList): 

    """ 

    A choicelist with two values "Yes" and "No". 

 

    .. django2rst:: 

 

        from lino.modlib.system.choicelists import YesNo 

        rt.show(YesNo) 

 

    Used e.g. to define parameter panel fields for BooleanFields:: 

 

      foo = dd.YesNo.field(_("Foo"), blank=True) 

 

 

    """ 

    verbose_name_plural = _("Yes or no") 

add = YesNo.add_item 

add('y', _("Yes"), 'yes') 

add('n', _("No"), 'no') 

 

 

class Genders(ChoiceList): 

    """ 

    Defines the two possible choices "male" and "female" 

    for the gender of a person. 

 

    .. django2rst:: 

 

            from lino.modlib.system.choicelists import Genders 

            rt.show(Genders) 

 

 

    See :ref:`lino.tutorial.human` for examples. 

    See :doc:`/dev/choicelists`. 

    """ 

 

    verbose_name = _("Gender") 

 

add = Genders.add_item 

add('M', _("Male"), 'male') 

add('F', _("Female"), 'female') 

 

 

class PeriodEvent(Choice): 

 

    def add_filter(self, qs, obj): 

 

        if isinstance(obj, datetime.date): 

            obj = AttrDict(start_date=obj, end_date=obj) 

 

        if obj.start_date is None or obj.end_date is None: 

            return qs 

 

        if self.name == 'started': 

            qs = qs.filter(start_date__gte=obj.start_date) 

            qs = qs.filter(start_date__lte=obj.end_date) 

        elif self.name == 'ended': 

            qs = qs.filter(end_date__isnull=False) 

            qs = qs.filter(end_date__gte=obj.start_date) 

            qs = qs.filter(end_date__lte=obj.end_date) 

        elif self.name == 'active': 

            qs = qs.filter(Q(start_date__isnull=True) | 

                           Q(start_date__lte=obj.end_date)) 

            qs = qs.filter(Q(end_date__isnull=True) | 

                           Q(end_date__gte=obj.start_date)) 

        return qs 

 

 

class PeriodEvents(ChoiceList): 

    """The list of things you can observe on a 

    :class:`DatePeriod`. The default list has the following 

    choices: 

 

    .. django2rst:: 

 

        from lino.modlib.system.choicelists import PeriodEvents 

        rt.show(PeriodEvents) 

 

    """ 

    verbose_name = _("Observed event") 

    verbose_name_plural = _("Observed events") 

    item_class = PeriodEvent 

 

 

add = PeriodEvents.add_item 

add('10', _("Starts"), 'started') 

add('20', _("Is active"), 'active') 

add('30', _("Ends"), 'ended')