00001
00002
00003
00004 licence={}
00005 licence['en']="""
00006 file db.py
00007 this file is part of the project scolasync
00008
00009 Copyright (C) 2010 Georges Khaznadar <georgesk@ofset.org>
00010
00011 This program is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version3 of the License, or
00014 (at your option) any later version.
00015
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU General Public License for more details.
00020
00021 You should have received a copy of the GNU General Public License
00022 along with this program. If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025 import os.path, sqlite3, subprocess
00026 import version
00027 from globaldef import userShareDir
00028
00029 database= None
00030 cursor=None
00031
00032
00033
00034
00035
00036
00037 def openDb():
00038 global database, cursor
00039 dir=os.path.expanduser(userShareDir)
00040 if not os.path.isdir(dir):
00041 subprocess.call("mkdir %s" %dir, shell=True)
00042 database = sqlite3.connect(os.path.join(dir,"db"))
00043 cursor=database.cursor()
00044 cursor.execute('''create table if not exists owners (stickid text, uuid text, tatoo text, student text)''')
00045 cursor.execute('''create table if not exists version (major text, minor text)''')
00046 cursor.execute('''create table if not exists preferences (checkable int, mv int, schoolfile text, workdir text, manfile text, refresh_enabled int, refresh_delay int)''')
00047 database.commit()
00048 checkVersion(version.major(), version.minor())
00049
00050
00051
00052
00053
00054
00055
00056
00057 def checkVersion(major, minor):
00058 cursor.execute('''select * from version''')
00059 values=cursor.fetchone()
00060 if values == None:
00061
00062 cursor.execute('''insert into version values (?,?)''', (version.major(), version.minor()))
00063 else:
00064 major, minor = values
00065 if major < version.major():
00066 raise KeyError, "The database version is too old!"
00067 elif minor < version.minor():
00068 cursor.execute("""update version
00069 set minor=?
00070 where major=?""", (version.minor(), version.major()))
00071 database.commit()
00072
00073
00074
00075
00076
00077
00078
00079 def hasStudent(student):
00080 global cursor
00081 cursor.execute("select * from owners where student=?", (student,))
00082 return cursor.fetchone() != None
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092 def knowsId(stickid, uuid,tattoo):
00093 global cursor
00094 cursor.execute("select * from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
00095 return cursor.fetchone() != None
00096
00097
00098
00099
00100
00101 def tattooList():
00102 global cursor
00103 cursor.execute("select tatoo from owners")
00104 return cursor.fetchmany()
00105
00106
00107
00108
00109
00110
00111 def readStudent(stickid, uuid, tattoo):
00112 global cursor
00113 cursor.execute("select student from owners where stickid=? and uuid=? and tatoo=?", (stickid, uuid,tattoo))
00114 s = cursor.fetchone()
00115 if s != None:
00116 return s[0]
00117 else:
00118 return None
00119
00120
00121
00122
00123
00124
00125 def readPrefs():
00126 global cursor
00127 cursor.execute("select checkable, mv, schoolfile,workdir, manfile, refresh_enabled, refresh_delay from preferences")
00128 s = cursor.fetchone()
00129 if s != None:
00130 checkable = s[0]==1
00131 mv = s[1]==1
00132 schoolFile = s[2]
00133 workdir = s[3]
00134 manfile = s[4]
00135 refreshEnabled = s[5]==1
00136 refreshDelay = s[6]
00137 return {"checkable" : checkable,
00138 "mv" : mv,
00139 "schoolFile" : schoolFile,
00140 "workdir" : workdir,
00141 "manfile" : manfile,
00142 "refreshEnabled": refreshEnabled,
00143 "refreshDelay" : refreshDelay
00144 }
00145 else:
00146
00147 return {"checkable" : True,
00148 "mv" : False,
00149 "schoolFile" : "/usr/share/scolasync/exemple/SCONET_test.xml",
00150 "workdir" : "Travail",
00151 "manfile" : "/usr/share/scolasync/help/manualPage_fr_FR.html",
00152 "refreshEnabled": False,
00153 "refreshDelay" : 30
00154 }
00155
00156
00157
00158
00159
00160 def setWd(newDir):
00161 cursor.execute("""update preferences set workdir=?""",
00162 (newDir,))
00163 database.commit()
00164
00165
00166
00167
00168
00169
00170 def writeStudent(stickid, uuid, tattoo, student):
00171 global database, cursor
00172 if knowsId(stickid, uuid, tattoo):
00173 cursor.execute("""update owners
00174 set student=?
00175 where stickid=? and uuid=? and tatoo=?""", (student, stickid, uuid, tattoo))
00176 else:
00177 cursor.execute("""insert into owners
00178 values (?,?,?,?)""", (stickid, uuid, tattoo, student))
00179 database.commit()
00180
00181
00182
00183
00184
00185
00186 def writePrefs(prefs):
00187 global database, cursor
00188 if prefs["checkable"]:
00189 checkable=1
00190 else:
00191 checkable=0
00192 if prefs["mv"]:
00193 mv=1
00194 else:
00195 mv=0
00196 if prefs["refreshEnabled"]:
00197 refreshEnabled=1
00198 else:
00199 refreshEnabled=0
00200 cursor.execute("select checkable from preferences")
00201 s = cursor.fetchone()
00202 newValues=(checkable, mv, prefs["schoolFile"], prefs["workdir"], prefs["manfile"], refreshEnabled, prefs["refreshDelay"])
00203 if s != None:
00204 cursor.execute("""update preferences
00205 set checkable=?, mv=?, schoolfile=?, workdir=?, manfile=?, refresh_enabled=?, refresh_delay=?""",
00206 newValues)
00207 else:
00208 cursor.execute("""insert into preferences
00209 values (?,?,?,?,?,?,?)""",
00210 newValues)
00211 database.commit()
00212
00213
00214 if database == None:
00215 openDb()
00216