How to handle actions in Django/Tastypie?

I'm writing a web app which purpose is to act as an intermediate layer between an application and Amazon's API. I figured a REST style API would fit and chose Tastypie to ease the implementation.

I've created a model called Instance and a Tastypie resource to go with it. To simplify a bit, let's say a PUT to that resource would launch an EC2 instance and a DELETE would stop it. Where is the proper place to handle the these actions where I need to communicate with the Amazon's API? Should it go in the resource code, the model code or elsewhere?

Also, what is the most appropriate way to return error messages to the client?

Answers


I would do it like this:

  • PUT creates a new Instance and stores it in the database
  • A Django Post Save Signal on the Instance model executes a special thingy:

in models.py:

@receiver(post_save, sender=Instance, dispatch_uid="create_instance")
def create_instance(sender, **kwargs):
    instance = kwargs['instance']
    created = kwargs['created']
    raw = kwargs['raw']
    if instance and created and not raw:
        from my_project.my_app.tasks import create_ec2_instance
        result = create_ec2_instance(instance)
        if result:
             instance.started = True
             instance.save()

in tasks.py:

def create_ec2_instance(instace):
    # do the calls to ec2 to create the instance and get a result form it
    return the_result_from_ec2

Need Your Help

keyword/phrase density from a mysql database

php lucene sphinx keyword

I have a LAMP setup, with the mysql database essentially being a catalog of products. Since the database changes frequently as new products are added, it's cumbersome to manually maintain a list of

How to get current System date in SmartGwt application

java javascript date smartgwt

How to get current system date in Smartgwt application (Merely javascript). I need date to be printed as 25 October 2010 something like this. Currently i am using the following code: String

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.