2014-03-08 22:36:25 +01:00
|
|
|
import logging
|
2014-04-19 20:36:01 +02:00
|
|
|
import uuid
|
2014-04-26 11:17:10 +02:00
|
|
|
from eventplanner.models import Event
|
2014-06-20 12:24:01 +02:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
2014-04-26 11:17:10 +02:00
|
|
|
from apiclient.channel import Channel
|
|
|
|
from django.db import models
|
2014-03-08 22:36:25 +01:00
|
|
|
|
2014-04-26 11:17:10 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
2014-03-08 22:36:25 +01:00
|
|
|
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
class UserGCalCoupling(models.Model):
|
2014-06-20 12:24:01 +02:00
|
|
|
# For every user in this table the gcal coupling is activated
|
2019-01-05 11:27:15 +01:00
|
|
|
user = models.OneToOneField(User, on_delete=models.CASCADE)
|
|
|
|
email = models.CharField(max_length=1024)
|
2014-06-20 12:24:01 +02:00
|
|
|
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
class GCalMapping(models.Model):
|
2014-04-26 11:17:10 +02:00
|
|
|
"""Mapping between event id at google and local event id"""
|
2019-01-05 11:27:15 +01:00
|
|
|
gcal_id = models.CharField(max_length=64)
|
|
|
|
event = models.OneToOneField(Event, primary_key=True, on_delete=models.CASCADE)
|
2014-03-08 22:36:25 +01:00
|
|
|
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
class GCalPushChannel(models.Model):
|
2014-04-19 20:36:01 +02:00
|
|
|
"""This table has either zero or one entry. Required to store if a channel already exists,
|
2014-04-26 11:17:10 +02:00
|
|
|
when it expires and how to stop (renew) the channel
|
2014-04-19 20:36:01 +02:00
|
|
|
"""
|
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
id = models.CharField(max_length=128, primary_key=True)
|
|
|
|
address = models.CharField(max_length=256)
|
|
|
|
token = models.CharField(max_length=128)
|
|
|
|
resource_id = models.CharField(max_length=128)
|
|
|
|
expiration = models.IntegerField()
|
2014-04-19 20:36:01 +02:00
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
def toGChannel(self):
|
|
|
|
return Channel('web_hook', self.id, self.token, self.address, self.expiration, resource_id=self.resource_id)
|
2014-04-19 20:36:01 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2019-01-05 11:27:15 +01:00
|
|
|
def fromGChannel(gChannel):
|
|
|
|
return GCalPushChannel(id=gChannel.id,
|
|
|
|
address=gChannel.address,
|
|
|
|
token=gChannel.token,
|
|
|
|
expiration=gChannel.expiration,
|
|
|
|
resource_id=gChannel.resource_id)
|
2014-04-19 20:36:01 +02:00
|
|
|
|
|
|
|
@staticmethod
|
2019-01-05 11:27:15 +01:00
|
|
|
def createNew(callbackUrl, service, ttl=None):
|
|
|
|
gChannel = Channel('web_hook', str(uuid.uuid4()), 'blechreizGcal', callbackUrl, params={'ttl': int(ttl)})
|
|
|
|
response = service.events().watch(calendarId='primary', body=gChannel.body()).execute()
|
|
|
|
gChannel.update(response)
|
2014-04-19 20:36:01 +02:00
|
|
|
|
2019-01-05 11:27:15 +01:00
|
|
|
dbChannel = GCalPushChannel.fromGChannel(gChannel)
|
2014-04-19 20:36:01 +02:00
|
|
|
dbChannel.save()
|
|
|
|
|
|
|
|
@staticmethod
|
2019-01-05 11:27:15 +01:00
|
|
|
def stop(service, gChannel):
|
2014-04-19 20:36:01 +02:00
|
|
|
channelService = service.channels()
|
2019-01-05 11:27:15 +01:00
|
|
|
channelService.stop(body=gChannel.body()).execute()
|
|
|
|
GCalPushChannel.fromGChannel(gChannel).delete()
|