Beispiel #1
0
def new_item(request):
    errors = {'__all__': []}
    try:
        if not request.user.is_authenticated():
            errors['__all__'].append("user must be logged in to create an item")
            raise JsonResponseError
        
        form = ItemForm(request.POST)
        
        if not form.is_valid():
            errors.update(form.field_errors_dict())
            raise JsonResponseError
        
        item = Item(
            title = form.cleaned_data['title'],
            description = form.cleaned_data['description'],
            lat = form.cleaned_data['lat'],
            lng = form.cleaned_data['lng'],
            user = request.user
        )
        
        item.relist()
        item.save()
        
        response = {'success': True, 'item_id': item.id}
        return HttpResponse(simplejson.dumps(response))
    
    except JsonResponseError:
        response = {'success': False, 'errors': errors}
        return HttpResponse(simplejson.dumps(response))
Beispiel #2
0
def get_items(request):
    errors = {'__all__': []}
    try:
        form = GetItemsForm(request.GET)
        
        if not form.is_valid():
            errors.update(form.field_errors_dict())
            raise JsonResponseError
        
        if form.cleaned_data["use_q"]:
            q = "(default: %s) AND lat_f: [%s TO %s] AND lng_f: [%s TO %s]" % (
                form.cleaned_data["q"],
                form.cleaned_data["sw_lat"],
                form.cleaned_data["ne_lat"],
                form.cleaned_data["sw_lng"],
                form.cleaned_data["ne_lng"]
            )
            items = Item.find_by_solr(q)
            
        elif form.cleaned_data["use_map_bounds"]:
            items = Item.objects.filter(
                lat__gte = form.cleaned_data["sw_lat"],
                lat__lte = form.cleaned_data["ne_lat"],
                lng__gte = form.cleaned_data["sw_lng"],
                lng__lte = form.cleaned_data["ne_lng"]
            ).order_by('expires_at').reverse()[:100]
        else:
            items = Item.objects.order_by('expires_at').reverse()[:100]
        
        item_dicts = []
        for item in items:
            if not item.expired(): item_dicts.append( { 'item': item.simple_dict() } )
        
        response = { 'success': True, 'items': item_dicts }
        return HttpResponse(simplejson.dumps(response))
    
    except JsonResponseError:
        response = {'success': False, 'errors': errors}
        return HttpResponse(simplejson.dumps(response))