Пример #1
0
def get_all_threads(folder_path):
    """
        Returns list of all threads in shared folder.
    """
    result = []

    # search through all directories
    for tmpdir, subdirs, subfiles in os.walk(folder_path + '/.Comments'):
        # ignore hidden files
        if '.' in tmpdir and '.Comments' not in tmpdir:
            continue

        for subdir in subdirs:
            if '.' not in subdir and comment_file_name_pattern.match(subdir):
                if not len(os.listdir(os.path.join(tmpdir, subdir))) > 0:
                    continue

                # getting timestamp info about freshest comment
                comments = sorted(os.listdir(os.path.join(tmpdir, subdir)), reverse=True)
                lastcomment = load_json(os.path.join(tmpdir, subdir, comments[1]))

                # if comments doesn't contain meta file - ignore thread
                if 'meta' not in comments:
                    continue

                metadata = load_json(os.path.join(tmpdir, subdir, 'meta'))
                data = {
                    'fullpath': os.path.join(tmpdir, subdir),
                    'timestamp': metadata['timestamp'],
                    'name': metadata['topic'],
                    'type': 'thread',
                    'path': tmpdir.replace(folder_path + '/.Comments', tmpdir == folder_path + '/.Comments' and '/' or ''),
                    'numberofcomments': len(os.listdir(os.path.join(tmpdir, subdir))) - 1,
                    'unreadcomment': False,
                    'lastcomment': lastcomment['timestamp']
                }

                # searching for unread comments
                for comment in comments:
                    if comment == 'meta':
                        continue

                    comm = load_json(os.path.join(tmpdir, subdir, comment))
                    if config['uid'] not in comm['readby']:
                        data['unreadcomment'] = True
                        break

                result.append(data)

    return result
Пример #2
0
def get_comments_from_path(request):
    """
        Return all the comments from specified directory INSIDE the shared folder.
        Keys in request.body:
            insidepath - relative path inside shared folder
            folderpath - path of shared folder
    """
    try:
        data = json.loads(request.body)
        result = []

        full_path = os.path.join(data['folderpath'], '.Comments', data['insidepath'][1:])

        if not os.path.isdir(full_path):
            return JsonResponse([], safe=False)

        for comment_folder in os.listdir(full_path):
            if not comment_file_name_pattern.match(comment_folder):
                continue

            # get info about thread
            temp_path = os.path.join(full_path, comment_folder)
            meta = load_json(os.path.join(temp_path, 'meta'))
            thread_data = {
                'fullpath': temp_path,
                'timestamp': meta['timestamp'],
                'name': meta['topic'],
                'type': 'thread',
                'path': temp_path.replace(data['folderpath'], ''),
                'numberofcomments': len(os.listdir(temp_path)) - 1,
                'unreadcomment': False,
            }

            # get comments
            for comment_file in os.listdir(temp_path):
                if comment_file == 'meta':
                    continue

                comment = load_json(os.path.join(temp_path, comment_file))
                comment['topic'] = thread_data

                result.append(comment)

        return JsonResponse({'comments': result}, safe=False)
    except Exception:
        return HttpResponseServerError('Wystąpił nieznany błąd podczas pobierania komentarzy.')
Пример #3
0
def get_all_comments(request):
    """
        Returns all comments written in whole shared folder.
        Request.body contains only 'path' key.
    """
    try:
        path = json.loads(request.body)['path'] + '/.Comments'
        result = []

        for tmpdir, subdirs, subfiles in os.walk(path):
            for subdir in subdirs:
                if not comment_file_name_pattern.match(subdir):
                    continue

                # get info about thread (needed in gui to take shortcut to thread or gather stats)
                meta = load_json(os.path.join(tmpdir, subdir, 'meta'))
                thread_data = {
                    'fullpath': os.path.join(tmpdir, subdir),
                    'timestamp': meta['timestamp'],
                    'name': meta['topic'],
                    'type': 'thread',
                    'path': os.path.join(tmpdir, subdir).replace(path, ''),
                    'numberofcomments': len(os.listdir(os.path.join(tmpdir, subdir))) - 1,
                    'unreadcomment': False,
                }

                # finally get comments
                for commentfile in os.listdir(os.path.join(tmpdir, subdir)):
                    if commentfile == 'meta':
                        continue

                    comment = load_json(os.path.join(tmpdir, subdir, commentfile))
                    comment['topic'] = thread_data

                    result.append(comment)

        return JsonResponse({'comments': result, 'stats': get_stats(result)}, safe=False)
    except Exception:
        return HttpResponseServerError('Wystąpił nieznany błąd podczas pobierania komentarzy.')
Пример #4
0
def get_file_list_from_location(folder_path, location):
    """
        Returns list of files inside specified location.
    """
    result = []
    location = location[1:]
    location_full_path = location == '/' and folder_path or os.path.join(folder_path, location)

    # getting sorted folders only
    for temp in [f for f in sorted(os.listdir(location_full_path)) if os.path.isdir(os.path.join(location_full_path, f)) and '.' not in f]:
        full_path = os.path.join(folder_path, location, temp)
        result.append({
            'fullpath': full_path,
            'name': temp,
            'type': 'folder',
            'insidepath': full_path.replace(folder_path, '')
        })

    # getting sorted files only
    for temp in [f for f in sorted(os.listdir(location_full_path)) if os.path.isfile(os.path.join(location_full_path, f))]:
        full_path = full_path = os.path.join(folder_path, location, temp)
        result.append({
            'fullpath': full_path,
            'name': temp,
            'type': 'file',
            'unrolled': False,
            'threads': [],
            'insidepath': full_path.replace(folder_path, '')
        })

    # if location contains no comments, then return result
    if not os.path.exists(os.path.join(folder_path, '.Comments', location)) or not os.path.isdir(
            os.path.join(folder_path, '.Comments', location)):
        return result

    # getting threads only
    for temp in os.listdir(os.path.join(folder_path, '.Comments', location)):
        full_path = os.path.join(folder_path, '.Comments', location, temp)

        if not comment_file_name_pattern.match(temp) or not os.path.isdir(full_path):
            continue

        # getting metadata and lastcomment for it's timestamp
        metadata = load_json(os.path.join(full_path, 'meta'))
        comments = sorted(os.listdir(full_path), reverse=True)
        lastcomment = load_json(os.path.join(full_path, comments[1]))

        # main thread data
        data = {
            'timestamp': metadata['timestamp'],
            'name': metadata['topic'],
            'type': 'thread',
            'numberofcomments': len(os.listdir(full_path)) - 1,
            'unreadcomment': False,
            'lastcomment': lastcomment['timestamp'],
            'fullpath': full_path,
            'insidepath': full_path.replace(folder_path, '')
        }

        # searching for unread comments
        for comment in comments[1:]:
            comm = load_json(os.path.join(full_path, comment))
            if config['uid'] not in comm['readby'].keys():
                data['unreadcomment'] = True
                break

        # updating files which are thread about
        if len(metadata['fileabout']) > 0:
            for res in result:
                if res['type'] != 'file':
                    continue

                if res['insidepath'] == metadata['fileabout']:
                    if data['unreadcomment']:
                        res['unreadcomment'] = True
                    res['threads'].append(copy.deepcopy(data))
        else:
            result.append(data)

    return result