def item_create(user, user_id, checklist_id): """ Creates a new item from input and adds to the items table Parameters: user: User The user object for the user trying to make the request user_id: integer The id number of the current user checklist_id: integer The checklist id number for the current checklist Returns: Tuple containing the dict of the new item and status code """ item_fields = item_schema.load(request.json) checklist = Checklist.query.get(checklist_id) if user.id != checklist.owner_id: return abort( 401, description= "You do not have permission to add an item to this checklist.") new_item = Item() new_item.name = item_fields["name"] new_item.index = item_fields["index"] new_item.checklist_id = item_fields["checklist_id"] checklist.items.append(new_item) db.session.commit() return (jsonify(item_schema.dump(new_item)), 201)
def create_item(): try: title = request.form['title'] location = request.form['location'] description = request.form['description'] tags = request.form.getlist('tags') except: return {'error': 'missing required params'}, 400 photo_url = request.form.get('photo_url') owner_uid = g.uid item = Item(title=title, location=location, description=description, tags=tags, photo_url=photo_url, owner_uid=owner_uid) db = firestore.client() try: db.collection(ITEMS_COLLECTION).add(item.to_dict()) except: return {'error': 'something went wrong, please try again later'}, 500 return item.to_dict(), 201
def test_get_list_with_one_item_and_one_mark_item_interface(self): session = self.bot.get_session() list = List('1') session.add(list) session.commit() session.add(Item('дело №1', list)) session.add(Item('дело №2', list, state=ItemState.MARK)) session.commit() answer = self.bot.message_handler('??', '1') self.assertEqual('1) дело №1\n' '<strike>2) дело №2</strike>', answer)
def test_get_list_with_two_items_interface(self): session = self.bot.get_session() list = List('1') session.add(list) session.commit() session.add(Item('дело №1', list)) session.add(Item('дело №2', list)) session.commit() answer = self.bot.message_handler('??', '1') self.assertEqual('1) дело №1\n' '2) дело №2', answer)
def test_get_list_with_one_item_and_one_delete_item_interface(self): session = self.bot.get_session() list = List('1') session.add(list) session.commit() session.add(Item('дело №1', list)) session.add(Item('дело №2', list, state=ItemState.DELETE)) session.commit() answer = self.bot.message_handler('??', '1') self.assertEqual('1) дело №1', answer)
def test_get_items_from_different_lists_interface(self): session = self.bot.get_session() list1 = List('1') session.add(list1) session.commit() session.add(Item('дело №1', list1)) list2 = List('2') session.add(list2) session.commit() session.add(Item('дело №2', list2)) session.commit() answer = self.bot.message_handler('??', '1') self.assertEqual('1) дело №1', answer) answer = self.bot.message_handler('??', '2') self.assertEqual('1) дело №2', answer)
def update_item(item_id): response, response_code = get_item(item_id) if response_code != 200: return response, response_code item = Item.from_dict(response) if item.owner_uid != g.uid: return {'error': 'user does not own this item'}, 401 title = request.form.get('title') location = request.form.get('location') description = request.form.get('description') tags = request.form.getlist('tags') photo_url = request.form.get('photo_url') item.update(title=title, location=location, description=description, tags=tags, photo_url=photo_url) db = firestore.client() try: db.collection(ITEMS_COLLECTION).document(item_id).update( item.to_dict()) except: return {'error': 'something went wrong, please try again later'}, 500 return item.to_dict(), 200
def create_trade(): try: selling_item_id = request.form['item_id'] except: return {'error': 'missing required params'}, 400 response, response_code = get_item(selling_item_id) if response_code != 200: return response, response_code item = Item.from_dict(response) if (item.owner_uid == g.uid): return {'error': 'cannot request trade with your own item'}, 400 # TODO: add check to make sure user hasn't already requested trade with this item. db = firestore.client() try: new_trade_ref = db.collection(TRADES_COLLECTION).document() except: return {'error': 'could not create new trade, try again later'}, 500 new_trade = Trade(new_trade_ref.id, g.uid, item.owner_uid, selling_item_id) try: new_trade_ref.set(new_trade.to_dict()) except: return {'error': 'could not push new trade, try again later'}, 500 return new_trade.to_dict(), 201
def add_item(self, chat_id, text): session = self.get_session() list = session.query(List).filter_by(chat_id=chat_id).first() if list is None: list = List(chat_id) session.add(Item(text, list)) session.commit() return f'Добавлено "{text}"'
def test_mark_one_item_interface_2(self): session = self.bot.get_session() list = List('1') session.add(list) session.commit() session.add(Item('дело №2', list)) session.commit() answer = self.bot.message_handler('** 1', '1') self.assertEqual('Зачеркнуто "дело №2"', answer)
def test_mark_one_item_base(self): session = self.bot.get_session() list = List('1') session.add(list) session.commit() item = Item('дело №1', list) session.add(item) session.commit() self.bot.message_handler('** 1', '1') self.assertEqual(ItemState.MARK, item.state)
def get_item(item_id): db = firestore.client() try: item_dict = db.collection(ITEMS_COLLECTION).document( item_id).get().to_dict() item = Item.from_dict(item_dict) item_info = item.to_dict() item_info['item_id'] = item_id except: return {'error': 'item not found'}, 404 return item_info, 200
def delete_item(item_id): response, response_code = get_item(item_id) if response_code != 200: return response, response_code item = Item.from_dict(response) if item.owner_uid != g.uid: return {'error': 'user does not own this item'}, 401 db = firestore.client() try: db.collection(ITEMS_COLLECTION).document(item_id).delete() except: return {'error': 'something went wrong, please try again later'}, 500 return item.to_dict(), 200
def seed_db(): """ Custom flask db command to seed tables with fake data for testing """ faker = Faker() users = [] for i in range(10): user = User() user.username = f"testuser{i + 1}" user.email = f"test{i + 1}@test.com" user.password = bcrypt.generate_password_hash("123456").decode("utf-8") db.session.add(user) users.append(user) db.session.commit() checklists = [] for i in range(10): # Pick random user to create a checklist for user = random.choice(users) checklist = Checklist() checklist.title = faker.catch_phrase() checklist.is_group = random.choice([True, False]) checklist.owner_id = user.id user.owned_checklists.append(checklist) user.checklists.append(checklist) # If group list, pick random users who aren't the owner to add to association table if checklist.is_group: num_members = random.randint(2, 5) for i in range(num_members): member = random.choice(users) if member != user: member.checklists.append(checklist) checklists.append(checklist) db.session.commit() for i in range(30): # Pick a random checklist to create an item for checklist = random.choice(checklists) item = Item() item.name = faker.catch_phrase() # Randomly assign status, if True add current datetime for completion item.status = random.choice([True, False]) if item.status: item.completion_date = datetime.now() item.checklist_id = checklist.id # If group list, get members and append None # Randomly pick a member to assign to the item, None indicates item is unassigned if checklist.is_group: users = checklist.users.copy() users.append(None) user = random.choice(users) if user: item.assigned_id = user.id user.items.append(item) else: item.assigned_id = None checklist.items.append(item) db.session.commit() print("TABLES SEEDED")