def _create(): errors = {} #list of data entry errors if self.username is None or self.first_name is None or self.last_name is None: if self.username is None: errors["username"] = "******" elif self.first_name is None: errors["first_name"] = "No First Name" else: errors["last_name"] = "No Last Name" return errors if self.username is None: raise ValueError("username was not supplied") elif self.first_name is None: raise ValueError("first name not entered") elif self.last_name is None: raise ValueError("last name not entered") user = User(self.username, self.first_name, self.last_name) user = _set_user(user) if _is_valid(user): try: user.password = sha256_digest(password) except ValueError as err: print(err.message) raise ValueError("No password supplied") with transaction.manager: DBSession.add(user) else: print("Invalid user")
def delete(self, requisition_id): requisition = get(requisition_id) requisition.void = True with transaction.manager: DBSession.merge(requisition) transaction.commit()
def _update(): if _is_valid(j_user): user = get({"user_id": self.id}) user = _set_user(user) with transaction.manager: DBSession.merge(user) transaction.commit()
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 _update(): purchase_order_id = self.id purchase_order = _set_purchase_order(get({"id": purchase_order_id})) with transaction.manager: DBSession.merge(purchase_order) transaction.commit()
def _update(): with transaction.manager: requisition = DBSession.get(requisition.id) #requisition = _set_requisition(requisition) requisition.department = department_id requisition.expected_date = expected_date DBSession.merge(requisition) transaction.commit()
def _create(): requisition = Requisition() #requisition = _set_requisition(requisition) requisition.department = department_id requisition.expected_date = expected_date with transaction.manager: DBSession.add(requisition) transaction.commit()
def _create(): with transaction.manager: item = Item(name) item.notes = notes item.category = category item.unit_of_measurement = unit_of_measurement item.created_by = user item.modified_by = user DBSession.add(item)
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 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 _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 _all(self): """ Return all items """ users = DBSession.query(Item).order_by(Item.id). \ options(joinedload("branches")).all() or None return users
def _last(): """ Navigate to last item """ item = DBSession.query(Item).\ order_by(desc(Item.id)).first() return item
def _first(): """ internal method to navigate to the first item """ item = DBSession.query(Item).order_by(Item.id).first() return item
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 _last(): """ Navigate to last item_category """ item_category = DBSession.query(ItemCategory).\ order_by(desc(ItemCategory.id)).first() return self._to_json(item_category)
def _last(): """ Navigate to last supplier """ supplier = DBSession.query(Supplier).\ order_by(desc(Supplier.id)).first() return self._to_json(supplier)
def _create(): """ Create a new Group """ group = Group(name) group.created_by = User.get_by_username(created_by) group.modified_by = User.get_by_username(modified_by) group.created_by = group.created_by.id group.modified_by = group.modified_by.id created_date = datetime.now() group.created_date = created_date group.modified_date = created_date with transaction.manager: DBSession.add(group) transaction.commit()
def _all(self): """ Return all suppliers """ users = DBSession.query(Supplier).order_by(Supplier.id). \ options(joinedload("branches")).all() or None return users
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 _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 _create(): with transaction.manager: item_category = ItemCategory(name) item_category.notes = notes item_category.created_by = user item_category.modified_by = user j_item_category = DBSession.add(item_category) j_item_category = self.get(LAST=True) return j_item_category
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 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 _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()