今後も使いそうなので作っておく。
環境はflex2とs2flex2とdjango開発版。
簡単な入力チェック機能付き。
flex側はTitleWindowなので別途それを表示するための機能を追加する必要あり。
flex側表示用mxml(UserEntryWindow.mxml)
<?xml version="1.0" encoding="utf-8" ?> <mx:TitleWindow xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:s2="http://www.seasar.org/s2flex2/mxml" title="ユーザ登録" creationComplete="init();" > <s2:RemoteService id="accountService" gatewayUrl="http://localhost:8000/gateway/" destination="accountService" showBusyCursor="true" useAMF0="false" result="event.token.resultHandler(event)" fault="event.token.faultHandler(event)" /> <mx:Script source="UserEntryWindow_script.as" /> <mx:StringValidator id="vfirst_name" source="{ifirst_name}" property="text" required="true" /> <mx:EmailValidator id="vemail" source="{iemail}" property="text" required="true" /> <mx:RegExpValidator id="vusername" source="{iusername}" property="text" expression="[a-zA-Z0-9_\-]" /> <mx:RegExpValidator id="vpassword" source="{ipassword}" property="text" expression="[a-zA-Z0-9_\-]" /> <mx:Form percentWidth="100"> <mx:FormItem label="ニックネーム" percentWidth="100"> <mx:TextInput id="ifirst_name" percentWidth="100" /> </mx:FormItem> <mx:FormItem label="メールアドレス" percentWidth="100"> <mx:TextInput id="iemail" percentWidth="100" /> </mx:FormItem> <mx:FormItem label="ログイン名" percentWidth="100"> <mx:TextInput id="iusername" percentWidth="100" /> </mx:FormItem> <mx:FormItem label="パスワード" percentWidth="100"> <mx:TextInput id="ipassword" percentWidth="100" displayAsPassword="true" /> </mx:FormItem> </mx:Form> <mx:ControlBar percentWidth="100" id="ctrlBar"> <mx:Spacer width="100%" /> <mx:Button label="登録" id="userEntryBtn" click="userEntry(event);" /> <mx:Button label="キャンセル" id="cancelBtn" click="cancelEntry(event);" /> </mx:ControlBar> </mx:TitleWindow>
flex側スクリプト部分(UserEntryWindow_script.as)
// vim:set fileencoding=utf-8: import flash.events.*; import mx.controls.*; import mx.rpc.*; import mx.rpc.events.*; import mx.managers.PopUpManager; import mx.validators.Validator; import org.seasar.flex2.rpc.remoting.*; public static const CODE_SUCCESS:int = 1; public static const CODE_WARN:int = 2; public static const CODE_ERROR:int = -1; private var validators:Array; private function init():void { validators = [vfirst_name, vemail, vusername, vpassword]; } private function userEntry(event:Event):void { var validateResults:Array = Validator.validateAll(validators); if (validateResults.length > 0) { Alert.show("入力にエラーがあります"); return; } var userEntryForm:Object = new Object(); userEntryForm.first_name = ifirst_name.text; userEntryForm.email = iemail.text; userEntryForm.username = iusername.text; userEntryForm.password = ipassword.text; var token:AsyncToken = accountService.create_user(userEntryForm); token.resultHandler = handleUserCreateResult; token.faultHandler = handleUserCreateFault; } private function cancelEntry(event:Event):void { closeWindow(); } private function handleUserCreateResult(event:ResultEvent):void { var result:Object = event.result; if (result.code == CODE_SUCCESS) { Alert.show("ユーザーを作成しました"); closeWindow(); } else { Alert.show("ユーザー作成に失敗しました:\n「" + result.message + "」"); } } private function handleUserCreateFault(event:FaultEvent):void { Alert.show(event.fault.faultDetail); } private function closeWindow():void { PopUpManager.removePopUp(this); }
django側(url.py)
from django.conf.urls.defaults import * from django.conf import settings import amf.django urlpatterns = patterns('', (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}), (r'^gateway/accountService/(.*)', 'amf.django.views', {'views': 'samplepj.main.account_views'}), )
django側(account_views.py)
# vim:set fileencoding=utf-8: from django.db import IntegrityError from samplepj.main.models import * CODE_SUCCESS = 1 CODE_WARN = 2 CODE_ERROR = -1 def create_user(request, user_form): user = User() user.first_name = user_form["first_name"] user.email = user_form["email"] user.username = user_form["username"] user.set_password(user_form["password"]) try: user.save() result = {'code': CODE_SUCCESS} except IntegrityError: result = {'code': CODE_ERROR, 'message': "ログイン名が重複しています"} return result
動いたことは動いた。