-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from royh02/master
Discord Event Announcements Finished
- Loading branch information
Showing
11 changed files
with
255 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,3 +74,4 @@ data/ | |
|
||
# Virtual environment | ||
/venv | ||
.vscode/settings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from datetime import datetime | ||
|
||
from django.db import migrations, models | ||
from django.db.migrations.operations.special import RunPython | ||
|
||
|
||
def date_to_datetime(apps, schema_editor): | ||
Event = apps.get_model("db_data", "Event") | ||
for event in Event.objects.all(): | ||
d = event.date | ||
event.start_time = datetime(year=d.year, month=d.month, day=d.day) | ||
event.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("db_data", "0020_auto_20200728_2346")] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="event", name="start_time", field=models.DateTimeField(null=True) | ||
), | ||
migrations.AddField( | ||
model_name="event", name="end_time", field=models.DateTimeField(null=True) | ||
), | ||
migrations.RunPython(date_to_datetime), | ||
migrations.RemoveField(model_name="event", name="date"), | ||
migrations.RemoveField(model_name="event", name="time"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from datetime import datetime | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
def date_to_datetime(apps, schema_editor): | ||
Event = apps.get_model("db_data", "Event") | ||
for event in Event.objects.all(): | ||
d = event.date | ||
event.start_time = datetime(year=d.year, month=d.month, day=d.day) | ||
event.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("db_data", "0020_auto_20200728_2346")] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="event", name="start_time", field=models.DateTimeField(null=True) | ||
), | ||
migrations.AddField( | ||
model_name="event", name="end_time", field=models.DateTimeField(null=True) | ||
), | ||
migrations.RunPython(date_to_datetime), | ||
migrations.RemoveField(model_name="event", name="date"), | ||
migrations.RemoveField(model_name="event", name="time"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import datetime | ||
from enum import Enum | ||
|
||
from django.utils import timezone | ||
|
||
from apps.db_data.models import Event | ||
|
||
|
||
class AnnouncementType(Enum): | ||
WEEK = "week" | ||
TOMORROW = "tomorrow" | ||
TODAY = "today" | ||
HOUR = "hour" | ||
B_TIME = "now" # Berkeley Time | ||
|
||
|
||
def get_events_in_time_delta(requested_atype: AnnouncementType): | ||
""" | ||
Retrieves a list of Event objects within the requested | ||
time delta. | ||
requested_atype will take in enum constants in | ||
AnnouncementType | ||
""" | ||
now = timezone.now().astimezone(timezone.get_current_timezone()) | ||
events = get_events_in_time_range(*timeify(requested_atype)) | ||
return events | ||
|
||
|
||
def get_events_in_time_range(start_time, end_time): | ||
""" | ||
Takes in a two datetime objects, start_time and end_time | ||
Returns events within the datetime range | ||
""" | ||
events = Event.objects.filter(start_time__gte=start_time).filter( | ||
start_time__lte=end_time | ||
) | ||
return events | ||
|
||
|
||
def timeify(requested_atype: AnnouncementType): | ||
""" | ||
Converts requested_tdelta string into a corresponding datetime object | ||
""" | ||
now = timezone.now() | ||
time_ranges = { | ||
AnnouncementType.WEEK: (now, now + datetime.timedelta(weeks=1)), | ||
AnnouncementType.TOMORROW: ( | ||
now + datetime.timedelta(days=1), | ||
now + datetime.timedelta(days=1, hours=23, minutes=59, seconds=59), | ||
), | ||
AnnouncementType.TODAY: ( | ||
now, | ||
now + datetime.timedelta(hours=23, minutes=59, seconds=59), | ||
), | ||
AnnouncementType.HOUR: ( | ||
now + datetime.timedelta(hours=1), | ||
now + datetime.timedelta(hours=1, minutes=59, seconds=59), | ||
), | ||
AnnouncementType.B_TIME: (now, now + datetime.timedelta(minutes=10)), | ||
} | ||
return time_ranges[requested_atype] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Generated by Django 2.2.19 on 2021-05-21 03:01 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("discordbot", "0004_auto_20210321_0152")] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="connectfourgame", | ||
name="player1", | ||
field=models.BigIntegerField(help_text="Discord User ID of player 1"), | ||
), | ||
migrations.AlterField( | ||
model_name="connectfourgame", | ||
name="player2", | ||
field=models.BigIntegerField(help_text="Discord User ID of player 2"), | ||
), | ||
migrations.AlterField( | ||
model_name="connectfourgame", | ||
name="winner", | ||
field=models.IntegerField( | ||
blank=True, | ||
help_text="Null if no winner, 1 or 2 if winner exists", | ||
null=True, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ slack_bolt | |
|
||
# apps.discordbot | ||
discord.py | ||
schedule | ||
pyfiglet | ||
cowpy | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters