Update app.py
Browse files
app.py
CHANGED
|
@@ -300,122 +300,124 @@ def main():
|
|
| 300 |
|
| 301 |
# Task Entry
|
| 302 |
elif menu == "π₯ Task Entry":
|
| 303 |
-
with st.form(key="task_form", clear_on_submit=True):
|
| 304 |
st.subheader("β Add New Task")
|
| 305 |
col1, col2 = st.columns(2)
|
| 306 |
-
|
| 307 |
-
# Left Column
|
| 308 |
-
with col1:
|
| 309 |
-
task = st.text_area("π Task Description", height=100)
|
| 310 |
-
task_type = st.selectbox(
|
| 311 |
-
"π¦ Task Type",
|
| 312 |
-
["Design", "Procurement", "Construction", "Testing", "Other"]
|
| 313 |
-
)
|
| 314 |
-
|
| 315 |
-
# Right Column
|
| 316 |
-
with col2:
|
| 317 |
-
projects = [p.id for p in db.collection("projects").stream()] + ["Add New Project"]
|
| 318 |
-
project = st.selectbox("ποΈ Project", projects)
|
| 319 |
-
if project == "Add New Project":
|
| 320 |
-
project = st.text_input("β¨ New Project Name")
|
| 321 |
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
end_date = st.date_input(
|
| 329 |
-
"π Repeat Until",
|
| 330 |
-
min_value=date + relativedelta(days=1),
|
| 331 |
-
help="Recurrence end date (inclusive)"
|
| 332 |
)
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
if submitted:
|
| 345 |
-
if not task.strip():
|
| 346 |
-
st.error("β Task description cannot be empty!")
|
| 347 |
-
st.stop()
|
| 348 |
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
if recurrence != "None"
|
| 352 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
st.stop()
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
})
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
current_date += relativedelta(weeks=1)
|
| 386 |
-
elif recurrence == "Monthly":
|
| 387 |
-
current_date += relativedelta(months=1)
|
| 388 |
-
if current_date <= end_date:
|
| 389 |
-
dates.append(current_date)
|
| 390 |
-
|
| 391 |
-
# Batch write to Firestore
|
| 392 |
-
batch = db.batch()
|
| 393 |
-
for idx, task_date in enumerate(dates):
|
| 394 |
-
task_ref = db.collection("tasks").document()
|
| 395 |
-
batch.set(task_ref, {
|
| 396 |
-
"user": st.session_state.email,
|
| 397 |
-
"task": task.strip(),
|
| 398 |
-
"type": task_type,
|
| 399 |
-
"project": project,
|
| 400 |
-
"status": status,
|
| 401 |
-
"date": str(task_date),
|
| 402 |
-
"recurrence": {
|
| 403 |
-
"type": recurrence,
|
| 404 |
-
"original_date": str(date),
|
| 405 |
-
"sequence": idx + 1
|
| 406 |
-
},
|
| 407 |
-
"attachments": attachments,
|
| 408 |
-
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 409 |
-
})
|
| 410 |
-
batch.commit()
|
| 411 |
-
|
| 412 |
-
st.success(f"β
Created {len(dates)} {'task' if len(dates) == 1 else 'tasks'} with {len(attachments)} attachments!")
|
| 413 |
-
st.rerun()
|
| 414 |
-
|
| 415 |
-
except cloudinary.exceptions.Error as e:
|
| 416 |
-
st.error(f"β Cloudinary upload failed: {str(e)}")
|
| 417 |
-
except Exception as e:
|
| 418 |
-
st.error(f"β Error: {str(e)}")
|
| 419 |
|
| 420 |
# Task Explorer (Updated Date Filter)
|
| 421 |
elif menu == "π Task Explorer":
|
|
|
|
| 300 |
|
| 301 |
# Task Entry
|
| 302 |
elif menu == "π₯ Task Entry":
|
| 303 |
+
with st.form(key="task_form", clear_on_submit=True): # Form starts here
|
| 304 |
st.subheader("β Add New Task")
|
| 305 |
col1, col2 = st.columns(2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
+
# Left Column
|
| 308 |
+
with col1:
|
| 309 |
+
task = st.text_area("π Task Description", height=100)
|
| 310 |
+
task_type = st.selectbox(
|
| 311 |
+
"π¦ Task Type",
|
| 312 |
+
["Design", "Procurement", "Construction", "Testing", "Other"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
)
|
| 314 |
+
|
| 315 |
+
# Right Column
|
| 316 |
+
with col2:
|
| 317 |
+
projects = [p.id for p in db.collection("projects").stream()] + ["Add New Project"]
|
| 318 |
+
project = st.selectbox("ποΈ Project", projects)
|
| 319 |
+
if project == "Add New Project":
|
| 320 |
+
project = st.text_input("β¨ New Project Name")
|
| 321 |
+
|
| 322 |
+
status = st.selectbox("π Status", ["Pending", "In Progress", "Completed"])
|
| 323 |
+
date = st.date_input("π
Due Date", min_value=datetime.today())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
+
recurrence = st.selectbox("π Repeat", ["None", "Daily", "Weekly", "Monthly"])
|
| 326 |
+
end_date = None
|
| 327 |
+
if recurrence != "None":
|
| 328 |
+
end_date = st.date_input(
|
| 329 |
+
"π Repeat Until",
|
| 330 |
+
min_value=date + relativedelta(days=1),
|
| 331 |
+
help="Recurrence end date (inclusive)"
|
| 332 |
+
)
|
| 333 |
+
|
| 334 |
+
# File Upload Section
|
| 335 |
+
uploaded_files = st.file_uploader(
|
| 336 |
+
"π Attachments",
|
| 337 |
+
type=["pdf", "docx", "xlsx", "png", "jpg", "jpeg"],
|
| 338 |
+
accept_multiple_files=True,
|
| 339 |
+
help="Upload relevant files (max 10MB each)"
|
| 340 |
+
)
|
| 341 |
+
|
| 342 |
+
# Submit Button - MUST be inside the form context
|
| 343 |
+
submitted = st.form_submit_button("πΎ Save Task", use_container_width=True)
|
| 344 |
+
|
| 345 |
+
# Form Submission Handling
|
| 346 |
+
if submitted:
|
| 347 |
+
if not task.strip():
|
| 348 |
+
st.error("β Task description cannot be empty!")
|
| 349 |
st.stop()
|
| 350 |
+
|
| 351 |
+
try:
|
| 352 |
+
# Validate recurrence
|
| 353 |
+
if recurrence != "None" and (not end_date or end_date <= date):
|
| 354 |
+
st.error("β End date must be after initial due date")
|
| 355 |
+
st.stop()
|
| 356 |
+
|
| 357 |
+
# Process attachments
|
| 358 |
+
attachments = []
|
| 359 |
+
if uploaded_files:
|
| 360 |
+
for file in uploaded_files:
|
| 361 |
+
if file.size > 10 * 1024 * 1024: # 10MB limit
|
| 362 |
+
st.error(f"β File {file.name} exceeds 10MB limit")
|
| 363 |
+
st.stop()
|
| 364 |
+
|
| 365 |
+
# Upload to Cloudinary
|
| 366 |
+
result = cloudinary.uploader.upload(
|
| 367 |
+
file,
|
| 368 |
+
folder=f"attachments/{st.session_state.user_uid}/",
|
| 369 |
+
resource_type="auto"
|
| 370 |
+
)
|
| 371 |
|
| 372 |
+
attachments.append({
|
| 373 |
+
"name": file.name,
|
| 374 |
+
"url": result['secure_url'],
|
| 375 |
+
"type": file.type,
|
| 376 |
+
"size": file.size
|
| 377 |
+
})
|
| 378 |
+
|
| 379 |
+
# Generate recurring dates
|
| 380 |
+
dates = [date]
|
| 381 |
+
if recurrence != "None":
|
| 382 |
+
current_date = date
|
| 383 |
+
while current_date < end_date:
|
| 384 |
+
if recurrence == "Daily":
|
| 385 |
+
current_date += relativedelta(days=1)
|
| 386 |
+
elif recurrence == "Weekly":
|
| 387 |
+
current_date += relativedelta(weeks=1)
|
| 388 |
+
elif recurrence == "Monthly":
|
| 389 |
+
current_date += relativedelta(months=1)
|
| 390 |
+
if current_date <= end_date:
|
| 391 |
+
dates.append(current_date)
|
| 392 |
+
|
| 393 |
+
# Batch write to Firestore
|
| 394 |
+
batch = db.batch()
|
| 395 |
+
for idx, task_date in enumerate(dates):
|
| 396 |
+
task_ref = db.collection("tasks").document()
|
| 397 |
+
batch.set(task_ref, {
|
| 398 |
+
"user": st.session_state.email,
|
| 399 |
+
"task": task.strip(),
|
| 400 |
+
"type": task_type,
|
| 401 |
+
"project": project,
|
| 402 |
+
"status": status,
|
| 403 |
+
"date": str(task_date),
|
| 404 |
+
"recurrence": {
|
| 405 |
+
"type": recurrence,
|
| 406 |
+
"original_date": str(date),
|
| 407 |
+
"sequence": idx + 1
|
| 408 |
+
},
|
| 409 |
+
"attachments": attachments,
|
| 410 |
+
"created_at": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 411 |
})
|
| 412 |
+
batch.commit()
|
| 413 |
+
|
| 414 |
+
st.success(f"β
Created {len(dates)} {'task' if len(dates) == 1 else 'tasks'} with {len(attachments)} attachments!")
|
| 415 |
+
st.rerun()
|
| 416 |
+
|
| 417 |
+
except cloudinary.exceptions.Error as e:
|
| 418 |
+
st.error(f"β Cloudinary upload failed: {str(e)}")
|
| 419 |
+
except Exception as e:
|
| 420 |
+
st.error(f"β Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
# Task Explorer (Updated Date Filter)
|
| 423 |
elif menu == "π Task Explorer":
|