1 from librarian import html
3 from django.utils import simplejson as json
5 from django.views.generic.simple import direct_to_template
7 from django.conf import settings
8 from django.http import HttpResponseRedirect
9 from django.contrib.auth.decorators import login_required
11 from explorer import forms, models
13 repo = hg.Repository(settings.REPOSITORY_PATH)
15 def file_list(request):
16 return direct_to_template(request, 'explorer/file_list.html', extra_context={
17 'objects': repo.all_files(),
20 def file_xml(request, path):
21 if request.method == 'POST':
22 form = forms.BookForm(request.POST)
24 repo.add_file(path, form.cleaned_data['text'])
26 # add references to comment
27 issues = _get_issues_for_file(path)
28 commit_message = _add_references(form.cleaned_data['commit_message'], issues)
29 print 'Commiting with: ' + commit_message
31 repo.commit(message=commit_message, user=form.cleaned_data['user'])
32 return HttpResponseRedirect(request.get_full_path())
34 form = forms.BookForm()
35 form.fields['text'].initial = repo.get_file(path).data()
37 return direct_to_template(request, 'explorer/file_xml.html', extra_context={
40 'image_folders_form': forms.ImageFoldersForm(),
44 def file_html(request, path):
45 return direct_to_template(request, 'explorer/file_html.html', extra_context={
46 'object': html.transform(repo.get_file(path).data(), is_file=False),
48 'image_folders_form': forms.ImageFoldersForm(),
51 def folder_images(request, folder):
52 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
53 'images': models.get_images_from_folder(folder),
56 def _add_references(message, issues):
57 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
59 def _get_issues_for_file(path):
60 if not path.endswith('.xml'):
61 raise ValueError('Path must end with .xml')
67 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
68 return json.loads(uf.read())
69 except urllib2.HTTPError: