Aucune description

maldisql.py 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import MySQLdb
  2. class maldisql:
  3. def __init__(self, host="localhost", db="maldisoft", user="maldiuser"):
  4. passwd = ""
  5. self.db = MySQLdb.connect(host=host, db=db, user=user, passwd=passwd)
  6. self.db.autocommit(True)
  7. self.c = self.db.cursor(MySQLdb.cursors.DictCursor)
  8. # New user functions
  9. def AddUser(self, email, passwd, name, last):
  10. try:
  11. self.c.execute("""INSERT into USER (email, passwd, name, last, lastaccess) values
  12. (%s, %s, %s, %s, CURRENT_TIMESTAMP)""",
  13. (email, passwd, name, last))
  14. except MySQLdb.IntegrityError, e:
  15. return "%s|%s" % ("Duplicate", e)
  16. except MySQLdb.Error, e:
  17. return e
  18. return self.c.lastrowid
  19. def GetUser(self, uid):
  20. try:
  21. self.c.execute("""select email, name, last, lastaccess from USER where uid=%s""", uid)
  22. except MySQLdb.Error, e:
  23. return e
  24. return self.c.fetchone()
  25. def UpdatePassword(self, pwd, uid):
  26. try:
  27. self.c.execute("""update USER set passwd=%s where uid=%s""", (pwd, uid))
  28. except MySQLdb.Error, e:
  29. return e
  30. return None
  31. def UpdateUser(self, uid, email, name, last):
  32. try:
  33. self.c.execute("""update USER set email=%s, name=%s, last=%s where uid=%s""", (email, name, last, uid))
  34. except MySQLdb.Error, e:
  35. return e
  36. return None
  37. def ChangePassword(self, passwd):
  38. try:
  39. self.c.execute("""update USER set passwd=%s""", (passwd,))
  40. except MySQLdb.Error, e:
  41. return e
  42. return None
  43. def ChangeEmail(self, email, uid):
  44. try:
  45. self.c.execute("""update USER set email=%s where uid=%s""", (email,uid))
  46. except MySQLdb.Error, e:
  47. return e
  48. return None
  49. def Login(self, email, passwd):
  50. try:
  51. self.c.execute("""select uid, passwd from USER where email=%s""", email)
  52. except MySQLdb.Error, e:
  53. return None
  54. user=self.c.fetchone()
  55. if user and user["passwd"] == passwd:
  56. try:
  57. self.c.execute("""update USER set lastaccess=CURRENT_TIMESTAMP where uid=%s""", user["uid"])
  58. except:
  59. return None
  60. return user["uid"]
  61. else:
  62. return None
  63. # Create experiment functions
  64. def CreateExperiment(self, name, description, uid):
  65. try:
  66. self.c.execute("""INSERT into EXPERIMENT (uid, name, description) values
  67. (%s, %s, %s)""",
  68. (uid, name, description))
  69. except MySQLdb.IntegrityError, e:
  70. return "%s|%s" % ("Duplicate", e)
  71. except MySQLdb.Error, e:
  72. return e
  73. return int(self.c.lastrowid)
  74. def UpdateExperiment(self, name, description, eid):
  75. try:
  76. self.c.execute("""update EXPERIMENT set name=%s, description=%s where eid=%s""",
  77. (name, description, eid))
  78. except MySQLdb.Error, e:
  79. return e
  80. return None
  81. def DeleteExperiment(self, eid):
  82. try:
  83. self.c.execute("""delete from EXPERIMENT where eid=%s""", eid)
  84. except:
  85. return False
  86. return True
  87. def GetExperiments(self, uid):
  88. try:
  89. self.c.execute("""select eid, name, description, date from EXPERIMENT where uid=%s""", uid)
  90. except MySQLdb.Error, e:
  91. return e
  92. return self.c.fetchall()
  93. def CheckEIDOwner(self, eid, uid):
  94. try:
  95. self.c.execute("""select eid from EXPERIMENT where eid=%s and uid=%s""", (eid, uid))
  96. except MySQLdb.Error, e:
  97. return False
  98. return True
  99. def GetColEID(self, eid, uid):
  100. try:
  101. self.c.execute("""select eid from USER_EXPERIMENT where eid=%s and uid=%s""", (eid, uid))
  102. except MySQLdb.Error, e:
  103. return e
  104. return self.c.fetchone()
  105. def GetExperiment(self, eid):
  106. try:
  107. self.c.execute("""select uid, name, description, date from EXPERIMENT where eid=%s""", eid)
  108. except MySQLdb.Error, e:
  109. return e
  110. return self.c.fetchone()
  111. def ChangePassword(self, passwd):
  112. try:
  113. self.c.execute("""update USER set passwd=%s""", (passwd,))
  114. except MySQLdb.Error, e:
  115. return e
  116. def AddCollaborator(self, uid, eid):
  117. try:
  118. self.c.execute("""INSERT into USER_EXPERIMENT (uid, eid) values (%s, %s)""", (uid, eid))
  119. except MySQLdb.IntegrityError, e:
  120. return "%s|%s" % ("Duplicate", e)
  121. except MySQLdb.Error, e:
  122. return e
  123. return self.c.lastrowid
  124. def AddExperimentCollaborator(self, eid, email):
  125. try:
  126. self.c.execute("""select uid from USER where email=%s""", email)
  127. user = self.c.fetchone()
  128. except:
  129. return "Fail! Some error with the collaborator email."
  130. if not user:
  131. return "No such collaborator in the system"
  132. return self.AddCollaborator(user["uid"], eid)
  133. def GetExperimentCollaborators(self, eid):
  134. try:
  135. self.c.execute("""select USER.name as name, USER.last as last, email, ueid from USER, USER_EXPERIMENT where USER.uid = USER_EXPERIMENT.uid and USER_EXPERIMENT.eid=%s""", eid)
  136. except MySQLdb.Error, e:
  137. return e
  138. return self.c.fetchall()
  139. def RemoveExperimentCollaborator(self, ueid):
  140. try:
  141. self.c.execute("""delete from USER_EXPERIMENT where ueid=%s""", ueid)
  142. except:
  143. return False
  144. return True
  145. def GetCollaborations(self, uid):
  146. try:
  147. self.c.execute("""select USER_EXPERIMENT.eid as eid, EXPERIMENT.name as name, description, date from EXPERIMENT, USER_EXPERIMENT where EXPERIMENT.eid = USER_EXPERIMENT.eid and USER_EXPERIMENT.uid=%s""", uid)
  148. except MySQLdb.Error, e:
  149. return e
  150. return self.c.fetchall()
  151. def AddSample(self, eid, uuid, name):
  152. try:
  153. self.c.execute("""INSERT into SAMPLE (eid, uuid, name) values (%s, %s, %s)""",
  154. (eid, uuid, name))
  155. except MySQLdb.IntegrityError, e:
  156. return "%s|%s" % ("Duplicate", e)
  157. except MySQLdb.Error, e:
  158. return e
  159. return self.c.lastrowid
  160. def GetSamples(self, eid):
  161. try:
  162. self.c.execute("""select sid, name, uuid, lastmodified from SAMPLE where eid=%s""", eid)
  163. except MySQLdb.Error, e:
  164. return e
  165. return self.c.fetchall()
  166. def GetSample(self, sid):
  167. try:
  168. self.c.execute("""select eid, name, uuid, lastmodified from SAMPLE where sid=%s""", sid)
  169. except MySQLdb.Error, e:
  170. return e
  171. return self.c.fetchone()
  172. def AddPeak(self, sid, peak):
  173. try:
  174. self.c.execute("""INSERT into PEAK (sid, peak) values (%s, %s)""",
  175. (sid, peak))
  176. except MySQLdb.IntegrityError, e:
  177. return "%s|%s" % ("Duplicate", e)
  178. except MySQLdb.Error, e:
  179. return e
  180. return self.c.lastrowid
  181. def AddExperimentPeak(self, eid, peak):
  182. try:
  183. self.c.execute("""INSERT into EXPERIMENT_PEAK (eid, peak) values (%s, %s)""",
  184. (eid, peak))
  185. except MySQLdb.IntegrityError, e:
  186. return "%s|%s" % ("Duplicate", e)
  187. except MySQLdb.Error, e:
  188. return e
  189. return self.c.lastrowid
  190. def GetExperimentPeaks(self, eid):
  191. try:
  192. self.c.execute("""select * from EXPERIMENT_PEAK where eid=%s""", eid)
  193. except MySQLdb.Error, e:
  194. return e
  195. return self.c.fetchall()
  196. def RemovePeak(self, epid):
  197. try:
  198. self.c.execute("""delete from EXPERIMENT_PEAK where ep_id=%s""", epid)
  199. except:
  200. return False
  201. return True
  202. def GetPeaks(self, sid):
  203. try:
  204. self.c.execute("""select pid, peak from PEAK where sid=%s""", sid)
  205. except MySQLdb.Error, e:
  206. return e
  207. peaks = self.c.fetchall()
  208. for peak in peaks:
  209. try:
  210. self.c.execute("""select intensity_id, peak, intensity from INTENSITY where pid=%s""", peak["pid"])
  211. peak["intensities"] = self.c.fetchall()
  212. except MySQLdb.Error, e:
  213. return e
  214. return peaks
  215. def AddIntensity(self, pid, peak, intensity):
  216. try:
  217. self.c.execute("""INSERT into INTENSITY (pid, peak, intensity) values (%s, %s, %s)""",
  218. (pid, peak, intensity))
  219. except MySQLdb.IntegrityError, e:
  220. return "%s|%s" % ("Duplicate", e)
  221. except MySQLdb.Error, e:
  222. return e
  223. return self.c.lastrowid
  224. def close(self):
  225. self.db.close()
  226. def main():
  227. check1 = 1
  228. msql = maldisql()
  229. if check1:
  230. uid1 = msql.AddUser("cheo@gmail.com","tuxi", "Jose", "Ortiz")
  231. uid2 = msql.AddUser("kary@gmail.com","tuxi", "Kariluz", "Davila")
  232. print uid1
  233. print msql.ChangePassword("sleepy")
  234. print msql.ChangeEmail("cheojr@gmail.com", uid1)
  235. eid = msql.CreateExperiment("EXp1", "Esto es la descr", uid1)
  236. print "Experiment", eid
  237. print eid, uid2
  238. print msql.AddCollaborator(uid2, eid)
  239. sid = msql.AddSample(eid, "hola", "asi se llama el sample")
  240. print sid
  241. pid = msql.AddPeak(sid, 1.3)
  242. print pid
  243. print msql.AddIntensity(pid, 100.3)
  244. print msql.GetUser(uid1)
  245. print msql.GetExperiments(uid1)
  246. print msql.GetCollaborations(uid2)
  247. print msql.GetSamples(eid)
  248. print msql.GetSample(sid)
  249. print msql.GetPeaks(sid)
  250. msql.close()
  251. if __name__ == "__main__":
  252. main()