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

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

# Copyright 2013-2015 Luc Saffre 

# License: BSD (see file COPYING for details) 

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

 

""" 

from builtins import str 

from builtins import object 

 

import logging 

logger = logging.getLogger(__name__) 

 

try: 

    import bleach 

except ImportError: 

    bleach = None 

 

from django.utils.translation import ugettext_lazy as _ 

from django.contrib.humanize.templatetags.humanize import naturaltime 

 

from lino.modlib.gfks.mixins import Controllable 

 

from lino.api import dd 

from lino import mixins 

from lino.modlib.users.mixins import ByUser, UserAuthored 

from lino.utils.xmlgen.html import E 

 

 

@dd.python_2_unicode_compatible 

class Comment( 

        mixins.CreatedModified, 

        UserAuthored, 

        # mixins.Hierarchical, 

        Controllable): 

    """A **comment** is a short text which some user writes about some 

    other database object. 

 

    .. attribute:: short_text 

 

        A short "abstract" of your comment. This should not be more 

        than one paragraph. 

 

    """ 

 

    ALLOWED_TAGS = ['a', 'b', 'i', 'em'] 

 

    class Meta(object): 

        app_label = 'comments' 

        abstract = dd.is_abstract_model(__name__, 'Comment') 

        verbose_name = _("Comment") 

        verbose_name_plural = _("Comments") 

 

    short_text = dd.RichTextField(_("Short text")) 

    more_text = dd.RichTextField(_("More text"), blank=True) 

 

    def __str__(self): 

        return u'%s #%s' % (self._meta.verbose_name, self.pk) 

 

    def as_li(self, ar): 

        """Return this comment as a list item. If `bleach 

        <http://bleach.readthedocs.org/en/latest/>`_ is installed, all 

        tags except some will be removed when 

 

        """ 

        txt = ar.parse_memo(self.short_text) 

        if bleach is None: 

            chunks = [txt] 

        else: 

            chunks = [bleach.clean( 

                txt, tags=self.ALLOWED_TAGS, strip=True)] 

 

        by = _("{0} by {1}").format( 

            naturaltime(self.created), str(self.user)), 

        chunks += [ 

            " (", E.tostring(ar.obj2html(self, by)), ")" 

        ] 

        if self.more_text: 

            chunks.append(" (...)") 

 

        html = ''.join(chunks) 

        return "<li>" + html + "</li>" 

 

dd.update_field(Comment, 'user', editable=False) 

 

 

from .ui import *