def save_snippet(request): # unpack lock secret, starting point, identity, text lock_secret = request.POST.getone('lock_secret') starting_point = int(request.POST.getone('starting_point')) identity_name = request.POST.getone('identity_name') identity_email = request.POST.getone('identity_email') snippet_text = request.POST.getone('snippet_text') inline = request.POST.get('inline') == '1' with repos.commit_lock: # find and validate the lock repo = repos.repo_from_request(request) index = repo.index if not repos.lock_is_valid(repo, index, 'snippet', starting_point, lock_secret): raise ValueError('Invalid lock') # save the snippet text repos.save_snippet_text(repo, index, starting_point, snippet_text) # remove the lock repos.remove_lock(repo, index, 'snippet', starting_point) # remove the snippet from remaining snippets repos.remove_snippet_from_remaining(repo, index, starting_point) # commit with identity os.environ['GIT_AUTHOR_NAME'] = identity_name os.environ['GIT_AUTHOR_EMAIL'] = identity_email commit_message = 'snippet: save' if inline: commit_message += ' (inline)' index.commit(commit_message) # return structure of snippet including resolved speaker names # for potential rendering master = repo.tree('master') speakers_map = repos.speakers_map(master) text = snippet_text.strip() lines = _split_lines_and_expand_abbreviations(text, speakers_map) return Response(json.dumps(lines), content_type='application/json')
def view(request): repo = repos.repo_from_request(request) master = repo.tree('master') transcription_info = repos.transcription_info(master) raw_snippets = {} for obj in master: if isinstance(obj, git.Blob): name, ext = os.path.splitext(obj.name) if ext == '.txt': try: starting_point = int(name) except ValueError: pass else: raw_snippets[starting_point] = obj.data_stream.read().decode('utf8') # Go through all snippets, whether they've been transcribed or not. snippets = [] speakers_map = repos.speakers_map(master) for starting_point in range(0, transcription_info['duration'], _snippet_ms()): text = raw_snippets.get(starting_point, '').strip() lines = _split_lines_and_expand_abbreviations(text, speakers_map) snippets.append((starting_point, lines)) return dict( _standard_response(repo, master), snippets=sorted(snippets), )
def read(request): repo, commit = repos.repo_from_request(request) # Return cached if found. cache_key = 'view-{0}'.format(commit.hexsha) content, mtime = cache.get_cached_content(cache_key) if content is None or request.GET.has_key('nocache'): mtime = commit.authored_date tree = commit.tree transcription_info, _ = repos.json_file_at_commit( repo, 'transcription.json', commit, required=True) raw_snippets = {} for obj in tree: if isinstance(obj, git.Blob): name, ext = os.path.splitext(obj.name) if ext == '.txt': try: starting_point = int(name) except ValueError: pass else: raw_snippets[starting_point] = obj.data_stream.read().decode('utf8') # Go through all snippets, whether they've been transcribed or not. snippets = [] speakers_map = repos.speakers_map(repo, commit) for starting_point in range(0, transcription_info['duration'], _snippet_ms()): text = raw_snippets.get(starting_point, '').strip() lines = _split_lines_and_expand_abbreviations(text, speakers_map) snippets.append((starting_point, lines)) data = dict( _standard_response(repo, commit), snippets=sorted(snippets), preamble_incomplete=repos.file_at_commit( repo, 'preamble_incomplete.html', commit, )[0], preamble_completed=repos.file_at_commit( repo, 'preamble_completed.html', commit, )[0], ) content = render('fanscribed:templates/view.mako', data, request=request) cache.cache_content(cache_key, content, mtime) return Response(content, date=mtime)
def snippets_updated(request): """Return formatted snippets that have been updated since the given revision.""" repo, request_commit = repos.repo_from_request(request) since_rev = request.GET.getone('since') # Return cached if found. cache_key = 'updated-{0}-{1}'.format(request_commit.hexsha, since_rev) content, mtime = cache.get_cached_content(cache_key) if content is None or request.GET.has_key('nocache'): since_commit = repo.commit(since_rev) files_updated = set() for commit in repo.iter_commits(request_commit): # Have we reached the end? if commit == since_commit: break # Look for snippet changes. for filename in commit.stats.files: if len(filename) == 20 and filename.endswith('.txt'): files_updated.add(filename) tree = request_commit.tree speakers_map = repos.speakers_map(repo, request_commit) snippets = [] for filename in files_updated: starting_point = int(filename[:16]) snippet = dict( starting_point=starting_point, ) text = tree[filename].data_stream.read().strip() snippet['lines'] = _split_lines_and_expand_abbreviations(text, speakers_map) snippets.append(snippet) data = dict( latest_revision=repos.latest_revision(repo), snippets=snippets, ) content = json.dumps(data) mtime = request_commit.authored_date cache.cache_content(cache_key, content, mtime) return Response(content, content_type='application/json', date=mtime)