def edit_comment(request): """ Edit existing comment. Returns nothing. Keys in request.body: comment - dict with new comment's data. Keys: comment - new content which will replace the old one timestamp - timestamp of the moment first version of comment was created uid - ID of comment's author fullthreadpath - full path of thread directory where the comment files are stored """ try: if not check_connection(): return HttpResponseServerError('Brak połączenia z Internetem.') edited_comment = json.loads(request.body)['comment'] comment_path = os.path.join(json.loads(request.body)['threadfullpath'], edited_comment['timestamp'] + file_name_separator + edited_comment['uid']) # get old comment and perform changes comment = load_json(comment_path) comment['history'].append({'timestamp': get_timestamp(), 'comment': edited_comment['comment']}) comment['comment'] = edited_comment['comment'] save_json(comment_path, comment) return JsonResponse({}, safe=False) except Exception: return HttpResponseServerError('Wystąpił nieznany błąd podczas edycji komentarza.')
def write_comment(request): """ Put text from form and create comment file inside thread. Keys in request.body: comment - text of comment fullthreadpath - full path of thread directory where the comment files are stored """ try: if not check_connection(): return HttpResponseServerError('Brak połączenia z Internetem.') data = json.loads(request.body) if len(data['comment']) == 0: return HttpResponseServerError('Treść komentarza nie może być pusta.') #obtaining timestamp timestamp = get_timestamp() # creating new comment new_comment = { 'uid': config['uid'], 'timestamp': timestamp, 'readby': { config['uid']: timestamp }, 'comment': data['comment'], 'history': [{'timestamp': timestamp, 'comment': data['comment']}] } # saving comment to new file with format of comment_file_name_pattern (see common.py) save_json(os.path.join(data['fullthreadpath'], new_comment['timestamp'] + file_name_separator + config['uid']), new_comment) return JsonResponse(new_comment, safe=False) except Exception: return HttpResponseServerError('Wystąpił nieznany błąd podczas dodawania komentarza.')
def write_new_thread(request): """ Prepare file system (create directory etc.) for comment files and create the first one. Keys in request.body: insidepath - relative path inside shared folder folderpath - path of shared folder topic - text of thread's topic fileabout - if thread is a discussion about some shared file, this indicates which file it is (only name of the file, not full path) comment - text of first comment """ try: if not check_connection(): return HttpResponseServerError('Brak połączenia z Internetem.') # getting and preparing data timestamp = get_timestamp() data = json.loads(request.body) # generating full path of a thread directory full_thread_path = os.path.join(data['folderpath'], '.Comments', data['insidepath'][1:], timestamp + file_name_separator + config['uid']) if os.path.isdir(full_thread_path): return HttpResponseServerError('Podany wątek już istnieje.') # thread dict sent to GUI response = { 'timestamp': timestamp, 'name': data['topic'], 'type': 'thread', 'numberofcomments': 1, 'unreadcomment': False, 'lastcomment': timestamp, 'fullpath': full_thread_path, 'insidepath': full_thread_path.replace(data['folderpath'] + '/.Comments', ''), } # data stored in file system as 'meta' file meta = { 'uid': config['uid'], 'timestamp': timestamp, 'topic': data['topic'], 'fileabout': '' if data['fileabout'] == "<brak>" else os.path.join(data['insidepath'], data['fileabout']) } # data stored in comment file comment = { 'uid': config['uid'], 'timestamp': timestamp, 'comment': data['comment'], 'readby': { config['uid']: timestamp }, 'history': [{ 'timestamp': timestamp, 'comment': data['comment'] }] } # creating thread's directory and writing data to files os.makedirs(full_thread_path) save_json(os.path.join(full_thread_path, 'meta'), meta) save_json(os.path.join(full_thread_path, timestamp + file_name_separator + config['uid']), comment) return JsonResponse(response, safe=False) except Exception: return HttpResponseServerError('Wystąpił nieznany błąd podczas dodawania nowego wątku.')