GAEO de お小遣い管理(5)

第1回 プロジェクト作成
第2回 共通部品とログイン画面
第3回 ユーザ登録画面
第4回 お小遣い情報管理
と来まして、第5回は科目管理画面「Items」を作ります。

科目情報管理「Items」index.html

お小遣いの科目一覧・登録画面を作成します。
場所は「/application/templates/items/」の直下です。
入力欄と、登録分の一覧を表示します。

{% extends "../base.html" %}
{% block title %}おこづかい管理-項目管理{% endblock %}
{% block content %}
<p>
  <Table border="1" cellpadding="2" cellspacing="0">
  <thead>
    <tr>
      <th>並び順</th>
      <th>項目名</th>
      <th> </th>
    </tr>
  </thead>
  <tbody>
  {% for it in items %}
  <form action="/items/update" method="post">
    <tr>
      <td align="center">
        <input type="text" size="1" name="orderNo" value="{{ it.orderNo }}">
        <input type="hidden" name="itemID" value="{{ it.itemID }}">
      </td>
      <td>
        <input type="text" name="itemNm" value="{{ it.itemNm }}">
      </td>
      <td>
        <input type="submit" value="登録">
      </td>
    </tr>
  </form>
  {% endfor %}
  </tbody>
  <!-- 新規行 -->
  <tfoot>
  <form action="/items/update" method="post">
    <tr>
      <td align="center">
        <input type="text" size="1" name="orderNo">
        <input type="hidden" name="itemID" value="0">
      </td>
      <td>
        <input type="text" name="itemNm">
      </td>
      <td>
        <input type="submit" value="追加">
      </td>
    </tr>
  </form>
  </tfoot>
  </Table>
</p>
<p>
  <a href="/smmain">戻る</a>
</p>
{% endblock %}

科目情報管理「Items」items.py

続いて、科目情報の処理部分「items.py」
場所は「/application/controller」の直下。

# -*- coding: utf-8 -*-
from google.appengine.ext import db
from gaeo.controller import BaseController
from model.items import Items
from const import cstItemCol, cstUserCol

class ItemsController(BaseController):
    def index(self):
        # セッションチェック
        if self.session.has_key(cstUserCol.USER_ID) is False:
            self.redirect("/logout")
        if len(str(self.session[cstUserCol.USER_ID])) == 0:
            self.redirect("/logout")
            
        # 項目一覧取得
        self.items = db.GqlQuery("SELECT * FROM Items " \
                                 " WHERE userID = :1" \
                                 " ORDER BY orderNo",
                                 self.session[cstUserCol.USER_ID])

    def update(self):
        # セッションチェック
        if self.session.has_key(cstUserCol.USER_ID) is False:
            self.redirect("/logout")
        if len(str(self.session[cstUserCol.USER_ID])) == 0:
            self.redirect("/logout")
            
        # 値取得
        tmpUserID = self.session[cstUserCol.USER_ID]
        tmpItemID = self.params.get('itemID', None)
        tmpItemNm = self.params.get('itemNm', None)
        tmpOrderNo = self.params.get('orderNo', None)

        if tmpItemID == 0 or tmpItemID == '0' :
            # itemCD が0なら新規登録
            allItem = Items.all()
            newItemID = allItem.count() + 1
            Items(itemID = str(newItemID)
                , itemNm = tmpItemNm
                , orderNo = int(tmpOrderNo)
                , userID = int(tmpUserID)).put()

        else :
            # その他の場合、変更/削除
            # データ取得
            datItem = Items.all()
            datItem.filter(cstItemCol.ITEM_ID + " = ", str(tmpItemID))
            datItem.filter(cstItemCol.USER_ID + " = ", int(tmpUserID))

            if datItem.count() != 0 :
                if tmpOrderNo == 0 or tmpOrderNo == '0' :
                    # 並び順が0なら削除
                    datItem.get().delete()
                else :
                    # それ以外は変更
                    datItem.get().delete()
                    Items(itemID = str(tmpItemID)
                        , itemNm = tmpItemNm
                        , orderNo = int(tmpOrderNo)
                        , userID = int(tmpUserID)).put()
                
        self.redirect('/items')

ソース内のコメントにもありますが、並び順を0にすると削除できる構造にしてます。
ただ、入力チェックを行ってないので、今後追加する必要があります・・・。


次回は最後のログアウト処理を作って、総括します。