def patch(self, id): """ Patch Offer """ data = request.json o = Offer.objects.get_or_404(id=id) if o.facilitator.id != g.client.id: abort(400, error='Not authorized') if len(data) == 0: abort(400, error='No data') if data.get('name'): os = Offer.objects(name=data['name']).first() if os is not None and os.id != o.id: abort(400, error='Name already exist') o.name = data['name'] if data.get('tags'): o.tags = data['tags'] if data.get('price'): o.price = data['price'] if data.get('description'): o.description = data['description'] o.save() return 'Offer successfully patched', 204
def post(self): """ Add Offer """ data = request.json if g.client.type != 'facilitator': abort(400, error='You must be facilitator') f = Facilitator.objects.get_or_404(id=g.client.id) if Offer.objects(name=data['name']).count() > 0: abort(400, error='Name already exist') o = Offer(facilitator=f, name=data['name'], tags=data.get('tags'), price=data['price'], description=data.get('description')) o.save() return o