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

#!/usr/bin/env python 

# encoding: utf-8 

 

# Copyright (C) 2015 Chintalagiri Shashank 

# 

# This file is part of tendril. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU Affero General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU Affero General Public License for more details. 

# 

# You should have received a copy of the GNU Affero General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

 

""" 

Docstring for controller 

""" 

 

from sqlalchemy.orm.exc import NoResultFound 

from tendril.utils.db import with_db 

from tendril.entityhub.db.model import SerialNumber 

from tendril.entityhub import serialnos 

 

from sqlalchemy import desc 

from model import DocStoreDocument 

 

from tendril.utils import log 

logger = log.get_logger(__name__, log.DEFAULT) 

 

 

@with_db 

def get_sno_documents(serialno=None, session=None): 

    if not isinstance(serialno, SerialNumber): 

        serialno = serialnos.get_serialno_object(sno=serialno, session=session) 

    return session.query(DocStoreDocument).filter_by(serialno=serialno).all() 

 

 

@with_db 

def get_doctype_documents(doctype=None, limit=None, session=None): 

    q = session.query(DocStoreDocument).filter( 

        DocStoreDocument.doctype == doctype 

    ) 

    q = q.order_by(desc(DocStoreDocument.created_at)) 

    if limit: 

        q = q.limit(limit) 

    return q.all() 

 

 

@with_db 

def get_serialno_doctype_documents(serialno=None, doctype=None, 

                                   limit=None, session=None): 

    q = session.query(DocStoreDocument) 

    if serialno is not None: 

        if not isinstance(serialno, SerialNumber): 

            serialno = serialnos.get_serialno_object(sno=serialno, 

                                                     session=session) 

        q = q.filter_by(serialno=serialno) 

    if doctype: 

        q = q.filter(DocStoreDocument.doctype == doctype) 

 

    q = q.order_by(desc(DocStoreDocument.created_at)) 

    if limit: 

        q = q.limit(limit) 

    return q.all() 

 

 

@with_db 

def get_snos_by_document_doctype(doctype=None, series=None, 

                                 limit=None, session=None): 

    if doctype is None: 

        raise AttributeError("doctype cannot be None") 

    if series is not None and not isinstance(series, str): 

        raise AttributeError('series must be a string or None') 

 

    q = session.query(SerialNumber) 

    if series is not None: 

        q = q.filter(SerialNumber.sno.like('{0}%'.format(series))) 

 

    q = q.join(DocStoreDocument.serialno) 

    q = q.filter(DocStoreDocument.doctype == doctype) 

    q = q.order_by(desc(SerialNumber.created_at)) 

 

    if limit is not None: 

        q.limit(limit) 

 

    return q.all() 

 

 

@with_db 

def register_document(serialno=None, docpath=None, doctype=None, 

                      efield=None, clobber=True, session=None): 

    if serialno is None: 

        raise AttributeError("serialno cannot be None") 

    if docpath is None: 

        raise AttributeError('docpath cannot be None') 

    if doctype is None: 

        raise AttributeError('doctype cannot be None') 

 

    if not isinstance(serialno, SerialNumber): 

        serialno = serialnos.get_serialno_object(sno=serialno, 

                                                 session=session) 

 

    try: 

        q = session.query(DocStoreDocument).filter_by( 

            serialno=serialno, docpath=docpath 

        ) 

        existing = q.one() 

    except NoResultFound: 

        # default does not exist yet, so add it... 

        session.add( 

            DocStoreDocument(docpath=docpath, doctype=doctype, 

                             efield=efield, serialno_id=serialno.id, 

                             serialno=serialno) 

        ) 

    else: 

        if clobber is True: 

            existing.serialno = serialno 

            existing.doctype = doctype 

            existing.docpath = docpath 

            existing.efield = efield 

            session.add(existing) 

        else: 

            raise ValueError("Document already exists, and clobber is False") 

 

 

@with_db 

def deregister_document(docpath=None, session=None): 

    if docpath is None: 

        raise AttributeError('docpath cannot be None') 

    if not isinstance(docpath, DocStoreDocument): 

        docpath = session.query( 

            DocStoreDocument).filter_by(docpath=docpath).one() 

    session.delete(docpath) 

    session.flush()