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

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

# Copyright 2013-2014 Luc Saffre 

# License: BSD (see file COPYING for details) 

 

""" 

The :xfile:`models.py` module of :mod:`lino.modlib.events`. 

""" 

 

from __future__ import unicode_literals 

from builtins import str 

 

from django.db import models 

from django.conf import settings 

from django.utils.translation import ugettext_lazy as _ 

from django.utils.translation import string_concat 

 

from lino.utils.xmlgen.html import E 

from lino.api import dd 

from lino import mixins 

from lino.core.constants import _handle_attr_name 

 

 

def sepjoin(items, sep=', '): 

    rv = [] 

    for i in items: 

        if len(rv): 

            rv.append(sep) 

        rv.append(str(i)) 

    return rv 

 

 

class Place(mixins.BabelNamed): 

    city = dd.ForeignKey( 

        'countries.Place', 

        related_name='events_place_set', 

        blank=True, null=True) 

 

 

class Places(dd.Table): 

    model = Place 

 

 

class Feature(mixins.BabelNamed): 

    pass 

 

 

class Features(dd.Table): 

    model = Feature 

 

 

class Type(mixins.BabelNamed): 

 

    events_column_names = models.CharField( 

        max_length=100, 

        default="when:30 what:40 where:30") 

 

    def EventsByType(self, year=None, **kw): 

        kw.update(master_instance=self) 

 

        if year is None: 

            year = settings.SITE.today().year 

        kw.update(filter=models.Q(date__year=year)) 

 

        return EventsByType.request(**kw) 

 

 

class Types(dd.Table): 

    model = Type 

 

 

class Event(mixins.BabelNamed): 

    date = models.DateField(_("Date")) 

    place = dd.ForeignKey(Place, blank=True, null=True) 

    type = dd.ForeignKey(Type) 

    features = models.ManyToManyField(Feature) 

    #~ cities = models.ManyToManyField('countries.Place') 

    #~ description = dd.BabelTextField(format='plain',blank=True) 

    url = models.URLField(blank=True) 

 

 

@dd.python_2_unicode_compatible 

class Stage(mixins.Sequenced): 

    event = dd.ForeignKey('events.Event', related_name="stages") 

    city = dd.ForeignKey('countries.Place', related_name="stages") 

 

    def __str__(self): 

        return str(self.city) 

 

    def get_siblings(self): 

        return self.event.stages.order_by('seqno') 

 

dd.update_field(Event, 'name', blank=True) 

 

 

class StagesByEvent(dd.Table): 

    model = Stage  # Event.cities.through 

    master_key = 'event' 

 

 

class FeaturesByEvent(dd.Table): 

    model = Event.features.through 

    master_key = 'event' 

 

 

class Events(dd.Table): 

    model = Event 

    order_by = ['date'] 

    column_names = "date place type" 

    detail_layout = """ 

    type date place 

    StagesByEvent FeaturesByEvent 

    """ 

 

    @dd.displayfield(string_concat(_("When"), "?")) 

    def when(self, obj, ar): 

        #~ rv = dbutils.dtosl(obj.date) 

        rv = dd.fdf(obj.date) 

        if obj.url: 

            # replaces spaces by newline to avoid large column 

            rv = '\n'.join(rv.split()) 

            rv = E.a(rv, href=obj.url) 

        return rv 

 

    @dd.displayfield(string_concat(_("Where"), "?")) 

    def where(self, obj, ar): 

        if obj.place is not None: 

            return E.p(str(obj.place), ' ', E.b(str(obj.place.city))) 

        # remember: "von Ans nach Eupen und nicht andersrum" 

        return E.p(*sepjoin(obj.stages.order_by('seqno'), ' -- ')) 

 

    @dd.displayfield(string_concat(_("What"), "?")) 

    def what(self, obj, ar): 

        chunks = [] 

        if obj.name: 

            chunks += [E.b(str(obj)), E.br()] 

        chunks += sepjoin(obj.features.all()) 

        #~ if obj.url: 

            #~ chunks += [E.br(),E.a(_("More"),href=obj.url)] 

        return E.p(*chunks) 

 

 

class EventsByType(Events): 

    master_key = 'type' 

    #~ column_names = "when what where" 

 

    @classmethod 

    def get_column_names(self, ar): 

        return ar.master_instance.events_column_names 

 

    @classmethod 

    def get_handle_name(self, ar): 

        hname = _handle_attr_name 

        #~ hname = super(PrintEntriesByBudget,self).get_handle_name(ar) 

        hname += ar.master_instance.events_column_names.replace(" ", "_") 

        return hname