1 from librarian import html
2 import hg, urllib2, time
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, HttpResponse
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(),
24 def file_xml(request, path):
25 if request.method == 'POST':
26 form = forms.BookForm(request.POST)
28 print 'Saving whole text.', request.user.username
30 repo.add_file(path, form.cleaned_data['content'])
31 repo.commit(message='Local save at %s' % time.ctime(), user=request.user.username)
33 repo.in_branch('local_'+request.user.username, save_action);
38 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
39 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
41 form = forms.BookForm()
42 form.fields['content'].initial = repo.get_file(path).data()
44 return direct_to_template(request, 'explorer/edit_text.html', extra_context={
48 def file_dc(request, path):
49 if request.method == 'POST':
50 form = forms.DublinCoreForm(request.POST)
57 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
58 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
60 fulltext = repo.get_file(path).data()
61 form = forms.DublinCoreForm(text=fulltext)
63 return direct_to_template(request, 'explorer/edit_dc.html', extra_context={
68 # Display the main editor view
69 def display_editor(request, path):
70 return direct_to_template(request, 'explorer/editor.html', extra_context={
78 def xmleditor_panel(request, path):
79 form = forms.BookForm()
80 text = repo.get_file(path).data()
82 return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
88 def gallery_panel(request, path):
89 return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
91 'form': forms.ImageFoldersForm(),
95 def htmleditor_panel(request, path):
96 return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
98 'html': html.transform(repo.get_file(path).data(), is_file=False),
102 def dceditor_panel(request, path):
103 text = repo.get_file(path).data()
104 form = forms.DublinCoreForm(text=text)
106 return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
115 def folder_images(request, folder):
116 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
117 'images': models.get_images_from_folder(folder),
121 def _add_references(message, issues):
122 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
124 def _get_issues_for_file(path):
125 if not path.endswith('.xml'):
126 raise ValueError('Path must end with .xml')
132 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
133 return json.loads(uf.read())
134 except urllib2.HTTPError: