diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..756f6bd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea/ +/tmp/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..05875bf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM python:3.10-slim + +# Set the working directory +WORKDIR /app + +# Copy the requirements file +COPY requirements.txt . + +# Install Python dependencies +RUN pip install -r requirements.txt + +# Copy the application code +COPY main.py . + +# Expose the application port +EXPOSE 8000 + +# Run the application +CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] diff --git a/build-local.sh b/build-local.sh new file mode 100644 index 0000000..922dc7d --- /dev/null +++ b/build-local.sh @@ -0,0 +1 @@ +docker build -t local/icalendar-maker . diff --git a/docker-run.sh b/docker-run.sh new file mode 100644 index 0000000..22c64b6 --- /dev/null +++ b/docker-run.sh @@ -0,0 +1,5 @@ +docker stop icalendar-maker +docker rm icalendar-maker +docker network create icalendar +# Ce programme sert à lancer le icalendar-maker dans un docker localement pour tester +docker run -p 8000:8000 --name icalendar-maker --volume=${PWD}/tmp:/tmp --network icalendar local/icalendar-maker diff --git a/install.sh b/install.sh new file mode 100644 index 0000000..498c20a --- /dev/null +++ b/install.sh @@ -0,0 +1,2 @@ +#!/bin/sh +pip install -r requirements.txt diff --git a/main.py b/main.py new file mode 100644 index 0000000..c3fcdcb --- /dev/null +++ b/main.py @@ -0,0 +1,72 @@ +import os +from typing import List + +import fastapi +import uvicorn +from ics import Calendar, Event +import pydantic +from datetime import datetime +from fastapi.responses import FileResponse + + +# pydantic model for icalendar events +class EventData(pydantic.BaseModel): + name: str + description: str + location: str + start: datetime + end: datetime + recurrence: str + attendees: List[str] + organizer: str + uid: str + url: str + + +app = fastapi.FastAPI() + + +@app.get("/") +def read_root(): + return {"Application": "ICalendar Maker"} + + +# this function returns an icalendar file +@app.get("/generer/icalendar") +async def generer_icalendar(event_data: EventData): + cal = Calendar() + event = Event() + event.name = event_data.name + event.begin = event_data.start + event.end = event_data.end + event.description = event_data.description + event.location = event_data.location + event.recurrence = event_data.recurrence + event.uid = event_data.uid + event.attendees = event_data.attendees + event.organizer = event_data.organizer + event.url = event_data.url + cal.events.add(event) + + # filename is uid and current timestamp + filename = event.uid + datetime.now().strftime("%Y%m%dT%H%M%S") + ".ics" + + # create a tmp folder if it doesn't exist + if not os.path.exists("/tmp"): + os.mkdir("/tmp") + print("tmp folder created") + + # write a file named calendar.ics containing file_content + with open(f"/tmp/{filename}", "w") as f: + f.write(cal.serialize()) + f.close() + print(f"file {filename} created") + + # return a pydantic FileResponse + return FileResponse(filename=f"{filename}", + path=f"/tmp/{filename}", + media_type="text/calendar") + + +if __name__ == "__main__": + uvicorn.run(app, host="0.0.0.0", port=8000) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..0f1e862 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,10 @@ +anyio==3.6.2 +click==8.1.3 +fastapi==0.89.1 +h11==0.14.0 +ics==0.7.2 +pydantic==1.10.4 +pytz==2022.7.1 +sniffio==1.3.0 +starlette==0.22.0 +uvicorn==0.20.0 diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..76ea2df --- /dev/null +++ b/run.sh @@ -0,0 +1 @@ +uvicorn main:app --reload \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..cba3bcc --- /dev/null +++ b/setup.py @@ -0,0 +1,12 @@ +from setuptools import setup + +setup( + name='icalendar-maker', + version='', + packages=[''], + url='', + license='', + author='francois', + author_email='', + description='' +) diff --git a/test.http b/test.http new file mode 100644 index 0000000..2266b9c --- /dev/null +++ b/test.http @@ -0,0 +1,19 @@ +# Test your FastAPI endpoints + +GET http://127.0.0.1:8000/generer/icalendar +Content-Type: application/json +Accept: text/calendar +Accept-Encoding: gzip, deflate + +{ + "summary": "test", + "description": "test", + "location": "test", + "start": "2024-01-01T00:00:00", + "end": "2024-01-01T01:00:00", + "recurrence": "RRULE:FREQ=DAILY;COUNT=1", + "uid": "test", + "attendees": ["guest1@jevalide.ca", "guest2@jevalide.ca"], + "organizer": "francois@jevalide.ca", + "url": "https://jevalide.ca/reunion" +}