def getallcomments(request): result = [] try: path = json.loads(request.body)['path'] + '/.Comments' for tmpdir, subdirs, subfiles in os.walk(path): for subdir in subdirs: if not pattern.match(subdir): continue meta = loadJSON(os.path.join(tmpdir, subdir, 'meta')) threaddata = { '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, } for commentfile in os.listdir(os.path.join(tmpdir, subdir)): if commentfile == 'meta': continue comment = loadJSON(os.path.join(tmpdir, subdir, commentfile)) comment['topic'] = threaddata result.append(comment) return JsonResponse({'comments': result, 'stats': getstats(result)}, safe=False) except Exception: return HttpResponseServerError('Wystąpił nieznany błąd podczas pobierania komentarzy.')
def getallthreads(folderpath): result = [] for tmpdir, subdirs, subfiles in os.walk(folderpath + '/.Comments'): if '.' in tmpdir and '.Comments' not in tmpdir: continue slashornot = tmpdir == folderpath + '/.Comments' and '/' or '' for subdir in subdirs: if '.' not in subdir and 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 = loadJSON(os.path.join(tmpdir, subdir, comments[1])) # if comments doesn't contain meta file - ignore thread if 'meta' not in comments: continue metadata = loadJSON(os.path.join(tmpdir, subdir, 'meta')) data = { 'fullpath': os.path.join(tmpdir, subdir), 'timestamp': metadata['timestamp'], 'name': metadata['topic'], 'type': 'thread', 'path': tmpdir.replace(folderpath + '/.Comments', slashornot), '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 = loadJSON(os.path.join(tmpdir, subdir, comment)) if config['uid'] not in comm['readby'].keys(): data['unreadcomment'] = True break result.append(data) return result
def getcommentsfrompath(request): result = [] try: data = json.loads(request.body) fullpath = os.path.join(data['folderpath'], '.Comments', data['insidepath'][1:]) if not os.path.isdir(fullpath): return JsonResponse([], safe=False) for commentfolder in os.listdir(fullpath): if not pattern.match(commentfolder): continue temppath = os.path.join(fullpath, commentfolder) meta = loadJSON(os.path.join(temppath, 'meta')) threaddata = { 'fullpath': temppath, 'timestamp': meta['timestamp'], 'name': meta['topic'], 'type': 'thread', 'path': temppath.replace(data['folderpath'], ''), 'numberofcomments': len(os.listdir(temppath)) - 1, 'unreadcomment': False, } for commentfile in os.listdir(temppath): if commentfile == 'meta': continue comment = loadJSON(os.path.join(temppath, commentfile)) comment['topic'] = threaddata result.append(comment) return JsonResponse({'comments': result}, safe=False) except Exception: return HttpResponseServerError('Wystąpił nieznany błąd podczas pobierania komentarzy.')
def getfilelistfromlocation(folderpath, location): result = [] location = location[1:] locationfullpath = location == '/' and folderpath or os.path.join(folderpath, location) # getting sorted folders only for temp in [f for f in sorted(os.listdir(locationfullpath)) if os.path.isdir(os.path.join(locationfullpath, f)) and '.' not in f]: fullpath = fullpath = os.path.join(folderpath, location, temp) result.append({ 'fullpath': fullpath, 'name': temp, 'type': 'folder', 'insidepath': fullpath.replace(folderpath, '') }) #getting sorted files only for temp in [f for f in sorted(os.listdir(locationfullpath)) if os.path.isfile(os.path.join(locationfullpath, f))]: fullpath = fullpath = os.path.join(folderpath, location, temp) result.append({ 'fullpath': fullpath, 'name': temp, 'type': 'file', 'unrolled': False, 'threads': [], 'insidepath': fullpath.replace(folderpath, '') }) # if location contains no comments, then return result if not os.path.exists(os.path.join(folderpath, '.Comments', location)) or not os.path.isdir( os.path.join(folderpath, '.Comments', location)): return result # getting threads only for temp in os.listdir(os.path.join(folderpath, '.Comments', location)): fullpath = os.path.join(folderpath, '.Comments', location, temp) if not pattern.match(temp) or not os.path.isdir(fullpath): continue # getting metadata and lastcomment for it's timestamp metadata = loadJSON(os.path.join(fullpath, 'meta')) comments = sorted(os.listdir(fullpath), reverse=True) lastcomment = loadJSON(os.path.join(fullpath, comments[1])) # main thread data data = { 'timestamp': metadata['timestamp'], 'name': metadata['topic'], 'type': 'thread', 'numberofcomments': len(os.listdir(fullpath)) - 1, 'unreadcomment': False, 'lastcomment': lastcomment['timestamp'], 'fullpath': fullpath, 'insidepath': fullpath.replace(folderpath, '') } # searching for unread comments for comment in comments[1:]: comm = loadJSON(os.path.join(fullpath, 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