def setup(): # Setup database connection config = configparser.ConfigParser() config.read("config.ini") db = SqlitePersistence(config.get("Database", "path")) # truncate all tables from previous run. # DO NOT INCLUDE IN PRODUCTION CODE, otherwise you will overwrite your data each time the program is started. db.truncate_all_tables() # Seed the database with items. for i in range(1, 31): item = Item() item.rig = "X0%i" % i item.description = "Something that needs to be handed over" item.case = "CAS-12345" db.save(item) # Complete random items. for i in range(1, 31): rand = random.random() # Items with lower ids are more likely to be completed than items with higher ids. if (rand * 2) + ((31 - i) / 30) > 1: db.complete(Item, i)
def test_get_incomplete_items(self): item2 = Item() item2.rig = "X02" item2.description = "Another description" item2.case = "CAS-23456" self._db.save(item2) self._db.complete(Item, 2) incomplete_items = self._db.get_incomplete_items() self.assertEqual(1, len(incomplete_items)) self.assertEqual(self._item.rig, incomplete_items[0].rig)
def test_get_items_completed_in_past_day(self): item2 = Item() item2.rig = "X02" item2.description = "Another description" item2.case = "CAS-23456" item2.completed_at = datetime.datetime.now() - datetime.timedelta(days=6) self._db.save(item2) self._db.complete(Item, 2) item1 = self._db.find(Item, 1) self._db.find(Item, item1.id) complete_items = self._db.get_items_completed_in_past_day() self.assertEqual(1, complete_items.count())
def new_category_item(category_name): """Add a new item.""" if request.method == "POST": item = Item() if request.form["title"]: item.title = request.form["title"].strip() if request.form["description"]: item.description = request.form["description"].strip() if request.form["category_name"]: category_name = request.form["category_name"].strip() category = session.query(Category).filter_by( name=category_name).one_or_none() item.category_id = category.id try: item.user_id = login_session["user_id"] session.add(item) session.commit() flash("Item '{}' Successfully Added".format(item.title), "success") return redirect(url_for( "item.show_item", category_name=item.category.name, item_title=item.title)) except exc.IntegrityError: session.rollback() flash( "You can not add this item since another item already " " exists in the database with the same title and category.", "warning") return redirect(url_for( "item.new_item", category_name=category_name)) else: categories = session.query(Category).order_by( asc(Category.name)).all() return render_template( "new_item.html", categories=categories, category_name=category_name)