def get(self, **kwargs): """ Retrieve a user or list of users depending on the arguments passed to the function """ #Determine if user is searching by id or username user_id = kwargs.pop("user_id", None) username = kwargs.pop("username", None) try: print(user_id) user_id = int(user_id) except TypeError or ValueError: user_id = 0 if username is None and user_id == 0: print("USERNAME IS NONW AND USERID = 0") return self._all() elif user_id > 0 and username is None: print("USERNAME IS NONW AND USERID = 0") return DBSession.query(User).get(user_id) elif user_id == 0 and username is not None: print("USERNAME IS NOT NONE AND USERID = 0") if len(username) == 0: raise ValueError("Illegal username entered") users = DBSession.query(User). \ filter(or_(User.username.like('%' + username + '%'),\ User.fullname.like('%' + username + '%'))).all() return users elif user_id > 0 and username is not None: print("BOTH ARE FILLED INUSERNAME IS NOT NONE AND USERID = 0") users = DBSession.query(User).filter( \ or_(User.id == user_id,\ User.username.like('%' + username + '%'))).all() return
def index(self, class_name): if isinstance(class_name, User): with transaction.manager: users = [user.id for user in DBSession.query(User).all()] #rint [("User: %s class: %s") % (user.serialize, class_name.serialize) for user in users if user == class_name] return users.index(class_name.id) elif isinstance(class_name, Permission): with transaction.manager: permissions = DBSession.query(Permission).all() return permissions.index(class_name) return -1
def _last(): """ Navigate to last item_category """ item_category = DBSession.query(ItemCategory).\ order_by(desc(ItemCategory.id)).first() return self._to_json(item_category)
def _all(self): """ Return all suppliers """ users = DBSession.query(Supplier).order_by(Supplier.id). \ options(joinedload("branches")).all() or None return users
def _last(): """ Navigate to last supplier """ supplier = DBSession.query(Supplier).\ order_by(desc(Supplier.id)).first() return self._to_json(supplier)
def _first(): """ internal method to navigate to the first supplier """ supplier = DBSession.query(Supplier).order_by(Supplier.id).first() return self._to_json(supplier)
def _first(): """ internal method to navigate to the first item """ item = DBSession.query(Item).order_by(Item.id).first() return item
def _last(): """ Navigate to last item """ item = DBSession.query(Item).\ order_by(desc(Item.id)).first() return item
def _all(self): """ Return all items """ users = DBSession.query(Item).order_by(Item.id). \ options(joinedload("branches")).all() or None return users
def _first(): """ internal method to navigate to the first item_category """ item_category = DBSession.query(ItemCategory).order_by(ItemCategory.id).first() return self._to_json(item_category)
def permissions(request): permissions = DBSession.query(Permission).all() for permission in permissions: print(("%s %s %s") % ( permission.id, permission.name, permission.description, )) return permissions
def get(self, *args, **kwargs): """ Retrieve a specific Requisition model that matches specific criteria i.e. id value submitted a list of requisitions """ def __first(): requisition = DBSession.query(Requisition).order_by(\ Requisition.id).first() return requisition def __last(): requisition = DBSession.query(Requisition).order_by(\ desc(Requisition.id)).first() return requisition if args: #if requisition id has been submitted as parameter # return Requisition model that matches the id try: requisition_id = int(args[0]) requisition = DBSession.query(Requisition).get(requisition_id) return requisition except ValueError as v_error: raise v_error #TODO better exception handling elif kwargs: if "FIRST" in kwargs: return __first() elif "LAST" in kwargs: return __last() else: #return all requisitions requistions = DBSession.query(Requisition).order_by(\ Requisition.id).all() return requisitions return None
def get(self, *args, **kwargs): """ Copy the state an instance onto the persistent instance with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. """ def _first(): """ internal method to navigate to the first item """ item = DBSession.query(Item).order_by(Item.id).first() return item def _last(): """ Navigate to last item """ item = DBSession.query(Item).\ order_by(desc(Item.id)).first() return item def _all(): items = DBSession.query(Item).all() return items if args: s_id = args[0] try: s_id = int(s_id) item = DBSession.query(Item).get(s_id); # return self._to_json(item) return item except TypeError as err: print(err) elif kwargs: if "FIRST" in kwargs: return _first() elif "LAST" in kwargs: return _last() else: return _all() return None
def _create(): supplier = DBSession.query(Supplier).get(supplier_id) print(supplier.to_json) if supplier is None: raise TypeError("Could not find supplier with id: %s" % supplier_id) branch = SupplierBranch(supplier_id) branch = _setattr(branch) del branch.id with transaction.manager: DBSession.add(branch)
def _update(): with transaction.manager: item_category = DBSession.query(ItemCategory).get(item_category_id) if item_category is None: _create() item_category.name = name item_category.notes = notes item_category.modified_by = user #user.id item_category = DBSession.merge(item_category) #region_invalidate(_add) return self._to_json(item_category)
def _update(): with transaction.manager: item = DBSession.query(Item).get(item_id) if item is None: _create() item.name = name item.category = category item.unit_of_measurement = unit_of_measurement item.notes = notes item.modified_by = user #user.id item = DBSession.merge(item) #region_invalidate(_add) return self._to_json(item)
def delete(self, j_group): try: name = j_group.pop("group.name") self.id = j_group.pop("group.id") except KeyError as err: print(err) raise KeyError() with transaction.manager: group = DBSession.query(Group).get(self.id) #Complete deletion may not be the best option. An alternative - #option is to void the instance #DBSession.delete(group) group.void = True DBSession.merge(group) transaction.commit()
def delete(self, item): """ Mark a Item as deleted. The database delete operation occurs upon flush(). """ item = DBSession.query(Item).get(item.id) #next_item = item.next() if not item: raise ValueError("Did not find Item") #if _validate(item): with transaction.manager: DBSession.delete(item) return True return False #delete operation failed
def delete(self, supplier): """ Mark a Supplier as deleted. The database delete operation occurs upon flush(). """ supplier = DBSession.query(Supplier).get(supplier.id) #next_supplier = supplier.next() if not supplier: raise ValueError("Did not find Supplier") #if _validate(supplier): with transaction.manager: DBSession.delete(supplier) return True return False #delete operation failed
def _update(): with transaction.manager: supplier = DBSession.query(Supplier).get(supplier_id) if supplier is None: _create() supplier.name = name supplier.tel_1 = tel_1 supplier.tel_2 = tel_2 supplier.fax = fax supplier.email = email supplier.website = website supplier.address = address supplier.notes = notes supplier.modified_by = user #user.id supplier = DBSession.merge(supplier) #region_invalidate(_add) return self._to_json(supplier)
def _update(): """ Update an existing Group """ try: self.id = j_group.pop("group.id") self.name = j_group.pop("group.name") except KeyError as err: print(err) raise KeyError with transaction.manager: group = DBSession.query(Group).get(self.id) if group is None: raise ValueError("Group not found") group.name = self.name group.modified_by = self.modified_by group.modified_date = self.now DBSession.merge(group) transaction.commit()
def __last(): requisition = DBSession.query(Requisition).order_by(\ desc(Requisition.id)).first() return requisition
def delete_branch(self, j_branch): branch_id = j_branch.pop("suppliers_branch_id", None) if not branch_id: raise KeyError("Invalid Branch") branch = DBSession.query(SupplierBranch).get(branch_id) with transaction.manager: DBSession.delete(branch)
def _update(): branch = DBSession.query(SupplierBranch).get(branch_id) branch = _setattr(branch) with transaction.manager: DBSession.merge(branch)
def __first(): requisition = DBSession.query(Requisition).order_by(\ Requisition.id).first() return requisition
def get(self, **kwargs): return DBSession.query(Group).filter(void=False)
def _all(self): with transaction.manager: return DBSession.query(Permission). \ order_by(Permission.id).all()
def _all(): item_categories = DBSession.query(ItemCategory).all() return item_categories
def _all(): items = DBSession.query(Item).all() return items
def _update(): item = DBSession.query(Item).get(item_id) _setattr(item) with transaction.manager: DBSession.merge(item) return True