Commits

Anonymous committed f908037

add: tutorial-beginner

Comments (0)

Files changed (5)

+indexes:
+
+# AUTOGENERATED
+
+# This index.yaml is automatically updated whenever the dev_appserver
+# detects that a new type of query is run.  If you want to manage the
+# index.yaml file manually, remove the above marker line (the line
+# saying "# AUTOGENERATED").  If you want to manage some indexes
+# manually, move them above the marker line.  The index.yaml file is
+# automatically uploaded to the admin console when you next deploy
+# your application using appcfg.py.
+
 # vim: fileencoding=utf8
 
-import os
 import wsgiref.handlers
 
 from google.appengine.ext import webapp
-from google.appengine.api import users
-from google.appengine.ext.webapp import template
 
-from models import *
-
-"""
-TODO
-====
-
-- Paginator
-- js で最新の Status の日付を取得してサーバに送ることで新しい Status を貰う
-"""
-
-class MainHandler(webapp.RequestHandler):
-  def get(self):
-    return self.response.out.write('Hello world!')
-
-class PublicTimelineHandler(webapp.RequestHandler):
-  """
-  {{ account.account_id }}
-  {% for status in public_timeline %}
-  {% if not status.protected %}
-    {{ status.text }}
-    {{ status.creatd }}
-    {{ status.tags }}
-  {% endif %}
-  {% endfor %}
-  """
-  def get(self):
-    public_timeline = Status.all()
-
-    template_values = {
-      'public_timeline': public_timeline,
-    }
-
-    path = os.path.join(os.path.dirname(__file__), 'public_timeline.html')
-    return self.response.out.write(template.render(path, template_values))
-
-class HomeTimelineHandler(webapp.RequestHandler):
-  """
-  {{ account.account_id }}
-  {% for status in home_timeline %}
-    {% if not status.protected %}
-      {{ status.text }}
-      {{ status.creatd }}
-      {{ status.tags }}
-    {% endif %}
-  {% endfor %}
-  """
-  def get(self):
-    user = users.get_current_user()
-    account = Account.all().filter('user =', user).get()
-    if not user or not account:
-      return self.redirect("/")
-    home_timeline = account.timeline.all()
-    template_values = {
-      'home_timeline': home_timeline,
-      'account': account,
-    }
-    path = os.path.join(os.path.dirname(__file__), 'home_timeline.html')
-    return self.response.out.write(template.render(path, template_values))
-
-class DetailStatusHandler(webapp.RequestHandler):
-  def get(self):
-    pass
-
-class UpdateStatusHandler(webapp.RequestHandler):
-  def post(self):
-    user = users.get_current_user()
-    account = Account.all().filter('user =', user).get()
-
-
-
-class UserTimelineHandler(webapp.RequestHandler):
-  """
-  {{ account.account_id }}
-  {% if protected %}
-    {% for status in user_timeline %}
-      {{ status.text }}
-      {{ status.creatd }}
-      {{ status.tags }}
-    {% endfor %}
-  {% endif %}
-  """
-  def get(self, account_id):
-    user = users.get_current_user()
-    visitor_account = Account.all().filter('user =', user).get()
-    account = Account.all().filter('account_id =', account_id).get()
-    user_timeline = Status.all().filter('account =', account)
-    if not user_timeline:
-      self.error(404)
-      return self.response.out.write('404')
-
-    protected = True or account.check_protected(visitor_account)
-    template_values = {
-      'user_timeline': user_timeline,
-      'account': account,
-      'protected': protected,
-    }
-    path = os.path.join(os.path.dirname(__file__), 'user_timeline.html')
-    return self.response.out.write(template.render(path, template_values))
-
-class FollowingHandler(webapp.RequestHandler):
-  def get(self):
-    user = users.get_current_user()
-    account = Account.all().filter('user =', user).get()
-    if not user or not account:
-      return self.redirect("/")
-    template_values = {
-      'followings': account.followings,
-      'account': account,
-    }
-    path = os.path.join(os.path.dirname(__file__), 'followings.html')
-    return self.response.out.write(template.render(path, template_values))
-
-  def post(self):
-    pass
-
-class FollowersHandler(webapp.RequestHandler):
-  def get(self):
-    user = users.get_current_user()
-    account = Account.all().filter('user =', user).get()
-    if not user or not account:
-      return self.redirect("/")
-
-    template_values = {
-      'followers': account.followers,
-      'account': account,
-    }
-
-    path = os.path.join(os.path.dirname(__file__), 'followers.html')
-    return self.response.out.write(template.render(path, template_values))
+from views import *
 
 def main():
   application = webapp.WSGIApplication(
-      [(r'/', PublicTimelineHandler),
-       (r'/home/', HomeTimelineHandler),
-       (r'/(<account_id>.*)/', UserTimelineHandler),
-       (r'/(<account_id>.*)/(<status_id>.*)', MainHandler),
-       (r'/(<account_id>.*)/following/', FollowingHandler),
-       (r'/(<account_id>.*)/followers/', FollowersHandler),
-       (r'/update/', MainHandler)],
-       debug=True)
-  wsgiref.handlers.CGIHandler().run(application)
-
+    [
+      (r'/', PublicTimelineHandler),
+      (r'/home/', HomeTimelineHandler),
+      (r'/(?P<account_name>.*)/', UserTimelineHandler),
+      (r'/(?P<account_name>.*)/(?P<status_id>.*)', StatusDetailHandler),
+    ], debug=True)
+    wsgiref.handlers.CGIHandler().run(application)
 
 if __name__ == '__main__':
   main()
 # vim: fileencoding=utf8
 
-import string
-import random
-
 from google.appengine.ext import db
 from google.appengine.api import users
 
-LETTERS_AND_DIGITS = string.letters + string.digits
-
-"""
-"""
-
 class Account(db.Model):
   """
-  >>> users.User()
-  >>> account = Account(key_name='spam')
+  >>> user = users.User(mail='test@example.com')
+  >>> account = Account()
+  >>> account.user = user
+  >>> account.account_name = u'test'
+  >>> db.save(account)
   """
   user = db.UserProperty(required=True)
-  account_id = db.StringProperty(required=True)
-  access_key_id = db.StringProperty(default=self.generate_random_string(20))
-  secret_access_key = db.StringProperty(default=self.generate_random_string(20))
-  timeline = db.ListProperty(db.Key)
-  followings = db.ListProperty(db.Key)
-  image = db.BlobProperty(required=False)
+  account_name = db.StringProperty(required=True)
   created = db.DateTimeProperty(auto_now_add=True)
-  protected = db.BooleanProperty(default=False)
-
-  @property
-  def followers(self):
-    """
-    >>> for i in account.followers: Account.get_by_key(i)
-    """
-    return Account.all().filter('followings =', self.key())
-
-  def generate_random_string(self, length):
-    return ''.join([random.choice(LETTERS_AND_DIGITS) for i in range(length)])
-
-  # TODO Refactoring
-  def chech_protected(self, account):
-    if account is None:
-      return True
-    if not isinstance(account, Account):
-      return True
-    if account.key() in self.followings:
-      return False
-    return True
-
-class Tag(db.Model):
-  name = db.StringProperty()
-  created = db.DateTimeProperty(auto_now_add=True)
-
-  def statuses(self):
-    return Status.all().filter('tags =', self.key())
 
 class Status(db.Model):
   """
-  >>> account
-  >>> status = Status(parent=account)
-  >>> status.account = account
-  >>> status.text = 'spam'
-  >>> status.put()
+  >>> user = users.User(mail='test@example.com')
+  >>> account = Account()
+  >>> account.user = user
+  >>> account.account_name = u'test'
+  >>> db.save(account)
+  >>> status = Status()
+  >>> status.text = u'test message.'
+  >>> db.save(status)
   """
+  status_id = db.IntegerProperty(required=True)
   account = db.ReferenceProperty(Account)
   text = db.StringProperty(required=True)
   created = db.DateTimeProperty(auto_now_add=True)
-  replies = db.LinkProperty(db.Key)
-  tags = db.LinkProperty(db.Key)
 
 import os
 import string
 import random
+import urllib
 import unittest
 
-from google.appengine.api import (apiproxy_stub_map,
-                                  datastore_file_stub,
-                                  mail_stub,
-                                  urlfetch_stub,
-                                  user_service_stub)
+from google.appengine.api import (apiproxy_stub_map, datastore_file_stub,
+                                  mail_stub, urlfetch_stub, user_service_stub)
 from google.appengine.ext import db, search
 from google.appengine.api import users
 
 
 APP_ID = u'monologista'
 AUTH_DOMAIN = u'gmail.com'
-#USER_EMAIL = u'test@example.com'
 
 class TestDatastore(unittest.TestCase):
   """
   """
-  def create_account(self, account_id):
-    user = users.User(email=(account_id + '@example.com'))
-    account = Account(key_name=account_id, user=user, account_id=account_id)
+  def create_account(self, account_name):
+    user = users.User(email=(account_name + '@example.com'))
+    account = Account(key_name=account_name, user=user, account_name=account_name)
     account.put()
     return account
 
     apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub)
     apiproxy_stub_map.apiproxy.RegisterStub('user', user_service_stub.UserServiceStub())
 
-    self.alice = self.create_account(account_id='alice')
-    self.bob = self.create_account(account_id='bob')
-    self.carol = self.create_account(account_id='carol')
-    self.dave = self.create_account(account_id='dave')
+    #self.alice = self.create_account(account_id='alice')
+    #self.bob = self.create_account(account_id='bob')
+    #self.carol = self.create_account(account_id='carol')
+    #self.dave = self.create_account(account_id='dave')
 
-  def update_clip(self, clip):
-    for follower in clip.account.followers:
-      follower.timeline.append(clip.key())
-      follower.put()
-    else:
-      account = clip.account
-      account.timeline.append(clip.key())
-      account.put()
-
-  def test_clip_append_or_remove(self):
-    alice = self.alice
-    bob = self.bob
-    carol = self.carol
-
-    bob.followings.append(alice.key())
-    bob.put()
-
-    carol.followings.append(alice.key())
-    carol.put()
-
-    self.failUnlessEqual(len(bob.followings), 1)
-    self.failUnlessEqual(len(carol.followings), 1)
-
-    self.failUnlessEqual(alice.followers.count(), 2)
-
-    clip = Clip(account=alice, url='http://www.google.com', title='google')
-    clip.put()
-    self.update_clip(clip)
-
-    self.failUnlessEqual(len(alice.timeline), 1)
-    bob = Account.get_by_key_name('bob')
-    self.failUnlessEqual(len(bob.timeline), 1)
-    carol = Account.get_by_key_name('carol')
-    self.failUnlessEqual(len(carol.timeline), 1)
-
-    self.failUnlessEqual(len(alice.followings), 0)
-    self.failUnlessEqual(len(bob.followings), 1)
-    self.failUnlessEqual(len(carol.followings), 1)
-
-    self.failUnlessEqual(alice.followers.count(), 2)
-    self.failUnlessEqual(bob.followers.count(), 0)
-    self.failUnlessEqual(carol.followers.count(), 0)
-
-  def update_status(self, status):
-    for follower in status.account.followers:
-      follower.timeline.append(status.key())
-      follower.put()
-    else:
-      account = status.account
-      account.timeline.append(status.key())
-      account.put()
-
-  def test_status_append_or_remove(self):
-    alice = self.alice
-    bob = self.bob
-    carol = self.carol
-
-    bob.followings.append(alice.key())
-    bob.put()
-
-    carol.followings.append(alice.key())
-    carol.put()
-
-    #alice = Account.get_by_key_name('alice')
-    #bob = Account.get_by_key_name('bob')
-    #carol = Account.get_by_key_name('carol')
-
-    self.failUnlessEqual(len(alice.followings), 0)
-    self.failUnlessEqual(len(bob.followings), 1)
-    self.failUnlessEqual(len(carol.followings), 1)
-
-    self.failUnlessEqual(alice.followers.count(), 2)
-    self.failUnlessEqual(bob.followers.count(), 0)
-    self.failUnlessEqual(carol.followers.count(), 0)
-
-    status = Status(account=alice, status='hello world')
-    status.put()
-    self.update_status(status)
-
-    alice = Account.get_by_key_name('alice')
-    self.failUnlessEqual(len(alice.timeline), 1)
-    bob = Account.get_by_key_name('bob')
-    self.failUnlessEqual(len(bob.timeline), 1)
-    carol = Account.get_by_key_name('carol')
-    self.failUnlessEqual(len(carol.timeline), 1)
-
-    alice = Account.get_by_key_name('alice')
-    bob = Account.get_by_key_name('bob')
-    carol = Account.get_by_key_name('carol')
-
-    self.failUnlessEqual(len(alice.followings), 0)
-    self.failUnlessEqual(len(bob.followings), 1)
-    self.failUnlessEqual(len(carol.followings), 1)
-
-    self.failUnlessEqual(alice.followers.count(), 2)
-    self.failUnlessEqual(bob.followers.count(), 0)
-    self.failUnlessEqual(carol.followers.count(), 0)
-
-  def test_following_append_or_remove(self):
-    """
-    """
-    alice = self.alice
-    bob = self.bob
-    carol = self.carol
-
-    alice.followings.append(bob.key())
-    alice.put()
-    self.failUnlessEqual(len(alice.followings), 1)
-    alice.followings.append(carol.key())
-    alice.put()
-    self.failUnlessEqual(len(alice.followings), 2)
-
-    alice.followings.remove(bob.key())
-    alice.put()
-    self.failUnlessEqual(len(alice.followings), 1)
-    alice.followings.remove(carol.key())
-    alice.put()
-    self.failUnlessEqual(len(alice.followings), 0)
-
-    alice = Account.get_by_key_name('alice')
-    bob = Account.get_by_key_name('bob')
-    carol = Account.get_by_key_name('carol')
-
-    self.failUnlessEqual(len(alice.followings), 0)
-    self.failUnlessEqual(len(bob.followings), 0)
-    self.failUnlessEqual(len(carol.followings), 0)
-
-    self.failUnlessEqual(alice.followers.count(), 0)
-    self.failUnlessEqual(bob.followers.count(), 0)
-    self.failUnlessEqual(carol.followers.count(), 0)
-
-  def test_followers_append_or_remove(self):
-    alice = self.alice
-    bob = self.bob
-    carol = self.carol
-
-    bob.followings.append(alice.key())
-    bob.put()
-    carol.followings.append(alice.key())
-    carol.put()
-
-    self.failUnlessEqual(len(bob.followings), 1)
-    self.failUnlessEqual(len(carol.followings), 1)
-
-    alice = Account.get_by_key_name('alice')
-
-    self.failUnlessEqual(len(alice.followings), 0)
-    self.failUnlessEqual(alice.followers.count(), 2)
-
-    bob.followings.remove(alice.key())
-    bob.put()
-    carol.followings.remove(alice.key())
-    carol.put()
-
-    alice = Account.get_by_key_name('alice')
-    bob = Account.get_by_key_name('bob')
-    carol = Account.get_by_key_name('carol')
-
-    self.failUnlessEqual(len(alice.followings), 0)
-    self.failUnlessEqual(len(bob.followings), 0)
-    self.failUnlessEqual(len(carol.followings), 0)
-
-    self.failUnlessEqual(alice.followers.count(), 0)
-    self.failUnlessEqual(bob.followers.count(), 0)
-    self.failUnlessEqual(carol.followers.count(), 0)
-
-  def test_timeline(self):
-    alice = self.alice
-    bob = self.bob
-    carol = self.carol
-
-    bob.followings.append(alice.key())
-    bob.put()
-
-    carol.followings.append(alice.key())
-    carol.put()
-
-    alice = Account.get_by_key_name('alice')
-    bob = Account.get_by_key_name('bob')
-    carol = Account.get_by_key_name('carol')
-
-    self.failUnlessEqual(len(alice.followings), 0)
-    self.failUnlessEqual(len(bob.followings), 1)
-    self.failUnlessEqual(len(carol.followings), 1)
-
-    self.failUnlessEqual(alice.followers.count(), 2)
-    self.failUnlessEqual(bob.followers.count(), 0)
-    self.failUnlessEqual(carol.followers.count(), 0)
-
+  def tearDown(self):
+    pass
 
 if __name__ == '__main__':
-    unittest.main()
-
+  unittest.main()
+# vim: fileencoding=utf8
+
+import os
+
+from google.appengine.ext import webapp
+from google.appengine.api import users
+from google.appengine.ext.webapp import template
+
+from models import *
+
+class PublicTimelineHandler(webapp.RequestHandler):
+  """
+  {{ account.account_name }}
+  {% for status in public_timeline %}
+    {{ status.text }}
+    {{ status.creatd }}
+  {% endfor %}
+  """
+  def get(self):
+    user = users.get_current_user()
+    account = Account.all().filter('user =', user).get()
+    public_timeline = Status.all()
+    dictionary = {
+      'public_timeline': public_timeline,
+      'account': account,
+    }
+    path = os.path.join(os.path.dirname(__file__), './templates/public_timeline.html')
+    return self.response.out.write(template.render(path, dictionary))
+
+class HomeTimelineHandler(webapp.RequestHandler):
+  """
+  {{ account.account_name }}
+  {% for status in home_timeline %}
+    {{ status.text }}
+    {{ status.creatd }}
+  {% endfor %}
+  """
+  def get(self):
+    user = users.get_current_user()
+    account = Account.all().filter('user =', user).get()
+    if not user or not account:
+      return self.redirect("/")
+    home_timeline = account.timeline
+    dictionary = {
+      'home_timeline': home_timeline,
+      'account': account,
+    }
+    path = os.path.join(os.path.dirname(__file__), './templates/home_timeline.html')
+    return self.response.out.write(template.render(path, dictionary))
+
+  def post(self):
+    user = users.get_current_user()
+    account = Account.all().filter('user =', user).get()
+    if not user or not account:
+      return self.redirect("/")
+
+class StatusDetailHandler(webapp.RequestHandler):
+  def get(self, account_name, status_id):
+    """
+    TODO: what id from status
+    {{ account.account_name }}
+    {{ status.key.id }}
+    """
+    user = users.get_current_user()
+    account = Account.all().filter('user =', user).get()
+    status_account = Account.all().filter('account_name =', account_name).get()
+    if not status_account:
+      self.error(404)
+      return self.response.out.write('404')
+    dictionary = {
+      'account': account,
+      'status': timeline_account,
+    }
+    path = os.path.join(os.path.dirname(__file__), './templates/status_detail.html')
+    return self.response.out.write(template.render(path, dictionary))
+
+class UserTimelineHandler(webapp.RequestHandler):
+  """
+  {{ account.account_name }}
+  {% for status in user_timeline %}
+    {{ status.text }}
+    {{ status.creatd }}
+    {{ status.tags }}
+  {% endfor %}
+  """
+  def get(self, account_name):
+    user = users.get_current_user()
+    account = Account.all().filter('user =', user).get()
+    if not visitor_account:
+      self.error(404)
+      return self.response.out.write('404')
+    timeline_account = Account.all().filter('account_name =', account_name).get()
+    user_timeline = Status.all().filter('account =', account)
+    dictionary = {
+      'account': account,
+      'user_timeline': user_timeline,
+      'timeline_account': timeline_account,
+    }
+    path = os.path.join(os.path.dirname(__file__), './templates/user_timeline.html')
+    return self.response.out.write(template.render(path, dictionary))