def attend(event_id): schema = EventSchema() event = Event.get(id=event_id) event.attended_by.add(g.current_user.id) db.commit() return schema.dumps(event)
def show(event_id): schema = EventSchema() event = Event.get(id=event_id) if not event: abort(404) return schema.dumps(event)
def create(): schema = EventSchema() try: data = schema.load(request.get_json()) event = Event(**data, created_by=g.current_user) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(event), 201
def update(event_id): schema = EventSchema() event = Event.get(id=event_id) if not event: return jsonify({'message': 'Event not found'}), 404 if not event.user.id == g.current_user.id: return jsonify({'message': 'User not authorized'}), 404 try: data = schema.load(request.get_json()) event.set(**data) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(event)
def update(event_id): schema = EventSchema() event = Event.get(id=event_id) if not event: abort(404) try: data = schema.load(request.get_json()) event.set(**data) db.commit() except ValidationError as err: return jsonify({ 'message': 'Validation failed', 'errors': err.messages }), 422 return schema.dumps(event)
def index(): schema = EventSchema(many=True) events = Event.select() return schema.dumps(events)