def remove_item(item_id): """ Remove item from data base :param item_id: :return string: JSON """ item = get_item_by_id(item_id) or None # check the item exist if not item: return jsonify({'error': 'This record don\'t exist'}) # check the user is the owner if int(item.author) != int(g.user.id): return jsonify( {'error': 'You don\'t have permission to delete this record'}) images = get_images_by_item_id(item_id) # remove images files for image in images: path = ''.join([BASE_DIR, image.url]) if os.path.isfile(path): os.unlink(path) # remove item and images from database delete_item(item_id) remove_images_by_item_id(item_id) return jsonify({'message': 'Record was deleted'})
def on_update(itemid): print('updating item id: {}'.format(itemid)) if ENV == 'DEV': tpm = 6000 else: tpm = 600 d = call_with_throttling(get_item, args=(itemid, ), threshold_per_minute=tpm) if 'error' in d: if d['error'] in ['not found']: try: print('deleting id: {}'.format(itemid)) LC.delete('item', itemid) ItemCT.delete(itemid) delete_item(itemid) ai1.task_done(itemid) ai2.task_done(itemid) except: traceback.print_exc() return d else: raise ValueError('unknown error: {}'.format(d)) # check if we should save this item in the first place # we only accept a few cates if 'cid' in d and not need_crawl(d['cid']): WC.add(d['id']) return d # for connection errors, we simply raise exception here # the exceptions will be captured in LC.update_if_needed # the task will not clean up and will be requeued by requeue worker if not d: raise ValueError('item incomplete error: {}'.format(d)) elif d and 'shopid' in d: try: update_item(d) except: traceback.print_exc() raise ValueError('item update failed: {}'.format(d)) if LC.need_update('shop', d['shopid']): # queue shop jobs as1.put(d['shopid']) return d
def delete_car(item_id): """ Remove car and all images :param item_id: :return: """ # Get car car = get_item_by_id(item_id) # check if the user is the owner if int(car.author) != int(session['uid']): # crate a error message and redirect user flash('You don\'t have permission to remove this object', 'error') return redirect('/profile', 302) if request.method == 'POST': # get images images = get_images_by_item_id(item_id) # get title title = car.title # remove images files for image in images: # get absolute path to image path = ''.join([BASE_DIR, image.url]) # if file exist remove the image file if os.path.isfile(path): os.unlink(path) # remove images from from database remove_images_by_item_id(item_id) # remove data of car from database delete_item(item_id) # crate a success message and redirect user flash('Car: "%s" was removed' % title, 'success') return redirect('/profile', 302) return render('catalog/delete_car.html', brands=brands, car=car.serialize, csrf_token=csrf_token)
def deleteItem(category_id=None, item_id=None): if not is_logged_in(): flash("You must be logged in to perform that action.") return redirect(url_for('getCatalog')) item = get_item(item_id) if not is_authorized(item.user_id): flash("You may only delete items you have created.") return redirect(url_for('getCategory', category_id=category_id)) if request.method == 'POST' and item_id: delete_item(item_id) return redirect(url_for('getCategory', category_id=category_id)) else: item = get_item(item_id) return render_template('delete.html', category_id=item.category_id, item=item)
def handle_delete(request): data = request.json event = None if not data.get('auth', None) == config.AUTH_KEY: return {'status': 'unauthorized'} if 'id' in data: event = Event.query.get(data['id']) elif 'title' in data: event = Event.query.filter_by(title=data['title']).first() else: return {'status': 'failed', 'error': "No valid search specified. Valid search fields are id or name"} if event is None: return {'status': 'failed', 'error': 'No event found'} delete_item(event) return {'status': 'success', 'event': convert_to_dict(event)}
def delete_item_handler(catalog_id, item_id): """Deletes an item in a catalog Parameters: - catalog_id: integer id of catalog to which item belongs - item_id: integer id of item to delete Returns: - HTTP response redirecting to catalog page """ catalog_id = int(catalog_id) catalog_entity = models.get_catalog_by_id(catalog_id) item_id = int(item_id) item_entity = models.get_item_by_id(catalog_id, item_id) # Check parameters if not catalog_entity: raise BadRequest('Could not find catalog with id %d' % catalog_id) if not (item_entity): raise BadRequest('Could not find item with id %d!' % item_id) if not catalog_entity.user_can_edit(models.get_current_user()): raise Unauthorized models.delete_item(catalog_id, item_id) return flask.redirect('/catalog/%d' % catalog_entity.key.id())
'url': '4url', 'category_id': categories[3].id, 'user_id': user_id }, { 'name': '4name1', 'description': '4description1', 'url': '4url1', 'category_id': categories[3].id, 'user_id': user_id }] for item in items: add_item(name=item['name'], description=item['description'], url=item['url'], category_id=item['category_id'], user_id=item['user_id']) if __name__ == '__main__': # always clean slate test data delete_user(user_id=None) delete_item(item_id=None) delete_category(category_id=None) print 'populate_users' populate_users() print 'populate_categories' populate_categories() print 'populate_items' populate_items()