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
15 def inner(request, *args, **kwargs):
16 kwargs['repo'] = hg.Repository(settings.REPOSITORY_PATH)
17 return func(request, *args, **kwargs)
21 def file_list(request, repo):
22 return direct_to_template(request, 'explorer/file_list.html', extra_context={
23 'objects': repo.all_files(),
30 def file_xml(request, repo, path):
31 if request.method == 'POST':
32 form = forms.BookForm(request.POST)
34 print 'Saving whole text.', request.user.username
36 print 'In branch: ' + repo.repo[None].branch()
37 print repo._add_file(path, form.cleaned_data['content'])
38 print repo.repo.status()
39 print repo._commit(message='Local save at %s' % time.ctime(), user=request.user.username)
41 print repo.in_branch(save_action, models.user_branch(request.user) );
46 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
47 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
49 form = forms.BookForm()
50 form.fields['content'].initial = repo.get_file(path, models.user_branch(request.user))
51 return direct_to_template(request, 'explorer/edit_text.html', extra_context={
56 def file_dc(request, path, repo):
57 if request.method == 'POST':
58 form = forms.DublinCoreForm(request.POST)
65 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
66 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
68 fulltext = repo.get_file(path, models.user_branch(request.user))
69 form = forms.DublinCoreForm(text=fulltext)
71 return direct_to_template(request, 'explorer/edit_dc.html', extra_context={
76 # Display the main editor view
77 def display_editor(request, path):
78 return direct_to_template(request, 'explorer/editor.html', extra_context={
79 'hash': path, 'panel_list': ['lewy', 'prawy'],
87 def xmleditor_panel(request, path, repo):
88 form = forms.BookForm()
89 text = repo.get_file(path, models.user_branch(request.user))
91 return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
97 def gallery_panel(request, path):
98 return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
100 'form': forms.ImageFoldersForm(),
104 def htmleditor_panel(request, path, repo):
105 user_branch = models.user_branch(request.user)
106 return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
108 'html': html.transform(repo.get_file(path, user_branch), is_file=False),
113 def dceditor_panel(request, path, repo):
114 user_branch = models.user_branch(request.user)
115 text = repo.get_file(path, user_branch)
116 form = forms.DublinCoreForm(text=text)
118 return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
127 def folder_images(request, folder):
128 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
129 'images': models.get_images_from_folder(folder),
133 def _add_references(message, issues):
134 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
136 def _get_issues_for_file(path):
137 if not path.endswith('.xml'):
138 raise ValueError('Path must end with .xml')
144 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
145 return json.loads(uf.read())
146 except urllib2.HTTPError: