Categories
Javascript

Extend Google Apps With Custom Scripts

Let’s get to the point straight way – we can extend the functionality of Google apps with our own scripts. Google allows us to create small scripts written in JS and attach these scripts to certain events (aka triggers) of the respective applications. When these events occur, the scripts are executed. For example, we can attach a script to the event when the user loads a document, or edits a spreadsheet or submits a form. The custom code we write allows the opportunity to do some extra processing. Thus it allows us to achieve a lot more than the default functionality. What are the common use cases?

— Adding our custom functions to spreadsheets
— Adding our own Menus to the user interface
— Creating Macros
— Do more with Forms: send customized messages
— Get sophisticated Gmail statistics
— Send out customized email messages to many people on the fly

and a lot more…

Google Developers Zone has it covered pretty nicely in the “Google Apps Script” section. Have a look yourself!

Just as a demonstration of what could be done, here’s a custom script I wrote to try it out. This script is for a form. It is attached to the “Form Submit” event of the form. When the form is submitted, my custom function gets an event. The event object has a “FormResponse” object which we can use to collect the field names and the values. Then we use “MailApp” object to send the submitted data to an email address:

We could do a lot more than that by adding a flick of our own imagination. Cool, no?

Categories
Python

Django REST Framework: Quick Introduction

Okay, so you know how cool Django is? Well, Django Rest Framework brings the Django like simplicity in building REST APIs.

Installation

Use pip to install:

Add to Installed Apps:

ModelViewSet: Doing The Magic

ModelViewSet takes a Model and automates the required REST access to it. Let’s see how simple it is.

We have a Model like this:

We will create a class based view for allowing REST access to this Model:

Add to urls.py to add URL handlers for the view:

Now run syncdb and runserver commands from manage.py. If you visit: localhost:8000 (or whatever the address of the development server on your machine), you should be able to see the api root. In the URLConf defined above, we should be able to access the Photo model on “/photo”. Use a REST client to try it out.

Authentication

Django Rest Framework supports multiple authentication systems. Here, we shall see how we can enable basic http auth to the REST APIs.

Add this to settings.py:

That’s it. Now we have the Django Auth system hooked into Django Rest Framework. Any user registered with the default django auth is a now valid user for the REST APIs as well. But how do we find the authenticated user? It’s simple as always. You can grab “request.user” for the user data and “request.auth” for extra auth data (specially when working with oauth implementation).

Let’s see how we can use in the class based views we demonstrated above:

Here we have overridden the list() method to demonstrate how to use the request object. If any user tries to access without authenticating, the request.user will be set to AnonymousUser. If authenticated, it will be set to the authenticated user.

By overriding the methods of a ModelViewSet, we can customize the response based on authentication status. We can also use Permission & Throttling policies for easier control. I’ll try to write about those in future blog posts.

Categories
Python

Django: Flush (Clear) Database

In Django, we often need to remove all the tables (with data) and recreate them from scratch. Specially, after changing a model structure, if we’re not using any database migration tool, we usually manually drop the table and run syncdb again. But there’s a quick way –

Django 1.5 and Higher

We have to collect the output of sqlclear and then pass it to dbshell.

Django 1.4

On earlier versions of Django, you could use “reset ” for removing tables from a particular app.

DEPRECATED: The command is deprecated and newer versions use the flush command. Check out the Django manual for more details.