Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

400 lines
11KB

  1. from models.database import db
  2. import logging
  3. import mysql.connector
  4. logger = logging.getLogger(__name__)
  5. def get_categories():
  6. """
  7. Get all categories
  8. :return: List of categories
  9. """
  10. db.connect()
  11. cursor = db.cursor()
  12. query = ("SELECT * FROM project_category")
  13. try:
  14. cursor.execute(query)
  15. categories = cursor.fetchall()
  16. except mysql.connector.Error as err:
  17. logger.error("Failed executing query: %s", err)
  18. categories = []
  19. cursor.fetchall()
  20. exit(1)
  21. finally:
  22. cursor.close()
  23. db.close()
  24. return categories
  25. def set_project(categoryid, userid, project_title, project_description, project_status):
  26. """
  27. Store a project in the database
  28. :param categoryid: The id of the corresponding category
  29. :param userid: The id of the project owner
  30. :param project_title: The title of the project
  31. :param project_description: The project description
  32. :param project_status: The status of the project
  33. :type categoryid: str
  34. :type userid: str
  35. :type project_title: str
  36. :type project_description: str
  37. :type project_status: str
  38. :return: The id of the new project
  39. """
  40. db.connect()
  41. cursor = db.cursor()
  42. query = ("INSERT INTO projects VALUES (NULL, %s, %s, %s, %s, %s)")
  43. try:
  44. cursor.execute(query, (categoryid, userid, project_title, project_description, project_status))
  45. db.commit()
  46. users_projects = get_projects_by_owner(userid)
  47. projectid = users_projects[-1][0]
  48. except mysql.connector.Error as err:
  49. logger.error("Failed executing query: %s", err)
  50. projectid = None
  51. cursor.fetchall()
  52. exit(1)
  53. finally:
  54. cursor.close()
  55. db.close()
  56. return projectid
  57. def get_project_by_id(projectid):
  58. """
  59. Retrieve a project by its id
  60. :param projectid: The project id
  61. :type projectid: str
  62. :return: The selected project
  63. """
  64. db.connect()
  65. cursor = db.cursor()
  66. query = ("SELECT * FROM projects WHERE projectid = %s")
  67. try:
  68. cursor.execute(query, (projectid,))
  69. project = cursor.fetchall()
  70. except mysql.connector.Error as err:
  71. logger.error("Failed executing query: %s", err)
  72. project = []
  73. cursor.fetchall()
  74. exit(1)
  75. finally:
  76. cursor.close()
  77. db.close()
  78. return project[0]
  79. def update_project_status(projectid, status):
  80. """
  81. Change the status of a selected project
  82. :param projectid: The project id
  83. :param status: The status to change to, should be either open, in progress or finished
  84. :type projectid: str
  85. :type status: str
  86. """
  87. db.connect()
  88. cursor = db.cursor()
  89. query = ("UPDATE projects SET project_status = %s WHERE projectid = %s")
  90. try:
  91. cursor.execute(query, (status, projectid))
  92. db.commit()
  93. except mysql.connector.Error as err:
  94. logger.error("Failed executing query: %s", err)
  95. cursor.fetchall()
  96. exit(1)
  97. finally:
  98. cursor.close()
  99. db.close()
  100. def get_user_permissions(userid, projectid):
  101. """
  102. Get permissions for a selected users in a specific project
  103. :param userid: The id of the user
  104. :param projectid: The id of the project
  105. :type userid: str
  106. :type projectid: str
  107. :return: Permissions as an array of numbers as boolean values
  108. """
  109. db.connect()
  110. cursor = db.cursor()
  111. query = ("SELECT read_permission, write_permission, modify_permission \
  112. FROM projects_users WHERE projectid = %s AND userid = %s")
  113. try:
  114. cursor.execute(query, (projectid, userid))
  115. permissions = cursor.fetchall()
  116. except mysql.connector.Error as err:
  117. logger.error("Failed executing query: %s", err)
  118. cursor.fetchall()
  119. exit(1)
  120. finally:
  121. cursor.close()
  122. db.close()
  123. if len(permissions):
  124. return permissions[0]
  125. return [0, 0, 0]
  126. def is_owner(userid, projectid):
  127. return projectid in get_projects_by_owner(userid)
  128. def get_projects_by_status_and_category(categoryid, project_status):
  129. """
  130. Retrieve all projects from a category with a specific status
  131. :param catergoryid: The id of the category
  132. :param project_status: The status to filter on
  133. :type catergoryid: str
  134. :type project_status: str
  135. :return: A list of projects
  136. """
  137. db.connect()
  138. cursor = db.cursor()
  139. query = ("SELECT * FROM projects WHERE project_status = %s AND categoryid = %s")
  140. try:
  141. cursor.execute(query, (project_status, categoryid))
  142. projects = cursor.fetchall()
  143. except mysql.connector.Error as err:
  144. logger.error("Failed executing query: %s", err)
  145. projects = []
  146. cursor.fetchall()
  147. exit(1)
  148. finally:
  149. cursor.close()
  150. db.close()
  151. return projects
  152. def get_projects_by_owner(userid):
  153. """
  154. Retrieve all projects created by a specific user
  155. :param userid: The id of the user
  156. :type userid: str
  157. :return: An array of projects
  158. """
  159. db.connect()
  160. cursor = db.cursor()
  161. query = ("SELECT * FROM projects WHERE userid = %s")
  162. try:
  163. cursor.execute(query, (userid,))
  164. projects = cursor.fetchall()
  165. except mysql.connector.Error as err:
  166. logger.error("Failed executing query: %s", err)
  167. projects = []
  168. cursor.fetchall()
  169. exit(1)
  170. finally:
  171. cursor.close()
  172. db.close()
  173. return projects
  174. def get_projects_by_status_and_owner(userid, project_status):
  175. """
  176. Retrieve all projects owned by a user with a specific status
  177. :param userid: The id of the owner
  178. :param project_status: The status to filter on
  179. :type userid: str
  180. :type project_status: str
  181. :return: A list of projects
  182. """
  183. db.connect()
  184. cursor = db.cursor()
  185. query = ("SELECT * FROM projects WHERE project_status = %s AND userid = %s")
  186. try:
  187. cursor.execute(query, (project_status, userid))
  188. projects = cursor.fetchall()
  189. except mysql.connector.Error as err:
  190. logger.error("Failed executing query: %s", err)
  191. projects = []
  192. cursor.fetchall()
  193. exit(1)
  194. finally:
  195. cursor.close()
  196. db.close()
  197. return projects
  198. def get_projects_by_participant_and_status(userid, project_status):
  199. """
  200. Retrieve all projects where the user is a participant with specific status
  201. :param userid: The id of the participant
  202. :param project_status: The status to filter on
  203. :type userid: str
  204. :type project_status: str
  205. :return: A list of projects
  206. """
  207. db.connect()
  208. cursor = db.cursor()
  209. query = ("SELECT * FROM projects, projects_users WHERE projects.project_status = %s AND " +
  210. "projects_users.userid = %s AND projects_users.projectid = projects.projectid")
  211. try:
  212. cursor.execute(query, (project_status, userid))
  213. projects = cursor.fetchall()
  214. except mysql.connector.Error as err:
  215. logger.error("Failed executing query: %s", err)
  216. projects = []
  217. cursor.fetchall()
  218. exit(1)
  219. finally:
  220. cursor.close()
  221. db.close()
  222. return projects
  223. def set_task(projectid, task_title, task_description, budget):
  224. """
  225. Create a task
  226. :param projectid: The corresponding project id
  227. :param task_title: The title of the task
  228. :param task_description: The description of the task
  229. :param budget: The task budget
  230. :type projectid: str
  231. :type task_title: str
  232. :type task_description: str
  233. :type budget: str
  234. """
  235. db.connect()
  236. cursor = db.cursor()
  237. query = ("INSERT INTO tasks (projectid, title, task_description, budget, task_status) " +
  238. "VALUES (%s, %s, %s, %s, \"waiting for delivery\")")
  239. try:
  240. cursor.execute(query, (projectid, task_title, task_description, budget))
  241. db.commit()
  242. except mysql.connector.Error as err:
  243. logger.error("Failed executing query: %s", err)
  244. cursor.fetchall()
  245. exit(1)
  246. finally:
  247. cursor.close()
  248. db.close()
  249. def update_task_status(taskid, status):
  250. db.connect()
  251. cursor = db.cursor()
  252. query = ("UPDATE tasks SET task_status = %s WHERE taskid = %s")
  253. try:
  254. cursor.execute(query, (status, taskid))
  255. db.commit()
  256. except mysql.connector.Error as err:
  257. logger.error("Failed executing query: %s", err)
  258. cursor.fetchall()
  259. exit(1)
  260. finally:
  261. cursor.close()
  262. db.close()
  263. def get_tasks_by_project_id(projectid):
  264. """
  265. Get all tasks belonging to a project
  266. :param project_id: The id of the project holding the tasks
  267. :type project_id: str
  268. :return: List of tasks
  269. """
  270. db.connect()
  271. cursor = db.cursor()
  272. query = ("SELECT * FROM tasks WHERE projectid = %s")
  273. try:
  274. cursor.execute(query, (projectid,))
  275. tasks = cursor.fetchall()
  276. except mysql.connector.Error as err:
  277. logger.error("Failed executing query: %s", err)
  278. tasks = []
  279. cursor.fetchall()
  280. exit(1)
  281. finally:
  282. cursor.close()
  283. db.close()
  284. return tasks
  285. def set_task_file(taskid, filename):
  286. """
  287. Register a new task - file relationship
  288. :param taskid: The task id
  289. :param filename: The name of the file
  290. :type taskid: str
  291. :type filename: str
  292. """
  293. db.connect()
  294. cursor = db.cursor()
  295. query = ("INSERT INTO task_files (taskid, filename) VALUES (%s, %s)")
  296. try:
  297. cursor.execute(query, (taskid, filename))
  298. db.commit()
  299. except mysql.connector.Error as err:
  300. logger.error("Failed executing query: %s", err)
  301. cursor.fetchall()
  302. exit(1)
  303. finally:
  304. cursor.close()
  305. db.close()
  306. def get_task_files(taskid):
  307. """
  308. Retrieve all filenames registered in a task
  309. :param taskid: The task id
  310. :type taskid: str
  311. :return: An array of filenames
  312. """
  313. db.connect()
  314. cursor = db.cursor()
  315. query = ("SELECT filename FROM task_files WHERE taskid = %s")
  316. try:
  317. cursor.execute(query, (str(taskid),))
  318. filenames = cursor.fetchall()
  319. except mysql.connector.Error as err:
  320. logger.error("Failed executing query: %s", err)
  321. filenames = []
  322. cursor.fetchall()
  323. exit(1)
  324. finally:
  325. cursor.close()
  326. db.close()
  327. return filenames
  328. def set_projects_user(projectid, userid, read_permission="TRUE",
  329. write_permission="NULL", modify_permission="NULL"):
  330. """
  331. Add a user to a project with specific permissions
  332. :param projectid: The project id
  333. :param userid: The user id
  334. :param read_permission: Describes whether a user can view information about a project
  335. :param write_permission: Describes whether a user can add files to tasks
  336. :param modify_permission: Describes wheter a user can deliver tasks
  337. :type projectid: str
  338. :type userid: str
  339. :type read_permission: str
  340. :type write_permission: str
  341. """
  342. db.connect()
  343. cursor = db.cursor()
  344. query = ("INSERT INTO projects_users VALUES (%s, %s, %s, %s, %s)")
  345. try:
  346. cursor.execute(query, (projectid, userid, read_permission, write_permission, modify_permission))
  347. db.commit()
  348. except mysql.connector.Error as err:
  349. logger.error("Failed executing query: %s", err)
  350. cursor.fetchall()
  351. exit(1)
  352. finally:
  353. cursor.close()
  354. db.close()