Search code examples
pythondjangodjango-piston

Wrong redirections with django-piston


I have a problems with my django api. I have a website that works well, and now I want make a api with django-piston to make a android app. When I try to get some data, and I sent some key in the url, the handlers of my api project are empty, these are my urls.py and handlers.py inside api app.

urls.py

from django.conf.urls.defaults import *
from piston.resource import Resource
from myproject.api.handlers import ProgramadorHandler
from myproject.api.handlers import UserHandler
from myproject.api.handlers import GetProgHandler
from myproject.api.handlers import GetUserHandler

prog_handler = Resource(ProgramadorHandler)
user_handler = Resource(UserHandler)
get_prog_handler = Resource(GetProgHandler)
get_user_handler = Resource(GetUserHandler)

urlpatterns = patterns('',
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/programadores.(?P<emitter_format>.+)$', prog_handler),
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/programadores/(?P<prog_id>\d+)/prog.(?P<emitter_format>.+)$', get_prog_handler),
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/usuarios.(? P<emitter_format>.+)$', user_handler),
url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/user.(? P<emitter_format>.+)$', get_user_handler),
)

And this is my handlers.py

from piston.handler import BaseHandler
from piston.utils import rc, HttpStatusCode
from myproject.web.models import *
from django.contrib.auth import authenticate

class ProgramadorHandler(BaseHandler):
allowed_methods = ('GET',)
model = Programador

def read(self, request, username, password, api_key):
    user = authenticate(username=username, password=password)
#       if (api_key != 29010):
#           return rc.FORBIDDEN

    if user is not None:
        return Programador.objects.filter(nombre="Programador completo 2")

    return Programador.objects.all()


class GetProgHandler(BaseHandler):
allowed_methods = ('GET',)
model = Programador

def read(self, request, username, password, api_key, prog_id):
    user = authenticate(username=username, password=password)
    if user is None or (api_key != 29010):
        return rc.FORBIDDEN

    return Programador.objects.all()


class UserHandler(BaseHandler):
allowed_methods = ('GET',)
model = Usuario

def read(self, request, username, password, api_key):
    user = authenticate(username=username, password=password)
    if(api_key != 29010):
        return rc.FORBIDDEN

    return Usuario.objects.all()


class GetUserHandler(BaseHandler):
allowed_methods = ('GET',)
model = Usuario

def read(self, request, username, password, api_key):
    #user = authenticate(username=username, password=password)
#       if (api_key != 29010):
#           user = "hola"
        #return rc.FORBIDDEN

    return Usuario.objects.get(username="ersame")

How I can fix that?


Solution

  • I think you may have your regex's wrong in urls.py. The pattern specifier for the groups need to be inside the parenthesis, for example,

    instead of:

    url(r'^1.0/(?P<username>)\w+/(?P<password>)\w+/(?P<api_key>)\d+/programadores.(?P<emitter_format>.+)$', prog_handler), ...
    

    try:

    url(r'^1.0/(?P<username>\w+)/(?P<password>\w+)/(?P<api_key>\d+)/programadores.(?    P<emitter_format>.+)$', prog_handler), ...