From 286161b0912007c6f3859868a006e9818752a4aa Mon Sep 17 00:00:00 2001 From: Dominic DiTaranto Date: Mon, 28 Oct 2024 11:36:09 -0400 Subject: init commit --- web/__init__.py | 0 web/__pycache__/__init__.cpython-312.pyc | Bin 0 -> 149 bytes web/__pycache__/admin.cpython-312.pyc | Bin 0 -> 447 bytes web/__pycache__/apps.cpython-312.pyc | Bin 0 -> 449 bytes web/__pycache__/models.cpython-312.pyc | Bin 0 -> 2680 bytes web/__pycache__/views.cpython-312.pyc | Bin 0 -> 5430 bytes web/admin.py | 6 + web/apps.py | 6 + web/migrations/0001_initial.py | 46 +++++++ web/migrations/0002_alter_availability_user.py | 21 ++++ web/migrations/__init__.py | 0 .../__pycache__/0001_initial.cpython-312.pyc | Bin 0 -> 2163 bytes .../0002_alter_availability_user.cpython-312.pyc | Bin 0 -> 1107 bytes .../__pycache__/__init__.cpython-312.pyc | Bin 0 -> 160 bytes web/models.py | 38 ++++++ web/templates/base.html | 38 ++++++ web/templates/event.html | 91 ++++++++++++++ web/templates/index.html | 26 ++++ web/templates/registration/login.html | 1 + web/tests.py | 3 + web/views.py | 137 +++++++++++++++++++++ 21 files changed, 413 insertions(+) create mode 100644 web/__init__.py create mode 100644 web/__pycache__/__init__.cpython-312.pyc create mode 100644 web/__pycache__/admin.cpython-312.pyc create mode 100644 web/__pycache__/apps.cpython-312.pyc create mode 100644 web/__pycache__/models.cpython-312.pyc create mode 100644 web/__pycache__/views.cpython-312.pyc create mode 100644 web/admin.py create mode 100644 web/apps.py create mode 100644 web/migrations/0001_initial.py create mode 100644 web/migrations/0002_alter_availability_user.py create mode 100644 web/migrations/__init__.py create mode 100644 web/migrations/__pycache__/0001_initial.cpython-312.pyc create mode 100644 web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc create mode 100644 web/migrations/__pycache__/__init__.cpython-312.pyc create mode 100644 web/models.py create mode 100644 web/templates/base.html create mode 100644 web/templates/event.html create mode 100644 web/templates/index.html create mode 100644 web/templates/registration/login.html create mode 100644 web/tests.py create mode 100644 web/views.py (limited to 'web') diff --git a/web/__init__.py b/web/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/web/__pycache__/__init__.cpython-312.pyc b/web/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..dfdbe91 Binary files /dev/null and b/web/__pycache__/__init__.cpython-312.pyc differ diff --git a/web/__pycache__/admin.cpython-312.pyc b/web/__pycache__/admin.cpython-312.pyc new file mode 100644 index 0000000..9b5bd4a Binary files /dev/null and b/web/__pycache__/admin.cpython-312.pyc differ diff --git a/web/__pycache__/apps.cpython-312.pyc b/web/__pycache__/apps.cpython-312.pyc new file mode 100644 index 0000000..90ab549 Binary files /dev/null and b/web/__pycache__/apps.cpython-312.pyc differ diff --git a/web/__pycache__/models.cpython-312.pyc b/web/__pycache__/models.cpython-312.pyc new file mode 100644 index 0000000..47d02cc Binary files /dev/null and b/web/__pycache__/models.cpython-312.pyc differ diff --git a/web/__pycache__/views.cpython-312.pyc b/web/__pycache__/views.cpython-312.pyc new file mode 100644 index 0000000..4591619 Binary files /dev/null and b/web/__pycache__/views.cpython-312.pyc 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 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 Binary files /dev/null and b/web/migrations/__pycache__/0001_initial.cpython-312.pyc 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 Binary files /dev/null and b/web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc 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 Binary files /dev/null and b/web/migrations/__pycache__/__init__.cpython-312.pyc 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 @@ + + + + + Korabo + {% load static %} + {% load bootstrap5 %} + {% bootstrap_css %} + {% bootstrap_javascript %} + + + +
+ +

Korabo

+
+ + ~make it easy with コラボ~ +
+
+ home | + logout + +
+ +
+
+ +
+ {% block content %}{% endblock %} +
+ +
+ Version 1.0 | Created By Dominic DiTaranto | Questions/Suggestions: me@domdit.com | Last Update 09/22/2024 +
+ + + \ 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 %} + +

{{event.name}}

+
{% csrf_token %} + +
+ + + + + {% for datetime, day in morning.items %} + + {% endfor %} + + + + + + {% for datetime, day in morning.items %} + + + {% endfor %} + + + + {% for datetime, day in noon.items %} + + {% endfor %} + + + + {% for datetime, day in night.items %} + + {% endfor %} + + +
#{{ datetime }}
Morning +
+ {{ day.1 }} +
Afternoon +
+ {{ day.1 }} +
Night +
+ {{ day.1 }} +
+
+ +
+
+ Responses: {{user_responses}} / {{event.participants}} +
+
+ +
+
+ +
+ +
+
+
+

Best Times

+ {{ no_overlap_message }} + {% for day, users in best_days.items %} +
+
{{day}}
+
{{users}}
+
+ {% endfor %} +
+ + + + + + + +

+ + +{% 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 %} + + + + + + + + + + + + {% for event in events %} + + + + + + + {% endfor %} + +
Event NameAlready RespondedStart DateEnd Date
{{event.name}}{{event.responses}}{{event.start_date}}{{event.end_date}}
+ +{% 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() -- cgit v1.2.3-70-g09d2