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

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

# Copyright 2009-2015 Luc Saffre 

# License: BSD (see file COPYING for details) 

""" 

 

.. autosummary:: 

 

 

""" 

from builtins import str 

 

from django.conf import settings 

 

from lino.utils import curry 

 

from lino.core.frames import Frame 

 

from lino.core.requests import VirtualRow 

from lino.core.requests import ActionRequest 

from lino.core.actions import ShowEmptyTable 

from lino.core import actions 

from lino.core import fields 

 

from lino.mixins.printable import (Printable, DirectPrintAction) 

from lino.utils.xmlgen.html import E 

 

 

from django.utils.encoding import python_2_unicode_compatible 

 

 

@python_2_unicode_compatible 

class EmptyTableRow(VirtualRow, Printable): 

    """ 

    Base class for virtual rows of an :class:`EmptyTable`. 

    An EmptyTableRow instance 

    """ 

 

    pk = -99998 

 

    def __init__(self, table, **kw): 

        self._table = table 

        VirtualRow.__init__(self, **kw) 

 

    def __str__(self): 

        return str(self._table.label) 

 

    def get_print_language(self): 

        # same as Model.get_print_language 

        return settings.SITE.DEFAULT_LANGUAGE.django_code 

 

    def get_printable_context(self, ar=None, **kw): 

        # same as Model.get_printable_context 

        kw = ar.get_printable_context(**kw) 

        kw.update(this=self)  # preferred in new templates 

        kw.update(language=self.get_print_language()) 

        return kw 

 

    def get_template_groups(self): 

        return [self._table.app_label + '/' + self._table.__name__] 

 

    def filename_root(self): 

        return self._table.app_label + '.' + self._table.__name__ 

 

    def __getattr__(self, name): 

        """ 

        Since there is only one EmptyTableRow class, we simulate a 

        getter here by manually creating an InstanceAction. 

        """ 

        v = getattr(self._table, name) 

        if isinstance(v, actions.Action): 

            return actions.InstanceAction(v, self._table, self, None) 

        # 20130525 dd.Report calls `get_story` on `self`, not on the `cls` 

        if callable(v): 

            return curry(v, self) 

        #~ return v 

        #~ raise Exception("") 

        raise AttributeError( 

            "EmptyTableRow on %s has no action and no callable '%s'" % ( 

                self._table, name)) 

 

 

class EmptyTable(Frame): 

    """ 

    A "Table" that has exactly one virtual row and thus is visible 

    only using a Detail view on that row. 

    """ 

 

    #~ debug_permissions = True 

    #~ has_navigator = False 

    #~ hide_top_toolbar = True 

    hide_navigator = True 

    default_list_action_name = 'show' 

    default_elem_action_name = 'show' 

 

    @classmethod 

    def get_default_action(cls): 

        return ShowEmptyTable() 

 

    @classmethod 

    def create_instance(self, ar, **kw): 

        if self.parameters: 

            kw.update(ar.param_values) 

 

        obj = EmptyTableRow(self, **kw) 

        kw = ar.ah.store.row2dict(ar, obj) 

        obj._data = kw 

        obj.update(**kw) 

        return obj 

 

    @classmethod 

    def get_data_elem(self, name): 

        de = super(EmptyTable, self).get_data_elem(name) 

        if de is not None: 

            return de 

        a = name.split('.') 

        if len(a) == 2: 

            return getattr(getattr(settings.SITE.modules, a[0]), a[1]) 

 

 

class ReportRequest(ActionRequest): 

 

    def unsued_show_request(ar, **kwargs): 

        # self = ar.selected_rows[0] 

        self = None  # ar.actor.create_instance(ar) 

        story = ar.actor.get_story(self, ar) 

        ar.renderer.show_story(story) 

 

        # return '\n'.join(ar.story2rst( 

        #     ar.actor.get_story(self, ar), **kwargs)) 

 

 

class Report(EmptyTable): 

    """A special kind of :class:`EmptyTable` used to create complex 

    "reports". A report is a series of tables combined into a single 

    printable and previewable document. 

 

    """ 

 

    detail_layout = "body" 

 

    do_print = DirectPrintAction() 

 

    report_items = None 

 

    @classmethod 

    def request(self, **kw): 

        """Return an action request on this actor. 

 

        """ 

        kw.update(actor=self) 

        return ReportRequest(**kw) 

 

    @classmethod 

    def get_story(cls, self, ar): 

        """Yield a sequence of story items. These can be (1) 

        ElementTree elements or (2) AbstractTable or (3) action 

        requests. 

 

        """ 

        if cls.report_items is None: 

            raise Exception("{0} has no report_items".format(cls)) 

        for A in cls.report_items: 

            yield E.h2(str(A.label)) 

            if A.help_text: 

                yield E.p(str(A.help_text)) 

            yield A 

 

    @fields.virtualfield(fields.HtmlBox()) 

    def body(cls, self, ar): 

        return ar.story2html(self.get_story(ar)) 

 

    @classmethod 

    def as_appy_pod_xml(cls, self, apr): 

        chunks = tuple(apr.story2odt( 

            self.get_story(apr.ar), master_instance=self)) 

        return str('').join(chunks)  # must be utf8 encoded