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
14 # Some useful decorators
17 """Open a repository for this view"""
18 def view_with_repo(request, *args, **kwargs):
19 kwargs['repo'] = hg.Repository(settings.REPOSITORY_PATH)
20 return view(request, *args, **kwargs)
24 def ajax_login_required(view):
25 """Similar ro @login_required, but instead of redirect,
26 just return some JSON stuff with error."""
27 def view_with_auth(request, *args, **kwargs):
28 if request.user.is_authenticated():
29 return view(request, *args, **kwargs)
31 return HttpResponse( json.dumps({'result': 'access_denied'}) );
39 def file_list(request, repo):
40 return direct_to_template(request, 'explorer/file_list.html', extra_context={
41 'objects': repo.all_files(),
49 def file_xml(request, repo, path):
50 if request.method == 'POST':
51 form = forms.BookForm(request.POST)
53 print 'Saving whole text.', request.user.username
55 print 'In branch: ' + repo.repo[None].branch()
56 print repo._add_file(path, form.cleaned_data['content'])
57 print repo.repo.status()
58 print repo._commit(message='Local save at %s' % time.ctime(), user=request.user.username)
60 print repo.in_branch(save_action, models.user_branch(request.user) );
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 form = forms.BookForm()
69 data = repo.get_file(path, models.user_branch(request.user))
70 form.fields['content'].initial = data
71 return HttpResponse( json.dumps({'result': 'ok', 'content': data}) )
75 def file_dc(request, path, repo):
76 if request.method == 'POST':
77 form = forms.DublinCoreForm(request.POST)
84 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
85 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
87 fulltext = repo.get_file(path, models.user_branch(request.user))
88 form = forms.DublinCoreForm(text=fulltext)
89 return HttpResponse( json.dumps({'result': 'ok', 'content': fulltext}) )
91 # Display the main editor view
94 def display_editor(request, path):
95 return direct_to_template(request, 'explorer/editor.html', extra_context={
96 'hash': path, 'panel_list': ['lewy', 'prawy'],
105 def xmleditor_panel(request, path, repo):
106 form = forms.BookForm()
107 text = repo.get_file(path, models.user_branch(request.user))
109 return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
116 def gallery_panel(request, path):
117 return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
119 'form': forms.ImageFoldersForm(),
124 def htmleditor_panel(request, path, repo):
125 user_branch = models.user_branch(request.user)
126 return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
128 'html': html.transform(repo.get_file(path, user_branch), is_file=False),
134 def dceditor_panel(request, path, repo):
135 user_branch = models.user_branch(request.user)
136 text = repo.get_file(path, user_branch)
137 form = forms.DublinCoreForm(text=text)
139 return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
149 def folder_images(request, folder):
150 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
151 'images': models.get_images_from_folder(folder),
155 def _add_references(message, issues):
156 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
158 def _get_issues_for_file(path):
159 if not path.endswith('.xml'):
160 raise ValueError('Path must end with .xml')
166 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
167 return json.loads(uf.read())
168 except urllib2.HTTPError:
177 def pull_requests(request):
178 return direct_to_template(request, 'manager/pull_request.html', extra_context = {
179 'objects': models.PullRequest.objects.all()} )