Coverage for mycalendar / views.py: 12%

75 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2025-12-21 13:48 +0900

1'''View module. 

2 

3Copyright ycookjp 

4 

5https://github.com/ycookjp/ 

6 

7''' 

8from .models import MyCalendar 

9 

10from django.http import HttpRequest, HttpResponse, JsonResponse 

11from django.forms.models import model_to_dict 

12from django.core.exceptions import ValidationError 

13#from django.shortcuts import render 

14 

15import calendar 

16import json 

17 

18# Create your views here. 

19 

20def _check_date(year:int, month:int, day:int=None, errmsg:str=None): 

21 '''年月日の範囲チェックを実行する。 

22 

23 以下のチェックを実行する。 

24  

25 * 月:1以上12以下であること 

26 * 日:1以上パラメータで指定した年月の最終日以下であること 

27  

28 Args: 

29 year (int): 年 

30 month (int): 月 

31 day (int): 日 

32 errmsg (str): エラーメッセージ 

33  

34 Raises: 

35 ValidationError: 

36 * 月が1より小さい、または12より大きい場合 

37 * 日が1より小さい、または月の最終日より大きい場合 

38  

39 ''' 

40 if not errmsg: 

41 errmsg = 'Input date error.' 

42 if month < 1 or month > 12: 

43 raise ValidationError(errmsg) 

44 elif day!=None and (day < 1 or day > calendar.monthrange(year, month)[1]): 

45 raise ValidationError(errmsg) 

46 

47def _create_monthly(user:str, year:int, month:int): 

48 '''1ヶ月のカレンダー情報を登録する。 

49  

50 指定されたユーザー、年月の1日から最終日までのカレンダー情報を登録する。 

51  

52 Args: 

53 user (str): ユーザー名 

54 year (int): 年 

55 month (int): 月 

56  

57 Returns: 

58 登録したレコード数 

59  

60 ''' 

61 # 月の最終日を取得する 

62 last_day:int = calendar.monthrange(year, month)[1] 

63 # 指定された月の1日から最終日までのカレンダー情報を登録する。 

64 for index in range(1, last_day+1): 

65 instance = MyCalendar(user=user, year=year, month=month, day=index) 

66 instance.save() 

67 

68 return last_day 

69 

70def get_daily(request:HttpRequest, user:str , year:int, month:int, day:int): 

71 '''GET get_daily/<str:user\>/<int:year\>/<int:month\>/<int:day\>/ 

72  

73 年月日を指定してカレンダー情報を取得する。 

74  

75 * データベースから指定されたユーザー、年月日の MyCalendar オブジェクトを 

76 取得する。 

77 * データベースに指定されたユーザー、年月日の MyCalendar オブジェクトが存在 

78 しない場合は、ユーザーのその月の MyCalendar オブジェクトをデータベースに 

79 作成した上で、指定された年月日の MyCalendar オブジェクトを返す。 

80  

81 Args: 

82 request (HttpRequest): リクエストオブジェクト 

83 user (str): ユーザー名 

84 year (int): 年 

85 month (int): 月 

86 day(int): 日 

87  

88 Returns: 

89 JsonResponse: 指定された年月日の MyCalendar オブジェクトの内容を設定 

90 したJSONオブジェクトレスポンス・ボディに設定して返す。 

91  

92 { 

93 "id": ID, 

94 "user": "<user_name>", 

95 "year": <year>, 

96 "month": <month>, 

97 "day": <day>, 

98 "note": "<note>" 

99 } 

100  

101 Raises: 

102 ValidationError 

103  

104 * URLパラメータの month の値が 1 より小さい、または 12 より大きい場合 

105 * URLパラメータの day の値が 1 より大きい、または month で指定された 

106 月の最終日より大きい場合 

107  

108 ''' 

109 # 日付の入力チェック 

110 _check_date(year, month, day) 

111 

112 # 指定されたユーザー、年月日のMyCalendarオブジェクトを検索する 

113 cals = MyCalendar.objects.filter(user=user, year=year, month=month, day=day) 

114 

115 if len(cals) > 0: 

116 cal = cals[0] 

117 else: 

118 # 年月日を指定してMyCalendarが取得できない場合は1ヶ月分のMyCalendar 

119 # オブジェクトをデータベースに登録する 

120 _create_monthly(user, year, month) 

121 # データベースから引数で指定されたユーザー、年月日のMyClass 

122 # オブジェクトを取得する 

123 cal = MyCalendar.objects.get(user=user, year=year, month=month, day=day) 

124 

125 # モデルをDictionaryに変換する 

126 response_data = model_to_dict(cal) 

127 # JSON形式で応答を返す 

128 return JsonResponse(response_data) 

129 

130def get_monthly(request:HttpRequest, user:str, year:int, month:int): 

131 '''GET get_monthly/<str:user\>/<int:year\>/<int:month\>/ 

132  

133 年月を指定してカレンダー情報を取得する。 

134  

135 Args: 

136 request (HttpRequest): リクエストオブジェクト 

137 user (str): ユーザー名 

138 year (int): 年 

139 month (int): 月 

140  

141 Returns: 

142 JsonResponse: 指定された年月のカレンダー情報の配列を設定したJSON 

143 オブジェクトをレスポンス・ボディに設定して返す。 

144  

145 [ 

146 { 

147 "id": ID, 

148 "user": "<user_name>", 

149 "year": <year>, 

150 "month": <month>, 

151 "day": <day>, 

152 "note": "<note>" 

153 }, 

154 ... 

155 ] 

156  

157 Raises: 

158 ValidationError 

159  

160 * URLパラメータの month の値が 1 より小さい、または 12 より大きい場合 

161  

162 ''' 

163 # 日付の入力チェック 

164 _check_date(year, month, errmsg='Input month error.') 

165 

166 # 指定されたユーザー、年月の1日のカレンダー情報を取得する 

167 cals = MyCalendar.objects.filter(user=user, year=year, month=month, day=1) 

168 

169 # 指定された月のカレンダー情報が存在しない場合は、その月のデータを登録する。 

170 if len(cals) == 0: 

171 _create_monthly(user, year, month) 

172 

173 # 指定された年月のカレンダー情報を取得する 

174 cals = MyCalendar.objects.filter(user=user, year=year, month=month).order_by('day') 

175 

176 # モデルをDictionaryに変換する 

177 response_data = [] 

178 for cal in cals: 

179 response_data.append(model_to_dict(cal)) 

180 # JSON形式で応答を返す。 

181 return JsonResponse(response_data, safe=False) 

182 

183def save_daily(request:HttpRequest, user:str, year:int, month:int, day:int): 

184 '''PUT save_daily/<str:user\>/<int:year\>/<int:month\>/<int:day\>/ 

185  

186 年月日を指定してカレンダー情報を登録・更新する。 

187  

188 * リクエスト・ボディ 

189  

190 { 

191 "id": ID, 

192 "user": "<user_name>", 

193 "year": <year>, 

194 "month": <month>, 

195 "day": <day>, 

196 "note": "<note>" 

197 } 

198  

199 * 登録(レコードのインサート)の場合は、「id」は指定しない(項目なし 

200 または値が空) 

201 * URLパラメータで指定された年月のカレンダー情報がDBに登録されていない 

202 場合は、その年月の1ヶ月分のカレンダー情報をDBに登録した後に、 

203 リクエスト・ボディに設定されたカレンダー情報をDBに更新する 

204  

205 Args: 

206 request (HttpRequest): リクエストオブジェクト 

207 user (str): ユーザー名 

208 year (int): 年 

209 month (int): 月 

210 day(int): 日 

211  

212 Raises: 

213 ValidationError: 

214  

215 * URLパラメータの user、year、month、day がリクエスト・ボディの 

216 JSONオブジェクトのそれぞれの同名の属性と一致しないものがある場合 

217 * URLパラメータの month の値が 1 より小さい、または 12 より大きい場合 

218 * URLパラメータの day の値が 1 より大きい、または month で指定された 

219 月の最終日より大きい場合 

220  

221 ''' 

222 # 日付の入力チェック 

223 _check_date(year, month, day) 

224 

225 # リクエスト・ボディをJSONオブジェクトで取得する 

226 json_data = json.loads(request.body) 

227 # 取得したJSONオブジェクトをMyCalendarクラスのインスタンスに変換する 

228 mycalendar:MyCalendar = MyCalendar.deserialize(json_data) 

229 

230 # URLパラメータが登録データと一致することを確認する 

231 if mycalendar.user != user or mycalendar.year != year \ 

232 or mycalendar.month != month or mycalendar.day != day: 

233 raise ValidationError('URL parameter(s) and request body not matched.') 

234 

235 # URLパラメータで指定された年月の1日のカレンダー情報を取得して存在 

236 # しなければその月のカレンダー情報を登録する 

237 cals = MyCalendar.objects.filter(user=user, year=year, month=month, day=1) 

238 if len(cals) == 0: 

239 _create_monthly(user, year, month) 

240 

241 # リクエスト・ボディにidが指定されていない場合はDBから取得する 

242 if mycalendar.id == None: 

243 cal = MyCalendar.objects.get(user=user, year=year, month=month, day=day) 

244 mycalendar.id = cal.id 

245 # リクエストされた登録データをデータベース辞登録する 

246 mycalendar.save() 

247 

248 return HttpResponse() 

249 

250def save_monthly(request:HttpRequest, user:str, year:int, month:int): 

251 '''PUT save_monthly/<str:user\>/<int:year\>/<int:month\>/ 

252  

253 年月を指定してカレンダー情報を登録・更新する。 

254  

255 * リクエスト・ボディ 

256  

257 [ 

258 { 

259 "id": ID, 

260 "user": "<user_name>", 

261 "year": <year>, 

262 "month": <month>, 

263 "day": <day>, 

264 "note": "<note>" 

265 }, 

266 ... 

267 ] 

268  

269 * 登録(レコードのインサート)の場合は、「id」は指定しない(項目なし 

270 または値が空) 

271 * URLパラメータで指定された年月のカレンダー情報がDBに登録されていない 

272 場合は、その年月の1ヶ月分のカレンダー情報をDBに登録した後に、 

273 リクエスト・ボディに設定されたカレンダー情報をDBに更新する 

274  

275 Args: 

276 request (HttpRequest): リクエストオブジェクト 

277 user (str): ユーザー名 

278 year (int): 年 

279 month (int): 月 

280  

281 Raises: 

282 ValidationError: 

283  

284 * URLパラメータのmonthの値が1より小さい、または12より大きい場合 

285 * リクエスト・ボディのディクショナリ配列の個々の要素について、 

286 monthの値が1より小さい、または12より大きい場合 

287 * リクエスト・ボディのディクショナリ配列の個々の要素について、 

288 dayの値が1より小さい、または月の最終日より大きい場合 

289  

290 ''' 

291 # 日付の入力チェック 

292 _check_date(year, month, errmsg='Input month error.') 

293 

294 # リクエスト・ボディをJSONオブジェクトで取得する 

295 json_datas = json.loads(request.body) 

296 # 取得したJSONオブジェクトをMyCalendarクラスのインスタンスの配列に変換して 

297 # 入力チェックする 

298 request_data = [] 

299 for json_data in json_datas: 

300 mycalendar:MyCalendar = MyCalendar.deserialize(json_data) 

301 # URLパラメータの user、year、month と一致していることを確認する 

302 if mycalendar.user != user or mycalendar.year != year or mycalendar.month != month: 

303 raise ValidationError('URL parameter(s) and request body not matched.') 

304 # 日付のの入力チェック 

305 _check_date(mycalendar.year, mycalendar.month, mycalendar.day) 

306 # 変換したMyCalendarのインスタンスを配列に追加する 

307 request_data.append(mycalendar) 

308 

309 # URLパラメータで指定された年月の1日のカレンダー情報を取得して存在 

310 # しなければその月のカレンダー情報を登録する 

311 cals = MyCalendar.objects.filter(user=user, year=year, month=month, day=1) 

312 if len(cals) == 0: 

313 _create_monthly(user, year, month) 

314 

315 # idを取得するためにURLパラメータで指定された年月のレコードを取得する 

316 cals = MyCalendar.objects.filter(user=user, year=year, month=month).order_by('day') 

317 # リクエスト・ボディのMyCalenderインスタンス配列の要素純に処理する 

318 for data in request_data: 

319 # 更新対象のデータにidが設定されていない場合は年月のレコードのidを設定する 

320 if data.id == None: 

321 data.id = cals[data.day - 1].id 

322 # MyCalanderインスタンスの内容をDBに更新する 

323 data.save() 

324 

325 return HttpResponse() 

326 

327def delete_monthly(request:HttpRequest, user:str, year:int, month:int): 

328 '''DELETE delete_monthly/<str:user\>/<int:year\>/<int:month\>/ 

329  

330 年月を指定してカレンダー情報を削除する。 

331  

332 Args: 

333 request (HttpRequest): リクエストオブジェクト 

334 user (str): ユーザー名 

335 year (int): 年 

336 month (int): 月 

337  

338 Raises: 

339 ValidationError: 

340  

341 * URLパラメータのmonthの値が1より小さい、または12より大きい場合 

342  

343 ''' 

344 # 日付の入力チェック 

345 _check_date(year, month, errmsg='Input month error.') 

346 

347 # URLパラメータで指定された年月のカレンダー情報を削除する 

348 MyCalendar.objects.filter(user=user, year=year, month=month).delete() 

349 

350 return HttpResponse()