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

Commit e852e05

Browse files
committed
refactor getCreds
1 parent fc8d9d4 commit e852e05

File tree

3 files changed

+36
-50
lines changed

3 files changed

+36
-50
lines changed

gsoc/common/utils/build_tasks.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
from gsoc.models import Event, Timeline, UserProfile, GsocYear, BlogPostDueDate, Scheduler, ReaddUser
88
from gsoc.common.utils.tools import build_send_mail_json
99

10+
from googleapiclient.discovery import build
11+
from gsoc.common.utils.googleoauth import getCreds
12+
1013

1114
def build_pre_blog_reminders(builder):
1215
try:
@@ -154,29 +157,6 @@ def build_remove_user_details(builder):
154157
except Exception as e:
155158
return str(e)
156159

157-
import os
158-
from google.auth.transport.requests import Request
159-
from google.oauth2.credentials import Credentials
160-
from google_auth_oauthlib.flow import InstalledAppFlow
161-
from googleapiclient.discovery import build
162-
163-
SCOPES = ['https://www.googleapis.com/auth/calendar']
164-
165-
def getCreds():
166-
creds = None
167-
if os.path.exists('token.json'):
168-
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
169-
if not creds or not creds.valid:
170-
if creds and creds.expired and creds.refresh_token:
171-
creds.refresh(Request())
172-
else:
173-
flow = InstalledAppFlow.from_client_secrets_file(
174-
'credentials.json', SCOPES)
175-
creds = flow.run_local_server(port=0)
176-
with open('token.json', 'w') as token:
177-
token.write(creds.to_json())
178-
return creds
179-
180160

181161
def build_add_timeline_to_calendar(builder):
182162
data = json.loads(builder.data)

gsoc/common/utils/googleoauth.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
3+
from gsoc.settings import BASE_DIR
4+
from settings_local import ADMINS
5+
6+
from google.auth.transport.requests import Request
7+
from google.oauth2.credentials import Credentials
8+
from google_auth_oauthlib.flow import InstalledAppFlow
9+
10+
SCOPES = ['https://www.googleapis.com/auth/calendar']
11+
12+
13+
def getCreds():
14+
creds = None
15+
if os.path.exists(os.path.join(BASE_DIR, 'token.json')):
16+
creds = Credentials.from_authorized_user_file(
17+
os.path.join(BASE_DIR, 'token.json'),
18+
SCOPES
19+
)
20+
if not creds or not creds.valid:
21+
if creds and creds.expired and creds.refresh_token:
22+
creds.refresh(Request())
23+
else:
24+
flow = InstalledAppFlow.from_client_secrets_file(
25+
os.path.join(BASE_DIR, 'credentials.json'),
26+
SCOPES
27+
)
28+
creds = flow.run_local_server(port=0)
29+
with open(os.path.join(BASE_DIR, 'token.json'), 'w') as token:
30+
token.write(creds.to_json())
31+
return creds

gsoc/models.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,10 @@
4141
from gsoc.common.utils.tools import build_send_reminder_json
4242

4343
from gsoc.constants import *
44-
from gsoc.settings import BASE_DIR, PROPOSALS_PATH
44+
from gsoc.settings import PROPOSALS_PATH
4545
from settings_local import ADMINS
46+
from gsoc.common.utils.googleoauth import getCreds
4647

47-
from google.auth.transport.requests import Request
48-
from google.oauth2.credentials import Credentials
49-
from google_auth_oauthlib.flow import InstalledAppFlow
50-
51-
SCOPES = ['https://www.googleapis.com/auth/calendar']
5248

5349
# Util Functions
5450

@@ -57,27 +53,6 @@ def gen_uuid_str():
5753
return str(uuid.uuid4())
5854

5955

60-
def getCreds():
61-
creds = None
62-
if os.path.exists(os.path.join(BASE_DIR, 'token.json')):
63-
creds = Credentials.from_authorized_user_file(
64-
os.path.join(BASE_DIR, 'token.json'),
65-
SCOPES
66-
)
67-
if not creds or not creds.valid:
68-
if creds and creds.expired and creds.refresh_token:
69-
creds.refresh(Request())
70-
else:
71-
flow = InstalledAppFlow.from_client_secrets_file(
72-
os.path.join(BASE_DIR, 'credentials.json'),
73-
SCOPES
74-
)
75-
creds = flow.run_local_server(port=0)
76-
with open(os.path.join(BASE_DIR, 'token.json'), 'w') as token:
77-
token.write(creds.to_json())
78-
return creds
79-
80-
8156
# Patching
8257

8358
NewsBlogConfig.__str__ = lambda self: self.app_title

0 commit comments

Comments
 (0)