|
1 | 1 | from datetime import datetime |
| 2 | +import json |
2 | 3 | import logging |
3 | 4 | import os |
4 | 5 | import aiohttp |
@@ -43,8 +44,39 @@ def __init__(self): |
43 | 44 | self._cache = {} |
44 | 45 | self._cache_timestamps = {} |
45 | 46 | self._projects_loaded = False # ์๋ฒ ์์ ํ ํ ๋ฒ๋ง ๋ก๋ |
| 47 | + self.default_periods = self._load_default_periods() |
46 | 48 |
|
47 | 49 | self._initialized = True |
| 50 | + |
| 51 | + def _load_default_periods(self) -> Dict[str, Dict[str, str]]: |
| 52 | + """์์ฆ๋ณ ๊ธฐ๋ณธ ๊ธฐ๊ฐ ์ ๋ณด ๋ก๋""" |
| 53 | + env_path = os.getenv("DEFAULT_PERIODS_FILE") |
| 54 | + default_file_path = env_path or os.path.join( |
| 55 | + os.path.dirname(__file__), |
| 56 | + "..", |
| 57 | + "..", |
| 58 | + "config", |
| 59 | + "default_periods.json", |
| 60 | + ) |
| 61 | + try: |
| 62 | + with open(default_file_path, "r", encoding="utf-8") as f: |
| 63 | + return json.load(f) |
| 64 | + except FileNotFoundError: |
| 65 | + logger.warning( |
| 66 | + "๊ธฐ๋ณธ ๊ธฐ๊ฐ ํ์ผ์ด ์กด์ฌํ์ง ์์ต๋๋ค.", |
| 67 | + extra={"path": default_file_path}, |
| 68 | + ) |
| 69 | + except json.JSONDecodeError: |
| 70 | + logger.warning( |
| 71 | + "๊ธฐ๋ณธ ๊ธฐ๊ฐ ํ์ผ ํ์ฑ์ ์คํจํ์ต๋๋ค.", |
| 72 | + extra={"path": default_file_path}, |
| 73 | + ) |
| 74 | + except Exception: |
| 75 | + logger.exception( |
| 76 | + "๊ธฐ๋ณธ ๊ธฐ๊ฐ ํ์ผ ๋ก๋ ์ค ์ ์ ์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.", |
| 77 | + extra={"path": default_file_path}, |
| 78 | + ) |
| 79 | + return {} |
48 | 80 |
|
49 | 81 | async def verify_user_participation( |
50 | 82 | self, |
@@ -132,29 +164,86 @@ async def verify_user_participation( |
132 | 164 | if user_role is None: |
133 | 165 | raise NotEligibleError(f"์๋ฃ ๋ช
๋จ์ ์กด์ฌํ์ง ์์ต๋๋ค. ๐ฅฒ\n๋์ค์ฝ๋๋ฅผ ํตํด ์ง๋ฌธ๊ฒ์ํ์ ๋ฌธ์ํด์ฃผ์ธ์.") |
134 | 166 |
|
135 | | - period = project.get("properties", {}).get("๊ธฐ๊ฐ", {}).get("date", {}) |
| 167 | + study_status = properties.get("๋จ๊ณ", {}).get("select", {}) |
| 168 | + period_raw = project.get("properties", {}).get("๊ธฐ๊ฐ", {}).get("date", {}) or {} |
136 | 169 |
|
137 | | - if not period: |
| 170 | + if not study_status: |
138 | 171 | raise SystemError( |
139 | | - "ํ๋ก์ ํธ ๊ธฐ๊ฐ ์ ๋ณด๊ฐ ์์ต๋๋ค. " |
140 | | - "์คํฐ๋ ๋น๋์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์." |
| 172 | + "์คํฐ๋์ ์๋ฃ ์ ๋ณด๊ฐ ์์ต๋๋ค.\n๋์ค์ฝ๋๋ฅผ ํตํด ์ง๋ฌธ๊ฒ์ํ์ ๋ฌธ์ํด์ฃผ์ธ์." |
141 | 173 | ) |
142 | 174 |
|
143 | | - # ์ข
๋ฃ์ผ ๊ฒ์ฆ |
144 | | - if not period.get('end'): |
145 | | - raise SystemError( |
146 | | - "ํ๋ก์ ํธ ์ข
๋ฃ์ผ ์ ๋ณด๊ฐ ์์ต๋๋ค. " |
147 | | - "์คํฐ๋ ๋น๋์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์." |
| 175 | + if study_status.get("name") != "์๋ฃ": |
| 176 | + raise NotEligibleError( |
| 177 | + "์๋ฃ์ฆ์ ์คํฐ๋๊ฐ ์๋ฃ๋ ์ดํ ๋ฐ๊ธ ๊ฐ๋ฅํฉ๋๋ค.\n๋์ค์ฝ๋๋ฅผ ํตํด ์ง๋ฌธ๊ฒ์ํ์ ๋ฌธ์ํด์ฃผ์ธ์." |
148 | 178 | ) |
149 | 179 |
|
150 | | - end_date_str = period['end'] |
151 | | - end_date = datetime.strptime(end_date_str, "%Y-%m-%d").date() |
152 | | - today = datetime.now().date() |
| 180 | + fallback_period = self.default_periods.get(str(season), {}) |
| 181 | + raw_start = period_raw.get("start") |
| 182 | + raw_end = period_raw.get("end") |
153 | 183 |
|
154 | | - if today < end_date: |
155 | | - raise NotEligibleError( |
156 | | - f"์๋ฃ์ฆ์ ์๋ฃ ํ ๋ฐ๊ธ ๊ฐ๋ฅํฉ๋๋ค." |
| 184 | + # 1) ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ด๊ธฐํ |
| 185 | + period = { |
| 186 | + "start": fallback_period.get("start"), |
| 187 | + "end": fallback_period.get("end"), |
| 188 | + } |
| 189 | + |
| 190 | + # 2) Notion ๊ฐ์ด ์์ ํ๋ฉด ๋ฎ์ด์ฐ๊ธฐ |
| 191 | + if raw_start and raw_end: |
| 192 | + period["start"] = raw_start |
| 193 | + period["end"] = raw_end |
| 194 | + # 3) ํ์ชฝ๋ง ์์ ๋ |
| 195 | + elif raw_start or raw_end: |
| 196 | + if fallback_period: |
| 197 | + logger.warning( |
| 198 | + "์คํฐ๋ ๊ธฐ๊ฐ์ด ํ์ชฝ๋ง ์์ด ๊ธฐ๋ณธ ๊ธฐ๊ฐ์ผ๋ก ๋์ฒดํฉ๋๋ค.", |
| 199 | + extra={ |
| 200 | + "user_name": user_name, |
| 201 | + "season": season, |
| 202 | + "course_name": course_name, |
| 203 | + "project_id": project.get("id"), |
| 204 | + "raw_start": raw_start, |
| 205 | + "raw_end": raw_end, |
| 206 | + "fallback_start": period["start"], |
| 207 | + "fallback_end": period["end"], |
| 208 | + }, |
| 209 | + ) |
| 210 | + else: |
| 211 | + message = ( |
| 212 | + "์คํฐ๋ ๊ธฐ๊ฐ ์ ๋ณด๊ฐ ์์ต๋๋ค. " |
| 213 | + f"(season={season}, course={course_name}) " |
| 214 | + "config/default_periods.json์ ๊ธฐ๋ณธ ๊ธฐ๊ฐ์ ํ์ธํด์ฃผ์ธ์." |
| 215 | + ) |
| 216 | + logger.error( |
| 217 | + message, |
| 218 | + extra={ |
| 219 | + "user_name": user_name, |
| 220 | + "season": season, |
| 221 | + "course_name": course_name, |
| 222 | + "project_id": project.get("id"), |
| 223 | + "raw_start": raw_start, |
| 224 | + "raw_end": raw_end, |
| 225 | + "default_period_found": bool(fallback_period), |
| 226 | + }, |
| 227 | + ) |
| 228 | + raise SystemError(message) |
| 229 | + # 4) Notion ๊ฐ๋ ์๊ณ ๊ธฐ๋ณธ๊ฐ๋ ์์ |
| 230 | + elif not period["start"] and not period["end"]: |
| 231 | + message = ( |
| 232 | + "์คํฐ๋ ๊ธฐ๊ฐ ์ ๋ณด๊ฐ ์์ต๋๋ค. " |
| 233 | + f"(season={season}, course={course_name}) " |
| 234 | + "config/default_periods.json์ ๊ธฐ๋ณธ ๊ธฐ๊ฐ์ ํ์ธํด์ฃผ์ธ์." |
| 235 | + ) |
| 236 | + logger.error( |
| 237 | + message, |
| 238 | + extra={ |
| 239 | + "user_name": user_name, |
| 240 | + "season": season, |
| 241 | + "course_name": course_name, |
| 242 | + "project_id": project.get("id"), |
| 243 | + "default_period_found": bool(fallback_period), |
| 244 | + }, |
157 | 245 | ) |
| 246 | + raise SystemError(message) |
158 | 247 |
|
159 | 248 | logger.info( |
160 | 249 | "์ฌ์ฉ์ ๊ฒ์ฆ ์ฑ๊ณต", |
@@ -183,7 +272,7 @@ async def verify_user_participation( |
183 | 272 | "course_name": course_name, |
184 | 273 | }, |
185 | 274 | ) |
186 | | - raise Exception("ํด๋น ํ๋ก์ ํธ๊ฐ ๊ฒ์๋์ง ์์ต๋๋ค. \nDevFactory๋ก ์ฐ๋ฝ๋ถํ๋๋ฆฝ๋๋ค.") |
| 275 | + raise Exception("ํด๋น ํ๋ก์ ํธ๊ฐ ๊ฒ์๋์ง ์์ต๋๋ค. \n๋์ค์ฝ๋๋ฅผ ํตํด ์ง๋ฌธ๊ฒ์ํ์ ๋ฌธ์ํด์ฃผ์ธ์.") |
187 | 276 | except Exception as e: |
188 | 277 | raise e |
189 | 278 |
|
|
0 commit comments