def move_document_to_user_folder(resource_id, user): logger = LoggingHelper.getDebugLogger() client = GoogleDriveHelper.get_docs_client() dest_folder = user.get_profile().drive_folder_id if not dest_folder: logger.debug('Creating folder for {0}'.format(user.username)) dest_folder = GoogleDriveHelper.create_unistar_folder(user) user.get_profile().drive_folder_id = dest_folder user.get_profile().save() logger.debug('Moving {0} to {1}'.format(resource_id, GoogleDriveHelper._get_unistar_collection_name(user.username))) client.files().update(fileId=resource_id, body={"parents":[{'id':dest_folder}]}).execute()
def update_informations_task(): logger.debug('Starting update informations') client = GoogleDriveHelper.get_docs_client() params = {} params['includeDeleted'] = False result = client.changes().list(**params).execute() total_updated = 0 for item in result['items']: try: resource_id = GoogleDriveHelper.get_id_from_feed_url(item.id.text) if resource_id: is_updated = False title = item['file']['title'] updated = parser.parse(item['file']['modifiedDate']) updated = calendar.timegm(updated.utctimetuple()) logger.debug('Working on resource {0}'.format(resource_id)) document = Training.objects.get(resource_id=resource_id) if document.last_updated < updated: logger.debug( 'Updating last_updated for resource {0} from {1} to {2}' .format(resource_id, document.last_updated, updated)) document.last_updated = updated is_updated = True if document.title != title: logger.debug( u'Updating title for resource {0} from {1} to {2}'. format(resource_id, document.title, title)) document.title = title is_updated = True if is_updated: total_updated += 1 document.save() except Training.DoesNotExist: logger.debug('Unexisting resource {0}'.format(resource_id)) except Exception: LoggingHelper.getErrorLogger().error( 'An error occured while updating {0}'.format(item.id.text), exc_info=1) logger.debug('{0} resources updated'.format(total_updated))
def update_informations_task(): logger.debug('Starting update informations') client = GoogleDriveHelper.get_docs_client() params = {} params['includeDeleted'] = False result = client.changes().list(**params).execute() total_updated = 0 for item in result['items']: try: resource_id = GoogleDriveHelper.get_id_from_feed_url(item.id.text) if resource_id: is_updated = False title = item['file']['title'] updated = parser.parse(item['file']['modifiedDate']) updated = calendar.timegm(updated.utctimetuple()) logger.debug('Working on resource {0}'.format(resource_id)) document = Training.objects.get(resource_id=resource_id) if document.last_updated < updated: logger.debug( 'Updating last_updated for resource {0} from {1} to {2}'.format(resource_id, document.last_updated, updated)) document.last_updated = updated is_updated = True if document.title != title: logger.debug( u'Updating title for resource {0} from {1} to {2}'.format(resource_id, document.title, title)) document.title = title is_updated = True if is_updated: total_updated += 1 document.save() except Training.DoesNotExist: logger.debug('Unexisting resource {0}'.format(resource_id)) except Exception: LoggingHelper.getErrorLogger().error('An error occured while updating {0}'.format(item.id.text), exc_info=1) logger.debug('{0} resources updated'.format(total_updated))
def move_document_to_user_folder(resource_id, user): logger = LoggingHelper.getDebugLogger() client = GoogleDriveHelper.get_docs_client() dest_folder = user.get_profile().drive_folder_id if not dest_folder: logger.debug('Creating folder for {0}'.format(user.username)) dest_folder = GoogleDriveHelper.create_unistar_folder(user) user.get_profile().drive_folder_id = dest_folder user.get_profile().save() logger.debug('Moving {0} to {1}'.format( resource_id, GoogleDriveHelper._get_unistar_collection_name(user.username))) client.files().update(fileId=resource_id, body={ "parents": [{ 'id': dest_folder }] }).execute()
def handle(self, *args, **options): self.stdout.write('Executing Remove Docs editors to group command\n') client = GoogleDriveHelper.get_docs_client() trainings = Training.objects.all() for training in trainings: try: self.stdout.write('Entry : {0}\n'.format(training.resource_id)) entry = client.GetResourceById(training.resource_id) acl_feed = client.GetResourceAcl(entry) if acl_feed: for acl in acl_feed.entry: if acl.scope.type == 'user' and acl.role.value != 'owner': self.stdout.write( 'Remove ACL entry : {0}\n'.format( acl.scope.value)) client.DeleteAclEntry(acl) group_mail = '{0}@unishared.com'.format(GAPPS_GROUP_NAME) self.stdout.write('Add {0} as editor\n'.format(group_mail)) acl_entry_user = AclEntry(scope=AclScope(value=group_mail, type='user'), role=AclRole(value='writer')) client.AddAclEntry(entry, acl_entry_user, send_notifications=False) self.stdout.write('Add {0} as editor\n'.format( training.creator.email)) acl_entry_user = AclEntry(scope=AclScope( value=training.creator.email, type='user'), role=AclRole(value='writer')) client.AddAclEntry(entry, acl_entry_user, send_notifications=False) except Exception, e: print e
def live_detection_change_feed(last_result): logger.debug('Starting live detection with change feed') client = GoogleDriveHelper.get_docs_client() params = {'includeDeleted' : False} if last_result and last_result['startChangeId']: logger.debug('Last change id : {0}'.format(last_result['startChangeId'])) params['startChangeId'] = last_result['startChangeId'] result = client.changes().list(**params).execute() task_result = {'startChangeId' : int(result['largestChangeId']) + 1, 'items' : []} for item in result['items']: resource_id = item['file']['id'] updated = parser.parse(item['file']['modifiedDate']) title = item['file']['title'] try: last_entries = None if last_result: last_entries = [last_entry for last_entry in last_result['items'] if last_entry['document_id'] == resource_id] last_entry = last_entries[0] if last_entries and len(last_entries) > 0 else None document = Training.objects.get(resource_id=resource_id) logger.debug('Document recently modified : %s', resource_id) if document.title != title: logger.debug('Updating title of document {0}'.format(resource_id)) document.title=title updated = calendar.timegm(updated.utctimetuple()) document.last_updated = updated if last_entry: logger.debug('Previous entry found') logger.debug('Last entry : {0}'.format(last_entry)) if last_entry['live'] is None: logger.debug('Enabling live in DB for %s', resource_id) document.is_live = True document.is_displayed = True email_task.apply_async( ['Document live', Context({'document_url': GoogleDriveHelper.get_document_url(resource_id)}), 'document_live', [a[1] for a in settings.ADMINS], None, 'gmail']) else: logger.debug('Document already live %s', resource_id) task_result['items'].append({'document_id': resource_id, 'live': True}) else: logger.debug('No previous entry found') logger.debug('Creating entry %s', resource_id) task_result['items'].append({'document_id': resource_id, 'live': None}) document.save() except Training.DoesNotExist: logger.debug('Unexisting resource {0}'.format(resource_id)) except Exception: LoggingHelper.getErrorLogger().error('An error occurred while detecting live', exc_info=1) logger.debug('Disabling documents not live anymore') Training.objects.filter(~Q(resource_id__in=[item['file']['id'] for item in result['items']]), is_live=True).update(is_live=False) logger.debug('Result : %s', task_result) live_detection_change_feed.apply_async([task_result], eta=timezone.now()+timedelta(minutes=5)) return task_result
def live_detection_change_feed(last_result): logger.debug('Starting live detection with change feed') client = GoogleDriveHelper.get_docs_client() params = {'includeDeleted': False} if last_result and last_result['startChangeId']: logger.debug('Last change id : {0}'.format( last_result['startChangeId'])) params['startChangeId'] = last_result['startChangeId'] result = client.changes().list(**params).execute() task_result = { 'startChangeId': int(result['largestChangeId']) + 1, 'items': [] } for item in result['items']: resource_id = item['file']['id'] updated = parser.parse(item['file']['modifiedDate']) title = item['file']['title'] try: last_entries = None if last_result: last_entries = [ last_entry for last_entry in last_result['items'] if last_entry['document_id'] == resource_id ] last_entry = last_entries[0] if last_entries and len( last_entries) > 0 else None document = Training.objects.get(resource_id=resource_id) logger.debug('Document recently modified : %s', resource_id) if document.title != title: logger.debug( 'Updating title of document {0}'.format(resource_id)) document.title = title updated = calendar.timegm(updated.utctimetuple()) document.last_updated = updated if last_entry: logger.debug('Previous entry found') logger.debug('Last entry : {0}'.format(last_entry)) if last_entry['live'] is None: logger.debug('Enabling live in DB for %s', resource_id) document.is_live = True document.is_displayed = True email_task.apply_async([ 'Document live', Context({ 'document_url': GoogleDriveHelper.get_document_url(resource_id) }), 'document_live', [a[1] for a in settings.ADMINS], None, 'gmail' ]) else: logger.debug('Document already live %s', resource_id) task_result['items'].append({ 'document_id': resource_id, 'live': True }) else: logger.debug('No previous entry found') logger.debug('Creating entry %s', resource_id) task_result['items'].append({ 'document_id': resource_id, 'live': None }) document.save() except Training.DoesNotExist: logger.debug('Unexisting resource {0}'.format(resource_id)) except Exception: LoggingHelper.getErrorLogger().error( 'An error occurred while detecting live', exc_info=1) logger.debug('Disabling documents not live anymore') Training.objects.filter( ~Q(resource_id__in=[item['file']['id'] for item in result['items']]), is_live=True).update(is_live=False) logger.debug('Result : %s', task_result) live_detection_change_feed.apply_async([task_result], eta=timezone.now() + timedelta(minutes=5)) return task_result