Commits

Yang Zhang committed cc14da0

message notify system works

Comments (0)

Files changed (6)

 from gevent import monkey
 from uuid import uuid4
 monkey.patch_all()
-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
 @app.before_request
 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")
         g.db.close()
     gevent.joinall(defer_process)
 
+@template_rendered.connect_via(app)
+def when_template_rendered(sender, template, context, **extra):
+    if(session['message'].content != ""):
+        session['message'] = Message("")
+
+
 @app.context_processor
 def inject_configure():
     return dict(configure.__class__.__dict__)
         user = User(email,nickname,password)
         if(user.is_valid()):
             session['user'] = user
-            #message_collection.append(Message('Login Success'))
+            session['message'] = Message('Login Success')
             return redirect(request.form['to_url'])
         else:
-            #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'])
 
         user.save()
         session['user'] = user
         return redirect(url_for('index'))
+
 @app.route('/logout')
 def logout():
     if('user' in session):
         session.pop('user')
-        message_collection.append(Message('Logout Success'))
-    return render_template('index.html', message='logout success!')
+        session['message'] = Message('Logout Success')
+    return render_template('index.html')
+
 @app.errorhandler(404)
 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 = "py4u.com"
     TITLE = "PY4U" #<title>{{title}}</title>
     POSTNUMBER_PER_PAGE = 15
     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
 monkey.patch_all()
     book = sender
     book.comments = Comment.get_comments_by_postid(book.postid)
 
-@quick_connect('require_user_login')
-def require_user_login(*sender):
-    #message_collection.append(Message("You need to login first",'alert-error'))
-    pass
-
 @quick_connect('init_cache')
 def init_cache(sender):
     from application import app

serverpush/__init__.py

 from serverpush import sphapp,message_collection
-from model import Message

templates/base.html

     <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>
+    {#
     <script>
          $(document).ready(function(){
             if ("WebSocket" in window) {
             } 
         });
     </script>
+    #}
+    
   </head>
   <body>
     {%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>
         </div>
+        {%endif%}
         {% block extra_info %}{% endblock %}
         
         </div>
     </div>
     {%include 'static_footer.html'%}
 
-  <script>{% block script %}{% endblock %}
-  </script>
+
 
 
 

usercontroller.py

 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
 @userapp.before_request
 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')
 
 @userapp.context_processor