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

# Copyright 2009-2015 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

"""Database models for `lino.modlib.importfilter`. 

 

""" 

from builtins import str 

from builtins import object 

 

 

import logging 

logger = logging.getLogger(__name__) 

#~ from lino.utils import dblogger 

 

from django.conf import settings 

from django.contrib.contenttypes import models as contenttypes 

from django.contrib.contenttypes.models import ContentType 

from django.utils.encoding import force_text 

 

 

from django.db import models 

from django.utils.translation import ugettext_lazy as _ 

 

 

from lino.api import dd, rt 

 

from lino.utils.instantiator import Instantiator 

 

 

class FieldSeparators(dd.ChoiceList): 

    pass 

 

 

class Filter(dd.Model): 

    name = models.CharField(_("Name"), max_length=200) 

    content_type = dd.ForeignKey(contenttypes.ContentType, 

                                 verbose_name=_("Model")) 

    field_sep = models.CharField(_("Field separator"), max_length=10) 

    help_text = dd.RichTextField(_("HelpText"), 

                                 blank=True, null=True, format='plain') 

 

 

class Filters(dd.Table): 

    model = Filter 

    column_names = "name content_type *" 

    detail_layout = """ 

    name content_type field_sep 

    help_text 

    importfilters.ItemsByFilter 

    """ 

 

 

class Item(mixins.Sequenced): 

 

    class Meta(object): 

        verbose_name = _("Import Filter Item") 

        verbose_name_plural = _("Import Filter Items") 

 

    filter = dd.ForeignKey(Filter) 

    field = models.CharField(_("Field"), max_length=200) 

    column = models.IntegerField(_("Column")) 

 

    help_text = dd.RichTextField(_("HelpText"), 

                                 blank=True, null=True, format='plain') 

 

    @dd.chooser(simple_values=True) 

    def field_choices(cls, filter): 

        l = [] 

        if filter is not None: 

            model = filter.content_type.model_class() 

            meta = model._meta 

            for f in meta.fields: 

                if not getattr(f, '_lino_babel_field', False): 

                    l.append(f.name) 

            l.sort() 

        return l 

 

 

class Items(dd.Table): 

    model = Item 

    column_names = "column field help_text *" 

 

 

class ItemsByFilter(Items): 

    master_key = 'filter' 

    column_names = "column field help_text workflow_buttons *" 

 

 

class Import(dd.VirtualTable): 

    column_names = 'description obj2unicode' 

    parameters = dict( 

        filter=dd.ForeignKey(Filter), 

        data=dd.RichTextField(_("Data to import"), 

                              blank=True, null=True, 

                              format='plain')) 

 

    @classmethod 

    def get_data_rows(self, ar): 

        flt = ar.param_values.filter 

        build = Instantiator(flt.content_type.model_class()).build 

        for ln in ar.param_values.data.splitlines(): 

            ln = ln.strip() 

            if ln: 

                kw = dict() 

                cells = flt.field_sep.split(ln) 

                for item in flt.item_set.all(): 

                    if item.column: 

                        kw[item.field] = cells[item.column-1] 

                yield build(**kw) 

 

    @dd.displayfield(_("Description")) 

    def description(cls, obj, ar): 

        kw = dict() 

        flt = ar.param_values.filter 

        for item in flt.item_set.all(): 

            kw[item.field] = getattr(obj, item.field) 

        return str(kw) 

 

    @dd.displayfield(_("obj2unicode")) 

    def obj2unicode(cls, obj, ar): 

        return dd.obj2unicode(obj)