mdecdlab / lab-booking-cdlab

lab-booking-cdlab created by GitHub Classroom
GNU Affero General Public License v3.0
1 stars 1 forks source link

Calendar 取捨 #1

Open mdecourse opened 5 years ago

mdecourse commented 5 years ago

Javascript

https://fullcalendar.io/ https://fullcalendar.io/docs https://www.reddit.com/r/learnpython/comments/2lk6lj/how_to_implement_a_web_calendar/ https://github.com/kkarimi/flask-fullcalendar https://github.com/mrf345/flask_datepicker/

http://ui.toast.com/tui-calendar/

mdecourse commented 5 years ago

使用 Google Calendar 管理各實驗室使用與借用流程

統一由 cdlab@mde.tw 帳號設定 cdlab, cadlab 與 cadrlab 等三間電腦教室的 Google Calendar, 之後再設法利用 Google Calendar API, 以伺服器程式對群組的 Calendar 進行資料管理. https://blog.hubspot.com/marketing/google-calendar-tips

一般未升級的 Google 帳號, 無法使用 rooms 相關的 Calendar 功能, 意即無法在實驗室行事曆中檢查時段衝突的事件, 或許可以透過 Google Calendar API 程式達成.

http://wescpy.blogspot.com/2015/09/creating-events-in-google-calendar.html

from __future__ import print_function
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/calendar'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
GCAL = discovery.build('calendar', 'v3', http=creds.authorize(Http()))

GMT_OFF = '-07:00'      # PDT/MST/GMT-7
EVENT = {
    'summary': 'Dinner with friends',
    'start':  {'dateTime': '2015-09-15T19:00:00%s' % GMT_OFF},
    'end':    {'dateTime': '2015-09-15T22:00:00%s' % GMT_OFF},
    'attendees': [
        {'email': 'friend1@example.com'},
        {'email': 'friend2@example.com'},
    ],
}

e = GCAL.events().insert(calendarId='primary',
        sendNotifications=True, body=EVENT).execute()

print('''*** %r event added:
    Start: %s
    End:   %s''' % (e['summary'].encode('utf-8'),
        e['start']['dateTime'], e['end']['dateTime']))

其他參考

https://pypi.org/project/python-google-calendar-api/

from __future__ import print_function
import sys
import httplib2
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from oauth2client.service_account import ServiceAccountCredentials
from datetime import datetime

import os
import pytz

BASE_DIR = os.path.dirname(os.path.realpath(sys.argv[0]))
CLIENT_SECRET_FILE = 'calender_key.json' 
SCOPES = 'https://www.googleapis.com/auth/calendar'
scopes = [SCOPES]
APPLICATION_NAME = 'Google Calendar API Python'

class google_calendar_api:

    def build_service(self):
        credentials = ServiceAccountCredentials.from_json_keyfile_name(
            CLIENT_SECRET_FILE,
            SCOPES
            )

        http = credentials.authorize(httplib2.Http())

        service = build('calendar', 'v3', http=http, cache_discovery=False)

        return service

    def create_event(self, calendar_id, start, end, desc, ):

        service = self.build_service()
        event = service.events().insert(calendarId=calendar_id, body={
            'description':desc,
            'summary':desc,
            'start':{'dateTime':  start},
            'end':{'dateTime':  end},
        }).execute()
        return event['id']

    def update_event(self,calendar_id, event_id, start, end, desc):
        service = self.build_service()
        try:
            event = service.events().get(calendarId=calendar_id, eventId=event_id).execute()
        except HttpError as e:
            if e.resp.status==404:
                return self.create_event(calendar_id, start, end, desc)
        event["start"]={'dateTime':start}
        event["end"]={'dateTime':end}
        event["summary"]= desc
        event["description"]= desc
        updated_event = service.events().update(calendarId=calendar_id, eventId=event['id'], body=event).execute()
        return updated_event["id"]
mdecourse commented 5 years ago

https://www.twilio.com/blog/2018/06/how-i-keep-my-mom-updated-on-my-travel-schedule-with-twilio-and-google-calendar.html

目前正用 mdecourse At gmail 與 scrum1 At mde 進行測試

mdecourse commented 5 years ago

利用 Google Calendar 管理實驗室申請使用

電腦輔助設計室公開行事曆

from __future__ import print_function
import httplib2
import os

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/calendar'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Google Calendar API Quickstart'

def get_credentials():
    """Gets valid user credentials from storage.

    If nothing has been stored, or if the stored credentials are invalid,
    the OAuth2 flow is completed to obtain the new credentials.

    Returns:
        Credentials, the obtained credential.
    """
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir,
                                   'client_secret.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
        flow.user_agent = APPLICATION_NAME
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else: # Needed only for compatability with Python 2.6
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)
    return credentials

def main():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)
    event = {
  'summary': 'Google Calendar API ',
  'location': 'The location of the event',
  'description': 'Some Description of the Event',
  'start': {
    'dateTime': '2015-10-12T20:00:00-03:00',
    'timeZone': 'America/Argentina/Cordoba',
  },
  'end': {
    'dateTime': '2015-10-12T23:00:00-03:00',
    'timeZone': 'America/Argentina/Cordoba',
  },
  'recurrence': [
    'RRULE:FREQ=DAILY;COUNT=2'
  ],
  'attendees': [
    {'email': 'host@example.com'},
    {'email': 'host2@example.com'},
    {'email': 'host3@example.com'},
  ],
  'reminders': {
    'useDefault': False,
    'overrides': [
      {'method': 'email', 'minutes': 24 * 60},
      {'method': 'popup', 'minutes': 10},
    ],
  },
}

    event = service.events().insert(calendarId='primary', body=event).execute()
    print(event)

if __name__ == '__main__':
    main()