2011年7月1日金曜日

15.Implement get_notes

PostServiceクラスにget_notesメソッドを定義してみましょう。


@remote.method(GetNotesRequest, Notes)
def get_notes(self, request):
    query = guestbook.Greeting.all().order('-date')
    if request.on_or_before:
        when = datetime.datetime.utcfromtimestamp(request.on_or_before)
        query.filter('date <=', when)

    notes = []
    for note_model in query.fetch(request.limit):
    if note_model.date:
        when = int(time.mktime(note_model.date.utctimetuple()))
    else:
        when = None
    note = Note(text=note_model.content, when=when)
    notes.append(note)

    if request.order == GetNotesRequest.Order.TEXT:
        notes.sort(key=lambda note: note.text)

    return Notes(notes=notes)

モジュールの先頭でtimeをインポートするのを忘れずに。
import time

0 件のコメント:

コメントを投稿