def picker(request): """ Uhm. """ PARENT_FOLDER_ID = settings.GOOGLE_DRIVE_ROOT_FOLDER_ID drive = Drive() token = Drive.get_access_token() return render_to_response('pages/picker.html',locals(), context_instance=global_context(request))
def add_users_to_subdept(request, subdept_id, user_ids): drive = Drive() user = request.user erp_profile = user.erp_profile if not user.is_staff: return json.dumps({'message': 'Not Authorized'}) subdept = Subdept.objects.get(id=subdept_id) dept = subdept.dept append_string = '' if (not user.is_superuser) and erp_profile.core_relations.filter(id=dept.id).count() == 0 and \ erp_profile.supercoord_relations.filter(id=dept.id).count() == 0: return json.dumps( {'message': 'This subdept is not under your department !'}) for user_id in user_ids: user = User.objects.get(id=user_id.split('_')[1]) e = user.erp_profile if not (subdept in e.coord_relations.all()): append_string += render_to_string('portals/general/user.html', { 'user': e.user, 'link_type': 'subdept', 'link_id': subdept_id }, context_instance=global_context( request, token_info=False)) e.coord_relations.add(subdept) share_drive(drive, user, subdept.directory_id) return json.dumps({'message': 'done', 'append_string': append_string})
def google_refresh_token(request): ''' A one time view to get a refresh token from google ''' FLOW = Drive.create_flow() FLOW.params['state'] = xsrfutil.generate_token(settings.SECRET_KEY, request.user) authorize_url = FLOW.step1_get_authorize_url() return HttpResponseRedirect(authorize_url)
def create_page(request, name): # ? user = request.user if not user.is_staff: return json.dumps({'message': 'Not Authorized'}) p = Page.objects.create(name=name) drive = Drive() attach_drive_to_entity(drive, p) return json.dumps({'message': 'done', 'id': p.id, 'name': name})
def handle(self, **options): drive = Drive() entity_set = list(Dept.objects.all()) + list( Subdept.objects.all()) + list(Page.objects.all()) for entity in entity_set: time.sleep(2) print "executing ", entity.name if not entity.directory_id: attach_drive_to_entity(drive, entity) share_drive(drive, entity)
def global_context(request, token_info=True, user_info=True): """ Some basic variables useful in templates Usage : add context_instance=global_context(request) to the kwargs of the response function """ erp_profile = None profile = None if user_info: erp_profile = request.user.erp_profile if hasattr( request.user, "erp_profile") else None if hasattr(request.user, "profile"): profile = request.user.profile else: profile = None token = None if token_info and settings.USE_GOOGLE_DRIVE: drive = Drive() token = Drive.get_access_token() local_context = { 'user': request.user, 'erp_profile': erp_profile, 'user_profile': profile, 'session': request.session, 'google_access_token': token, 'experimental': settings.EXPERIMENTAL_MODE, 'SITE_URL': settings.SITE_URL, 'FEST_NAME': settings.FEST_NAME, 'SETTINGS': settings, } # Handle experimental mode. if 'experimental' in request.GET.keys(): # Take from get request.session['experimental'] = request.GET['experimental'] if ('experimental' in request.session.keys()): # Take from preset value local_context['experimental'] = request.session['experimental'] print local_context["experimental"] context = RequestContext(request, local_context) return context
def google_auth_callback(request): ''' Auth Callback to save the given google_api_credentials json into a file for later use ''' FLOW = Drive.create_flow() if not xsrfutil.validate_token(settings.SECRET_KEY, request.REQUEST['state'], request.user): return HttpResponseBadRequest() credential = FLOW.step2_exchange(request.REQUEST) # Store to file docs_client_json = str(credential.to_json()) file_path = settings.GOOGLE_API_CREDENTIALS_FILE_PATH with open(file_path, 'w') as f: f.write(docs_client_json) return HttpResponse("Restart your server for google drive to work.")
def create_subdept(request, dept_id, name): user = request.user if not user.is_staff: return json.dumps({'message': 'Not Authorized'}) if (not user.is_superuser) and user.erp_profile.core_relations.filter( id=dept_id).count( ) == 0 and user.erp_profile.supercoord_relations.filter( id=dept_id).count() == 0: return json.dumps( {'message': 'This subdept is not under your department !'}) s = Subdept.objects.create(dept=Dept.objects.get(id=dept_id), name=name) drive = Drive() attach_drive_to_entity(drive, s) return json.dumps({'message': 'done', 'id': s.id, 'name': name})
def global_context(request, token_info=True, user_info=True): """ Some basic variables useful in templates Usage : add context_instance=global_context(request) to the kwargs of the response function """ erp_profile = None profile = None if user_info: erp_profile = request.user.erp_profile if hasattr(request.user, "erp_profile") else None if hasattr(request.user, "profile"): profile = request.user.profile else: profile = None token = None if token_info and settings.USE_GOOGLE_DRIVE: drive = Drive() token = Drive.get_access_token() local_context = { 'user' : request.user, 'erp_profile' : erp_profile, 'user_profile' : profile, 'session' : request.session, 'google_access_token' : token, 'experimental' : settings.EXPERIMENTAL_MODE, 'SITE_URL' : settings.SITE_URL, 'FEST_NAME' : settings.FEST_NAME, 'SETTINGS' : settings, } # Handle experimental mode. if 'experimental' in request.GET.keys(): # Take from get request.session['experimental'] = request.GET['experimental'] if ('experimental' in request.session.keys()): # Take from preset value local_context['experimental'] = request.session['experimental'] print local_context["experimental"] context = RequestContext(request, local_context) return context