1
1
import abc
2
2
from dataclasses import dataclass , field
3
- from typing import List , Callable
3
+ from typing import List , Callable , Tuple
4
+ from datetime import datetime
4
5
5
6
from azure .cosmos import PartitionKey
6
7
from flask_restplus ._http import HTTPStatus
@@ -74,6 +75,35 @@ def create_sql_ignore_id_condition(id: str):
74
75
else :
75
76
return "AND c.id!=@ignore_id"
76
77
78
+ @staticmethod
79
+ def create_sql_date_range_filter (custom_args : dict ) -> str :
80
+ if 'start_date' and 'end_date' in custom_args :
81
+ return """
82
+ ((c.start_date BETWEEN @start_date AND @end_date) OR
83
+ (c.end_date BETWEEN @start_date AND @end_date))
84
+ """
85
+ else :
86
+ return ''
87
+
88
+ def find_all (self , partition_key_value : str , conditions : dict , custom_args : dict ):
89
+ custom_conditions = []
90
+ custom_conditions .append (
91
+ self .create_sql_date_range_filter (custom_args )
92
+ )
93
+
94
+ custom_params = [
95
+ {"name" : "@start_date" , "value" : custom_args .get ('start_date' )},
96
+ {"name" : "@end_date" , "value" : custom_args .get ('end_date' )},
97
+ ]
98
+
99
+ return CosmosDBRepository .find_all (
100
+ self ,
101
+ partition_key_value = partition_key_value ,
102
+ conditions = conditions ,
103
+ custom_conditions = custom_conditions ,
104
+ custom_params = custom_params
105
+ )
106
+
77
107
def on_create (self , new_item_data : dict ):
78
108
CosmosDBRepository .on_create (self , new_item_data )
79
109
@@ -157,6 +187,32 @@ def validate_data(self, data):
157
187
description = "There is another time entry in that date range" )
158
188
159
189
190
+ def get_last_day_of_month (year : int , month : int ) -> int :
191
+ from calendar import monthrange
192
+ return monthrange (year = year , month = month )[1 ]
193
+
194
+
195
+ def get_current_year () -> int :
196
+ return datetime .now ().year
197
+
198
+
199
+ def get_current_month () -> int :
200
+ return datetime .now ().month
201
+
202
+
203
+ def get_date_range_of_month (
204
+ year : int ,
205
+ month : int
206
+ ) -> Tuple [datetime , datetime ]:
207
+ first_day_of_month = 1
208
+ start_date = datetime (year = year , month = month , day = first_day_of_month )
209
+
210
+ # TODO : fix bound as this would exclude the last day
211
+ last_day_of_month = get_last_day_of_month (year = year , month = month )
212
+ end_date = datetime (year = year , month = month , day = last_day_of_month )
213
+ return start_date , end_date
214
+
215
+
160
216
class TimeEntriesCosmosDBDao (TimeEntriesDao , CosmosDBDao ):
161
217
def __init__ (self , repository ):
162
218
CosmosDBDao .__init__ (self , repository )
@@ -170,8 +226,29 @@ def check_whether_current_user_owns_item(cls, data: dict):
170
226
171
227
def get_all (self , conditions : dict = {}) -> list :
172
228
conditions .update ({"owner_id" : self .current_user_id ()})
229
+
230
+ if 'month' and 'year' in conditions :
231
+ month = int (conditions .get ("month" ))
232
+ year = int (conditions .get ("year" ))
233
+ conditions .pop ('month' )
234
+ conditions .pop ('year' )
235
+ elif 'month' in conditions :
236
+ month = int (conditions .get ("month" ))
237
+ year = get_current_year ()
238
+ conditions .pop ('month' )
239
+ else :
240
+ month = get_current_month ()
241
+ year = get_current_year ()
242
+
243
+ start_date , end_date = get_date_range_of_month (year , month )
244
+
245
+ custom_args = {
246
+ 'start_date' : start_date .isoformat (),
247
+ 'end_date' : end_date .isoformat ()
248
+ }
173
249
return self .repository .find_all (partition_key_value = self .partition_key_value ,
174
- conditions = conditions )
250
+ conditions = conditions ,
251
+ custom_args = custom_args )
175
252
176
253
def get (self , id ):
177
254
return self .repository .find (id ,
0 commit comments