Skip to content

OPTIONS for book apt #557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: set up python
uses: actions/setup-python@v1
with:
python-version: '3.9.21'
python-version: '3.9.22'

- name: install dependencies
run: |
Expand Down
127 changes: 86 additions & 41 deletions tcsocket/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


async def startup(app: web.Application):
settings: Settings = app['settings']
settings: Settings = app["settings"]
redis = await create_pool(settings.redis_settings)
app.update(
pg_engine=await create_engine(settings.pg_dsn),
Expand All @@ -35,65 +35,110 @@ async def startup(app: web.Application):


async def cleanup(app: web.Application):
app['pg_engine'].close()
await app['pg_engine'].wait_closed()
app['redis'].close()
await app['redis'].wait_closed()
await app['session'].close()
app["pg_engine"].close()
await app["pg_engine"].wait_closed()
app["redis"].close()
await app["redis"].wait_closed()
await app["session"].close()


async def _options_handler(request):
return web.Response(
headers={
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
}
)


async def _book_appointment_wrapper(request):
"""Wrapper for book_appointment that adds CORS headers to the response."""
response = await book_appointment(request)
response.headers.update(
{
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
}
)
return response


def setup_routes(app):
app.router.add_get(r'/', index, name='index')
app.router.add_get(r'/robots.txt', robots_txt, name='robots-txt')
app.router.add_get(r'/favicon.ico', favicon, name='favicon')
app.router.add_post(r'/companies/create', company_create, name='company-create')
app.router.add_get(r'/companies', company_list, name='company-list')

app.router.add_get(r'/{company}/options', company_options, name='company-options')

# to work with tutorcruncher websockets
app.router.add_post(r'/{company}/webhook/options', company_update, name='company-update')
app.router.add_post(r'/{company}/webhook/contractor', contractor_set, name='webhook-contractor')
app.router.add_post(r'/{company}/webhook/contractor/mass', contractor_set_mass, name='webhook-contractor-mass')
app.router.add_post(r'/{company}/webhook/clear-enquiry', clear_enquiry, name='webhook-clear-enquiry')
app.router.add_post(r'/{company}/webhook/appointments/{id:\d+}', appointment_webhook, name='webhook-appointment')
app.router.add_get(r"/", index, name="index")
app.router.add_get(r"/robots.txt", robots_txt, name="robots-txt")
app.router.add_get(r"/favicon.ico", favicon, name="favicon")
app.router.add_post(r"/companies/create", company_create, name="company-create")
app.router.add_get(r"/companies", company_list, name="company-list")

app.router.add_get(r"/{company}/options", company_options, name="company-options")

app.router.add_post(r"/{company}/webhook/options", company_update, name="company-update")
app.router.add_post(r"/{company}/webhook/contractor", contractor_set, name="webhook-contractor")
app.router.add_post(
r"/{company}/webhook/contractor/mass",
contractor_set_mass,
name="webhook-contractor-mass",
)
app.router.add_post(r"/{company}/webhook/clear-enquiry", clear_enquiry, name="webhook-clear-enquiry")
app.router.add_post(
r"/{company}/webhook/appointments/{id:\d+}",
appointment_webhook,
name="webhook-appointment",
)
app.router.add_post(
r'/{company}/webhook/appointments/mass', appointment_webhook_mass, name='webhook-appointment-mass'
r"/{company}/webhook/appointments/mass",
appointment_webhook_mass,
name="webhook-appointment-mass",
)
app.router.add_delete(
r'/{company}/webhook/appointments/{id:\d+}', appointment_webhook_delete, name='webhook-appointment-delete'
r"/{company}/webhook/appointments/{id:\d+}",
appointment_webhook_delete,
name="webhook-appointment-delete",
)
app.router.add_delete(
r'/{company}/webhook/appointments/clear', appointment_webhook_clear, name='webhook-appointment-clear'
r"/{company}/webhook/appointments/clear",
appointment_webhook_clear,
name="webhook-appointment-clear",
)

app.router.add_get(r'/{company}/contractors', contractor_list, name='contractor-list')
app.router.add_get(r'/{company}/contractors/{id:\d+}', contractor_get, name='contractor-get')
app.router.add_route(r'*', '/{company}/enquiry', enquiry, name='enquiry')
app.router.add_get(r'/{company}/subjects', subject_list, name='subject-list')
app.router.add_get(r'/{company}/qual-levels', qual_level_list, name='qual-level-list')
app.router.add_get(r'/{company}/labels', labels_list, name='labels')

app.router.add_get(r'/{company}/appointments', appointment_list, name='appointment-list')
app.router.add_get(r'/{company}/services', service_list, name='service-list')
app.router.add_get(r'/{company}/check-client', check_client, name='check-client')
app.router.add_post(r'/{company}/book-appointment', book_appointment, name='book-appointment')
app.router.add_get(r"/{company}/contractors", contractor_list, name="contractor-list")
app.router.add_get(r"/{company}/contractors/{id:\d+}", contractor_get, name="contractor-get")
app.router.add_route(r"*", "/{company}/enquiry", enquiry, name="enquiry")
app.router.add_get(r"/{company}/subjects", subject_list, name="subject-list")
app.router.add_get(r"/{company}/qual-levels", qual_level_list, name="qual-level-list")
app.router.add_get(r"/{company}/labels", labels_list, name="labels")

app.router.add_get(r"/{company}/appointments", appointment_list, name="appointment-list")
app.router.add_get(r"/{company}/services", service_list, name="service-list")
app.router.add_get(r"/{company}/check-client", check_client, name="check-client")
app.router.add_options(
r"/{company}/book-appointment",
_options_handler,
name="book-appointment-options",
)
app.router.add_post(
r"/{company}/book-appointment",
_book_appointment_wrapper,
name="book-appointment",
)


def create_app(loop, *, settings: Settings = None):
app = web.Application(middlewares=middleware)
settings = settings or Settings()
app['settings'] = settings
app["settings"] = settings

ctx = dict(
COMMIT=os.getenv('COMMIT', '-'),
RELEASE_DATE=os.getenv('RELEASE_DATE', '-'),
SERVER_NAME=os.getenv('SERVER_NAME', '-'),
COMMIT=os.getenv("COMMIT", "-"),
RELEASE_DATE=os.getenv("RELEASE_DATE", "-"),
SERVER_NAME=os.getenv("SERVER_NAME", "-"),
)
index_html = (THIS_DIR / 'index.html').read_text()
index_html = (THIS_DIR / "index.html").read_text()
for key, value in ctx.items():
index_html = re.sub(r'\{\{ ?%s ?\}\}' % key, escape(value), index_html)
app['index_html'] = index_html
index_html = re.sub(r"\{\{ ?%s ?\}\}" % key, escape(value), index_html)
app["index_html"] = index_html
app.on_startup.append(startup)
app.on_cleanup.append(cleanup)

Expand Down
Loading
Loading