def api_list_resource(self, feed): data = {} creds = build_creds.build_credentials( scope=[ "https://apps-apis.google.com/a/feeds/calendar/resource/" ], service_account_name=oauth_config['client_email'], private_key=oauth_config['private_key'], user=oauth_config['default_user'] ) auth2token = CreateToken(creds) client = CalendarResourceClient(domain=oauth_config['domain']) auth2token.authorize(client) if feed == 'feed': calendar_resources = str(client.GetResourceFeed()) else: calendar_resources = str(client.GetResourceFeed( uri="https://apps-apis.google.com/a/feeds/calendar/resource/2.0/%s/?%s" % ( oauth_config['domain'], feed))) nextpage, res = self.components.calendars.find_resource(calendar_resources) data['items'] = res data['next'] = nextpage data['previous'] = None data['page'] = "start=%s" % res[0]['resourceId'] self.context['data'] = data
def api_update_resource(self): resultMessage = {} try: current_user = users.get_current_user() creds = build_creds.build_credentials( scope=[ "https://apps-apis.google.com/a/feeds/calendar/resource/" ], service_account_name=oauth_config['client_email'], private_key=oauth_config['private_key'], user=oauth_config['default_user'] ) auth2token = CreateToken(creds) client = CalendarResourceClient(domain=oauth_config['domain']) auth2token.authorize(client) resource = json.loads(self.request.body) logging.info('json_resource_update_result: %s' % resource) check_resource = client.GetResource(resource_id=resource['resourceId']) check_result = self.components.calendars.find_resource(str(check_resource)) logging.info( 'OLD: %s | CHECK: %s ' % (resource['old_resourceCommonName'], check_result[0]['resourceCommonName'])) if resource['old_resourceCommonName'] != check_result[0]['resourceCommonName']: return 402 if 'resourceDescription' not in resource: resource['resourceDescription'] = '' else: resource['resourceDescription'] client.UpdateResource( resource_id=resource['resourceId'], resource_common_name=resource['resourceCommonName'], resource_description=resource['resourceDescription'], resource_type=resource['resourceType']) logging.info('calendar_resource_name: %s' % resource['resourceCommonName']) calendar_resource_email = client.GetResource(resource_id=resource['resourceId']) logging.info('calendar_resource_email: %s' % calendar_resource_email) res = self.components.calendars.find_resource(str(calendar_resource_email)) logging.info('resource_update_result: %s' % res) resultMessage['message'] = 'The app is in the process of updating the calendar.' resultMessage['items'] = res self.context['data'] = resultMessage resource['new_email'] = res[0]['resourceEmail'] sharded = "sharded" + ("1" if int(time.time()) % 2 == 0 else "2") deferred.defer(self.process_update_resource, resource, current_user.email(), _queue=sharded) except urllib2.HTTPError as e: logging.info('get_all_events: HTTPerror') logging.info(e) if e.code == 401: pass
def search(request): forwarding = None emails = None if request.method == "POST": form = SearchForm(request.POST) if form.is_valid(): cd = form.cleaned_data # space separated list of authorized API scopes for # the service account scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/' # create our email settings client client = EmailSettingsClient( domain=settings.DOMAIN_SUPER_USER_EMAIL.split('@')[1]) # obtain our street cred credentials = get_cred(settings.DOMAIN_SUPER_USER_EMAIL, scope) # fetch our access token auth2token = OAuth2TokenFromCredentials(credentials) # authorize our client auth2token.authorize(client) if cd['username']: try: forwarding = client.RetrieveForwarding( username=cd["username"]).property[1].value except: forwarding = None elif request.FILES.get('phile'): save_path = os.path.join(settings.MEDIA_ROOT, 'files/emailsettings.csv') path = default_storage.save(save_path, request.FILES['phile']) emails = [] with open(path, 'rb') as f: reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONE) for r in reader: try: if '@' in r[0]: forwarding = client.RetrieveForwarding( username=r[0].split( '@')[0]).property[1].value if forwarding: emails.append(forwarding) except: forwarding = None else: forwarding = 'Error' else: form = SearchForm() return render(request, 'emailsettings/search.html', { 'form': form, 'forwarding': forwarding, 'emails': emails })
def list_resource_memcache(self): params = {} result = [] nextpage = None # client = CalendarResourceClient(domain=oauth_config['domain']) # client.ClientLogin(email=oauth_config['default_user'], password=oauth_config['password'], source=APP_ID) creds = build_creds.build_credentials( scope=[ "https://apps-apis.google.com/a/feeds/calendar/resource/" ], service_account_name=oauth_config['client_email'], private_key=oauth_config['private_key'], user=oauth_config['default_user'] ) auth2token = CreateToken(creds) client = CalendarResourceClient(domain=oauth_config['domain']) auth2token.authorize(client) while True: if nextpage: params['uri'] = nextpage calendar_resources = str(client.GetResourceFeed(**params)) nextpage, res = self.find_resource(calendar_resources) for resource in res: result.append(dict( (k, v) for k, v in resource.iteritems() )) if not nextpage: break sortedResource = sorted(result, key=lambda resource: resource['resourceCommonName']) memcache.add('resource_list', sortedResource, 600) return sortedResource
def api_create_resource(self): resultMessage = {} current_user = users.get_current_user() creds = build_creds.build_credentials( scope=[ "https://apps-apis.google.com/a/feeds/calendar/resource/" ], service_account_name=oauth_config['client_email'], private_key=oauth_config['private_key'], user=oauth_config['default_user'] ) auth2token = CreateToken(creds) client = CalendarResourceClient(domain=oauth_config['domain']) auth2token.authorize(client) resource = json.loads(self.request.body) logging.info('create_resource: %s' % resource) try: resource['resourceId'] = generate_random_numbers(12) resource_list = memcache.get('resource_list') if resource_list is None: resource_list = self.components.calendars.list_resource_memcache() logging.info('resource_list: %s' % resource_list) for apiResource in resource_list: if apiResource['resourceCommonName'] == resource['resourceCommonName']: return 402 if 'resourceDescription' in resource: resourceDescription = resource['resourceDescription'] else: resourceDescription = None calendar_resource = client.CreateResource( resource_id=resource['resourceId'], resource_common_name=resource['resourceCommonName'], resource_description=resourceDescription, resource_type=resource['resourceType']) res = self.components.calendars.find_resource(str(calendar_resource)) logging.info('create_resource_result: %s' % res) action = 'A new Calendar Resource has been created' insert_audit_log( """A new Calendar Resource has been created. Resource ID: %s Resource Name: %s Resource Type: %s Resource Description: %s """ % ( resource['resourceId'], resource['resourceCommonName'], resource['resourceType'], resourceDescription), 'add new resource', current_user.email(), resource['resourceCommonName'], None, '') # AuditLogModel.new_resource_notification(config['email'], current_user.nickname(), resource) resultMessage['message'] = action resultMessage['items'] = res self.context['data'] = resultMessage except Exception as e: logging.info('create resource failed: %s' % e) return 406
def get_gdata_api(name, credentials, domain='', extra_kw={}): client = GDATA_SERVICES[name](domain=domain, **extra_kw) token = OAuth2TokenFromCredentials(credentials) return token.authorize(client)