Beispiel #1
0
    def test_one_import(self):
        """You should be able to only get one import running at a time"""
        self._login_admin()

        # Prep the db with 2 other imports ahead of this user's.
        # We have to commit these since the request takes place in a new
        # session/transaction.
        DBSession.add(
            ImportQueue(username=u'testing', file_path=u'testing.txt'))
        DBSession.add(
            ImportQueue(username=u'testing2', file_path=u'testing2.txt'))
        DBSession.flush()
        transaction.commit()

        res = self._upload()
        res.follow()

        # now let's hit the import page, we shouldn't get a form, but instead a
        # message about our import
        res = self.app.get('/admin/import')

        self.assertTrue('<form' not in res.body, "We shouldn't have a form")
        self.assertTrue('waiting in the queue' in res.body,
                        "We want to display a waiting message.")
        self.assertTrue('2 other imports' in res.body,
                        "We want to display a count message." + res.body)
Beispiel #2
0
    def test_completed_dont_count(self):
        """Once completed, we should get the form again"""
        self._login_admin()

        # add out completed one
        q = ImportQueue(username=u'admin', file_path=u'testing.txt')
        q.completed = datetime.now()
        q.status = 2
        DBSession.add(q)
        transaction.commit()

        # now let's hit the import page, we shouldn't get a form, but instead a
        # message about our import
        res = self.app.get('/admin/import')

        self.assertTrue('<form' in res.body, "We should have a form")
Beispiel #3
0
 def _add_demo_import(self):
     """DB Needs some imports to be able to query."""
     # add out completed one
     q = ImportQueue(username='******', file_path='testing.txt')
     DBSession.add(q)
     transaction.commit()
     return
Beispiel #4
0
    def import_bmarks(self):
        """Allow users to upload a bookmark export file for processing"""
        username = self.matchdict.get('username')

        # if auth fails, it'll raise an HTTPForbidden exception
        with ReqAuthorize(self.request):
            data = {}
            post = self.POST

            # We can't let them submit multiple times, check if this user has
            # an import in process.
            if ImportQueueMgr.get(username=username, status=NEW):
                # They have an import, get the information about it and shoot
                # to the template.
                return {
                    'existing': True,
                    'import_stats': ImportQueueMgr.get_details(
                        username=username)
                }

            if post:
                # we have some posted values
                files = post.get('import_file', None)

                if files is not None:
                    storage_dir_tpl = self.settings.get('import_files',
                                                        '/tmp/bookie')
                    storage_dir = storage_dir_tpl.format(
                        here=self.settings.get('app_root'))

                    out_fname = store_import_file(storage_dir, username, files)

                    # Mark the system that there's a pending import that needs
                    # to be completed
                    q = ImportQueue(username, unicode(out_fname))
                    DBSession.add(q)
                    DBSession.flush()
                    # Schedule a task to start this import job.
                    tasks.importer_process.delay(q.id)

                    return HTTPFound(
                        location=self.request.route_url('user_import',
                                                        username=username))
                else:
                    msg = self.request.session.pop_flash()
                    if msg:
                        data['error'] = msg
                    else:
                        data['error'] = None

                return data
            else:
                # we need to see if they've got
                # just display the form
                return {
                    'existing': False
                }
Beispiel #5
0
    def test_completed_dont_count(self):
        """Once completed, we should get the form again"""
        self._login_admin()

        # add out completed one
        q = ImportQueue(
            username=u'admin',
            file_path=u'testing.txt'
        )
        q.completed = datetime.now()
        q.status = 2
        DBSession.add(q)
        transaction.commit()

        # now let's hit the import page, we shouldn't get a form, but instead a
        # message about our import
        res = self.app.get('/admin/import')

        self.assertTrue('<form' in res.body, "We should have a form")
Beispiel #6
0
def import_bmarks(request):
    """Allow users to upload a delicious bookmark export"""
    rdict = request.matchdict
    username = rdict.get('username')

    # if auth fails, it'll raise an HTTPForbidden exception
    with ReqAuthorize(request):
        data = {}
        post = request.POST

        # we can't let them submit multiple times, check if this user has an
        # import in process
        if ImportQueueMgr.get(username=username, status=NEW):
            # they have an import, get the information about it and shoot to
            # the template
            return {
                'existing': True,
                'import_stats': ImportQueueMgr.get_details(username=username)
            }

        if post:
            # we have some posted values
            files = post.get('import_file', None)

            if files is not None:
                # save the file off to the temp storage
                out_dir = "{storage_dir}/{randdir}".format(
                    storage_dir=request.registry.settings.get(
                        'import_files',
                        '/tmp/bookie').format(
                            here=request.registry.settings.get('app_root')),
                    randdir=random.choice(string.letters),
                )

                # make sure the directory exists
                # we create it with parents as well just in case
                if not os.path.isdir(out_dir):
                    os.makedirs(out_dir)

                out_fname = "{0}/{1}.{2}".format(
                    out_dir, username, files.filename)
                out = open(out_fname, 'w')
                out.write(files.file.read())
                out.close()

                # mark the system that there's a pending import that needs to
                # be completed
                q = ImportQueue(username, out_fname)
                DBSession.add(q)
                DBSession.flush()
                # Schedule a task to start this import job.
                tasks.importer_process.delay(q.id)

                return HTTPFound(location=request.route_url('user_import',
                                                            username=username))
            else:
                msg = request.session.pop_flash()
                if msg:
                    data['error'] = msg
                else:
                    data['error'] = None

            return data
        else:
            # we need to see if they've got
            # just display the form
            return {
                'existing': False
            }