diff options
author | Dominic DiTaranto <domdit@gmail.com> | 2024-10-28 11:36:09 -0400 |
---|---|---|
committer | Dominic DiTaranto <domdit@gmail.com> | 2024-10-28 11:36:09 -0400 |
commit | 286161b0912007c6f3859868a006e9818752a4aa (patch) | |
tree | d016592f7fa38b93f15e818586a9d294d80b6f3c /web |
init commit
Diffstat (limited to 'web')
21 files changed, 413 insertions, 0 deletions
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 Binary files differnew file mode 100644 index 0000000..dfdbe91 --- /dev/null +++ b/web/__pycache__/__init__.cpython-312.pyc diff --git a/web/__pycache__/admin.cpython-312.pyc b/web/__pycache__/admin.cpython-312.pyc Binary files differnew file mode 100644 index 0000000..9b5bd4a --- /dev/null +++ b/web/__pycache__/admin.cpython-312.pyc diff --git a/web/__pycache__/apps.cpython-312.pyc b/web/__pycache__/apps.cpython-312.pyc Binary files differnew file mode 100644 index 0000000..90ab549 --- /dev/null +++ b/web/__pycache__/apps.cpython-312.pyc diff --git a/web/__pycache__/models.cpython-312.pyc b/web/__pycache__/models.cpython-312.pyc Binary files differnew file mode 100644 index 0000000..47d02cc --- /dev/null +++ b/web/__pycache__/models.cpython-312.pyc diff --git a/web/__pycache__/views.cpython-312.pyc b/web/__pycache__/views.cpython-312.pyc Binary files differnew file mode 100644 index 0000000..4591619 --- /dev/null +++ b/web/__pycache__/views.cpython-312.pyc 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 Binary files differnew file mode 100644 index 0000000..f6f6b17 --- /dev/null +++ b/web/migrations/__pycache__/0001_initial.cpython-312.pyc diff --git a/web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc b/web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc Binary files differnew file mode 100644 index 0000000..70e69e8 --- /dev/null +++ b/web/migrations/__pycache__/0002_alter_availability_user.cpython-312.pyc diff --git a/web/migrations/__pycache__/__init__.cpython-312.pyc b/web/migrations/__pycache__/__init__.cpython-312.pyc Binary files differnew file mode 100644 index 0000000..b45851f --- /dev/null +++ b/web/migrations/__pycache__/__init__.cpython-312.pyc 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() |