Yang Zhang committed cc14da0

message notify system works

Comments (0)

Files changed (6)

 from gevent import monkey
 from uuid import uuid4
-from flask import Flask,request,render_template,session,g,redirect,url_for,Response,make_response,send_file,abort
+from flask import Flask,request,render_template,template_rendered,session,g,redirect,url_for,Response,make_response,send_file,abort
 import sqlite3
 from mysignal import defer_process
 #from serverpush import sphapp,message_collection, Message
 from news import User, Post, Comment,Activity
 from functools import wraps
 from server_session import SqliteSessionInterface
-from configure import configure
+from configure import configure,Configure,Message
 from pybook import pybookapp,cache
 import datetime
 from usercontroller import userapp
 def before_request():
     """Make sure we are connected to the database each request."""
-    if "sessionid" not in session:
-        session['sessionid'] = str(uuid4())
+    if "message" not in session:
+        session['message'] = Message("Welcome to PY4U")
     g.db = connect_db()
     # enable foreign key support
     g.db.cursor().execute("PRAGMA foreign_keys = ON")
+def when_template_rendered(sender, template, context, **extra):
+    if(session['message'].content != ""):
+        session['message'] = Message("")
 def inject_configure():
     return dict(configure.__class__.__dict__)
         user = User(email,nickname,password)
             session['user'] = user
-            #message_collection.append(Message('Login Success'))
+            session['message'] = Message('Login Success')
             return redirect(request.form['to_url'])
-            #message_collection.append(Message('The email or password is wrong!', 'alert-error'))
+            session['message'] = Message('The email or password is wrong!', 'alert-error')
             return redirect('/login')
         #return render_template('login.html', message="The email or password is wrong!", to_url = request.form['to_url'])
         session['user'] = user
         return redirect(url_for('index'))
 def logout():
     if('user' in session):
-        message_collection.append(Message('Logout Success'))
-    return render_template('index.html', message='logout success!')
+        session['message'] = Message('Logout Success')
+    return render_template('index.html')
 def page_not_found(e):
     return render_template('404.html'), 404
+class Message(object):
+    def __init__(self, content, type = "alert-success"):
+        # ALERT_CLASS= {'BLOCK':'alert-block','ERROR':'alert-error', 'SUCESS':'alert-success','INFO':'alert-info'}
+        self.content = content
+        self.type = type
 class Configure(object):
     DOMAIN = ""
     TITLE = "PY4U" #<title>{{title}}</title>
     ActivityNUMBER_PER_PAGE = 5
     POST_SAVE = 7 #days
+    #NOTIFY_MESSAGE = Message("")
+    #{'BLOCK':'alert-block','ERROR':'alert-error', 'SUCESS':'alert-success','INFO':'alert-info'}
 configure = Configure()
 from blinker import signal
 from functools import wraps
 from news import Post,Comment
-from configure import Configure
-from serverpush import message_collection,Message
+from configure import Configure,Message
+from serverpush import message_collection
 import gevent
 from gevent import monkey
     book = sender
     book.comments = Comment.get_comments_by_postid(book.postid)
-def require_user_login(*sender):
-    #message_collection.append(Message("You need to login first",'alert-error'))
-    pass
 def init_cache(sender):
     from application import app


 from serverpush import sphapp,message_collection
-from model import Message


     <link href="/static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
     <script src="/static/jquery-1.8.2.min.js"></script>
     <script src="/static/bootstrap/js/bootstrap.min.js"></script>
+    <script>{% block script %}{% endblock %}</script>
+    {#
             if ("WebSocket" in window) {
+    #}
     {%include 'static_header.html'%}
     {%include 'static_navigation.html'%}
     <div class="container" id="content" style="min-height:600px;">
         <div style="height:38px;">
-        <div class="alert" style='display:none'>
+        {%if session['message'].content %}
+        <div class="alert {{session['message'].type}}">
           <button type="button" class="close" data-dismiss="alert">×</button>
-          <label id="alertmassage"></label>
+          <label id="alertmassage">{{session['message'].content}}</label>
+        {%endif%}
         {% block extra_info %}{% endblock %}
     {%include 'static_footer.html'%}
-  <script>{% block script %}{% endblock %}
-  </script>

 from flask import Blueprint, render_template,session,request,redirect
-from configure import configure
+from configure import configure,Message
 from news import Post, Comment
 from blinker import signal
 import sqlite3
 def require_login():
     if 'user' not in session:
-        signal('require_user_login').send("")
+        temp = Message("You need to login first","alert-error")
+        temp.showed = False
+        session['message'] = temp
         return redirect('/login')