Presentation is loading. Please wait.

Presentation is loading. Please wait.

Kausikram Krishnasayee

Similar presentations


Presentation on theme: "Kausikram Krishnasayee"— Presentation transcript:

1 Kausikram Krishnasayee
BEYOND DJANGO 101 Kausikram Krishnasayee

2 What do I have ? A simple application. Bare bone.
Written in 15 minutes time after reading the Django docs. Show the application first. Its barebone we have no authentication set. We wrote a 100 different templates for everything, and we barely got into a hackking mode. Django 101 documentation is great. Now we will be getting beyond that.

3 What Do we want ?? We want a whole set of features. That includes:
RESTfull API calls. Nice Api Authentication system. Subdomains Permissions Restrictions.

4 What will we be touching upon??
Serialization and serializers. Middlewares. Auth Backends. Testing Plugins.

5 Problem 1: I want a view to return xml.

6 What we do usually ... Django view Render to response XML template

7 What we can do? >>> from django.core.serializers import serialize >>> serialize("xml",ul) <?xml version="1.0" encoding="utf-8"?> <django-objects version="1.0"> <object pk="7508" model="tweet.tweet"> <field type="CharField" name="username">kausikram</field> <field type="CharField" name="text">This is my text</field> <field type="DateTimeField" name="time_tweeted"> :53:02</field> </object> </django-objects> >>> serialize("json",ul) [{ "pk": 7508, "model": "tweet.tweet", "fields": { "username": "kausikram", "text": “This is my text”, "time_tweeted": " :53:07", } }]

8 Quirks: >>> from django.core.serializers import serialize
>>> serialize("xml",ul) <?xml version="1.0" encoding="utf-8"?> <django-objects version="1.0"> <object pk="7508" model="auth.user"> <field type="CharField" name="username">silver.nobody</field> <field type="CharField" name="first_name">Nobody</field> <field type="CharField" name="last_name"> </field> <field type="CharField" name=" ">nobody</field> <field type="CharField" name="password">sha1$d295c$64f920c5c5a4451ba02442f85b64a</field> <field type="BooleanField"name="is_staff">False</field> <field type="BooleanField" name="is_active">True</field> <field type="BooleanField" name="is_superuser">False</field> <field type="DateTimeField" name="last_login"> :53:02</field> <field type="DateTimeField" name="date_joined"> :53:02</field> <field to="auth.group" name="groups" rel="ManyToManyRel"> <field to="auth.permission" name="user_permissions" rel="ManyToManyRel"> </object> </django-objects>

9 Lets write our own serializer
Step 1 : Extend django.serializer and play with it. Step2 : Register the serializer you just wrote in your settings file. Step 3 : Tada!

10 Serializer : Hiding fields

11 Serializer : Adding Custom Fields

12 Serializers : The Advantage
Portable My Boss decided JSON is the coolest thing. Editable Err can we change The XML structure ?? Consistent Oh Gosh! There are too many templates and views that generate random xml structures.

13 Problem 2 : I want to have a fancy pants API system that allows calls to views requiring authentication.

14 Middleware: What ? Web Browser Django Middlewares Your View

15 Middleware: What are we going to do?
Our Middleware Django Middlewares Your View Browser

16 Middleware : Solution Create a middleware that will basically perform a logic operation based on the API system you implemented. And if the logic operation ascertains a users credibility, then set request.user to equal that user's django.auth.contrib.models.User instance.

17 Problem 3 : I want a subdomain specific authentication system.

18 Auth Backends: What ? Browser Django Session Middleware
Django Auth Middleware Other Middlewares django.contrib.auth's get_user() auth_backend's get_user()

19 Auth Backends: What ? Set request.user and call
django.contrib.auth's login Django's Login View Django Login Form django.contrib.auth's authenticate() auth_backend's authenticate()

20 Auth Backend : Solution
Set request.user and call django.contrib.auth's login Custom Login View django.contrib.auth's authenticate() auth_backend's authenticate()

21 Problem 4: I want my test suite to be much more flexible.

22 Understanding Django Test Runner
django.db.connection connection.creation.create_test_db() Start of Test django.test.utils setup_test_environment() django.test.utils teardown_test_environment django.db.connection connection.creation.create_test_db() Test runner iterates over test cases

23 Django Nose Plugin Plugin's begin django.test.utils
setup_test_environment() django.db.connection connection.creation.create_test_db() Plugin's finalize Test runner iterates over test cases django.test.utils teardown_test_environment django.db.connection connection.creation.create_test_db()

24 Django Nose Plugin: allowing multiple DB test
Overwrite settings.DatabaseSettings Plugin's configure Plugin's begin django.test.utils setup_test_environment() django.db.connection connection.creation.create_test_db() Plugin's finalize Test runner iterates over test cases django.test.utils teardown_test_environment django.db.connection connection.creation.create_test_db()

25 Thank You!!


Download ppt "Kausikram Krishnasayee"

Similar presentations


Ads by Google