Re: Extending a custom built API

2010-03-09 Thread Nick
Thanks for the reference and critique, Tom. Currently this is an
internal API to connect two systems together. The query structure was
requested from one set of developers. I am planning on setting up an
external API for another project and this will be helpful in creating
a standard structure.

On Mar 9, 11:03 am, Tom Evans  wrote:
> On Mon, Mar 8, 2010 at 10:41 PM, Nick  wrote:
> > I am working on an api that outputs a list of JSON based on certain
> > criteria.  Currently if someone 
> > entershttp://example.mysite/slideshows/api?id=1
> > it returns a JSON serialized output of the slideshow with that ID.
>
> > What I would like to do is allow for multiple ids, so
> >http://example.mysite/slideshows/api?id=1,2,5,9,10will pull in JSON
> > values for each of those slideshows.
>
> > I'd like to keep from using a third party API solution, I have checked
> > them out and I like the customization with the hand built process
>
> > I am doing everything through a get process in a view, here it is:
>
> > def slideshowAPI2(request):
> >    error = False
> >    if 'id' in request.GET and request.GET['id']:
> >        id = request.GET['id']
> >        object = slideshow.objects.get(pk=id)
> >        return render_to_response('slideshow.json',
> >            {'object': object, 'id':id})
> >    if 'year' in request.GET and request.GET['year']:
> >        year = request.GET['year']
> >        object = serializers.serialize("json",
> > slideshow.objects.filter(publishdate__year=year))
> >        html = "%s" % object
> >        return HttpResponse(html)
> >    else:
> >        error = True
> >        return render_to_response('slideshow.json', {'error': True})
>
> Just a mild critique:
>
> http://example.mysite/slideshows/api?id=1,2,5,9,10
>
> This is not the usual or typical way to pass a list of values into a
> url query string. Typically, you would specify the id argument
> multiple times in the query string. This is how your browser, a JS
> framework, django or anything that is designed to parse query strings
> would expect to receive a list of values. Eg:
>
> http://example.mysite/slideshows/api?id=1=2=5=9=10
>
> In django, you can retrieve a list of values passed in a query string
> like this with the getlist() method on QueryDict[1]. Eg:
>
> if 'id' in request.POST:
>   ids = request.POST.getlist('id')
>
> Cheers
>
> Tom
>
> [1]http://docs.djangoproject.com/en/1.1/ref/request-response/#django.htt...

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Extending a custom built API

2010-03-09 Thread Tom Evans
On Mon, Mar 8, 2010 at 10:41 PM, Nick  wrote:
> I am working on an api that outputs a list of JSON based on certain
> criteria.  Currently if someone enters 
> http://example.mysite/slideshows/api?id=1
> it returns a JSON serialized output of the slideshow with that ID.
>
> What I would like to do is allow for multiple ids, so
> http://example.mysite/slideshows/api?id=1,2,5,9,10 will pull in JSON
> values for each of those slideshows.
>
> I'd like to keep from using a third party API solution, I have checked
> them out and I like the customization with the hand built process
>
> I am doing everything through a get process in a view, here it is:
>
>
> def slideshowAPI2(request):
>    error = False
>    if 'id' in request.GET and request.GET['id']:
>        id = request.GET['id']
>        object = slideshow.objects.get(pk=id)
>        return render_to_response('slideshow.json',
>            {'object': object, 'id':id})
>    if 'year' in request.GET and request.GET['year']:
>        year = request.GET['year']
>        object = serializers.serialize("json",
> slideshow.objects.filter(publishdate__year=year))
>        html = "%s" % object
>        return HttpResponse(html)
>    else:
>        error = True
>        return render_to_response('slideshow.json', {'error': True})
>

Just a mild critique:

http://example.mysite/slideshows/api?id=1,2,5,9,10

This is not the usual or typical way to pass a list of values into a
url query string. Typically, you would specify the id argument
multiple times in the query string. This is how your browser, a JS
framework, django or anything that is designed to parse query strings
would expect to receive a list of values. Eg:

http://example.mysite/slideshows/api?id=1=2=5=9=10

In django, you can retrieve a list of values passed in a query string
like this with the getlist() method on QueryDict[1]. Eg:

if 'id' in request.POST:
  ids = request.POST.getlist('id')


Cheers

Tom

[1] 
http://docs.djangoproject.com/en/1.1/ref/request-response/#django.http.QueryDict.getlist

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Extending a custom built API

2010-03-08 Thread Nick
Thanks, I did a little changing around but what you recommended was
spot on. Here is the final view

def slideshowAPI2(request):
error = False
if 'id' in request.GET and request.GET['id']:
id = request.GET.get('id')
ids = id.split(',')
object = slideshow.objects.filter(id__in=ids)
return render_to_response('slideshow.json', {'object':
object})
if 'year' in request.GET and request.GET['year']:
year = request.GET['year']
object = serializers.serialize("json",
slideshow.objects.filter(publishdate__year=year))
html = "%s" % object
return HttpResponse(html)
else:
error = True
return render_to_response('slideshow.json', {'error': True})


On Mar 8, 5:18 pm, felix  wrote:
> you are already basically there
>
> id = request.GET.get('id')
> if id:
>   ids = id.split(',')
>   slideshows = slideshow.objects.filter(id__in=ids)
>
> then returns that as json however you like
>
> On Mar 8, 11:41 pm, Nick  wrote:
>
> > I am working on an api that outputs a list of JSON based on certain
> > criteria.  Currently if someone 
> > entershttp://example.mysite/slideshows/api?id=1
> > it returns a JSON serialized output of the slideshow with that ID.
>
> > What I would like to do is allow for multiple ids, 
> > sohttp://example.mysite/slideshows/api?id=1,2,5,9,10willpull in JSON
> > values for each of those slideshows.
>
> > I'd like to keep from using a third party API solution, I have checked
> > them out and I like the customization with the hand built process
>
> > I am doing everything through a get process in a view, here it is:
>
> > def slideshowAPI2(request):
> >     error = False
> >     if 'id' in request.GET and request.GET['id']:
> >         id = request.GET['id']
> >         object = slideshow.objects.get(pk=id)
> >         return render_to_response('slideshow.json',
> >             {'object': object, 'id':id})
> >     if 'year' in request.GET and request.GET['year']:
> >         year = request.GET['year']
> >         object = serializers.serialize("json",
> > slideshow.objects.filter(publishdate__year=year))
> >         html = "%s" % object
> >         return HttpResponse(html)
> >     else:
> >         error = True
> >         return render_to_response('slideshow.json', {'error': True})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Re: Extending a custom built API

2010-03-08 Thread felix

you are already basically there

id = request.GET.get('id')
if id:
  ids = id.split(',')
  slideshows = slideshow.objects.filter(id__in=ids)

then returns that as json however you like



On Mar 8, 11:41 pm, Nick  wrote:
> I am working on an api that outputs a list of JSON based on certain
> criteria.  Currently if someone 
> entershttp://example.mysite/slideshows/api?id=1
> it returns a JSON serialized output of the slideshow with that ID.
>
> What I would like to do is allow for multiple ids, 
> sohttp://example.mysite/slideshows/api?id=1,2,5,9,10will pull in JSON
> values for each of those slideshows.
>
> I'd like to keep from using a third party API solution, I have checked
> them out and I like the customization with the hand built process
>
> I am doing everything through a get process in a view, here it is:
>
> def slideshowAPI2(request):
>     error = False
>     if 'id' in request.GET and request.GET['id']:
>         id = request.GET['id']
>         object = slideshow.objects.get(pk=id)
>         return render_to_response('slideshow.json',
>             {'object': object, 'id':id})
>     if 'year' in request.GET and request.GET['year']:
>         year = request.GET['year']
>         object = serializers.serialize("json",
> slideshow.objects.filter(publishdate__year=year))
>         html = "%s" % object
>         return HttpResponse(html)
>     else:
>         error = True
>         return render_to_response('slideshow.json', {'error': True})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.



Extending a custom built API

2010-03-08 Thread Nick
I am working on an api that outputs a list of JSON based on certain
criteria.  Currently if someone enters http://example.mysite/slideshows/api?id=1
it returns a JSON serialized output of the slideshow with that ID.

What I would like to do is allow for multiple ids, so
http://example.mysite/slideshows/api?id=1,2,5,9,10 will pull in JSON
values for each of those slideshows.

I'd like to keep from using a third party API solution, I have checked
them out and I like the customization with the hand built process

I am doing everything through a get process in a view, here it is:


def slideshowAPI2(request):
error = False
if 'id' in request.GET and request.GET['id']:
id = request.GET['id']
object = slideshow.objects.get(pk=id)
return render_to_response('slideshow.json',
{'object': object, 'id':id})
if 'year' in request.GET and request.GET['year']:
year = request.GET['year']
object = serializers.serialize("json",
slideshow.objects.filter(publishdate__year=year))
html = "%s" % object
return HttpResponse(html)
else:
error = True
return render_to_response('slideshow.json', {'error': True})

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.