def google_oauth_callback(request): state = request.GET['state'] credentials = Google.exchange_code(request.GET['code'], state) if state == 'login': profile_str = Google.get_profile(credentials) profile = json.loads(profile_str) try: user = MyUser.objects.get(email=profile['email']) except ObjectDoesNotExist: user = None if user is None: user = MyUser(name=profile['name'], email=profile['email'], picture_url=profile['picture']) user.save() request.session['user_id'] = user.id return redirect(reverse('connect_apps')) elif state == 'gmail': # store gmail oauth credentials in db try: gmail_auth = Gmail.objects.get(user__id=request.session['user_id']) except ObjectDoesNotExist: gmail_auth = None if gmail_auth is None: user = MyUser.objects.get(id=request.session['user_id']) gmail_auth = Gmail(user=user, credentials_json=credentials.to_json()) gmail_auth.save() else: gmail_auth.credentials_json = credentials.to_json() gmail_auth.save() return redirect(reverse('close_window'))
def upload_to_sfdc(request): gmail_service = Google.get_gmail_service(request.session['user_id']) attachment_id = request.GET['att_id'] message_id = request.GET['msg_id'] file_name = request.GET['file_name'] attachment = gmail_service.users().messages().attachments().get(userId='me', messageId=message_id, id=attachment_id).execute() file_data = attachment['data'] if file_data: sf_auth = Salesforce.objects.get(user__id=request.session['user_id']) Sfdc.insert_document(sf_auth, file_name, file_data) return HttpResponse('success')
def app(verbose, config_path, log_path, token_path): global cfg, google # Ensure paths are full paths if not config_path.startswith(os.path.sep): config_path = os.path.join( os.path.dirname(os.path.realpath(sys.argv[0])), config_path) if not log_path.startswith(os.path.sep): log_path = os.path.join(os.path.dirname(os.path.realpath(sys.argv[0])), log_path) if not token_path.startswith(os.path.sep): token_path = os.path.join( os.path.dirname(os.path.realpath(sys.argv[0])), token_path) # Load config from utils.config import Config cfg = Config(config_path=config_path).cfg # Load logger log_levels = {0: 'INFO', 1: 'DEBUG', 2: 'TRACE'} log_level = log_levels[verbose] if verbose in log_levels else 'TRACE' config_logger = { 'handlers': [{ 'sink': sys.stdout, 'backtrace': True if verbose >= 2 else False, 'level': log_level }, { 'sink': log_path, 'rotation': '30 days', 'retention': '7 days', 'enqueue': True, 'backtrace': True if verbose >= 2 else False, 'level': log_level }] } logger.configure(**config_logger) # Load google google = Google(cfg.client_id, cfg.client_secret, cfg.project_name, token_path) # Display params logger.info("%s = %r" % ("LOG_PATH".ljust(12), log_path)) logger.info("%s = %r" % ("LOG_LEVEL".ljust(12), log_level)) logger.info("") return
def list_threads(request): gmail_service = Google.get_gmail_service(request.session['user_id']) query = request.GET['query'] user_id = 'me' threads_resp = gmail_service.users().threads().list(userId=user_id, q=query).execute() threads = threads_resp['threads'] if 'threads' in threads_resp else [] threads_with_messages = [] for thread in threads: # do a get on thread using id to fill messages thread = gmail_service.users().threads().get(userId=user_id, id=thread['id']).execute() messages = thread['messages'] parsed_messages = [] if messages: # parse the message content for message in messages: parsed_messages.append(parse_message(message)) thread['messages'] = parsed_messages threads_with_messages.append(thread) data = json.dumps(threads_with_messages) return HttpResponse(data, mimetype='application/json')
def gmail_oauth(request): return redirect(Google.get_gmail_oauth_url())
def google_login(request): return redirect(Google.get_auth_url())
import dash_core_components as dcc import plotly.graph_objs as go import dash_table from dash.dependencies import Input, Output import pandas as pd import app.template as template from app.app import dash_app from utils.google import Google APPS_NAME = 'Promised Neverland Season 2' g = Google() df = g.get_data() df['Date'] = pd.to_datetime(df['Date']) df_table = df.copy() df_table['Date'] = df_table['Date'].dt.strftime('%m/%d/%Y') df_table = df_table.drop(columns=['Source']) dropdown_menu = dcc.Dropdown(id='data-input-' + APPS_NAME, options=[ {'label': 'MAL', 'value': 'Score'}], value=['Score'], multi=True) table = dash_table.DataTable( id='table', columns=[{"name": i, "id": i} for i in df_table.columns], style_cell={'textAlign': 'center'}, style_header={ 'backgroundColor': 'rgb(230, 230, 230)', 'fontWeight': 'bold'