Ejemplo n.º 1
0
def create_event(current_user):
    """
    Create an event from the sent json data.
    :param current_user: Current User
    :return:
    """
    if request.content_type == 'application/json':
        data = request.get_json().get("event")
        name = data.get('name') if data.get('name') is not None else None
        location = data.get('location') if data.get(
            'location') is not None else None
        time = data.get('time') if data.get('time') is not None else None
        eval_link = data.get('eval_link') if data.get(
            'eval_link') is not None else None
        if name and location and time:
            user_event = Event(name, location, time, current_user.id)
            user_event.event_eval_link = eval_link
            user_event.save()
            return response_for_created_event(user_event, 201)
        return response('failed',
                        'Missing some event data, nothing was changed', 400)
    return response('failed', 'Content-type must be json', 202)
Ejemplo n.º 2
0
def edit_event(current_user, event_id):
    """
    Validate the event Id. Also check for the data in the json payload.
    If the data exists update the event with the new data.
    :param current_user: Current User
    :param event_id: Event Id
    :return: Http Json response
    """
    if request.content_type == 'application/json':
        data = request.get_json().get("event")
        name = data.get('name') if data.get("name") is not None else ""
        location = data.get('location') if data.get(
            "location") is not None else ""
        time = data.get('time') if data.get("time") is not None else ""
        eval_link = data.get('eval_link') if data.get(
            "eval_link") is not None else ""
        updated_event = Event(name, location, time, current_user.id)
        updated_event.event_eval_link = eval_link
        if name or location or time or eval_link:
            try:
                int(event_id)
            except ValueError:
                return response('failed', 'Please provide a valid Event Id',
                                400)
            user_event = User.get_by_id(
                current_user.id).events.filter_by(event_id=event_id).first()
            if user_event:
                user_event.update(updated_event)
                return response_for_created_event(user_event, 201)
            return response(
                'failed', 'The Event with Id ' + event_id + ' does not exist',
                404)
        return response(
            'failed',
            'No attribute or value was specified, nothing was changed', 400)
    return response('failed', 'Content-type must be json', 202)