Commit 88d74609 authored by HERBERT Ryan's avatar HERBERT Ryan

added notification management for admins

parent 55e02082
# coding: utf8
from datetime import date
import gluon.contrib.simplejson
if request.env.http_origin:
response.headers['Access-Control-Allow-Origin'] = request.env.http_origin
response.headers['Access-Control-Allow-Credentials'] = 'true'
response.headers['Access-Control-Max-Age'] = 86400
ACCESS_DENIED = "access denied"
def index():
if not auth.is_admin() :
res = {"message": ACCESS_DENIED}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
query = db(db.notification).select(orderby=~db.notification.id)
return dict(message="Notifications",
query=query)
# serve for to add a notification
def add():
if (auth.is_admin()):
return dict(message=T('add notification'))
res = {"message": ACCESS_DENIED}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
# validate the form the user has posted
def add_form():
if (not auth.is_admin()):
res = {"message": ACCESS_DENIED}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
error = ""
if request.vars["message_content"] == "" :
error += "message content needed, "
if request.vars["message_type"] == "" :
error += "type needed, "
if request.vars["priority"] == "" :
error += "priority needed, "
if request.vars["expiration"] != "" :
try:
datetime.datetime.strptime(""+request.vars['expiration'], '%Y-%m-%d')
except ValueError:
error += "date (wrong format)"
if error=="" :
id = db.notification.insert(message_content=request.vars["message_content"],
message_type=request.vars["message_type"],
priority=request.vars["priority"],
expiration=request.vars["expiration"],
creator=auth.user_id)
res = {"redirect": "notification/index",
"args" : { "id" : id },
"message": "notification added"}
log.info(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else :
res = {"success" : "false",
"message" : error}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
# edit existing notification
def edit():
if (auth.is_admin()):
return dict(message=T('edit notification'))
res = {"message": ACCESS_DENIED}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
# process submitted edit form
def edit_form():
if (not auth.is_admin()):
res = {"message": ACCESS_DENIED}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
error = ""
if request.vars["message_content"] == "" :
error += "message body needed, "
if request.vars["message_type"] == "" :
error += "type needed, "
if request.vars["priority"] == "" :
error += "priority needed, "
if request.vars["expiration"] != "" :
try:
datetime.datetime.strptime(""+request.vars['expiration'], '%Y-%m-%d')
except ValueError:
error += "date (wrong format)"
if error=="" :
db.notification[request.vars['id']] = dict(message_content=request.vars["message_content"],
message_type=request.vars["message_type"],
priority=request.vars["priority"],
expiration=request.vars["expiration"])
res = {"redirect": "notification/index",
"args" : { "id" : request.vars['id'] },
"message": "notification updated"}
log.info(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
else :
res = {"success" : "false",
"message" : error}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
def delete():
if (not auth.is_admin()):
res = {"message": ACCESS_DENIED}
log.error(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
db(db.notification.id==request.vars['id']).delete()
res = {"redirect": "notification/index",
"success": "true",
"message": "notification " + request.vars['id'] + " deleted"}
log.info(res)
return gluon.contrib.simplejson.dumps(res, separators=(',',':'))
#
def get_active_notifications():
today = date.today()
if (request.vars['type'] is not None):
query = db(
((db.notification.expiration >= today)
| (db.notification.expiration == None))
& (db.notification.message_type == request.vars['type'])
).select()
else :
query = db(
(db.notification.expiration >= today) | (db.notification.expiration == None)
).select()
return query.json()
......@@ -175,6 +175,13 @@ db.define_table('analysis_file',
uploadfolder=defs.DIR_RESULTS,
length=LENGTH_UPLOAD, autodelete=AUTODELETE))
db.define_table('notification',
Field('creator', 'integer'),
Field('message_content', 'text'),
Field('message_type', 'string'),
Field('priority', 'string'),
Field('expiration', 'datetime'))
## after defining tables, uncomment below to enable auditing
auth.enable_record_versioning(db)
......
......@@ -8,6 +8,7 @@
<a class="button" onclick="db.call('group/index');">groups</a>
<a class="button" onclick="db.call('user/index');">users</a>
<a class="button" onclick="db.call('admin/index');">admin</a>
<a class="button" onclick="db.call('notification/index');">notifications</a>
- - -
{{pass}}
</div>
......
{{extend 'db_layout.html'}}
<form id="data_form" action="DB_ADDRESS/notification/add_form" enctype="multipart/form-data" method="post">
<table>
<tbody>
<tr>
<td ><label for="notification_type" id="notification_type__label">type: </label></td>
<td ><input class="string" id="notification_type" name="message_type" type="text" value=""><span>*</span></td>
<td ></td>
</tr>
<tr>
<td><label for="notification_priority" id="notification_priority__label">priority: </label></td>
<td><input class="string" id="notification_priority" name="priority" type="text" value=""><span>*</span></td>
<td></td>
</tr>
<tr>
<td><label for="notification_message_content" id="notification_message_content__label">message: </label></td>
<td><textarea class="string" id="notification_message_content" name="message_content" type="text" value=""></textarea><span>*</span></td>
<td></td>
</tr>
<tr>
<td><label for="notification_expiration" id="notification_expiration__label">expiration date: </label></td>
<td><input class="date" id="notification_expiration" name="expiration" type="text" value=""><span>(format: yyyy-mm-dd)</span></td>
<td></td>
</tr>
<tr>
<td></td>
<td ><input type="submit" value="save" class="btn"></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<div>
(* required fields)
</div>
{{extend 'db_layout.html'}}
{{
info = db.notification[request.vars["id"]]
}}
<form id="data_form" action="DB_ADDRESS/notification/edit_form" enctype="multipart/form-data" method="post">
<input type="HIDDEN" id="notification_id" name="id" value="{{=request.vars["id"]}}">
<table>
<tbody>
<tr>
<td ><label for="notification_message_type" id="notification_message_type__label">type: </label></td>
<td ><input class="string" id="notification_message_type" name="message_type" type="text" value="{{=info.message_type}}"><span>*</span></td>
<td ></td>
</tr>
<tr>
<td><label for="notification_priority" id="notification_priority__label">priority: </label></td>
<td><input class="string" id="notification_priority" name="priority" type="text" value="{{=info.priority}}"><span>*</span></td>
<td></td>
</tr>
<tr>
<td><label for="notification_message_content" id="notification_message_content__label">message: </label></td>
<td><textarea class="string" id="notification_message_content" name="message_content" type="text">{{=info.message_content}}</textarea><span>*</span></td>
<td></td>
</tr>
<tr>
<td><label for="notification_expiration" id="notification_expiration__label">expiration date: </label></td>
<td><input class="date" id="notification_expiration" name="expiration" type="text" value="{{=info.expiration.date()}}"><span>(format: yyyy-mm-dd)</span></td>
<td></td>
</tr>
<tr>
<td></td>
<td ><input type="submit" value="save" class="btn"></td>
<td></td>
</tr>
</tbody>
</table>
</form>
<div>
(* required fields)
</div>
{{extend 'db_layout.html'}}
<h3>{{=message}}</h3>
<div id="db_table_container">
<table class="db_table" id="table">
<thead>
<tr>
<td class="column_100"> Creator </td>
<td class="column_100"> Type </td>
<td class="column_100"> Priority </td>
<td class="column_100"> Message </td>
<td class="column_100"> Expiration Date </td>
<td class="column5"> </td>
<td class="column5"> </td>
<td class="column5"> </td>
</tr>
</thead>
<tbody>
{{for row in query:}}
<tr>
<td>{{=row.creator}}</td>
<td>{{=row.message_type}}</td>
<td>{{=row.priority}}</td>
<td>{{=row.message_content}}</td>
<td>{{=row.expiration}}</td>
<td onclick="db.call('notification/edit', {'id' : '{{=row.id}}'})">e</td>
<td onclick="db.call('notification/delete', {'id' : '{{=row.id}}'})">X</td>
</tr>
{{pass}}
</tbody>
</table>
<table class="db_table" id="db_fixed_header"></table>
</div>
<span class="button2" onclick="db.call('notification/add')"> + new notification </span>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment