예제 #1
0
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
예제 #2
0
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)
예제 #3
0
def sync_all(request):
    from netprintbox.data import DropboxUser

    for user in DropboxUser.all():
        if not user.is_pending:
            # XXX check the need to sync?
            taskqueue.add(url=request.route_path('sync_for_user'),
                          params={'key': user.key()},
                          countdown=random.randint(0, SLEEP_WAIT))
    return request.response
    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))
예제 #5
0
    def test_new_user(self):
        from netprintbox.data import DropboxUser

        service = self._getOUT()
        service.setup_user(self.key)

        q = DropboxUser.all()
        self.assertEqual(q.count(), 1)
        user = q.get()
        self.assertEqual(user.uid, self.uid)
        self.assertEqual(user.access_key, 'access_key')
        self.assertEqual(user.access_secret, 'access_secret')
예제 #6
0
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
예제 #7
0
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,
            }
예제 #8
0
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
예제 #9
0
    def test_existing_user(self):
        from netprintbox.data import DropboxUser
        from netprintbox.exceptions import BecomePendingUser

        user = create_user()
        with self.assertRaises(BecomePendingUser):
            user.put_pending(notify=False)

        service = self._getOUT()
        service.setup_user(self.key)

        q = DropboxUser.all()
        self.assertEqual(q.count(), 1)
        queried_user = q.get()
        self.assertEqual(user.uid, queried_user.uid)
        self.assertFalse(queried_user.is_pending)
예제 #10
0
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
예제 #11
0
    def setup_user(cls, request_key):
        from dropbox.client import DropboxClient

        request_token = OAuthRequestToken.get(request_key)

        session = cls.get_session()
        session.obtain_access_token(request_token)
        client = DropboxClient(session)
        account_info = client.account_info()

        uid = str(account_info['uid'])
        q = DropboxUser.all().filter('uid = ', uid)
        if q.count() == 0:
            user = DropboxUser(uid=uid,
                               email=account_info['email'],
                               display_name=account_info['display_name'],
                               access_key=session.token.key,
                               access_secret=session.token.secret,
                               country=account_info['country'])
        else:
            user = q.get()
            user.email = account_info['email']
            user.display_name = account_info['display_name']
            user.access_key = session.token.key
            user.access_secret = session.token.secret
            user.put_pending_away(_save=False)
            user.country = account_info['country']
        user.put()

        OAuthRequestToken.delete(request_key)

        return user
예제 #12
0
 def __init__(self, user):
     if isinstance(user, (basestring, db.Key)):
         user = DropboxUser.get(user)
     self.user = user