def pin(request): from netprintbox.data import DropboxUser, DropboxFileInfo data = json.loads(request.body) token = data['token'] if token != request.session.get_csrf_token(): raise exc.HTTPForbidden("CSRF token is unmatch.") key = request.session['netprintbox.dropbox_user.key'] user = DropboxUser.get(key) file_info = DropboxFileInfo.get(data['file_key']) if file_info.parent().uid != user.uid: raise exc.HTTPUnauthorized("The file_key is not found.") if data['pin'] == 'on': file_info.pin = True elif data['pin'] == 'off': file_info.pin = False else: raise ValueError file_info.put() data = { 'key': str(file_info.key()), 'path': file_info.path, 'netprint_id': file_info.netprint_id, 'netprint_name': file_info.as_netprint_name(), 'pin': file_info.pin, } response = request.response response.headers['Content-Type'] = 'application/json' response.body = json.dumps(data) return response
def setup_guide(request): from netprintbox.service import NetprintboxService, NetprintService from netprintbox.data import DropboxUser from netprintbox.exceptions import DropboxNotFound, InvalidNetprintAccountInfo key = request.session["netprintbox.dropbox_user.key"] try: user = DropboxUser.get(key) except: raise exc.HTTPUnauthorized service = NetprintboxService(user) if user.is_pending: need_reauthorize(request) need_to_create_account_info = False try: info = service.dropbox.list(ACCOUNT_INFO_PATH) need_to_create_account_info = info.get("is_deleted", False) except DropboxNotFound: need_to_create_account_info = True if need_to_create_account_info: service.dropbox.put(ACCOUNT_INFO_PATH, StringIO("[netprint]\n" "username=\n" "password="******"sync_for_user"), params={"key": key}) return step2(request)
def test_it(self): from netprintbox.views import authorize_callback from netprintbox.data import DropboxUser request = testing.DummyRequest({'oauth_token': 'token'}) response = authorize_callback(request) self.assertEqual(response.status_int, 302) actual_url = response.location expected_url = request.route_path('setup_guide') self.assertEqual(expected_url, actual_url) key = request.session['netprintbox.dropbox_user.key'] self.assertIsNotNone(DropboxUser.get(key))
def make_report_for_user(request): from netprintbox.service import NetprintboxService from netprintbox.data import DropboxUser user_key = request.POST['key'] user = DropboxUser.get(user_key) logging.debug("Making report for: %s", user.email) with handling_task_exception(user): service = NetprintboxService(user) service.make_report() return request.response
def get_namespace(request=None): from netprintbox.data import DropboxUser if request is None: request = get_current_request() if 'netprintbox.dropbox_user.key' in request.session: key = request.session['netprintbox.dropbox_user.key'] user = DropboxUser.get(key) else: user = None return { 'categorize_by': categorize_by, 'route_path': request.route_path, 'route_url': request.route_url, 'user': user, }
def sync_for_user(request): from netprintbox.service import NetprintboxService from netprintbox.data import DropboxUser user_key = request.POST['key'] user = DropboxUser.get(user_key) logging.debug("Checking for: %s", user.email) with handling_task_exception(user): service = NetprintboxService(user) service.sync() taskqueue.add(url=request.route_path('make_report_for_user'), params={'key': user_key}, countdown=random.randint(0, SLEEP_WAIT)) return request.response
def list_file(request): from netprintbox.data import DropboxUser key = request.session['netprintbox.dropbox_user.key'] user = DropboxUser.get(key) data = [{ 'key': str(file_info.key()), 'path': file_info.path, 'netprint_id': file_info.netprint_id, 'netprint_name': file_info.as_netprint_name(), 'pin': file_info.pin, } for file_info in user.own_files()] template = load_template('list_file.html', request=request) response = request.response response.body = template.substitute( json_data=json.dumps(data), csrf_token=request.session.new_csrf_token()) return response
def __init__(self, user): if isinstance(user, (basestring, db.Key)): user = DropboxUser.get(user) self.user = user