16
16
17
17
class Retriever :
18
18
"""Created when retrieving messages"""
19
+
19
20
@staticmethod
20
21
def retrieve_message_list (page , limit , user , ru_id = None , survey = None , cc = None , ce = None , label = None , descend = True ):
21
22
"""returns list of messages from db"""
22
- conditions = []
23
- status_conditions = []
24
23
25
24
if user .is_respondent :
26
- status_conditions .append (Status .actor == str (user .user_uuid ))
27
- else :
28
- status_conditions .append (Status .actor == constants .BRES_USER )
25
+ return Retriever ._retrieve_message_list_respondent (page , limit , user = user , ru_id = ru_id , survey = survey ,
26
+ cc = cc , ce = ce , label = label , descend = descend )
27
+ return Retriever ._retrieve_message_list_internal (page , limit , ru_id = ru_id , survey = survey ,
28
+ cc = cc , ce = ce , label = label , descend = descend )
29
+
30
+ @staticmethod
31
+ def _retrieve_message_list_respondent (page , limit , user , ru_id , survey , cc , ce , label , descend ):
32
+ """returns list of messages from db"""
33
+ conditions = []
34
+ status_conditions = [Status .actor == str (user .user_uuid )]
29
35
30
36
if label is not None :
31
37
status_conditions .append (Status .label == str (label ))
@@ -46,38 +52,95 @@ def retrieve_message_list(page, limit, user, ru_id=None, survey=None, cc=None, c
46
52
47
53
try :
48
54
t = db .session .query (SecureMessage .msg_id , func .max (Events .date_time ) # pylint:disable=no-member
49
- .label ('max_date' ))\
55
+ .label ('max_date' )) \
50
56
.join (Events ).join (Status ).outerjoin (Actors ) \
51
57
.filter (and_ (* conditions )) \
52
58
.filter (and_ (* status_conditions )) \
53
59
.filter (or_ (Events .event == EventsApi .SENT .value , Events .event == EventsApi .DRAFT_SAVED .value )) \
54
60
.group_by (SecureMessage .msg_id ).subquery ('t' )
55
61
56
62
if descend :
57
- result = SecureMessage .query \
58
- .filter (SecureMessage .msg_id == t .c .msg_id ) \
59
- .order_by (t .c .max_date .desc ()).paginate (page , limit , False )
60
-
63
+ order = t .c .max_date .desc ()
61
64
else :
62
- result = SecureMessage .query \
63
- .filter (SecureMessage .msg_id == t .c .msg_id ) \
64
- .order_by (t .c .max_date .asc ()).paginate (page , limit , False )
65
+ order = t .c .max_date .asc ()
66
+
67
+ result = SecureMessage .query \
68
+ .filter (SecureMessage .msg_id == t .c .msg_id ) \
69
+ .order_by (order ).paginate (page , limit , False )
65
70
66
71
except Exception as e :
67
72
logger .error ('Error retrieving messages from database' , error = e )
68
73
raise InternalServerError (description = "Error retrieving messages from database" )
69
74
70
75
return True , result
71
76
77
+ @staticmethod
78
+ def _retrieve_message_list_internal (page , limit , ru_id , survey , cc , ce , label , descend ):
79
+ """returns list of messages from db"""
80
+ conditions = []
81
+ status_reject_conditions = []
82
+ valid_statuses = []
83
+ actor_conditions = []
84
+
85
+ if label is not None :
86
+ valid_statuses .append (label )
87
+ if label in [Labels .INBOX .value , Labels .ARCHIVE .value , Labels .UNREAD .value ]:
88
+ actor_conditions .append (Actors .sent_from_internal == False ) # NOQA pylint:disable=singleton-comparison
89
+ if label in [Labels .DRAFT .value , Labels .SENT .value ]:
90
+ actor_conditions .append (Actors .sent_from_internal == True ) # NOQA pylint:disable=singleton-comparison
91
+ else :
92
+ status_reject_conditions .append (Labels .DRAFT_INBOX .value )
93
+ valid_statuses = [Labels .INBOX .value , Labels .DRAFT .value ]
94
+ actor_conditions .append (True )
95
+
96
+ if ru_id is not None :
97
+ conditions .append (SecureMessage .ru_id == str (ru_id ))
98
+
99
+ if survey is not None :
100
+ conditions .append (SecureMessage .survey == str (survey ))
101
+
102
+ if cc is not None :
103
+ conditions .append (SecureMessage .collection_case == str (cc ))
104
+
105
+ if ce is not None :
106
+ conditions .append (SecureMessage .collection_exercise == str (ce ))
107
+
108
+ try :
109
+ t = db .session .query (SecureMessage .msg_id , func .max (Events .date_time ) # pylint:disable=no-member ~ below used to obtain not in
110
+ .label ('max_date' )) \
111
+ .join (Events ).join (Status ).outerjoin (Actors ) \
112
+ .filter (and_ (* conditions )) \
113
+ .filter (or_ (* actor_conditions )) \
114
+ .filter (~ Status .label .in_ (status_reject_conditions )) \
115
+ .filter (Status .label .in_ (valid_statuses )) \
116
+ .filter (or_ (Events .event == EventsApi .SENT .value , Events .event == EventsApi .DRAFT_SAVED .value )) \
117
+ .group_by (SecureMessage .msg_id ).subquery ('t' )
118
+
119
+ if descend :
120
+ order = t .c .max_date .desc ()
121
+ else :
122
+ order = t .c .max_date .asc ()
123
+
124
+ result = SecureMessage .query \
125
+ .filter (SecureMessage .msg_id == t .c .msg_id ) \
126
+ .order_by (order ).paginate (page , limit , False )
127
+
128
+ except Exception as e :
129
+ logger .exception ('Error retrieving messages from database' , error = e )
130
+ raise InternalServerError (description = "Error retrieving messages from database" )
131
+
132
+ return True , result
133
+
72
134
@staticmethod
73
135
def unread_message_count (user ):
74
136
"""Count users unread messages"""
75
137
status_conditions = []
76
138
status_conditions .append (Status .actor == str (user .user_uuid ))
77
139
try :
78
- result = SecureMessage .query .join (Status ).filter (and_ (* status_conditions )).filter (Status .label == 'UNREAD' ).count ()
140
+ result = SecureMessage .query .join (Status ).filter (and_ (* status_conditions )).filter (
141
+ Status .label == 'UNREAD' ).count ()
79
142
except Exception as e :
80
- logger .error ('Error retrieving count of unread messages from database' , error = e )
143
+ logger .exception ('Error retrieving count of unread messages from database' , error = e )
81
144
raise InternalServerError (description = "Error retrieving count of unread messages from database" )
82
145
return result
83
146
@@ -86,33 +149,37 @@ def retrieve_thread_list(page, limit, user):
86
149
"""returns list of threads from db"""
87
150
status_conditions = []
88
151
conditions = []
152
+ actor_conditions = []
89
153
90
154
if user .is_respondent :
91
- status_conditions .append (Status .actor == str (user .user_uuid ))
155
+ actor_conditions .append (Status .actor == str (user .user_uuid ))
92
156
else :
93
- status_conditions .append (Status .actor == constants .BRES_USER )
157
+ actor_conditions .append (Status .actor == str (user .user_uuid ))
158
+ actor_conditions .append (Status .actor == constants .BRES_USER )
159
+ actor_conditions .append (Status .actor == constants .NON_SPECIFIC_INTERNAL_USER )
94
160
95
161
status_conditions .append (Status .label != Labels .DRAFT_INBOX .value )
96
162
97
163
try :
98
- t = db .session .query (SecureMessage .thread_id , func .max (Events .date_time ) # pylint:disable=no-member
99
- .label ('max_date ' )) \
164
+ t = db .session .query (SecureMessage .thread_id , func .max (Events .id ) # pylint:disable=no-member
165
+ .label ('max_id ' )) \
100
166
.join (Events ).join (Status ) \
101
167
.filter (and_ (* status_conditions )) \
168
+ .filter (or_ (* actor_conditions )) \
102
169
.filter (or_ (Events .event == EventsApi .SENT .value , Events .event == EventsApi .DRAFT_SAVED .value )) \
103
170
.group_by (SecureMessage .thread_id ).subquery ('t' )
104
171
105
172
conditions .append (SecureMessage .thread_id == t .c .thread_id )
106
- conditions .append (Events .date_time == t .c .max_date )
173
+ conditions .append (Events .id == t .c .max_id )
107
174
108
175
result = SecureMessage .query .join (Events ).join (Status ) \
109
176
.filter (or_ (Events .event == EventsApi .SENT .value , Events .event == EventsApi .DRAFT_SAVED .value )) \
110
177
.filter (and_ (* conditions )) \
111
178
.filter (and_ (* status_conditions )) \
112
- .order_by (t .c .max_date .desc ()).paginate (page , limit , False )
179
+ .order_by (t .c .max_id .desc ()).paginate (page , limit , False )
113
180
114
181
except Exception as e :
115
- logger .error ('Error retrieving messages from database' , error = e )
182
+ logger .exception ('Error retrieving messages from database' , error = e )
116
183
raise InternalServerError (description = "Error retrieving messages from database" )
117
184
118
185
return True , result
@@ -137,11 +204,14 @@ def retrieve_message(message_id, user):
137
204
def retrieve_thread (thread_id , user , page , limit ):
138
205
"""returns paginated list of messages for thread id"""
139
206
status_conditions = []
207
+ actor_conditions = []
140
208
141
209
if user .is_respondent :
142
- status_conditions .append (Status .actor == str (user .user_uuid ))
210
+ actor_conditions .append (Status .actor == str (user .user_uuid ))
143
211
else :
144
- status_conditions .append (Status .actor == constants .BRES_USER )
212
+ actor_conditions .append (Status .actor == str (user .user_uuid ))
213
+ actor_conditions .append (Status .actor == constants .BRES_USER )
214
+ actor_conditions .append (Status .actor == constants .NON_SPECIFIC_INTERNAL_USER )
145
215
146
216
status_conditions .append (Status .label != Labels .DRAFT_INBOX .value )
147
217
@@ -150,6 +220,7 @@ def retrieve_thread(thread_id, user, page, limit):
150
220
result = SecureMessage .query .join (Events ).join (Status ) \
151
221
.filter (SecureMessage .thread_id == thread_id ) \
152
222
.filter (and_ (* status_conditions )) \
223
+ .filter (or_ (* actor_conditions )) \
153
224
.filter (or_ (Events .event == EventsApi .SENT .value , Events .event == EventsApi .DRAFT_SAVED .value )) \
154
225
.order_by (Events .date_time .desc ()).paginate (page , limit , False )
155
226
@@ -168,7 +239,7 @@ def retrieve_draft(message_id, user):
168
239
"""returns single draft from db"""
169
240
170
241
try :
171
- result = SecureMessage .query .filter (SecureMessage .msg_id == message_id )\
242
+ result = SecureMessage .query .filter (SecureMessage .msg_id == message_id ) \
172
243
.filter (SecureMessage .statuses .any (Status .label == Labels .DRAFT .value )).first ()
173
244
if result is None :
174
245
logger .error ('Draft does not exist' , message_id = message_id )
@@ -203,7 +274,7 @@ def check_db_connection():
203
274
def check_msg_id_is_a_draft (draft_id , user ):
204
275
"""Check msg_id is that of a valid draft and return true/false if no ID is present"""
205
276
try :
206
- result = SecureMessage .query .filter (SecureMessage .msg_id == draft_id )\
277
+ result = SecureMessage .query .filter (SecureMessage .msg_id == draft_id ) \
207
278
.filter (SecureMessage .statuses .any (Status .label == Labels .DRAFT .value )).first ()
208
279
except Exception as e :
209
280
logger .error ('Error retrieving message from database' , error = e )
0 commit comments