Coverage for /media/ldata/code/tendril/tendril/entityhub/db/controller.py: 0%

# Coverage for /media/ldata/code/tendril/tendril/entityhub/db/controller.py : 0%

## 80 statements 0 run 80 missing 0 excluded

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 | #!/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.py """ from sqlalchemy.orm.exc import NoResultFound from tendril.utils.db import with_db from model import SerialNumberSeries from model import SerialNumber from model import SerialNumberAssociation from tendril.utils import log logger = log.get_logger(__name__, log.DEFAULT) @with_db def get_series_obj(series=None, session=None): return session.query(SerialNumberSeries).filter_by(series=series).one() @with_db def create_series_obj(series=None, start_seed=None, session=None): if not series or not isinstance(series, str): raise ValueError('series should be a string, got : ' + repr(series)) if not start_seed or not isinstance(series, str): raise ValueError('start_seed should be a string, got : ' + repr(start_seed)) sobj = SerialNumberSeries(series=series, last_seed=start_seed) session.add(sobj) session.flush() return sobj @with_db def get_all_serialnos(session=None): return session.query(SerialNumber).all() @with_db def get_serialnos_by_efield(efield=None, session=None): return session.query(SerialNumber).filter_by(efield=efield).all() @with_db def get_serialnos_by_series(series=None, session=None): if not series or not isinstance(series, str): raise ValueError('series should be a string, got : ' + repr(series)) return session.query(SerialNumber).filter( SerialNumber.sno.like('{0}%'.format(series))).all() @with_db def get_serialno_object(sno=None, session=None): if sno is None: raise AttributeError("sno cannot be None") return session.query(SerialNumber).filter_by(sno=sno).one() @with_db def register_serialno(sno=None, efield=None, session=None): sobj = SerialNumber(sno=sno, efield=efield) session.add(sobj) session.flush() return sobj @with_db def delete_serialno(sno=None, session=None): if sno is None: raise AttributeError("sno cannot be None") if not isinstance(sno, SerialNumber): sno = get_serialno_object(sno=sno, session=session) session.delete(sno) session.flush() @with_db def link_serialno(child=None, parent=None, association_type=None, session=None): if not isinstance(child, SerialNumber): child = get_serialno_object(sno=child, session=session) if not isinstance(parent, SerialNumber): parent = get_serialno_object(sno=parent, session=session) try: existing = session.query(SerialNumberAssociation).filter_by( child_id=child.id, parent_id=parent.id).one() session.delete(existing) session.flush() except NoResultFound: pass assoc_object = SerialNumberAssociation(child_id=child.id, child=child, parent_id=parent.id, parent=parent, association_type=association_type) session.add(assoc_object) session.flush() return assoc_object @with_db def get_child_snos(serialno=None, child_efield=None, child_series=None, session=None): if serialno is None: raise ValueError("serialno cannot be None") if not isinstance(serialno, SerialNumber): serialno = get_serialno_object(sno=serialno, session=session) q = session.query(SerialNumberAssociation).filter_by( parent_id=serialno.id ) q = q.join(SerialNumberAssociation.child) if child_efield is not None: q = q.filter(SerialNumber.efield == child_efield) if child_series is not None: q = q.filter(SerialNumber.sno.like(child_series+'%')) return [x.child.sno for x in q.all()]