project_to_user.py 2.16 KB
Newer Older
1
2
3
from re import T
from flask import Blueprint, request, jsonify
from backend.database_config.database import DB
4
from backend.models.user_model import User_project, User
5
from backend.models.project_model import Project
6

7
8
9

user_project = Blueprint('user_project', __name__)

10
11
@user_project.route('/user_project', methods=['GET'])
def get_all_assignments():
12
  assignments = User_project.query.all()
13

14
  assignments_list = [{"username" : x.username,
15
                    "project_id" : x.project_id, 
16
17
                    "id" : x.id} for x in assignments]
  return jsonify(assignments_list)
18
19
 

20
21
@user_project.route('/user_project/project/<project_id>', methods=['GET'])
def get_users_for_project(project_id):
22
23
24
25
26
  entries = User_project.query.filter_by(project_id=project_id)
  assignments_list = [{"username" : x.username,
                    "project_id" : x.project_id, 
                    "id" : x.id} for x in entries]
  return jsonify(assignments_list)
27

28
29
30
31
32
33
34
35
@user_project.route('/user_project/project_task/<project_id>', methods=['GET'])
def get_users_for_project_for_tasks(project_id):
  entries = User_project.query.filter_by(project_id=project_id)
  assigned_users = [{"value" : query.username,
                     "label" : query.firstname + " " + query.surname}
                     for query in (User.query.get(x.username) for x in entries)]
  return jsonify(assigned_users)

36
37
38
@user_project.route('/user_project/username/<username>', methods=['GET'])
def get_projects_for_user(username):
  entries = User_project.query.filter_by(username=username)
39
40
41
42
43
44
45
  project_list = [{"id" : query.id,
                    "name" : query.name, 
                    "status" : query.status,
                    "description" : query.description,
                    "tag" : query.tag,
                    "location" : query.location,
                    "files" : query.files} for query in (Project.query.get(x.project_id) for x in entries)]
46
47
  return jsonify(project_list)

48
49
50
51
52
53
@user_project.route('/user_project', methods=['POST'])
def assign_project_to_user():
  username, project_id = (request.json['username'], request.json['project_id'])
  entry = User_project(username=username, project_id=project_id)
  DB.add(entry)
  return ''