Jus trying to figure out passing the auth parameters (username, password) from flex client to django backend using pyamf, it turn around to be simple to just pass parameters instead of a class object, Here is the code.
1. In the gateway.py add the following
# Try to use Django User model and methods
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
pyamf.register_class(User, 'com.example.model.UserVO')
def userLogin(request, username=None, password=None):
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return user
return None
def userLogout(request):
logout(request)
return True
gw = DjangoGateway({
"UserService.userLogin": userLogin,
"UserService.userLogout": userLogout,
})
2. In the mxml, here it goes.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import com.atlife.model.UserVO;
import mx.utils.ArrayUtil;
import mx.controls.Alert;
[Bindable] private var loginData:UserVO
private var connection:NetConnection;
private var gateway:String = "http://localhost:8000/gateway/";
private function init():void
{
connection = new NetConnection();
connection.connect(gateway);
}
private function loginUser():void
{
connection.call("UserService.userLogin",
new Responder(onResult, onFault), txt_username.text, txt_password.text);
}
private function onResult(data:Object):void
{
if (data != null)
{
loginData = data as UserVO;
welcomeUser.text = "Hi " + loginData.username;
logout.visible = true;
} else {
welcomeUser.text = "Entered wrong username or password";
logout.visible = false;
}
clearUser();
}
private function onLogout():void
{
if (loginData != null)
{
connection.call("UserService.userLogout",
new Responder(onResult_logout, onFault));
}
}
private function onResult_logout(data:Object):void
{
if (data == true)
{
welcomeUser.text = loginData.username + " logout successfully";
logout.visible = false;
loginData = new UserVO();
clearUser();
}
}
private function onFault(data:Object):void
{
trace(data);
}
private function clearUser():void
{
txt_username.text = "";
txt_password.text = "";
}
]]>
</mx:Script>
<mx:VBox width="100%" height="100%">
<mx:Text id="welcomeUser" textAlign="center" width="100%" fontSize="18" color="red" />
<mx:VDividedBox width="100%" height="100%">
<mx:Button id="logout" visible="false" label="Logout" click="onLogout()"/>
<mx:Panel title="User Login" width="100%" height="100%" layout="horizontal" cornerRadius="10">
<mx:Form width="100%" height="100%" cornerRadius="10">
<mx:FormHeading label="Enter Details" />
<mx:FormItem label="User Name: " width="100%" required="true">
<mx:TextInput id="txt_username" text="" width="100%" />
</mx:FormItem>
<mx:FormItem label="Password: " width="100%" required="true">
<mx:TextInput id="txt_password" width="100%" displayAsPassword="true"/>
</mx:FormItem>
<mx:FormItem label="" direction="horizontal" width="100%">
<mx:Button label="Clear" click="clearUser()" />
<mx:Button label="Login" click="loginUser()" />
</mx:FormItem>
</mx:Form>
</mx:Panel>
</mx:VDividedBox>
</mx:VBox>
</mx:WindowedApplication>
3. The UserVO model object can be used to in the validate user objects in the later stage.
package com.example.model
{
[RemoteClass(alias="com.example.model.UserVO")]
[Bindable]
public class UserVO
{
//Most of the variable won't be required, possibly can use it for future,
//make sure to use correct data types
public var id:int;
public var username:String;
public var password:String;
public var email:String;
public var first_name:String;
public var last_name:String;
public var date_joined:String;
public var groups:String;
public var is_active:Boolean;
public var is_staff:Boolean;
public var is_superuser:Boolean;
public var last_login:String;
public var user_permissions:String;
public var backend:String;
}
}