Coverage for src/resources/group_resource.py : 97%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from flask import request
2from flask_restx import Resource
3from flask_jwt_extended import jwt_required, get_jwt_identity
5from src.utils import validation_error
7from src.service import GroupService
8from src.dto import GroupDto
9from src.schemas import GroupCreateSchema, GroupAddMemberSchema
11api = GroupDto.api
12creation_success = GroupDto.creation_success
13added_success = GroupDto.added_success
15group_create_schema = GroupCreateSchema()
16group_add_member_schema = GroupAddMemberSchema()
19@api.route("")
20class GroupsResource(Resource):
21 """ Groups endpoint
22 """
24 group_creation = GroupDto.group_creation
26 @api.doc(
27 "Create a group",
28 responses={
29 201: ("Group data successfully created", creation_success),
30 401: ("Authentication required"),
31 }
32 )
33 @jwt_required
34 @api.expect(group_creation, validate=True)
35 def post(self):
36 """ Create group using name and current user """
37 user_uuid = get_jwt_identity()
39 # Grab the json data
40 group_data = request.get_json()
42 # Validate data
43 if (errors := group_create_schema.validate(group_data)):
44 return validation_error(False, errors)
46 return GroupService.create_group(group_data["name"], user_uuid)
49@api.route("/<int:group_id>")
50class GroupResource(Resource):
51 """ Group endpoint
52 """
54 @api.doc(
55 "Get a specific group",
56 responses={
57 201: ("Group data successfully sended", creation_success),
58 401: ("Authentication required"),
59 }
60 )
61 @jwt_required
62 def get(self, group_id):
63 """ Get a specific group """
64 return GroupService.get_group_data(group_id)
66 @api.doc(
67 "Leave group (if current user is the group owner, this group will be deleted)",
68 responses={
69 204: ("Successfully leaving"),
70 401: ("Authentication required"),
71 404: ("Group not found!"),
72 }
73 )
74 @jwt_required
75 def delete(self, group_id):
76 """ Leave group (if current user is the group owner, this group will be deleted) """
77 user_uuid = get_jwt_identity()
79 return GroupService.leave_group(group_id, user_uuid)
82@api.route("/<int:group_id>/invitations")
83class GroupInvitationsResource(Resource):
84 """ Group invitations endpoint
85 """
87 group_add_member = GroupDto.group_add_member
89 @api.doc(
90 "Invite user to a group",
91 responses={
92 201: ("Member successfully invited to the group", added_success),
93 401: ("Authentication required"),
94 403: ("Unable to invite a member to a not owned group!"),
95 404: ("Group or Member not found!"),
96 }
97 )
98 @jwt_required
99 @api.expect(group_add_member, validate=True)
100 def post(self, group_id):
101 """ Invite member to a group """
102 user_uuid = get_jwt_identity()
104 # Grab the json data
105 group_data = request.get_json()
107 # Validate data
108 if (errors := group_add_member_schema.validate(group_data)):
109 return validation_error(False, errors)
111 return GroupService.invite_user(group_id, group_data["uuid"], user_uuid)
114@api.route("/<int:group_id>/invitations/<string:user_uuid>")
115class GroupInvitationsUResource(Resource):
116 """ Group invitations endpoint
117 """
119 @api.doc(
120 "Accept invitation to a group",
121 responses={
122 201: ("Member successfully added to the group", added_success),
123 401: ("Authentication required"),
124 403: ("Unable to accept an invitation that is not intended to you"),
125 404: ("Group or User or Invitation not found!"),
126 }
127 )
128 @jwt_required
129 def put(self, group_id, user_uuid):
130 """ Accept invitation to a group """
131 current_user_uuid = get_jwt_identity()
133 return GroupService.accept_invitation(group_id, user_uuid, current_user_uuid)
135 @api.doc(
136 "Delete invitation to a group",
137 responses={
138 201: ("Invitation to the group successfully deleted"),
139 401: ("Authentication required"),
140 403: ("Unable to delete an invitation that is not intended to you if you are not the group owner"),
141 404: ("Group or User or Invitation not found!"),
142 }
143 )
144 @jwt_required
145 def delete(self, group_id, user_uuid):
146 """ Delete invitation to a group """
147 current_user_uuid = get_jwt_identity()
149 return GroupService.delete_invitation(group_id, user_uuid, current_user_uuid)