def delete(item_id): owner = current_user._get_current_object() item = models.Item.objects(id=item_id, owner=owner).first() if not item: return abort(get_item_error_not_found()) item.delete() return render_json()
def delete(stock_id): owner = current_user._get_current_object() stock = models.Stock.objects(id=stock_id, owner=owner).first() if not stock: return abort(get_stock_error_not_found()) stock.delete() return render_json()
def get(building_id): schema = schemas.BuildingSchema() owner = current_user._get_current_object() building = models.Building.objects(id=building_id, owner=owner).first() if not building: return abort(get_building_error_not_found()) return render_json(schema.dump(building).data)
def delete(building_id): owner = current_user._get_current_object() building = models.Building.objects(id=building_id, owner=owner).first() if not building: return abort(get_building_error_not_found()) building.delete() return render_json()
def on_connect(self) -> None: # TODO: verify if it works like expected # only `connect` event requires authentication so `current_user` # is set to authenticated `User` instance only in the context of # this event, so we retrieve this `User` instance and store it # in instance variable so it can be accessed in any other events. self.user = current_user._get_current_object() # noqa: WPS437 self.send( {MESSAGE_KEY: f'hello {self.user.email}!'}, room=request.sid, )
def create(stock_id): adder = current_user._get_current_object() schema = schemas.InventorySchema() stock = models.Stock.objects.with_id(stock_id) try: inventory_data = schema.load(request.get_json()) except Exception as e: print(e) response_dict = request.get_json() response_dict.update(e.messages) response = render_json(response_dict) response.status_code = 400 abort(response) data = inventory_data.data print('data:', data) data['adder'] = adder data['stock'] = stock item = None if data['item'] and len(data['item']) > 0: try: item = models.Item.objects.with_id(data['item']) except: pass elif len(data['item_upc']) > 0: item = models.Item.objects(upc=data['item_upc']).first() if not item: item = items.add_item(data['item_upc']) if not item: return abort(items.get_item_not_found_error()) data['item'] = item data.pop('item_upc') inventory = models.Inventory(status='active', **data) nutrition = models.Nutrition.objects(item=item).first() if nutrition: inventory.serving_size_unit = nutrition.facts.serving_size_unit inventory.serving_size_quantity = nutrition.facts.serving_size_quantity inventory.total_serving_size = data['quantity'] * \ nutrition.facts.servings_per_container inventory.available_serving_size = inventory.total_serving_size inventory.save() return render_json(schema.dump(inventory).data)
def deactivate_application(building_id, application_id): owner = current_user._get_current_object() building = models.Building.objects(id=building_id, owner=owner).first() if not building: return abort(get_building_error_not_found()) building.activated_applications = [ aa for aa in building.activated_applications if str(aa.id) != application_id] building.save() return render_json()
def create(): schema = schemas.RoomSchema() try: room_data = schema.load(request.get_json()).data except Exception as e: response_dict = request.get_json() response_dict.update(e.messages) response = render_json(response_dict) response.status_code = 400 abort(response) user = current_user._get_current_object() room = models.Room(user=user, **room_data) room.save() return render_json(schema.dump(room).data)
def create(): schema = schemas.GroupSchema() try: group_data = schema.load(request.get_json()).data except Exception as e: response_dict = request.get_json() response_dict.update(e.messages) response = render_json(response_dict) response.status_code = 400 abort(response) user = current_user._get_current_object() group = models.Group(user=user, **group_data) group.save() return render_json(schema.dump(group).data)
def inner_wrapper(id: int, *args, **kwargs): # current_user is of LocalProxy instance, it's how flask manage sessions # jwt extension possibly utilize this use so inorder to get to object behind it # I use method that provided by werkzeug library # https://werkzeug.palletsprojects.com/en/1.0.x/local/#werkzeug.local.LocalProxy._get_current_object if isinstance(current_user._get_current_object(), model) and current_user.id == id: # prevent unnecessary fetch of current user if ids mathches return f(current_user, *args, **kwargs) model_instance = model.find_by_id(id) if model_instance is None: return errors.not_found( f"{model.__name__.title()} with this id: `{id}` is not exists." ) return f(model_instance, *args, **kwargs)
def create(): schema = schemas.BuildingSchema() try: building_data = schema.load(request.get_json()) except Exception as e: response_dict = request.get_json() response_dict.update(e.messages) response = render_json(response_dict) response.status_code = 400 abort(response) owner = current_user._get_current_object() building = models.Building(owner=owner, status='active', **building_data.data) building.save() return render_json(schema.dump(building).data)
def activate_application(building_id): owner = current_user._get_current_object() building = models.Building.objects(id=building_id, owner=owner).first() if not building: return abort(get_building_error_not_found()) try: schema = schemas.ApplicationSchema() app_dict = schema.load(request.get_json(), partial=True).data application = models.Application.objects.with_id(app_dict['id']) except Exception as e: from . import applications return abort(applications.get_application_error_not_found()) activated_application = models.ActivatedApplication( id=application.id, owner=owner, application=application) building.activated_applications.append(activated_application) building.save() schema = schemas.BuildingSchema() return render_json(schema.dump(building).data)
def list(): schema = schemas.BuildingSchema(many=True) owner = current_user._get_current_object() buildings = models.Building.objects(owner=owner) return render_json(schema.dump(buildings).data)
def consume(stock_id): stock = models.Stock.objects.with_id(stock_id) schema = schemas.InventoryConsumingItemSchema() consumer = current_user._get_current_object() try: consuming_data = schema.load(request.get_json()).data except Exception as e: print(e) response_dict = request.get_json() response_dict.update(e.messages) response = render_json(response_dict) response.status_code = 400 abort(response) item = models.Item.objects.with_id(consuming_data['item']) inventories = models.Inventory.objects( stock=stock, item=item, status='active', available_serving_size__gt=0).order_by( '-expired_date' ) consuming_size = consuming_data['consuming_size'] for inventory in inventories: consumption = models.Consumption( item=item, stock=stock, consumer=consumer, inventory=inventory, consuming_unit=inventory.serving_size_unit ) print('consumer:', consumer) if consuming_size <= inventory.available_serving_size: print('consume <=', inventory.id, consuming_size) inventory.available_serving_size -= consuming_size consumption.consuming_size = consuming_size consuming_size -= consuming_size if inventory.available_serving_size == 0: inventory.status = 'out of inventory' else: print('consume > ', inventory.id, inventory.available_serving_size) consumption.consuming_size = inventory.available_serving_size consuming_size -= inventory.available_serving_size inventory.available_serving_size = 0 consumption.status = 'active' consumption.save() inventory.consumptions.append(consumption) inventory.save() if consuming_size == 0: break consuming_data['item'] = item consuming_data['id'] = None return render_json(schema.dump(consuming_data).data)