Attention une mise à jour du service Gitlab va être effectuée le mardi 30 novembre entre 17h30 et 18h00. Cette mise à jour va générer une interruption du service dont nous ne maîtrisons pas complètement la durée mais qui ne devrait pas excéder quelques minutes. Cette mise à jour intermédiaire en version 14.0.12 nous permettra de rapidement pouvoir mettre à votre disposition une version plus récente.

notification.py 5.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 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)

23 24 25 26 27
def info():

	query = db.notification[request.vars['id']]

	return dict(query=query)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

# 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 = ""
46 47
    if request.vars['title'] =="":
        error += "title needed, "
48 49 50 51 52 53 54 55 56 57 58 59 60
    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=="" :
61
        id = db.notification.insert(title=request.vars["title"],
62
        						message_content=XML(request.vars["message_content"], sanitize=True).xml(),
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
                               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=="" :
111
        db.notification[request.vars['id']] = dict(title=request.vars["title"],
112
        						message_content=XML(request.vars["message_content"], sanitize=True).xml(),
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
                               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()
HERBERT Ryan's avatar
HERBERT Ryan committed
157
    #TODO sanitize this response
158
    return query.as_json()