summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--db.sqlite3bin0 -> 147456 bytes
-rw-r--r--korabo/__init__.py0
-rw-r--r--korabo/__pycache__/__init__.cpython-312.pycbin0 -> 152 bytes
-rw-r--r--korabo/__pycache__/settings.cpython-312.pycbin0 -> 2627 bytes
-rw-r--r--korabo/__pycache__/urls.cpython-312.pycbin0 -> 1320 bytes
-rw-r--r--korabo/__pycache__/wsgi.cpython-312.pycbin0 -> 638 bytes
-rw-r--r--korabo/asgi.py16
-rw-r--r--korabo/settings.py127
-rw-r--r--korabo/template_filters.py6
-rw-r--r--korabo/urls.py26
-rw-r--r--korabo/wsgi.py16
-rwxr-xr-xmanage.py22
-rw-r--r--web/__init__.py0
-rw-r--r--web/__pycache__/__init__.cpython-312.pycbin0 -> 149 bytes
-rw-r--r--web/__pycache__/admin.cpython-312.pycbin0 -> 447 bytes
-rw-r--r--web/__pycache__/apps.cpython-312.pycbin0 -> 449 bytes
-rw-r--r--web/__pycache__/models.cpython-312.pycbin0 -> 2680 bytes
-rw-r--r--web/__pycache__/views.cpython-312.pycbin0 -> 5430 bytes
-rw-r--r--web/admin.py6
-rw-r--r--web/apps.py6
-rw-r--r--web/migrations/0001_initial.py46
-rw-r--r--web/migrations/0002_alter_availability_user.py21
-rw-r--r--web/migrations/__init__.py0
-rw-r--r--web/migrations/__pycache__/0001_initial.cpython-312.pycbin0 -> 2163 bytes
-rw-r--r--web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pycbin0 -> 1107 bytes
-rw-r--r--web/migrations/__pycache__/__init__.cpython-312.pycbin0 -> 160 bytes
-rw-r--r--web/models.py38
-rw-r--r--web/templates/base.html38
-rw-r--r--web/templates/event.html91
-rw-r--r--web/templates/index.html26
-rw-r--r--web/templates/registration/login.html1
-rw-r--r--web/tests.py3
-rw-r--r--web/views.py137
33 files changed, 626 insertions, 0 deletions
diff --git a/db.sqlite3 b/db.sqlite3
new file mode 100644
index 0000000..c754ade
--- /dev/null
+++ b/db.sqlite3
Binary files differ
diff --git a/korabo/__init__.py b/korabo/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/korabo/__init__.py
diff --git a/korabo/__pycache__/__init__.cpython-312.pyc b/korabo/__pycache__/__init__.cpython-312.pyc
new file mode 100644
index 0000000..ff53bd8
--- /dev/null
+++ b/korabo/__pycache__/__init__.cpython-312.pyc
Binary files differ
diff --git a/korabo/__pycache__/settings.cpython-312.pyc b/korabo/__pycache__/settings.cpython-312.pyc
new file mode 100644
index 0000000..d48b1e7
--- /dev/null
+++ b/korabo/__pycache__/settings.cpython-312.pyc
Binary files differ
diff --git a/korabo/__pycache__/urls.cpython-312.pyc b/korabo/__pycache__/urls.cpython-312.pyc
new file mode 100644
index 0000000..66d8688
--- /dev/null
+++ b/korabo/__pycache__/urls.cpython-312.pyc
Binary files differ
diff --git a/korabo/__pycache__/wsgi.cpython-312.pyc b/korabo/__pycache__/wsgi.cpython-312.pyc
new file mode 100644
index 0000000..dd689e4
--- /dev/null
+++ b/korabo/__pycache__/wsgi.cpython-312.pyc
Binary files differ
diff --git a/korabo/asgi.py b/korabo/asgi.py
new file mode 100644
index 0000000..78c0d6c
--- /dev/null
+++ b/korabo/asgi.py
@@ -0,0 +1,16 @@
+"""
+ASGI config for korabo project.
+
+It exposes the ASGI callable as a module-level variable named ``application``.
+
+For more information on this file, see
+https://docs.djangoproject.com/en/5.1/howto/deployment/asgi/
+"""
+
+import os
+
+from django.core.asgi import get_asgi_application
+
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'korabo.settings')
+
+application = get_asgi_application()
diff --git a/korabo/settings.py b/korabo/settings.py
new file mode 100644
index 0000000..cd4f4e7
--- /dev/null
+++ b/korabo/settings.py
@@ -0,0 +1,127 @@
+"""
+Django settings for korabo project.
+
+Generated by 'django-admin startproject' using Django 5.1.2.
+
+For more information on this file, see
+https://docs.djangoproject.com/en/5.1/topics/settings/
+
+For the full list of settings and their values, see
+https://docs.djangoproject.com/en/5.1/ref/settings/
+"""
+
+from pathlib import Path
+
+# Build paths inside the project like this: BASE_DIR / 'subdir'.
+BASE_DIR = Path(__file__).resolve().parent.parent
+
+
+# Quick-start development settings - unsuitable for production
+# See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/
+
+# SECURITY WARNING: keep the secret key used in production secret!
+SECRET_KEY = 'django-insecure-2zbb@9m=%exl38+&(1*4lp5dz(kg__p9uqba0junq096h6i^ni'
+
+# SECURITY WARNING: don't run with debug turned on in production!
+DEBUG = True
+
+ALLOWED_HOSTS = []
+
+
+# Application definition
+
+INSTALLED_APPS = [
+ 'web',
+ 'django.contrib.admin',
+ 'django.contrib.auth',
+ 'django.contrib.contenttypes',
+ 'django.contrib.sessions',
+ 'django.contrib.messages',
+ 'django.contrib.staticfiles',
+ 'bootstrap5'
+]
+
+MIDDLEWARE = [
+ 'django.middleware.security.SecurityMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.common.CommonMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+ 'django.middleware.clickjacking.XFrameOptionsMiddleware',
+]
+
+ROOT_URLCONF = 'korabo.urls'
+
+TEMPLATES = [
+ {
+ 'BACKEND': 'django.template.backends.django.DjangoTemplates',
+ 'DIRS': ['web/templates'],
+ 'APP_DIRS': True,
+ 'OPTIONS': {
+ 'context_processors': [
+ 'django.template.context_processors.debug',
+ 'django.template.context_processors.request',
+ 'django.contrib.auth.context_processors.auth',
+ 'django.contrib.messages.context_processors.messages',
+ ],
+ },
+ },
+]
+
+WSGI_APPLICATION = 'korabo.wsgi.application'
+
+
+# Database
+# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
+
+DATABASES = {
+ 'default': {
+ 'ENGINE': 'django.db.backends.sqlite3',
+ 'NAME': BASE_DIR / 'db.sqlite3',
+ }
+}
+
+
+# Password validation
+# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
+
+AUTH_PASSWORD_VALIDATORS = [
+ {
+ 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
+ },
+ {
+ 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
+ },
+ {
+ 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
+ },
+ {
+ 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
+ },
+]
+
+
+# Internationalization
+# https://docs.djangoproject.com/en/5.1/topics/i18n/
+
+LANGUAGE_CODE = 'en-us'
+
+TIME_ZONE = 'UTC'
+
+USE_I18N = True
+
+USE_TZ = True
+
+
+# Static files (CSS, JavaScript, Images)
+# https://docs.djangoproject.com/en/5.1/howto/static-files/
+
+STATIC_URL = 'static/'
+
+# Default primary key field type
+# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
+
+DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
+LOGIN_REDIRECT_URL = '/'
+LOGOUT_REDIRECT_URL = '/'
diff --git a/korabo/template_filters.py b/korabo/template_filters.py
new file mode 100644
index 0000000..c3a3af8
--- /dev/null
+++ b/korabo/template_filters.py
@@ -0,0 +1,6 @@
+from django.template.defaulttags import register
+
+
+@register.filter
+def get_item(dictionary, key):
+ return dictionary.get(key) \ No newline at end of file
diff --git a/korabo/urls.py b/korabo/urls.py
new file mode 100644
index 0000000..fbbfded
--- /dev/null
+++ b/korabo/urls.py
@@ -0,0 +1,26 @@
+"""
+URL configuration for korabo project.
+
+The `urlpatterns` list routes URLs to views. For more information please see:
+ https://docs.djangoproject.com/en/5.1/topics/http/urls/
+Examples:
+Function views
+ 1. Add an import: from my_app import views
+ 2. Add a URL to urlpatterns: path('', views.home, name='home')
+Class-based views
+ 1. Add an import: from other_app.views import Home
+ 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
+Including another URLconf
+ 1. Import the include() function: from django.urls import include, path
+ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
+"""
+from django.contrib import admin
+from django.urls import path, include
+from web import views
+
+urlpatterns = [
+ path("accounts/", include("django.contrib.auth.urls")),
+ path('admin/', admin.site.urls),
+ path("", views.index, name="index"),
+ path("event/<int:event_id>", views.event, name="event"),
+]
diff --git a/korabo/wsgi.py b/korabo/wsgi.py
new file mode 100644
index 0000000..c132c75
--- /dev/null
+++ b/korabo/wsgi.py
@@ -0,0 +1,16 @@
+"""
+WSGI config for korabo project.
+
+It exposes the WSGI callable as a module-level variable named ``application``.
+
+For more information on this file, see
+https://docs.djangoproject.com/en/5.1/howto/deployment/wsgi/
+"""
+
+import os
+
+from django.core.wsgi import get_wsgi_application
+
+os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'korabo.settings')
+
+application = get_wsgi_application()
diff --git a/manage.py b/manage.py
new file mode 100755
index 0000000..e797df2
--- /dev/null
+++ b/manage.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+"""Django's command-line utility for administrative tasks."""
+import os
+import sys
+
+
+def main():
+ """Run administrative tasks."""
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'korabo.settings')
+ try:
+ from django.core.management import execute_from_command_line
+ except ImportError as exc:
+ raise ImportError(
+ "Couldn't import Django. Are you sure it's installed and "
+ "available on your PYTHONPATH environment variable? Did you "
+ "forget to activate a virtual environment?"
+ ) from exc
+ execute_from_command_line(sys.argv)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/web/__init__.py b/web/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/web/__init__.py
diff --git a/web/__pycache__/__init__.cpython-312.pyc b/web/__pycache__/__init__.cpython-312.pyc
new file mode 100644
index 0000000..dfdbe91
--- /dev/null
+++ b/web/__pycache__/__init__.cpython-312.pyc
Binary files differ
diff --git a/web/__pycache__/admin.cpython-312.pyc b/web/__pycache__/admin.cpython-312.pyc
new file mode 100644
index 0000000..9b5bd4a
--- /dev/null
+++ b/web/__pycache__/admin.cpython-312.pyc
Binary files differ
diff --git a/web/__pycache__/apps.cpython-312.pyc b/web/__pycache__/apps.cpython-312.pyc
new file mode 100644
index 0000000..90ab549
--- /dev/null
+++ b/web/__pycache__/apps.cpython-312.pyc
Binary files differ
diff --git a/web/__pycache__/models.cpython-312.pyc b/web/__pycache__/models.cpython-312.pyc
new file mode 100644
index 0000000..47d02cc
--- /dev/null
+++ b/web/__pycache__/models.cpython-312.pyc
Binary files differ
diff --git a/web/__pycache__/views.cpython-312.pyc b/web/__pycache__/views.cpython-312.pyc
new file mode 100644
index 0000000..4591619
--- /dev/null
+++ b/web/__pycache__/views.cpython-312.pyc
Binary files differ
diff --git a/web/admin.py b/web/admin.py
new file mode 100644
index 0000000..9c6efc5
--- /dev/null
+++ b/web/admin.py
@@ -0,0 +1,6 @@
+from django.contrib import admin
+from web.models import Event, EventAdmin, Availability
+
+
+admin.site.register(Event, EventAdmin)
+admin.site.register(Availability)
diff --git a/web/apps.py b/web/apps.py
new file mode 100644
index 0000000..682e923
--- /dev/null
+++ b/web/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class WebConfig(AppConfig):
+ default_auto_field = 'django.db.models.BigAutoField'
+ name = 'web'
diff --git a/web/migrations/0001_initial.py b/web/migrations/0001_initial.py
new file mode 100644
index 0000000..5107a35
--- /dev/null
+++ b/web/migrations/0001_initial.py
@@ -0,0 +1,46 @@
+# Generated by Django 5.1.2 on 2024-10-22 20:31
+
+import django.db.models.deletion
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = [
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Event',
+ fields=[
+ ('created_at', models.DateTimeField(auto_now_add=True, null=True)),
+ ('updated_at', models.DateTimeField(auto_now=True, null=True)),
+ ('active', models.BooleanField(default=True)),
+ ('id', models.AutoField(primary_key=True, serialize=False)),
+ ('name', models.TextField()),
+ ('start_date', models.DateTimeField()),
+ ('end_date', models.DateTimeField()),
+ ('participants', models.IntegerField()),
+ ],
+ options={
+ 'db_table': 'events',
+ },
+ ),
+ migrations.CreateModel(
+ name='Availability',
+ fields=[
+ ('created_at', models.DateTimeField(auto_now_add=True, null=True)),
+ ('updated_at', models.DateTimeField(auto_now=True, null=True)),
+ ('active', models.BooleanField(default=True)),
+ ('id', models.AutoField(primary_key=True, serialize=False)),
+ ('user', models.TextField()),
+ ('time_table', models.JSONField()),
+ ('event_id', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='web.event')),
+ ],
+ options={
+ 'db_table': 'availabilities',
+ },
+ ),
+ ]
diff --git a/web/migrations/0002_alter_availability_user.py b/web/migrations/0002_alter_availability_user.py
new file mode 100644
index 0000000..4fdf65b
--- /dev/null
+++ b/web/migrations/0002_alter_availability_user.py
@@ -0,0 +1,21 @@
+# Generated by Django 4.2.16 on 2024-10-24 17:59
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ('web', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='availability',
+ name='user',
+ field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
+ ),
+ ]
diff --git a/web/migrations/__init__.py b/web/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/web/migrations/__init__.py
diff --git a/web/migrations/__pycache__/0001_initial.cpython-312.pyc b/web/migrations/__pycache__/0001_initial.cpython-312.pyc
new file mode 100644
index 0000000..f6f6b17
--- /dev/null
+++ b/web/migrations/__pycache__/0001_initial.cpython-312.pyc
Binary files differ
diff --git a/web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc b/web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc
new file mode 100644
index 0000000..70e69e8
--- /dev/null
+++ b/web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc
Binary files differ
diff --git a/web/migrations/__pycache__/__init__.cpython-312.pyc b/web/migrations/__pycache__/__init__.cpython-312.pyc
new file mode 100644
index 0000000..b45851f
--- /dev/null
+++ b/web/migrations/__pycache__/__init__.cpython-312.pyc
Binary files differ
diff --git a/web/models.py b/web/models.py
new file mode 100644
index 0000000..f303552
--- /dev/null
+++ b/web/models.py
@@ -0,0 +1,38 @@
+from django.contrib import admin
+from django.db import models
+from django.contrib.auth.models import User
+
+
+
+class BaseModel(models.Model):
+ created_at = models.DateTimeField(blank=True, null=True, auto_now_add=True)
+ updated_at = models.DateTimeField(blank=True, null=True, auto_now=True)
+ active = models.BooleanField(default=True)
+
+ class Meta:
+ abstract = True
+
+
+class Event(BaseModel):
+ id = models.AutoField(primary_key=True)
+ name = models.TextField(blank=False, null=False)
+ start_date = models.DateTimeField(blank=False, null=False)
+ end_date = models.DateTimeField(blank=False, null=False)
+ participants = models.IntegerField(blank=False)
+
+ class Meta:
+ db_table = 'events'
+
+
+class EventAdmin(admin.ModelAdmin):
+ list_display = ('name', 'start_date', 'end_date', 'active')
+
+
+class Availability(BaseModel):
+ id = models.AutoField(primary_key=True)
+ event_id = models.ForeignKey(Event, on_delete=models.CASCADE)
+ user = models.ForeignKey(User, on_delete=models.CASCADE)
+ time_table = models.JSONField()
+
+ class Meta:
+ db_table = 'availabilities'
diff --git a/web/templates/base.html b/web/templates/base.html
new file mode 100644
index 0000000..d6ba2da
--- /dev/null
+++ b/web/templates/base.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="UTF-8">
+ <title>Korabo</title>
+ {% load static %}
+ {% load bootstrap5 %}
+ {% bootstrap_css %}
+ {% bootstrap_javascript %}
+</head>
+<body style="min-height: 100%">
+
+ <div class="container mt-4" style="min-height:85vh">
+
+ <h1 style="margin-bottom:-8px;">Korabo</h1>
+ <div style="float:left;">
+
+ <small>~make it easy with コラボ~</small>
+ </div>
+ <div style="float:right;">
+ <a href="{% url 'index' %}">home</a> |
+ <a href="{% url 'logout' %}">logout</a>
+
+ </div>
+
+ <div style="clear:both;"></div>
+ <hr>
+
+ <br>
+ {% block content %}{% endblock %}
+ </div>
+
+ <div>
+ <small class="p-5">Version 1.0 | Created By <a href="https://www.domdit.com" target="_blank">Dominic DiTaranto</a> | Questions/Suggestions: me@domdit.com | Last Update 09/22/2024</small>
+ </div>
+
+</body>
+</html> \ No newline at end of file
diff --git a/web/templates/event.html b/web/templates/event.html
new file mode 100644
index 0000000..60535c7
--- /dev/null
+++ b/web/templates/event.html
@@ -0,0 +1,91 @@
+{% extends 'base.html' %}
+
+{% block content %}
+
+<h4>{{event.name}}</h4>
+<form action="/event/{{event.id}}" method="post"> {% csrf_token %}
+
+ <div class="table-responsive-lg">
+ <table class="table">
+ <thead>
+ <tr>
+ <th scope="col">#</th>
+ {% for datetime, day in morning.items %}
+ <td><small>{{ datetime }}</small></td>
+ {% endfor %}
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <th scope="row"><small>Morning</small></th>
+ {% for datetime, day in morning.items %}
+ <td>
+ <input id="{{day.0}}" type="checkbox" name="{{day.0}}"/><br>
+ {{ day.1 }}
+ </td>
+
+ {% endfor %}
+ </tr>
+ <tr>
+ <th scope="row"><small>Afternoon</small></th>
+ {% for datetime, day in noon.items %}
+ <td>
+ <input id="{{day.0}}" type="checkbox" name="{{day.0}}"/><br>
+ {{ day.1 }}
+ </td>
+ {% endfor %}
+ </tr>
+ <tr>
+ <th scope="row"><small>Night</small></th>
+ {% for datetime, day in night.items %}
+ <td>
+ <input id="{{day.0}}" type="checkbox" name="{{day.0}}"/><br>
+ {{ day.1 }}
+ </td>
+ {% endfor %}
+ </tr>
+ </tbody>
+ </table>
+ </div>
+
+ <div style="margin-top:10px;">
+ <div style="float:left">
+ Responses: {{user_responses}} / {{event.participants}}
+ </div>
+ <div style="float:right">
+ <button class="btn btn-success" type="submit">Submit</button>
+ </div>
+ </div>
+
+</form>
+
+<div style="clear:both;"></div>
+<hr>
+<div>
+ <h4>Best Times</h4>
+ {{ no_overlap_message }}
+ {% for day, users in best_days.items %}
+ <div class="row">
+ <div class="col-3" style="border: dotted black 1px;">{{day}}</div>
+ <div class="col-4" style="border: dotted black 1px;">{{users}}</div>
+ </div>
+ {% endfor %}
+</div>
+
+
+
+
+
+<script>
+ active_dates = {{ active_dates|safe }};
+ if (active_dates) {
+ for (let i = 0; i < active_dates.length; i++) {
+ document.getElementById(active_dates[i]).checked = true;
+ }
+ }
+</script>
+
+<br><br>
+
+
+{% endblock %}
diff --git a/web/templates/index.html b/web/templates/index.html
new file mode 100644
index 0000000..5f2150b
--- /dev/null
+++ b/web/templates/index.html
@@ -0,0 +1,26 @@
+{% extends 'base.html' %}
+
+{% block content %}
+
+<table class="table">
+ <thead>
+ <tr>
+ <td>Event Name</td>
+ <td>Already Responded</td>
+ <td>Start Date</td>
+ <td>End Date</td>
+ </tr>
+ </thead>
+ <tbody>
+ {% for event in events %}
+ <tr>
+ <td><a href="{% url 'event' event.id %}">{{event.name}}</a></td>
+ <td>{{event.responses}}</td>
+ <td>{{event.start_date}}</td>
+ <td>{{event.end_date}}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+</table>
+
+{% endblock %}
diff --git a/web/templates/registration/login.html b/web/templates/registration/login.html
new file mode 100644
index 0000000..afdff9b
--- /dev/null
+++ b/web/templates/registration/login.html
@@ -0,0 +1 @@
+{% extends "admin/login.html" %}
diff --git a/web/tests.py b/web/tests.py
new file mode 100644
index 0000000..7ce503c
--- /dev/null
+++ b/web/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/web/views.py b/web/views.py
new file mode 100644
index 0000000..cc537c4
--- /dev/null
+++ b/web/views.py
@@ -0,0 +1,137 @@
+from django.shortcuts import render
+from web.models import Event, Availability
+from django.shortcuts import get_object_or_404, redirect
+from datetime import timedelta
+from django.contrib.auth.decorators import login_required
+
+
+@login_required()
+def index(request):
+ parsed_events = []
+
+ for active_event in Event.objects.filter(active=True).order_by('start_date'):
+ data = {
+ 'id': active_event.id,
+ 'name': active_event.name,
+ 'start_date': active_event.start_date,
+ 'end_date': active_event.end_date,
+ 'responses': ', '.join([x.user.username for x in active_event.availability_set.all()])
+ }
+
+ parsed_events.append(data)
+
+ context = {
+ 'events': parsed_events
+ }
+
+ return render(request, "index.html", context)
+
+
+@login_required()
+def event(request, event_id):
+
+ event = get_object_or_404(Event, pk=event_id)
+
+ if request.method == 'POST':
+ handle_availability_update(event, request)
+ return redirect('event', event_id=event_id)
+
+ elif request.method == 'GET':
+ day_count = (event.end_date - event.start_date).days + 1
+ morning = {}
+ noon = {}
+ night = {}
+ all_availabilities = {}
+ requesting_user_submitted = False
+
+ context = {
+ 'event': event,
+ 'morning': morning,
+ 'noon': noon,
+ 'night': night,
+ 'user_availability': None,
+ 'active_dates': None,
+ 'user_responses': 0,
+ 'best_days': {},
+ 'no_overlap_message': ''
+ }
+
+ user_availability = Availability.objects.filter(event_id=event)
+ if user_availability:
+ for user_avail in user_availability:
+ context['user_responses'] += 1
+
+ for time in user_avail.time_table.get('active', []):
+ if time not in all_availabilities:
+ all_availabilities[time] = []
+
+ all_availabilities[time].append(user_avail.user.username)
+
+ if request.user == user_avail.user:
+ requesting_user_submitted = True
+ context['active_dates'] = user_avail.time_table['active']
+
+ context['all_availabilities'] = all_availabilities
+
+ for i in range(day_count):
+ date = event.start_date + timedelta(days=i)
+
+ human_readable_date = date.strftime('%m/%d/%Y %A')
+ for idx, time_period_array in enumerate([morning, noon, night]):
+ date_id = date.strftime(f'%m/%d/%Y %A-{idx}')
+ users = ', '.join(all_availabilities.get(date_id, []))
+
+ if not requesting_user_submitted:
+ users = ''
+
+ time_period_array[human_readable_date] = (date_id, users)
+
+ if all_availabilities:
+ most_avail = None
+ for day in sorted(all_availabilities, key=lambda k: len(all_availabilities[k]), reverse=True):
+ if not most_avail:
+ most_avail = len(all_availabilities[day])
+
+ if len(all_availabilities[day]) >= most_avail - 1 and len(all_availabilities[day]) != 1:
+
+ day_arr = day.split('-')
+ if day_arr[-1] == '0':
+ hday = day_arr[0] + ' Morning'
+
+ if day_arr[-1] == '1':
+ hday = day_arr[0] + ' Noon'
+
+ if day_arr[-1] == '2':
+ hday = day_arr[0] + ' Night'
+
+ context['best_days'][hday] = ', '.join(all_availabilities[day])
+
+ if not requesting_user_submitted:
+ all_availabilities = {}
+
+ if context['best_days'] == {}:
+ context['no_overlap_message'] = 'There are currently no overlapping times or dates!'
+
+ return render(request, "event.html", context)
+
+def handle_availability_update(event, request):
+ availability_data = {
+ 'active': []
+ }
+
+ for date, value in request.POST.items():
+ if date != 'csrfmiddlewaretoken':
+ availability_data['active'].append(date)
+
+ user_availability = Availability.objects.filter(event_id=event).filter(user=request.user)
+ if not user_availability:
+ a = Availability(
+ event_id=event,
+ user=request.user,
+ time_table=availability_data
+ )
+ a.save()
+ else:
+ avail = user_availability[0]
+ avail.time_table = availability_data
+ avail.save()