您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

180 行
5.8KB

  1. from models.database import db
  2. def get_categories():
  3. """
  4. Get all categories
  5. :return: List of categories
  6. """
  7. cursor = db.cursor()
  8. query = ("SELECT * FROM project_category")
  9. cursor.execute(query)
  10. categories = cursor.fetchall()
  11. cursor.close()
  12. return categories
  13. def set_project(categoryid, userid, project_title, project_description, project_status):
  14. """
  15. Store a project in the database
  16. :param categoryid: The id of the corresponding category
  17. :param userid: The id of the project owner
  18. :param project_title: The title of the project
  19. :param project_description: The project description
  20. :param project_status: The status of the project
  21. :type categoryid: str
  22. :type userid: str
  23. :type project_title: str
  24. :type project_description: str
  25. :type project_status: str
  26. :return: The id of the new project
  27. """
  28. cursor = db.cursor()
  29. query = ("INSERT INTO projects VALUES (NULL, \"" +
  30. categoryid + "\", \"" + userid + "\", \"" + project_title + "\", \"" +
  31. project_description + "\", \"" + project_status + "\")")
  32. cursor.execute(query)
  33. db.commit()
  34. cursor.close()
  35. users_projects = get_projects_by_owner(userid)
  36. projectid = users_projects[-1][0]
  37. return projectid
  38. def get_project_by_id(projectid):
  39. """
  40. Retrieve a project by its id
  41. :param projectid: The project id
  42. :type projectid: str
  43. :return: The selected project
  44. """
  45. cursor = db.cursor()
  46. query = ("SELECT * FROM projects WHERE projectid = \"" +
  47. projectid + "\"")
  48. cursor.execute(query)
  49. project = cursor.fetchall()
  50. return project
  51. def get_projects_by_status_and_category(categoryid, project_status):
  52. """
  53. Retrieve all projects from a category with a specific status
  54. :param catergoryid: The id of the category
  55. :param project_status: The status to filter on
  56. :type catergoryid: str
  57. :type project_status: str
  58. :return: A list of projects
  59. """
  60. cursor = db.cursor()
  61. query = ("SELECT * FROM projects WHERE project_status = \"" +
  62. project_status + "\" AND categoryid = \"" + categoryid + "\"")
  63. cursor.execute(query)
  64. projects = cursor.fetchall()
  65. return projects
  66. def get_projects_by_owner(userid):
  67. cursor = db.cursor()
  68. query = ("SELECT * FROM projects WHERE userid = \"" + userid + "\"")
  69. cursor.execute(query)
  70. projects = cursor.fetchall()
  71. return projects
  72. def get_projects_by_status_and_owner(userid, project_status):
  73. """
  74. Retrieve all projects owned by a user with a specific status
  75. :param userid: The id of the owner
  76. :param project_status: The status to filter on
  77. :type userid: str
  78. :type project_status: str
  79. :return: A list of projects
  80. """
  81. cursor = db.cursor()
  82. query = ("SELECT * FROM projects WHERE project_status = \"" +
  83. project_status + "\" AND userid = \"" + userid + "\"")
  84. cursor.execute(query)
  85. projects = cursor.fetchall()
  86. return projects
  87. def get_projects_by_participant_and_status(userid, project_status):
  88. """
  89. Retrieve all projects where the user is a participant with specific status
  90. :param userid: The id of the participant
  91. :param project_status: The status to filter on
  92. :type userid: str
  93. :type project_status: str
  94. :return: A list of projects
  95. """
  96. cursor = db.cursor()
  97. query = ("SELECT * FROM projects, projects_users WHERE projects.project_status = \"" +
  98. project_status + "\" AND projects_users.userid = \"" + userid +
  99. "\" AND projects_users.projectid = projects.projectid")
  100. cursor.execute(query)
  101. projects = cursor.fetchall()
  102. return projects
  103. def set_task(projectid, task_title, task_description, budget):
  104. """
  105. Create a task
  106. :param projectid: The corresponding project id
  107. :param task_title: The title of the task
  108. :param task_description: The description of the task
  109. :param budget: The task budget
  110. :type projectid: str
  111. :type task_title: str
  112. :type task_description: str
  113. :type budget: str
  114. """
  115. cursor = db.cursor()
  116. query = ("INSERT INTO tasks (projectid, title, task_description, budget, task_status) VALUES (\"" +
  117. projectid + "\", \"" + task_title + "\", \"" +
  118. task_description + "\", \"" + budget + "\", \"waiting for delivery\")")
  119. cursor.execute(query)
  120. db.commit()
  121. cursor.close
  122. def get_tasks_by_project_id(projectid):
  123. """
  124. Get all tasks belonging to a project
  125. :param project_id: The id of the project holding the tasks
  126. :type project_id: str
  127. :return: List of tasks
  128. """
  129. cursor = db.cursor()
  130. query = ("SELECT * FROM tasks WHERE projectid = \"" + projectid + "\"")
  131. cursor.execute(query)
  132. tasks = cursor.fetchall()
  133. cursor.close
  134. return tasks
  135. def set_task_file(taskid, filename):
  136. cursor = db.cursor()
  137. query = ("INSERT INTO task_files (taskid, filename) VALUES (\"" +
  138. taskid + "\", \"" + filename + "\")")
  139. print(query)
  140. cursor.execute(query)
  141. db.commit()
  142. cursor.close()
  143. def get_task_files(taskid):
  144. cursor = db.cursor()
  145. query = ("SELECT filename FROM task_files WHERE taskid = \"" + str(taskid) + "\"")
  146. cursor.execute(query)
  147. filenames = cursor.fetchall()
  148. print(query)
  149. print(filenames)
  150. cursor.close
  151. return filenames
  152. def set_projects_user(projectid, userid, read_permission="TRUE",
  153. write_permission="NULL", modify_permission="NULL"):
  154. cursor = db.cursor()
  155. query = ("INSERT INTO projects_users VALUES (\"" + projectid + "\", \"" +
  156. userid + "\", " + read_permission + ", " +
  157. write_permission + ", " + modify_permission + ")")
  158. cursor.execute(query)
  159. db.commit()
  160. cursor.close()