Skip to content
This repository was archived by the owner on Aug 26, 2024. It is now read-only.

Add GSoC End Date to Calendar #508

Merged
merged 7 commits into from
Jul 8, 2022
Merged
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
37 changes: 34 additions & 3 deletions gsoc/common/utils/build_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.utils import timezone
from django.conf import settings

from gsoc.models import (Event, Timeline, UserProfile, GsocYear,
from gsoc.models import (Event, GsocEndDate, Timeline, UserProfile, GsocYear,
BlogPostDueDate, Scheduler, ReaddUser)
from gsoc.common.utils.tools import build_send_mail_json

Expand Down Expand Up @@ -215,8 +215,8 @@ def build_add_event_to_calendar(builder):
service = build("calendar", "v3", credentials=creds, cache_discovery=False)
event = {
"summary": data["title"],
"start": {"date": data["start_date"]},
"end": {"date": data["end_date"]},
"start": {"date": data["date"]},
"end": {"date": data["date"]},
}
cal_id = builder.timeline.calendar_id if builder.timeline else "primary"
item = Event.objects.get(id=data["id"])
Expand All @@ -237,3 +237,34 @@ def build_add_event_to_calendar(builder):
f"Please get the Access Token: " +
f"{settings.OAUTH_REDIRECT_URI + 'authorize'}"
)


def build_add_end_to_calendar(builder):
data = json.loads(builder.data)
creds = getCreds()
if creds:
service = build("calendar", "v3", credentials=creds, cache_discovery=False)
event = {
"summary": data["title"],
"start": {"date": data["date"]},
"end": {"date": data["date"]},
}
cal_id = builder.timeline.calendar_id if builder.timeline else "primary"
if not data["event_id"]:
event = (
service.events()
.insert(calendarId=cal_id, body=event)
.execute()
)
item = GsocEndDate.objects.get(id=data["id"])
item.event_id = event.get("id")
item.save()
else:
service.events().update(
calendarId=cal_id, eventId=data["event_id"], body=event
).execute()
else:
raise Exception(
f"Please get the Access Token: " +
f"{settings.OAUTH_REDIRECT_URI + 'authorize'}"
)
46 changes: 44 additions & 2 deletions gsoc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,8 @@ class Builder(models.Model):
("build_remove_user_details", "build_remove_user_details"),
("build_add_timeline_to_calendar", "build_add_timeline_to_calendar"),
("build_add_bpdd_to_calendar", "build_add_bpdd_to_calendar"),
("build_add_event_to_calendar", "build_add_event_to_calendar")
("build_add_event_to_calendar", "build_add_event_to_calendar"),
("build_add_end_to_calendar", "build_add_end_to_calendar")
)

category = models.CharField(max_length=40, choices=categories)
Expand Down Expand Up @@ -883,14 +884,49 @@ def save(self, *args, **kwargs):
class GsocEndDate(models.Model):
timeline = models.OneToOneField(Timeline, on_delete=models.CASCADE)
date = models.DateField()
event_id = models.CharField(max_length=255, null=True, blank=True)

def add_to_calendar(self):
builder_data = json.dumps({
"id": self.id,
"title": "GSoC End",
"date": str(self.date.strftime('%Y-%m-%d')),
"event_id": self.event_id
})
try:
builder = Builder.objects.get(
category="build_add_end_to_calendar",
timeline=self.timeline,
)
builder.activation_date = datetime.datetime.now()
builder.built = None
builder.data = builder_data
builder.save()
except Builder.DoesNotExist:
Builder.objects.create(
category="build_add_end_to_calendar",
activation_date=datetime.datetime.now(),
data=builder_data,
timeline=self.timeline,
)

def delete_from_calendar(self):
if self.event_id:
creds = getCreds()
if creds:
service = build("calendar", "v3", credentials=creds, cache_discovery=False)
service.events().delete(
calendarId=self.timeline.calendar_id, eventId=self.event_id
).execute()

def save(self, *args, **kwargs):
try:
builder = Builder.objects.get(
timeline=self.timeline,
category="build_revoke_student_perms",
)
builder.activation_date = self.date
builder.activation_date = datetime.datetime.now()
builder.built = None
builder.save()
except Builder.DoesNotExist:
Builder.objects.create(
Expand Down Expand Up @@ -1544,6 +1580,12 @@ def due_date_add_to_calendar(sender, instance, **kwargs):
instance.add_to_calendar()


# Add GSoCEndDate to Google Calendar
@receiver(models.signals.post_save, sender=GsocEndDate)
def due_date_add_to_calendar(sender, instance, **kwargs):
instance.add_to_calendar()


# Publish the duedate to Github pages
@receiver(models.signals.post_save, sender=BlogPostDueDate)
def duedate_publish_to_github_pages(sender, instance, **kwargs):
Expand Down