Commits

Kang-min Wang  committed 10ba72d

add auto renew the Joke

  • Participants
  • Parent commits cb41d14

Comments (0)

Files changed (3)

 from google.appengine.ext.db import BadValueError
 import model
 import os
+from django.utils import simplejson
+
 
 class MainPage(webapp.RequestHandler):
 
         except BadValueError:
             self.response.out.write('<center><p align="center">You should input some data! chu~mi~  ^.< </p><p align="center"><a href="/">&laquo; 返回</a></p>')
 
+class GetData(webapp.RequestHandler):
+    """
+    Get JW
+    """
+    def get(self):
+        data = model.Joke.all().fetch(30);
+
+        if len(data) > 0:
+            dd = []
+            for d in data:
+                tmp = {}
+                tmp['jWhen'] = d.jWhen
+                tmp['jWhere'] = d.jWhere
+                tmp['jWho'] = d.jWho
+                tmp['jWhat'] = d.jWhat
+
+                dd.append(tmp)
+
+            self.response.out.write(simplejson.dumps(dd))
+
 
 def main():
     application = webapp.WSGIApplication([
                         ('/', MainPage),
-                        ('/save', SaveData)
+                        ('/save', SaveData),
+                        ('/get', GetData)
                         ],
                         debug=True)
     util.run_wsgi_app(application)

File templates/layout.html

 	<title>COSCUP 2010 / GNOME.Asia 梗全集 {% block title %}{% endblock %}</title>
 	<meta name="author" content="mrmoneyc">
 	<link rel="stylesheet" type="text/css" href="/s/css/layout.css">
+    {% block head %}{% endblock %}
 </head>
 <body>
 	<div class="cc">

File templates/main.html

 
 {% block nav %} - Index{% endblock %}
 
+{% block head %}
+<!-- include jQuery -->
+<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
+<script language="JavaScript">
+<!--
+var secs;
+var timeout;
+var timerID = null;
+var timerRunning = false;
+var delay = 1000;
+
+function InitializeTimer()
+{
+    // Set the length of the timer, in seconds
+    timeout = 2; 
+    secs = timeout;
+    StopTheClock();
+    StartTheTimer();
+}
+
+function StopTheClock()
+{
+    if(timerRunning)
+        clearTimeout(timerID)
+    timerRunning = false
+}
+
+function StartTheTimer()
+{
+    if (secs==0)
+    {
+        secs = timeout;
+        //StopTheClock()
+        // Here's where you put something useful that's
+        // supposed to happen after the allotted time.
+        // For example, you could display a message:
+        //alert("You have just wasted 10 seconds of your life.")
+        GetJoke();
+        timerID = self.setTimeout("StartTheTimer()", delay)
+    }
+    else
+    {
+        self.status = secs
+        secs = secs - 1
+        timerRunning = true
+        timerID = self.setTimeout("StartTheTimer()", delay)
+    }
+}
+
+function SentJoke(){
+    /* Sent JW */
+}
+function GetJoke(){
+    //Get JW
+    var url = '/get';
+    $.getJSON(url,'',function(d){
+            $('#MyJoke').empty();
+            var html="";
+            $.each(d,function(i,item){
+                html+="\""+item['jWhat']+"\" by "+item['jWho'] + " at " + item['jWhere']+"<br />";
+            });
+            $('#MyJoke').append(html);
+    });
+}
+
+$(function(){
+        GetJoke();
+        InitializeTimer();
+});
+//-->
+</script>
+
+
+{% endblock %}
+
 {% block menu %}
 {% endblock %}
 
                 <p>梗: <textarea name="txtWhat" id="txtWhat" rows="2" cols="70"></textarea></p>
 			</td>
 		</tr>
+        {% comment %}
 		<tr>
 			<td align="center" valign="middle" colspan=2>
 			{% for show in jResult %}
 				</p>
 			</td>
 		</tr>
+        {% endcomment %}
+        <tr>
+            <td>
+                <br />
+                <div id='MyJoke' align='center'>來點新梗吧!!</div>
+            </td>
+        </tr>
 	</table>
 </form>
 {% endblock %}