-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCRCaseTrello.py
115 lines (79 loc) · 3.7 KB
/
CRCaseTrello.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import requests
import json
import trelloCard
import googleDoc
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive']
def main():
"""Shows basic usage of the Drive v3 API.
Prints the names and ids of the first 10 files the user has access to.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
# opens the keys.json which has the ID for the parent folder and the ID for the template folder
with open('keys.json') as json_file:
data = json.load(json_file)
parentID = data['GoogleDriveFolder']
templateID = data["GoogleDocTemplate"]
# service object API
service = build('drive', 'v3', credentials=creds)
# create name for folder and trello card
folderName = input('Add Folder Name: ')
# stages data for API folder creation
body = {
'name': folderName,
'mimeType': "application/vnd.google-apps.folder"
}
if parentID:
body['parents'] = [parentID]
new_folder = service.files().create(body = body).execute()
# grabs the new folder's ID for reference
case_google_id = new_folder['id']
# list of template folders to include
folderTemplateList = ['screenshots', 'tests']
# loops through template list and adds them to the newly created folder
for folderType in folderTemplateList:
subBody = {
'name': folderType,
'mimeType': "application/vnd.google-apps.folder",
'parents': [case_google_id]
}
service.files().create(body = subBody).execute()
# copys case template google doc to the new folder
service.files().copy(fileId= templateID, body={'parents': [case_google_id], 'name': folderName}).execute()
newCard = trelloCard.createCard(folderName, case_google_id)
# email address may not be available: https://stackoverflow.com/questions/42247377/trello-api-e-mail-address-of-my-card-returns-null
## sounds like if you do newCard.json and load, and parse the json, the email address MIGHT be available (rather than using API, have to use direct REQUEST to the wbe facing version)
newDocId = googleDoc.googleDoc(newCard, folderName)
# trelloFile = drive_service.files().update(fileId=newDocId,
trelloFile = service.files().get(fileId=newDocId, fields='parents').execute()
previous_parents = ",".join(trelloFile.get('parents'))
# Move the file to the new folder
trelloFile = service.files().update(fileId=newDocId,
addParents=case_google_id,
removeParents=previous_parents,
fields='id, parents').execute()
if __name__ == '__main__':
main()