Giter Club home page Giter Club logo

dj-webmachine's Introduction

dj-webmachine

dj-webmachine is an application layer that adds HTTP semantic awareness on top of Django and provides a simple and clean way to connect that to your applications' behavior. dj-webmachine also offers you the possibility to build simple API based on your model and the tools to create automatically docs and clients from it (work in progress).

dj-webmachine is released under the MIT license. See the LICENSE file for the complete license.

Copyright (c) 2010 Benoit Chesneau <[email protected]> Copyright 2010 (c) Hyperweek - http://hyperweek.net

Install

Make sure that you have a working Python 2.x >=2.5 installed and Django >= 1.1.

With pip

$ pip install dj-webmachine

From source

Get the dj-webmachine code:

$ git clone https://github.com/benoitc/dj-webmachine.git
$ cd dj-webmachine

Or using a tarbal:

$ wget http://github.com/benoitc/dj-webmachine/tarball/master -o dj-webmachine.tar.gz
$ tar xvzf dj-webmachine.tar.gz
$ cd dj-webmachine-$HASH/

and install:

$ sudo python setup.py install

dj-webmachine in 5 minutes

We will quickly create an Hello world accepting HTML and JSON.

$ django-admin startproject helloworld
$ cd helloworld
$ python manage.py startapp hello

In the hello folder create a file named resources.py:

import json
from webmachine import Resource

class Hello(Resource):

    def content_types_provided(self, req, resp):
        """" define the content type we render accoridng the Accept
        header.
        """
        return ( 
            ("", self.to_html),
            ("application/json", self.to_json)
        )

    def to_html(self, req, resp):
        return "<html><body>Hello world!</body></html>\n"

    def to_json(self, req, resp):
        return "%s\n" % json.dumps({"message": "hello world!", "ok": True})

Add dj-webmachine and your hello app to INSTALLED_APPS in your settings:

INSTALLED_APPS = (
    ...
    'webmachine',
    'helloworld.hello'
)

Put your the Hello resource in your urls.py:

from django.conf.urls.defaults import *

from helloworld.hello.resource import Hello

urlpatterns = patterns('',
    (r'^$', Hello()),
)

Launch your application:

$ python manage.py runserver

Take a look! Point a web browser at http://localhost:8000/

Or with curl:

$ curl http://127.0.0.1:8000
<html><body>Hello world!</body></html>

$ curl http://127.0.0.1:8000 -H "Accept: application/json"
{"message": "hello world!", "ok": true}    

The first line ask the hello page as html while the second using the same url ask for JSON.

To learn how to do more interresting things, checkout some examples or read more documentations .

dj-webmachine's People

Contributors

bavovna avatar benoitc avatar brutasse avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dj-webmachine's Issues

KeyError in content_types_provided when using wm.route decorator

When you use the decorator wm.route on a function to register a new route, the WM implementation of the content_types_provided method can generate a KeyError if the request method is not in the wm methods dictionary.

For example, using the helloworld2 example included in the project, if you send a POST request on the root url:
curl -X POST http://localhost:8000/

a KeyError is raised:

File "/Users/Ludo/prog/django/core/handlers/base.py", line 111, in get_response
  response = callback(request, _callback_args, *_callback_kwargs)
File "/Users/Ludo/prog/django/webmachine/resource.py", line 646, in **call**
  return self._process(request, _args, *_kwargs)
File "/Users/Ludo/prog/django/webmachine/resource.py", line 605, in _process
  ctypes = [ct for (ct, func) in (self.content_types_provided(req, resp) or [])]
File "/Users/Ludo/prog/django/webmachine/route.py", line 208, in content_types_provided
  fun = self.methods[req.method]
KeyError: 'POST'

I am not sure about a fix, but either checking earlier in resource.Resource._process for not allowed method or directly in the content_types_provided should be fine.

if-modified-since UTC

l17 in decisions.py do:

return req.if_modified_since > datetime.datetime.now(UTC)

But our 'now' may not be UTC when using django 'TIMZONE' in settings.py.

If confirmed, I will try get a fix.

Check PUT

Hello,

When I do an HTTP form PUT with the resource method resource_exists returning False the method self.from_form then self.to_json still get called as if resource_exists returned True.

It works for DELETE so I was wondering if it was intended.

http://www.friendpaste.com/2KHY4sG57Xjd9zmzHn4Vd0

def content_types_accepted(self, req, resp):
        return [ 
            ("application/x-www-form-urlencoded", self.from_form)
        ]

I had to do

def from_form(self, req, resp):
    if not self.resource_exists(req, resp):
        raise HTTPNotFound()
    return

In order to return 404 in case the resource did not exist.

There's no way to return data with other status codes.

There's currently no way to return data along with a status 201 "Created" ( or any other status codes ) beyond manually writing encoders for each provided type and manually monkey patching the response content and headers (like content_type etc).

... which makes this framework useful for some API's but not for RESTful ones. Being able to return the created ID, for example, along with a 201 - Created is essential.

I've created a small patch which addresses this issue:
https://github.com/Go2Africa/dj-webmachine/commit/dc8b42686c48c914e52797b82711099da71d3eee

Wrong import [DOC]

it's not "from helloworld.hello.resource import Hello".
It's "from helloworld.hello.ressources import Hello"

Add JSONP support

Having JSONP support should be great to use the API form other domains.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.