示例#1
0
def item_show_action(request, id):
    context = {}
    items = Item.objects.filter(pk=id)
    if len(items) == 0:
        return render(request, 'items/item_404.html', context)

    item = items[0]
    context['item'] = Item.serialize_one(item, request.user)
    return render(request, 'items/item_show.html', context)
示例#2
0
def filter_item_listing_action(request):
    def default_response(profile):
        items = Item.objects.exclude(seller=profile)
        item_list = Item.serialize_many(items, request.user)
        return JsonResponse(item_list)

    own_profile = Profile.objects.get(user=request.user)

    if request.method != 'GET':
        print("Only processing get requests.")
        return default_response(own_profile)

    query_json = request.GET.get('query_labels')
    if query_json is None:
        print("No query labels were given.")
        return default_response(own_profile)

    lat = request.GET.get('user_lat')
    lng = request.GET.get('user_lng')
    if lat is None or lng is None:
        print("No location given.")
        return default_response(own_profile)

    try:
        lat = float(lat)
        lng = float(lng)
    except:
        print("Query location not numbers.")
        return default_response(own_profile)

    # check if any labels are invalid
    query_labels = json.loads(query_json)
    tmp = validate_query_labels(query_labels)
    if tmp is None:
        return default_response(own_profile)
    (distance, values) = tmp

    # no labels selected, need to select all items first
    items = Item.objects.exclude(seller=own_profile)
    if len(values) > 0:
        for value in values:
            items = items.filter(labels__in=[value])

    # calculate items that are within the distance
    if not (distance is None) and not (lat is None) and not (lng is None):
        items = [item for item in items if item_haversine(item, lat, lng)]

    item_list = Item.serialize_many(items, request.user)
    return JsonResponse(item_list)
示例#3
0
def item_interest_action(request, item_id):
    item = Item.objects.get(pk=item_id)
    profile = Profile.objects.get(user=request.user)
    if item.seller == profile:
        result = {"message": "Cannot express interest in your own item."}
        return JsonResponse(result)

    if (profile not in item.interested.all()):
        item.interested.add(profile)
    else:
        item.interested.remove(profile)
    item.save()

    # return the updated item information
    item_ser = Item.serialize_one(item, request.user)
    return JsonResponse({"item": item_ser})
示例#4
0
def item_update_action(request, id):
    if request.method == 'GET':
        context = {}
        context['form'] = EditItemForm()
        return render(request, 'items/item_update.html', context)

    item = Item.objects.get(pk=id)
    if item.seller.user != request.user:
        result = {"message": "Cannot edit someone else's item."}
        return JsonResponse(result)

    name = request.POST.get('name')
    description = request.POST.get('description')
    price = request.POST.get('price')
    street = request.POST.get('street')
    city = request.POST.get('city')
    state = request.POST.get('state')
    zipcode = request.POST.get('zip')

    if name is None or description is None or price is None or street is None \
            or city is None or state is None or zipcode is None:
        result = {"message": "Missing parameters."}
        return JsonResponse(result)

    latitude, longitude = get_coordinates(request)

    item.name = name
    item.description = description
    item.street = street
    item.city = city
    item.state = state
    item.latitude = latitude
    item.longitude = longitude
    item.zip_code = zipcode
    item.save()

    new_price = Price.objects.create(start_date=datetime.now(timezone.utc),
                                     end_date=None,
                                     price=float(price),
                                     item=item)
    new_price.save()
    return JsonResponse(Item.serialize_one(item, request.user))
示例#5
0
 def default_response(profile):
     items = Item.objects.exclude(seller=profile)
     item_list = Item.serialize_many(items, request.user)
     return JsonResponse(item_list)
示例#6
0
def get_interest_listing(request):
    profile = Profile.objects.get(user=request.user)
    items = Item.objects.filter(interested__in=[profile])
    item_list = Item.serialize_many(items, request.user)
    return JsonResponse({"items": item_list})
示例#7
0
def get_personal_listing(request):
    profile = Profile.objects.get(user=request.user)
    items = Item.objects.filter(seller=profile)
    item_list = Item.serialize_many(items, request.user)
    return JsonResponse({"items": item_list})
示例#8
0
def get_storefront_listing(request):
    profile = Profile.objects.get(user=request.user)
    items = Item.objects.exclude(seller=profile)
    item_list = Item.serialize_many(items, request.user)
    return JsonResponse({"items": item_list})