Beispiel #1
0
def add_place(request):
    """
    Add new place to db.
    """
    if request.method == 'POST':
        data = request.data['place'].copy()
        data['author'] = BaseUser.objects.get(user=request.user).id
        data['price'] = 2.2
        photo = request.data['place']['photo']

        serializer = PlaceSerializer(data=data)
        if (serializer.is_valid()):
            serializer.save()
            place_id = serializer.data['pk']
            place = Place.objects.get(pk=place_id)
            photo_place = PlacePhoto.objects.create(place=place,
                                                    photo_url=photo)
            photo_place.save()
            return JSONResponse(
                {
                    'place': serializer.data,
                    'photo': photo_place.photo_url
                },
                status=201)
        return JSONResponse(serializer.errors, status=404)
Beispiel #2
0
def get_place_likes(request):
    """
    Get place like count.
    """
    place_id = request.data['place_id']
    like_count = Like.objects.filter(place=place_id).count()
    return JSONResponse({'like_count': like_count}, status=201)
Beispiel #3
0
def subscribe_trip(request):
    """
    Subscribe on trip.
    """
    trip = Trip.objects.get(pk=request.data['tripId'])
    _subscribe(request.user.id, trip)
    return JSONResponse({}, status=201)
Beispiel #4
0
def get_place_detail(request):
    """
    Get place detail by id.
    """
    id = request.data['place_id']
    place = Place.objects.get(pk=id)
    photo = PlacePhoto.objects.filter(place=place)
    place_data = ({
        'user':
        place.author.user.username if place.author is not None else "",
        'title':
        place.title,
        'description':
        place.description,
        'id':
        place.id,
        'latitude':
        place.latitude,
        'langtitude':
        place.langtitude,
        'category':
        place.category.name if place.category is not None else "",
        'photo':
        photo[0].photo_url if photo.exists() else ""
    })
    return JSONResponse(place_data, status=201)
Beispiel #5
0
def country_list(request):
    """
    Get country list
    """
    countries = Country.objects.all()
    serializer = CountrySerializer(countries, many=True)
    return JSONResponse(serializer.data, status=201)
Beispiel #6
0
def remove_route(request):
    """
    Remove route by id.
    """
    route = Route.objects.get(pk=request.data['routeId'])
    route.delete()
    return JSONResponse({'route': route.id}, status=201)
Beispiel #7
0
def categort_list(request):
    """
    Get category list
    """
    categories = Category.objects.all()
    serializer = CategorySerializer(categories, many=True)
    return JSONResponse(serializer.data, status=201)
Beispiel #8
0
def get_route_detail(request):
    """
    Get route detail with related places
    """
    route_id = request.data['routeId']
    route_entity = Route.objects.get(pk=route_id)
    route_place_entity = RoutePlace.objects.filter(route__pk=route_id)
    print route_place_entity[0].place.id
    places = []
    for route_place in route_place_entity:
        if route_place.place is not None:
            photo = PlacePhoto.objects.filter(place=route_place.place)
            place = {
                'id': route_place.place.id,
                'title': route_place.place.title,
                'description': route_place.place.description,
                'photo': photo[0].photo_url if photo.exists() else "",
                'latitude': route_place.place.latitude,
                'langtitude': route_place.place.langtitude,
            }
        places.append(place)
    route = {
        'name': route_entity.name,
        'description': route_entity.description,
        'places': places
    }
    return JSONResponse({'route': route}, status=201)
Beispiel #9
0
def place_list(request):
    """
    Get place list.
    """
    places = Place.objects.all()
    place_data = get_place_data(places)
    return JSONResponse({'places': place_data}, status=201)
Beispiel #10
0
def search(request):
    """
    Search place by entered title.
    """
    search_text = request.data['search_text']
    search_data = Place.objects.filter(title__icontains=search_text.lower())
    places = get_place_data(search_data)
    return JSONResponse({'places': places}, status=201)
Beispiel #11
0
def get_category_places(request):
    """
    Get place by selected category
    """
    category_id = request.data['category_id']
    places = Place.objects.filter(category=category_id)
    place_data = get_place_data(places)
    return JSONResponse({'places': place_data}, status=201)
Beispiel #12
0
def remove_place(request):
    """
    Delete place from db.
    """
    id = request.data['place_id']
    place = Place.objects.get(pk=id)
    place.delete()
    return JSONResponse({}, status=201)
Beispiel #13
0
def place_like(request):
    """
    Like place.
    """
    place_id = request.data['place']
    place = Place.objects.get(pk=place_id)
    user = BaseUser.objects.get(user=request.user.id)
    Like.objects.create(place=place, user=user)
    return JSONResponse({}, status=201)
Beispiel #14
0
def get_trip_by_user(request):
    """
    Get trips by user.
    """
    subsciber = BaseUser.objects.get(user=request.user.id)
    trip_enties = [
        x.trip for x in TripFollower.objects.filter(follower=subsciber)
    ]
    trips = _convert_to_json(trip_enties)
    return JSONResponse({'trips': trips}, status=201)
Beispiel #15
0
def get_route_list(request):
    """
    Get all routes
    """
    route_entities = Route.objects.all()
    routes = []
    for route in route_entities:
        routes.append({
            'id': route.pk,
            'name': route.name,
            'description': route.description
        })
    return JSONResponse({'routes': routes}, status=201)
Beispiel #16
0
def create_trip(request):
    """
    Create new trip.
    """
    route = Route.objects.get(pk=request.data['routeId'])
    author = BaseUser.objects.get(user=request.user.id)
    trip_entity = Trip.objects.create(route=route,
                                      author=author,
                                      date_from=request.data['dateFrom'],
                                      date_to=request.data['dateTo'])
    trip_entity.save()
    _subscribe(request.user.id, trip_entity)
    return JSONResponse({}, status=201)
Beispiel #17
0
def save_place_photo(request):
    """
    Bind photo to place.
    """
    photo = request.FILES['photo']
    path = 'media/' + photo.name
    with Image.open(photo) as pil_image:
        # Check format of input image
        if pil_image.format not in ('GIF', 'JPEG', 'PNG'):
            raise Exception(
                "Unsupport image type. Please upload bmp, png or jpeg")
        pil_image.save(path)
    return JSONResponse({'path': path}, status=201)
Beispiel #18
0
def discard_trip(request):
    """
    Discard from trip.
    """
    trip = Trip.objects.get(pk=request.data['tripId'])
    subsciber = BaseUser.objects.get(user=request.user.id)
    trip_follower = TripFollower.objects.filter(trip=trip, follower=subsciber)
    followers_count = TripFollower.objects.filter(trip=trip).count()
    if followers_count <= 1:
        trip_follower[0].delete()
        trip.delete()
    else:
        trip_follower[0].delete()
    return JSONResponse({}, status=201)
Beispiel #19
0
def get_trip_date(request):
    """
    Get trip date list.
    """
    trips = []
    trip_entities = TripFollower.objects.filter(
        trip__route__pk=request.data['tripId'])
    print request.data['tripId']
    for trip in trip_entities:
        trips.append({
            'tripId': trip.trip.pk,
            'dateFrom': trip.trip.date_from,
            'dateTo': trip.trip.date_to
        })
    return JSONResponse({'trips': trips}, status=201)
Beispiel #20
0
def save_route(request):
    """
    Save route instance after then 
    join place to route
    """
    route = request.data['route']
    author = BaseUser.objects.get(user=request.user.id)
    route_entity = Route.objects.create(name=route['name'],
                                        description=route.get('description'),
                                        author=author)
    route_entity.save()
    for place in route['selectedPlaces']:
        place_entity = Place.objects.get(pk=place['id'])
        route_place_entity = RoutePlace.objects.create(place=place_entity,
                                                       route=route_entity)
        route_place_entity.save()
    return JSONResponse({}, status=201)
Beispiel #21
0
def get_by_user(request):
    author = BaseUser.objects.get(user=request.user.id)
    places = Place.objects.filter(author__id=author.id)
    place_data = get_place_data(places)
    serializer = PlaceSerializer(places, many=True)
    return JSONResponse({'places': place_data}, status=201)
Beispiel #22
0
def get_all(request):
    """
    Get all trip.
    """
    trips = _convert_to_json(Trip.objects.all())
    return JSONResponse({'trips': trips}, status=201)
Beispiel #23
0
def get_place_page(request):
    page_id = request.data['page_id']
    places = get_page(page_id, Place.objects.all())
    serializer = PlaceSerializer(places, many=True)
    return JSONResponse(serializer.data, status=201)