Root.py 7.2 KB
Newer Older
gdetrez's avatar
gdetrez committed
1 2 3 4 5 6 7
#!/usr/bin/env python
# -*- coding: utf-8 -*-


import cherrypy, sys, os, datetime
sys.path.append('/usr/scripts/gestion/')

gdetrez's avatar
gdetrez committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
            
# ######################################################## #
#                 COMMAND LINE OPTION                      #
# ######################################################## #
# 
#

from optparse import OptionParser

parser = OptionParser()
parser.add_option("-d", "--dev",
    action="store_true", dest="dev", default=False,
    help="launch in dev mode")
parser.add_option("-p", "--port",
    action="store", type="int", dest="port",
    help="change server port")
     
(options, args) = parser.parse_args()


# ######################################################## #
#                          CONFIG                          #
# ######################################################## #
# 
# mise en place de la conf
#

# on suppose qu'en version de developpement, le script est lance depuis le shell
if (options.dev):
    cherrypy.config.update(file=os.getcwd() + "/conf/intranet.cfg")
38
    cherrypy.config.update(file=os.getcwd() + "/conf/dev.cfg") 
gdetrez's avatar
gdetrez committed
39 40 41 42 43
    settings={'global': { 'rootDir': os.getcwd() } }                                                                                                                                         
    cherrypy.config.update(settings)                                                                                                               
                
else:
    cherrypy.config.update(file="/usr/scripts/intranet/conf/intranet.cfg") 
44
    cherrypy.config.update(file="/usr/scripts/intranet/conf/prod.cfg") 
gdetrez's avatar
gdetrez committed
45 46 47 48 49 50 51

# changer le port ??
if (options.port):
    settings={'global':{'server.socketPort':options.port}}
    cherrypy.config.update(settings)                                                                                                               
    

gdetrez's avatar
gdetrez committed
52
# import du crans_ldap qu'il va bien
gdetrez's avatar
gdetrez committed
53 54
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
    from ldap_crans_test import crans_ldap
gdetrez's avatar
gdetrez committed
55
    cherrypy.log("settings : unsing test ldap : env=" + cherrypy.config.configMap["global"]["server.environment"], "LDAP")
gdetrez's avatar
gdetrez committed
56 57
else:
    from ldap_crans import crans_ldap
gdetrez's avatar
gdetrez committed
58
    cherrypy.log("settings : unsing prod ldap : env=" + cherrypy.config.configMap["global"]["server.environment"], "LDAP")
gdetrez's avatar
gdetrez committed
59 60
cherrypy.config.update({'global':{'crans_ldap':crans_ldap()}})

gdetrez's avatar
gdetrez committed
61 62 63 64 65 66 67
# ######################################################## #
#                     FILTRES MAISON                       #
# ######################################################## #

from plugins.domfilter import DOMFilter
from plugins.templatesfilter import TemplatesFilter
from plugins.verifdroitsfilter import VerifDroitsFilter
gdetrez's avatar
gdetrez committed
68
from crans.mail import quickSend                    
gdetrez's avatar
gdetrez committed
69 70 71 72 73 74
# ######################################################## #
#                         SERVER                           #
# ######################################################## #
class Intranet:
    __ldap = None
    def __init__(self):
75
        from pages import monCompte, impression, factures, digicode, mesmachines
gdetrez's avatar
gdetrez committed
76 77 78 79
        self.__ldap = cherrypy.config.configMap["global"]["crans_ldap"]
        
        # liste des modules disponibles
        self.monCompte = monCompte.monCompte()
80
        self.sous = factures.root()
gdetrez's avatar
gdetrez committed
81
        self.impression = impression.root()        
gdetrez's avatar
gdetrez committed
82
        self.digicode = digicode.root()
83
        self.mesMachines = mesmachines.root()
84
        
gdetrez's avatar
gdetrez committed
85
        # liste des modules en developpement
86 87
        #if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
        
gdetrez's avatar
gdetrez committed
88

gdetrez's avatar
gdetrez committed
89 90 91 92 93 94 95 96
            
            
    _cpFilterList = [TemplatesFilter(), DOMFilter(), VerifDroitsFilter()]
    
    def index(self):
        return {
            'template':'accueil',
            'values':{},
gdetrez's avatar
gdetrez committed
97
            'stylesheets':['accueil.css'],
gdetrez's avatar
gdetrez committed
98 99 100 101 102
            }
    index.exposed= True
    
    def info(self):
        return {
gdetrez's avatar
gdetrez committed
103 104 105 106
          'template':'info-diverses',
          'values':{},
          'stylesheets':['accueil.css'],
        }
gdetrez's avatar
gdetrez committed
107
    info.exposed = True
108

gdetrez's avatar
gdetrez committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    def send_error_repport(self, **kw):
        exp = "intranet"
        dest = cherrypy.config.get("mail.bugreport", "nounous@crans.org")
        subject = "Rapport de Bug"
        text = """
Bonsoir,

Ceci est un rapport de bug envoye depuis l'intranet.        

%s
        """ % "\n".join( [ "%s: %s" % (str(a), str(kw[a])) for a in kw] )
        quickSend(exp, dest, subject, text)
        return self.index()
    
    send_error_repport.exposed = True
    
    def testErreur(self):
        raise Exception, "sdlfkjqmsdklj"
    
    testErreur.exposed = True

130 131 132
    def test(self):
        return cherrypy.request.remote_addr
    test.exposed = True
gdetrez's avatar
gdetrez committed
133

gdetrez's avatar
gdetrez committed
134
    def _cp_on_http_error(self, status, message):
135 136 137 138 139 140 141 142 143 144 145 146
        if status==403:
            cherrypy.response.body = {
                'template':'error403',
                'values':{'status':status, 'message':message }, 
                'standalone':False,
                }
        elif status==404:
            cherrypy.response.body = {
                'template':'error',
                'values':{'status':status, 'message':message }, 
                'standalone':False,
                }
gdetrez's avatar
gdetrez committed
147 148 149
        elif status==500:
            from plugins.templatesfilter import TemplatesFilter
            TemplatesFilter().goWithThisDict({'template':'error', 'values':{'status':status, 'message':message }})
150 151 152
        else:
            cherrypy._cputil._cp_on_http_error(status, message)

gdetrez's avatar
gdetrez committed
153 154 155 156 157 158 159 160 161
    
# ######################################################## #
#                      LOGIN MAISON                        #
# ######################################################## #
# 
# Methode pour afficher la template de login
#
def login(from_page = '', login = None, password = '', error_msg=''):
    return {
gdetrez's avatar
gdetrez committed
162 163 164 165
     'template':'login',
     'values':{'login':login, 'password':password, 'from_page':from_page, 'message':error_msg},
     'standalone':True
    }
gdetrez's avatar
gdetrez committed
166 167 168 169 170
#
# methode qui verifie le login
#
def verifLogin(login = '', password = ''):
    message = None
gdetrez's avatar
gdetrez committed
171 172 173 174 175
    try:
        if login != '' and password != '':
            cherrypy.session['LDAP'] = crans_ldap()
            LDAP = cherrypy.session['LDAP']
            adh = LDAP.search('uid=' + login)['adherent'][0]
gdetrez's avatar
gdetrez committed
176 177 178 179
            if adh.checkPassword(password):
                cherrypy.session['uid'] = login
                cherrypy.session['session_key'] = True
                cherrypy.session['droits'] = adh.droits()
180
                cherrypy.log("User logged in : %s" % cherrypy.session['uid'], "LOGIN")
gdetrez's avatar
gdetrez committed
181 182
                return
            else:
gdetrez's avatar
gdetrez committed
183 184
                raise Exception, "Bad password"
        else:
gdetrez's avatar
gdetrez committed
185
            message = u"L'authentification a echoué."
gdetrez's avatar
gdetrez committed
186 187 188
            raise Exception, "Empty string"
    except Exception, e:
        cherrypy.log("%s (login:%s)" % (str(e), login), "LOGIN", 1)
gdetrez's avatar
gdetrez committed
189
        message = u"L'authentification a echoué."
190
    return message
gdetrez's avatar
gdetrez committed
191 192 193 194 195





gdetrez's avatar
gdetrez committed
196
# on indique tout ca a cherrypy
gdetrez's avatar
gdetrez committed
197 198 199 200 201 202 203 204
settings={'/': {
    'sessionAuthenticateFilter.checkLoginAndPassword': verifLogin,
    'sessionAuthenticateFilter.loginScreen': login
    }}
cherrypy.config.update(settings)


    
gdetrez's avatar
gdetrez committed
205 206 207 208 209
# ######################################################## #
#                 LANCEMENT DE CHERRYPY                    #
# ######################################################## #
cherrypy.root = Intranet()
cherrypy.server.start()