Ejemplo n.º 1
0
def add_trip(request):
    """
    Add a trip with current user as a default member to it
    :param request:
    :return: 400 if incorrect parameters are sent or database request failed
    :return: 201 successful
    """
    trip_name = request.POST.get('trip_name', None)
    start_date_tx = request.POST.get('start_date_tx', None)
    city_id = request.POST.get('city_id', None)

    if not trip_name or not start_date_tx or not city_id:
        # incorrect request received
        error_message = "Missing parameters in request. Send trip_name, city_id, start_date_tx"
        return Response(error_message, status=status.HTTP_400_BAD_REQUEST)

    try:
        city = City.objects.get(pk=city_id)
        trip = Trip(trip_name=trip_name,
                    city=city,
                    start_date_tx=start_date_tx)
        trip.save()
        trip.users.add(request.user)
    except Exception as e:
        error_message = str(e)
        return Response(error_message, status=status.HTTP_400_BAD_REQUEST)

    success_message = "Successfully added new trip."
    return Response(success_message, status=status.HTTP_201_CREATED)
Ejemplo n.º 2
0
def get_trip_by_id(id):
    trip = Trip.get_by_id(id)
    if trip:

        return jsonify(trip.to_dict()), 200

    return jsonify({'error': "Trips not found"}), 404
Ejemplo n.º 3
0
 def trip():
     if request.method == 'POST':
         name = str(request.data.get('name', ''))
         description = str(request.data.get('description', ''))
         date_starts = str(request.data.get('date_starts', ''))
         date_ends = str(request.data.get('date_ends', ''))
         owner = str(request.data.get('owner', ''))
         date_starts = datetime.datetime.strptime(date_starts,
                                                  '%d-%m-%Y %H:%M')
         date_ends = datetime.datetime.strptime(date_ends, '%d-%m-%Y %H:%M')
         if name and date_starts and date_ends:
             trip = Trip(name=name,
                         description=description,
                         date_starts=date_starts,
                         date_ends=date_ends,
                         owner=owner)
             trip.save()
             response = jsonify({
                 'id': trip.id,
                 'name': trip.name,
                 'description': trip.description,
                 'date_created': trip.date_created,
                 'date_starts': trip.date_starts,
                 'date_ends': trip.date_ends,
                 'owner': trip.owner
             })
             response.status_code = 201
             return response
     else:  #GET
         trips = Trip.get_all()
         results = []
         for trip in trips:
             obj = {
                 'id': trip.id,
                 'name': trip.name,
                 'description': trip.description,
                 'date_created': trip.date_created,
                 'date_starts': trip.date_starts,
                 'date_ends': trip.date_ends,
                 'owner': trip.owner
             }
             results.append(obj)
         response = jsonify(results)
         response.status_code = 200
         return response
Ejemplo n.º 4
0
def get_all_trips():

    trips = Trip.get_all()
    print(trips)
    if trips:
        trips_dict = [trip.to_dict() for trip in trips]
        return jsonify(trips_dict), 200

    return jsonify({'error': "Trips not found"}), 404
Ejemplo n.º 5
0
def create_trip(id):

    traveler_id = get_jwt_identity()
    country = request.json.get('country', None)
    cities = request.json.get('cities', None)
    activities = request.json.get('activities', None)
    start_date = request.json.get('start_date', None)
    end_date = request.json.get('end_date', None)

    new_trip = Trip(country=country,
                    cities=cities,
                    activities=activities,
                    start_date=start_date,
                    end_date=end_date,
                    traveler_id=traveler_id["id"])
    if new_trip:
        new_trip.create()
        return jsonify(new_trip.to_dict()), 201

    else:
        return {'error': 'Something went wrong'}, 409
Ejemplo n.º 6
0
def share_trip(id_traveler, id_trip):

    trip = Trip.get_by_id(id_trip)
    traveler_id = get_jwt_identity()

    if traveler_id == id_traveler:
        return {'error': 'Something went wrong'}, 405
    elif trip:
        shared = Shared_Trip.get_by_trip_id(trip.id)
        if id_traveler in shared:
            return {'error': 'You are on this trip'}, 405
        else:
            new_shared_trip = Shared_Trip(trip_id=trip.id,
                                          traveler_id=id_traveler)
            new_shared_trip.create()

            return jsonify(new_shared_trip.to_dict()), 201

        return {'error': 'Something went wrong'}, 404
Ejemplo n.º 7
0
def tripList(request):
    all_objs = Trip.objects.get(pk=1)
    all_dicts = Trip.toDict(all_objs)
    all_jsons = json.dumps(all_dicts)
    return HttpResponse(all_jsons)