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

# 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/>. 

""" 

This file is part of tendril 

See the COPYING, README, and INSTALL files for more information 

""" 

 

import idstring 

 

from tendril.utils.db import with_db 

 

from db import controller 

from sqlalchemy.orm.exc import NoResultFound 

 

from tendril.utils import log 

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

 

 

def get_series(sno): 

    return sno.split('-')[0] 

 

 

@with_db 

def get_serialno_object(sno=None, session=None): 

    return controller.get_serialno_object(sno=sno, session=session) 

 

 

@with_db 

def get_serialno_efield(sno=None, session=None): 

    return controller.get_serialno_object(sno=sno, session=session).efield 

 

 

@with_db 

def serialno_exists(sno=None, session=None): 

    try: 

        controller.get_serialno_object(sno=sno, session=session) 

        return True 

    except NoResultFound: 

        return False 

 

 

@with_db 

def register_serialno(sno=None, efield=None, session=None): 

    logger.info("Registering new serial number : " + sno) 

    return controller.register_serialno(sno=sno, efield=efield, 

                                        session=session) 

 

 

@with_db 

def link_serialno(child=None, parent=None, association_type=None, 

                  session=None): 

    if child is None: 

        raise AttributeError("child cannot be None") 

    if parent is None: 

        raise AttributeError("parent cannot be None") 

    logger.info("Linking " + child + " to parent " + parent) 

    return controller.link_serialno(child=child, parent=parent, 

                                    association_type=association_type, 

                                    session=session) 

 

 

@with_db 

def get_parent_serialnos(sno=None, session=None): 

    if sno is None: 

        raise AttributeError("child cannot be None") 

    return controller.get_serialno_object( 

        sno=sno, session=session).parents 

 

 

@with_db 

def get_child_serialnos(sno=None, session=None): 

    if sno is None: 

        raise AttributeError("child cannot be None") 

    return controller.get_serialno_object(sno=sno, session=session).children 

 

 

@with_db 

def delete_serialno(sno, recurse=False, session=None): 

    if recurse is True: 

        for snoi in get_child_serialnos(sno): 

            controller.delete_serialno(snoi, recurse, session) 

    controller.delete_serialno(sno, recurse, session) 

 

 

# def delete_series(series): 

#     for serialno in get_all_serialnos(): 

#         if get_series(serialno) == series: 

#             logger.info("Deleting Serial No : " + serialno) 

#             delete_serialno(serialno) 

#     logger.info("Deleting Series : " + series) 

#     nsno_table.delete(series=series) 

 

 

@with_db 

def get_serialno(series=None, efield=None, register=True, 

                 start_seed='100A', session=None): 

    series = series.upper() 

    series_obj = controller.get_series_obj(series=series, session=session) 

    if series_obj is not None: 

        last_seed = series_obj.last_seed 

        logger.debug("Found last seed for series " + str(series) + 

                     " : " + str(last_seed)) 

        generator = idstring.IDstring(seed=last_seed) 

        new_sno = generator + 1 

        if register is True: 

            logger.info("Updating seed for series " + series + 

                        " : " + str(new_sno.get_seed())) 

            series_obj.last_seed = new_sno.get_seed() 

            register_serialno( 

                sno=series + '-' + new_sno, 

                efield=efield, session=session 

            ) 

        else: 

            logger.info("Not updating seed for series " + series + 

                        " : " + str(new_sno.get_seed())) 

        return series + '-' + new_sno 

    else: 

        logger.info("Could not find series in db : " + series) 

        generator = idstring.IDstring(seed=start_seed) 

        if register is True: 

            logger.info("Creating series in db : " + series) 

            controller.create_series_obj( 

                series=series, start_seed=start_seed, session=session 

            ) 

            register_serialno( 

                sno=series + '-' + generator, 

                efield=efield, session=session 

            ) 

        else: 

            logger.info("NOT Creating series in db : " + series) 

        return series + '-' + generator