scripts/intranet/modules/quota/main.py
Antoine Durand-Gasselin 7b7b88acb0 suppression de trailing whitespaces
darcs-hash:20081002165739-bd074-d426f3830f4c7d2203eb80c94c5ccc7c38e9ba86.gz
2008-10-02 18:57:39 +02:00

106 lines
3.9 KiB
Python
Executable file

#! /usr/bin/env python
import cherrypy, tempfile, shutil, os
import crans.utils.quota
import crans.cp
from ClassesIntranet.ModuleBase import ModuleBase
class main(ModuleBase):
def category(self):
return "Personnel"
def title(self):
return "Quotas"
def icon(self):
quotas = self._get_quota()
for a_quota in quotas:
if a_quota['quota'] < a_quota['usage']:
return "icon_alert.png"
return "icon.png"
def _get_quota(self):
return crans.utils.quota.getUserQuota(cherrypy.session['uid'])
#return [{'%': 33.9, 'quota': 390.62, 'label': u'Dossier personnel', 'limite': 585.94, 'filesystem': '/home', 'usage': 420.32}, {'%': 0.1, 'quota': 100.00, 'label': u'Boite de r\xe9ception', 'limite': 150.00, 'filesystem': '/var/mail', 'usage': 0.06}]
##########################
# affichage
##########################
#
# methode qui affiche la template
#
def index(self ):
#return "youpiiii"
values = {}
try:
quotas = self._get_quota()
returned_quotas = []
for a_quota in quotas:
# calculate text equivalent
quota = a_quota['quota']
usage = a_quota['usage']
limite = a_quota['limite']
text_equiv = "["
text_equiv+= "#" * min( 10, int( 10 * usage / quota ) )
text_equiv+= "." * max(0, int( 10 * ( quota - usage ) / quota ) )
if limite > quota:
text_equiv+= "|"
limite_diff = limite - quota
diff_proportion = 10 * limite_diff / quota
depassement = max(0, usage - quota)
text_equiv+= "#" * min(0, int(diff_proportion* ( depassement / limite_diff ) ) )
text_equiv+= "." * max(0, int(diff_proportion*( limite_diff - depassement ) / limite_diff ) )
text_equiv+= "]"
a_returned_quota = {
"label":a_quota['label'],
"usage":a_quota['usage'],
"quota":a_quota['quota'],
"limite":a_quota['limite'],
"percents":a_quota['%'],
"%":a_quota['%'],
"text_equiv":text_equiv,
"svg_url":"barreSVG?filesystem=%s" % a_quota['filesystem'],
}
returned_quotas.append(a_returned_quota)
values = {'quotas': returned_quotas, 'e': "eeede"}
except Exception, e:
crans.cp.log('error getting quota for user %s : %s' % (cherrypy.session['uid'], str(e)), 'QUOTA', 1)
values = {'erreur':str(e)}
return {'template':'quota',
'values': values,
'stylesheets':['quota.css'],
'scripts':['quota.js', 'popup.js'],
}
index.exposed = True
def index_html(self ):
result = self.index()
result['template'] = 'quota_html'
return result
index_html.exposed = True
##########################
# SVG
##########################
#
# methode qui renvoie une barre en svg
#
def barreSVG(self, filesystem = ""):
try:
values = {'erreur':"Not found"}
quotas = self._get_quota()
for a_quota in quotas:
if a_quota['filesystem'] == filesystem:
values = {
"usage":a_quota['usage'],
"quota":a_quota['quota'],
"limite":a_quota['limite'],
"percents":a_quota['%'],
}
except Exception, e:
values = {'erreur':str(e) }
cherrypy.response.headers['Content-Type']="image/svg+xml"
return {'template':'barre.svg',
'values': values,
'standalone':True,
}
barreSVG.exposed= True