<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Building Blocks &#187; django</title>
	<atom:link href="http://joelhooks.com/category/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelhooks.com</link>
	<description>test until fear turns to boredom.</description>
	<lastBuildDate>Tue, 29 Jun 2010 18:45:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
		<item>
		<title>Django Authorization from Flex/AIR via PyAMF</title>
		<link>http://joelhooks.com/2008/09/21/django-authorization-from-flex-air-actionscript-via-pyamf/</link>
		<comments>http://joelhooks.com/2008/09/21/django-authorization-from-flex-air-actionscript-via-pyamf/#comments</comments>
		<pubDate>Mon, 22 Sep 2008 01:41:57 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[pyAMF]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=88</guid>
		<description><![CDATA[Django views serve nicely as service end-points for Flex applications. Here are some notes on maintaining authenticated sessions between a Flex/Air/Flash application and your Django backend. gateway.py 1 2 3 4 5 6 7 8 from pyamf.remoting.gateway.django import DjangoGateway &#160; import myproject.myapp.views as views &#160; gw = DjangoGateway&#40;&#123; 'login' : views.login_user, 'logout' : views.logout_user, &#125;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://69.164.207.135/wp-content/uploads/2008/09/pyamf-to-django.jpg" alt="" title="pyamf-to-django" width="500" height="121" class="aligncenter size-full wp-image-91" /></p>
<p>Django views serve nicely as service end-points for Flex applications. Here are some notes on maintaining authenticated sessions between a Flex/Air/Flash application and your Django backend.</p>
<p><strong>gateway.py</strong></p>

<div class="wp_codebox"><table><tr id="p884"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p88code4"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> pyamf.<span style="color: black;">remoting</span>.<span style="color: black;">gateway</span>.<span style="color: black;">django</span> <span style="color: #ff7700;font-weight:bold;">import</span> DjangoGateway
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> myproject.<span style="color: black;">myapp</span>.<span style="color: black;">views</span> <span style="color: #ff7700;font-weight:bold;">as</span> views
&nbsp;
gw = DjangoGateway<span style="color: black;">&#40;</span><span style="color: black;">&#123;</span>
    <span style="color: #483d8b;">'login'</span>                       : views.<span style="color: black;">login_user</span>,
    <span style="color: #483d8b;">'logout'</span>                     : views.<span style="color: black;">logout_user</span>,
<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p><strong>views.py</strong></p>

<div class="wp_codebox"><table><tr id="p885"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code" id="p88code5"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> pyamf
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span> <span style="color: #ff7700;font-weight:bold;">import</span> auth
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span>.<span style="color: black;">auth</span> <span style="color: #ff7700;font-weight:bold;">import</span> authenticate, login, logout
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span>.<span style="color: black;">auth</span>.<span style="color: black;">decorators</span> <span style="color: #ff7700;font-weight:bold;">import</span> login_required
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">contrib</span>.<span style="color: black;">auth</span>.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> User
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
    pyamf.<span style="color: black;">register_class</span><span style="color: black;">&#40;</span> User,  <span style="color: #483d8b;">'django.contrib.auth.models.User'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">ValueError</span>:
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Classes already registered&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> logout_user<span style="color: black;">&#40;</span>http_request<span style="color: black;">&#41;</span>:
    logout<span style="color: black;">&#40;</span>http_request<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> login_user<span style="color: black;">&#40;</span>http_request, username, password<span style="color: black;">&#41;</span>:
    <span style="color: #dc143c;">user</span> = authenticate<span style="color: black;">&#40;</span>username=username, password=password<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">user</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>:
        login<span style="color: black;">&#40;</span>http_request, <span style="color: #dc143c;">user</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #dc143c;">user</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
&nbsp;
@login_required
<span style="color: #ff7700;font-weight:bold;">def</span> registered_user_protected_function<span style="color: black;">&#40;</span>http_request<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;You are a registered user.&quot;</span>
&nbsp;
@login_required
<span style="color: #ff7700;font-weight:bold;">def</span> staff_protected_function<span style="color: black;">&#40;</span>http_request<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">if</span> http_request.<span style="color: #dc143c;">user</span>.<span style="color: black;">is_staff</span> <span style="color: #66cc66;">!</span>= <span style="color: #008000;">True</span>: <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">None</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;You are staff.&quot;</span></pre></td></tr></table></div>

<p><strong>from flex</strong></p>

<div class="wp_codebox"><table><tr id="p886"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p88code6"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">netConnection</span>:<span style="color: #0066CC;">NetConnection</span> = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">NetConnection</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">netConnection</span>.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://mysite.com/gateway&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">var</span> responder:Responder = <span style="color: #000000; font-weight: bold;">new</span> Responder<span style="color: #66cc66;">&#40;</span>loginResult, handleFault<span style="color: #66cc66;">&#41;</span>;
<span style="color: #0066CC;">netConnection</span>.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;login&quot;</span>, responder, <span style="color: #ff0000;">&quot;username&quot;</span>, <span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>The http_request carries a reference to the currently authenticated user throughout the session. This works for web based Flex application as well as AIR applications on the desktop. Note that I am using a try/except on the pyamf class registration calls. Because this is session based, the classes only need to be registered once. Without the trap, it throws a TypeError letting you know the registration has already taken place.</p>
<p><a href="http://docs.djangoproject.com/en/dev/topics/auth/">Django User Authentication Documentation</a><br />
All of the various things you can do with authentication in Django. It is, of course, based mostly on the use of the very nice Django HTML template system. While those bits aren't handy to the likes of us, it is a good read either way.</p>
<p><a href="http://pyamf.org/wiki/ByteArrayExample">pyAMF ByteArray example</a><br />
This example shows the basic structure for setting up Django/Flex communication. It doesn't cover authentication, but covers a good bit of territory with examples in Flash and Flex.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2008/09/21/django-authorization-from-flex-air-actionscript-via-pyamf/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Dynamic upload paths in Django</title>
		<link>http://joelhooks.com/2008/09/12/dynamic-upload-paths-in-django/</link>
		<comments>http://joelhooks.com/2008/09/12/dynamic-upload-paths-in-django/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 07:25:33 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=82</guid>
		<description><![CDATA[I'm refactoring my VE:Session application a bit to the recently released Django 1.0. They have made some outstanding progress with the project overall, but there are enough things that are radically different to cause me to lose sleep searching for answers to small problems. I'd simply overlooked their NewForms branch when I was building the [...]]]></description>
			<content:encoded><![CDATA[<p>I'm refactoring my <a href="http://vesession.com">VE:Session</a> application a bit to the recently released <a href="http://www.djangoproject.com/">Django 1.0</a>. They have made some outstanding progress with the project overall, but there are enough things that are radically different to cause me to lose sleep searching for answers to small problems. I'd simply overlooked their NewForms branch when I was building the application, and now I am paying for it by standing well behind the curve. It is fun to learn new things though, so that is the upside I suppose.</p>
<p>Anyway, back to the refactoring. In addition to updating the project to the current Django trunk, I want to unify the application under Django. I am currently using SlideshowPro for my image CMS functionality. It is a fine product, but it obsfusicates the application and certainly makes it harder to deploy. This is compounded by the fact that I am using an older version of SSP. Django has some out of the box options for filing your uploads in a data structure. I really need something more customizable for the application though, and I was feverishly searching for a solution. The previous examples were <a href="http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/">complex hacks</a> to get the job done. Which is fine, but they are now broken with the update to 1.0. D'oh. As luck would have it they are not neccesary any longer, and the same functionality can be added with a few simple lines that <a href="http://scottbarnham.com/blog/2008/08/25/dynamic-upload-paths-in-django/">Scott Barnham was kind enough to demonstrate</a>:</p>

<div class="wp_codebox"><table><tr id="p828"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p82code8"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> models
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> get_image_path<span style="color: black;">&#40;</span>instance, filename<span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">'photos/%s/%s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>instance.<span style="color: #008000;">id</span>, filename<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Photo<span style="color: black;">&#40;</span>models.<span style="color: black;">Model</span><span style="color: black;">&#41;</span>:
    image = models.<span style="color: black;">ImageField</span><span style="color: black;">&#40;</span>upload_to=get_image_path<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Awesome.</p>
<p>I've been knee deep in Java for the past few months, so it is <strong>really</strong> nice to get back into Python for a bit. With Django reaching a comfortable level of maturity, I see many more sleepless nights in my future.</p>
]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2008/09/12/dynamic-upload-paths-in-django/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Django Authentication from Flex</title>
		<link>http://joelhooks.com/2008/01/05/django-authentication-from-flex/</link>
		<comments>http://joelhooks.com/2008/01/05/django-authentication-from-flex/#comments</comments>
		<pubDate>Sun, 06 Jan 2008 03:34:43 +0000</pubDate>
		<dc:creator>Joel</dc:creator>
				<category><![CDATA[actionscript]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://joelhooks.com/?p=6</guid>
		<description><![CDATA[This method isn't very good, and I would recommend using this instead. This will still work, but I believe it is better to maintain a proper browser session and not do all this monkey work for authentication. Django 'salts' passwords for added protection. They use SHA1 with a random key applied to further obfuscate the [...]]]></description>
			<content:encoded><![CDATA[<div class="note-block">This method isn't very good, and I would recommend <a href="http://joelhooks.com/2008/09/21/django-authorization-from-flex-air-actionscript-via-pyamf/">using this instead</a>. This will still work, but I believe it is better to maintain a proper browser session and not do all this monkey work for authentication.</div>
<p>Django 'salts' passwords for added protection. They use SHA1 with a random key applied to further obfuscate the resulting hash. This is a good thing, but it threw me off for a bit. I don't want to send the unencrypted passwords over the wire, so I needed to grab the hash string from Django, break it apart, and reassemble it inside of flex. This did the trick:</p>

<div class="wp_codebox"><table><tr id="p610"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p6code10"><pre class="actionscript" style="font-family:monospace;"><span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> onGetUsersComplete<span style="color: #66cc66;">&#40;</span> re:ResultEvent <span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> users:<span style="color: #0066CC;">Array</span> = re.<span style="color: #006600;">result</span> as <span style="color: #0066CC;">Array</span>;
	<span style="color: #000000; font-weight: bold;">var</span> userArray:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> ii:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; ii <span style="color: #66cc66;">&lt;</span> users.<span style="color: #0066CC;">length</span>; ii++ <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">var</span> user:UserVO = users<span style="color: #66cc66;">&#91;</span>ii<span style="color: #66cc66;">&#93;</span> as UserVO;
		userArray.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span> user <span style="color: #66cc66;">&#41;</span>;
		<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> users<span style="color: #66cc66;">&#91;</span>ii<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">first_name</span> <span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">var</span> isValidUser:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
	loginService.<span style="color: #006600;">removeEventListener</span><span style="color: #66cc66;">&#40;</span>ResultEvent.<span style="color: #006600;">RESULT</span>, onGetUsersComplete<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> users.<span style="color: #0066CC;">length</span>; i++ <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> users<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">username</span> == <span style="color: #0066CC;">this</span>.<span style="color: #006600;">username</span> <span style="color: #66cc66;">&#41;</span>
		<span style="color: #66cc66;">&#123;</span>
			currentUser = users<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> as UserVO;
			<span style="color: #000000; font-weight: bold;">var</span> salt:<span style="color: #0066CC;">String</span> = users<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">password</span>.<span style="color: #0066CC;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'$'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>;
			<span style="color: #808080; font-style: italic;">//django specific password scheme</span>
			<span style="color: #0066CC;">password</span> = <span style="color: #ff0000;">'sha1$'</span> + salt + <span style="color: #ff0000;">'$'</span> + SHA1.<span style="color: #006600;">hash</span><span style="color: #66cc66;">&#40;</span> salt + <span style="color: #0066CC;">password</span> <span style="color: #66cc66;">&#41;</span>;
			loginService.<span style="color: #006600;">verify_credentials</span><span style="color: #66cc66;">&#40;</span> username, <span style="color: #0066CC;">password</span> <span style="color: #66cc66;">&#41;</span>;
			loginService.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ResultEvent.<span style="color: #006600;">RESULT</span>, onVerifyCredentialsResult, <span style="color: #000000; font-weight: bold;">false</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#41;</span>;
			isValidUser = <span style="color: #000000; font-weight: bold;">true</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #66cc66;">!</span>isValidUser <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#123;</span>
		sendNotification<span style="color: #66cc66;">&#40;</span> ApplicationFacade.<span style="color: #006600;">LOGIN_FAILED</span>, <span style="color: #ff0000;">&quot;Invalid Username&quot;</span> <span style="color: #66cc66;">&#41;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://joelhooks.com/2008/01/05/django-authentication-from-flex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
	</channel>
</rss>
