Exemple #1
0
def create_default_lists(user_id=None):
    with db.session():
        user_id = user_id or settings.USER_ID
        user = User.get_or_create(user_id)
        user.update_name()
        for list in settings.config['lists']:
            l = List.get(user_id, list['title'])
            if not l:
                l = List.create(user_id, list['title'], list.get('query'))
Exemple #2
0
def create_default_lists(user_id=None):
    with db.session():
        user_id = user_id or settings.USER_ID
        user = User.get_or_create(user_id)
        user.update_name()
        for list in settings.config['lists']:
            l = List.get(user_id, list['title'])
            if not l:
                l = List.create(user_id, list['title'], list.get('query'))
 def action_editusername(self, user, timestamp, username):
     from user.models import List
     old = user.nickname
     user.info['username'] = username
     user.update_name()
     if old != user.nickname:
         List.rename_user(old, user.nickname)
     user.save()
     return True
 def action_editusername(self, user, timestamp, username):
     from user.models import List
     old = user.nickname
     user.info['username'] = username
     user.update_name()
     if old != user.nickname:
         List.rename_user(old, user.nickname)
     user.save()
     return True
 def action_removelist(self, user, timestamp, name):
     from user.models import List
     l = List.get(user.id, name)
     if l:
         l.remove()
     user.clear_list_cache()
     return True
 def action_removelist(self, user, timestamp, name):
     from user.models import List
     l = List.get(user.id, name)
     if l:
         l.remove()
     user.clear_list_cache()
     return True
 def save_files(context, request, callback):
     listname = request.arguments.get('list', None)
     if listname:
         listname = listname[0]
         if isinstance(listname, bytes):
             listname = listname.decode('utf-8')
     with context():
         prefs = settings.preferences
         ids = []
         for upload in request.files.get('files', []):
             filename = upload.filename
             id = get_id(data=upload.body)
             ids.append(id)
             file = File.get(id)
             if not file:
                 prefix_books = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
                 prefix_imported = os.path.join(prefix_books, 'Imported/')
                 ox.makedirs(prefix_imported)
                 import_name = os.path.join(prefix_imported, filename)
                 n = 1
                 while os.path.exists(import_name):
                     n += 1
                     name, extension = filename.rsplit('.', 1)
                     import_name = os.path.join(prefix_imported, '%s [%d].%s' % (name, n, extension))
                 with open(import_name, 'wb') as fd:
                     fd.write(upload.body)
                 file = add_file(id, import_name, prefix_books)
     if listname and ids:
         l = List.get(settings.USER_ID, listname)
         if l:
             l.add_items(ids)
     response = json_response({'ids': ids})
     callback(response)
 def action_editlist(self, user, timestamp, name, new):
     from user.models import List
     l = List.get_or_create(user.id, name)
     if 'name' in new:
         l.name = new['name']
     l.save()
     user.clear_list_cache()
     return True
 def action_editlist(self, user, timestamp, name, new):
     from user.models import List
     l = List.get_or_create(user.id, name)
     if 'name' in new:
         l.name = new['name']
     l.save()
     user.clear_list_cache()
     return True
 def action_orderlists(self, user, timestamp, lists):
     from user.models import List
     idx = 0
     for name in lists:
         l = List.get_or_create(user.id, name)
         l.index_ = idx
         l.save()
         idx += 1
     return True
 def action_orderlists(self, user, timestamp, lists):
     from user.models import List
     idx = 0
     for name in lists:
         l = List.get_or_create(user.id, name)
         l.index_ = idx
         l.save()
         idx += 1
     return True
 def action_removelistitems(self, user, timestamp, name, ids):
     from user.models import List
     l = List.get(user.id, name)
     if l:
         l.remove_items(ids)
     return True
 def action_addlistitems(self, user, timestamp, name, ids):
     from user.models import List
     l = List.get_or_create(user.id, name)
     l.add_items(ids)
     return True
 def action_addlist(self, user, timestamp, name, query=None):
     from user.models import List
     l = List.create(user.id, name)
     return True
Exemple #15
0
def run_import(options=None):
    options = options or {}

    logger.debug("run_import")
    prefs = settings.preferences
    prefix = os.path.expanduser(options.get("path", prefs["importPath"]))
    if os.path.islink(prefix):
        prefix = os.path.realpath(prefix)
    if not prefix[-1] == os.sep:
        prefix += os.sep
    prefix_books = os.path.join(os.path.expanduser(prefs["libraryPath"]), "Books" + os.sep)
    prefix_imported = os.path.join(prefix_books, "Imported" + os.sep)
    if prefix_books.startswith(prefix) or prefix.startswith(prefix_books):
        error = "invalid path"
    elif not os.path.exists(prefix):
        error = "path not found"
    elif not os.path.isdir(prefix):
        error = "path must be a folder"
    else:
        error = None
    if error:
        trigger_event("activity", {"activity": "import", "progress": [0, 0], "status": {"code": 404, "text": error}})
        state.activity = {}
        return
    listname = options.get("list")
    if listname:
        listitems = []
    assert isinstance(prefix, str)
    books = []
    count = 0
    for root, folders, files in os.walk(prefix):
        for f in files:
            if not state.tasks.connected:
                return
            # if f.startswith('._') or f == '.DS_Store':
            if f.startswith("."):
                continue
            f = os.path.join(root, f)
            ext = f.split(".")[-1]
            if ext in extensions:
                books.append(f)
                count += 1
                if state.activity.get("cancel"):
                    state.activity = {}
                    return
                if count % 1000 == 0:
                    state.activity = {"activity": "import", "path": prefix, "progress": [0, count]}
                    trigger_event("activity", state.activity)
    state.activity = {"activity": "import", "path": prefix, "progress": [0, len(books)]}
    trigger_event("activity", state.activity)
    position = 0
    added = 0
    last = 0
    for f in ox.sorted_strings(books):
        position += 1
        if not os.path.exists(f):
            continue
        with db.session():
            id = media.get_id(f)
            file = File.get(id)
            if not file:
                f_import = f
                f = f.replace(prefix, prefix_imported)
                ox.makedirs(os.path.dirname(f))
                if options.get("mode") == "move":
                    shutil.move(f_import, f)
                else:
                    shutil.copy(f_import, f)
                file = add_file(id, f, prefix_books, f_import)
                file.move()
                added += 1
        if listname:
            listitems.append(file.item.id)
        if time.time() - last > 5:
            last = time.time()
            state.activity = {"activity": "import", "progress": [position, len(books)], "path": prefix, "added": added}
            trigger_event("activity", state.activity)

        if state.activity.get("cancel"):
            state.activity = {}
            return
    with db.session():
        if listname and listitems:
            l = List.get(settings.USER_ID, listname)
            if l:
                l.add_items(listitems)
    trigger_event(
        "activity",
        {
            "activity": "import",
            "progress": [position, len(books)],
            "path": prefix,
            "status": {"code": 200, "text": ""},
            "added": added,
        },
    )
    state.activity = {}
    remove_empty_folders(prefix_books)
    if options.get("mode") == "move":
        remove_empty_folders(prefix)
 def action_removelistitems(self, user, timestamp, name, ids):
     from user.models import List
     l = List.get(user.id, name)
     if l:
         l.remove_items(ids)
     return True
 def action_addlistitems(self, user, timestamp, name, ids):
     from user.models import List
     l = List.get_or_create(user.id, name)
     l.add_items(ids)
     return True
Exemple #18
0
def run_import(options=None):
    options = options or {}

    logger.debug('run_import')
    prefs = settings.preferences
    prefix = os.path.expanduser(options.get('path', prefs['importPath']))
    if os.path.islink(prefix):
        prefix = os.path.realpath(prefix)
    if not prefix[-1] == os.sep:
        prefix += os.sep
    prefix_books = os.path.join(os.path.expanduser(prefs['libraryPath']),
                                'Books' + os.sep)
    prefix_imported = os.path.join(prefix_books, 'Imported' + os.sep)
    if prefix_books.startswith(prefix) or prefix.startswith(prefix_books):
        error = 'invalid path'
    elif not os.path.exists(prefix):
        error = 'path not found'
    elif not os.path.isdir(prefix):
        error = 'path must be a folder'
    else:
        error = None
    if error:
        trigger_event(
            'activity', {
                'activity': 'import',
                'progress': [0, 0],
                'status': {
                    'code': 404,
                    'text': error
                }
            })
        state.activity = {}
        return
    listname = options.get('list')
    if listname:
        listitems = []
    assert isinstance(prefix, str)
    books = []
    count = 0
    for root, folders, files in os.walk(prefix):
        for f in files:
            if not state.tasks.connected:
                return
            #if f.startswith('._') or f == '.DS_Store':
            if f.startswith('.'):
                continue
            f = os.path.join(root, f)
            ext = f.split('.')[-1]
            if ext in extensions:
                books.append(f)
                count += 1
                if state.activity.get('cancel'):
                    state.activity = {}
                    return
                if count % 1000 == 0:
                    state.activity = {
                        'activity': 'import',
                        'path': prefix,
                        'progress': [0, count],
                    }
                    trigger_event('activity', state.activity)
    state.activity = {
        'activity': 'import',
        'path': prefix,
        'progress': [0, len(books)],
    }
    trigger_event('activity', state.activity)
    position = 0
    added = 0
    last = 0
    for f in ox.sorted_strings(books):
        position += 1
        if not os.path.exists(f):
            continue
        with db.session():
            id = media.get_id(f)
            file = File.get(id)
            if not file:
                f_import = f
                f = f.replace(prefix, prefix_imported)
                ox.makedirs(os.path.dirname(f))
                if options.get('mode') == 'move':
                    shutil.move(f_import, f)
                else:
                    shutil.copy(f_import, f)
                file = add_file(id, f, prefix_books, f_import)
                file.move()
                added += 1
        if listname:
            listitems.append(file.item.id)
        if time.time() - last > 5:
            last = time.time()
            state.activity = {
                'activity': 'import',
                'progress': [position, len(books)],
                'path': prefix,
                'added': added,
            }
            trigger_event('activity', state.activity)

        if state.activity.get('cancel'):
            state.activity = {}
            return
    with db.session():
        if listname and listitems:
            l = List.get(settings.USER_ID, listname)
            if l:
                l.add_items(listitems)
    trigger_event(
        'activity', {
            'activity': 'import',
            'progress': [position, len(books)],
            'path': prefix,
            'status': {
                'code': 200,
                'text': ''
            },
            'added': added,
        })
    state.activity = {}
    remove_empty_folders(prefix_books)
    if options.get('mode') == 'move':
        remove_empty_folders(prefix)
 def action_addlist(self, user, timestamp, name, query=None):
     from user.models import List
     l = List.create(user.id, name)
     return True