Exemple #1
0
  def dispatch(self, *args, **kwargs):
    action = self.kwargs.get('action')

    # Check oauth
    self.gc = GCalSync(self.request.user)
    if self.request.user.gcal_token:

      # Build runreport calendar
      if not self.request.user.gcal_id:
        self.gc.create_calendar('RunReport')

      # Delete all traces
      if action == 'delete':
        self.gc.cleanup()

    else:

      # First step: redirect to Google
      if action == 'redirect':
        return HttpResponseRedirect(self.gc.get_auth_url())

      # Second step: exchange code for token
      code = self.request.GET.get('code')
      if code:
        self.gc.exchange_token(code)

        # Create calendar
        self.gc.create_calendar('RunReport')

        # Init import in new calendar
        sync_gcal.delay(self.request.user)

    return super(GCalOauthView, self).dispatch(*args, **kwargs)
Exemple #2
0
class GCalOauthView(TemplateView):
  template_name = 'users/gcal/oauth.html'

  def dispatch(self, *args, **kwargs):
    action = self.kwargs.get('action')

    # Check oauth
    self.gc = GCalSync(self.request.user)
    if self.request.user.gcal_token:

      # Build runreport calendar
      if not self.request.user.gcal_id:
        self.gc.create_calendar('RunReport')

      # Delete all traces
      if action == 'delete':
        self.gc.cleanup()

    else:

      # First step: redirect to Google
      if action == 'redirect':
        return HttpResponseRedirect(self.gc.get_auth_url())

      # Second step: exchange code for token
      code = self.request.GET.get('code')
      if code:
        self.gc.exchange_token(code)

        # Create calendar
        self.gc.create_calendar('RunReport')

        # Init import in new calendar
        sync_gcal.delay(self.request.user)

    return super(GCalOauthView, self).dispatch(*args, **kwargs)

  def get_context_data(self, *args, **kwargs):
    context = super(GCalOauthView, self).get_context_data(*args, **kwargs)

    # List calendars
    cal_id = self.request.user.gcal_id
    if self.request.user.has_gcal() and cal_id:
      context['calendar'] = self.gc.get_calendar(cal_id)

    return context
Exemple #3
0
def sync_session_gcal(session, delete=False):
    """
  Sync a SportSession on Google calendar
  """
    from sport.gcal import GCalSync

    # Check the user has gcal
    user = session.day.week.user
    if not user.has_gcal():
        return

    # Start sync
    gc = GCalSync(user)
    if delete and session.gcal_id:
        gc.delete_event(session.gcal_id)
    else:
        gc.sync_sport_session(session)
Exemple #4
0
def sync_gcal(user):
  '''
  Sync all the user sport sessions
  when creating a new calendar
  '''
  if not user.has_gcal():
    return

  from sport.gcal import GCalSync
  from sport.models import SportSession

  sessions = SportSession.objects.filter(day__week__user=user)
  sessions = sessions.filter(gcal_id__isnull=True)
  sessions = sessions.order_by('-day__date')

  gc = GCalSync(user)
  for s in sessions:
    try:
      gc.sync_sport_session(s)
    except Exception, e:
      print 'Failed to create an event: %s' % (str(e), )
Exemple #5
0
def sync_gcal(user):
    """
  Sync all the user sport sessions
  when creating a new calendar
  """
    if not user.has_gcal():
        return

    from sport.gcal import GCalSync
    from sport.models import SportSession

    sessions = SportSession.objects.filter(day__week__user=user)
    sessions = sessions.filter(gcal_id__isnull=True)
    sessions = sessions.order_by("-day__date")

    gc = GCalSync(user)
    for s in sessions:
        try:
            gc.sync_sport_session(s)
        except Exception, e:
            print "Failed to create an event: %s" % (str(e),)
Exemple #6
0
def sync_session_gcal(session, delete=False):
  '''
  Sync a SportSession on Google calendar
  '''
  from sport.gcal import GCalSync

  # Check the user has gcal
  user = session.day.week.user
  if not user.has_gcal():
    return

  # Start sync
  gc = GCalSync(user)
  if delete and session.gcal_id:
    gc.delete_event(session.gcal_id)
  else:
    gc.sync_sport_session(session)