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
10 from django.core.urlresolvers import reverse
11 from django.core.paginator import Paginator, InvalidPage, EmptyPage
13 from django.contrib.auth.decorators import login_required
15 from explorer import forms, models
18 # Some useful decorators
21 """Open a repository for this view"""
22 def view_with_repo(request, *args, **kwargs):
23 kwargs['repo'] = hg.Repository(settings.REPOSITORY_PATH)
24 return view(request, *args, **kwargs)
28 def ajax_login_required(view):
29 """Similar ro @login_required, but instead of redirect,
30 just return some JSON stuff with error."""
31 def view_with_auth(request, *args, **kwargs):
32 if request.user.is_authenticated():
33 return view(request, *args, **kwargs)
35 return HttpResponse( json.dumps({'result': 'access_denied'}) );
42 def file_list(request, repo):
43 paginator = Paginator( repo.file_list(), 100);
44 bookform = forms.BookUploadForm()
47 page = int(request.GET.get('page', '1'))
52 files = paginator.page(page)
53 except (EmptyPage, InvalidPage):
54 files = paginator.page(paginator.num_pages)
56 return direct_to_template(request, 'explorer/file_list.html', extra_context={
57 'files': files, 'page': page, 'bookform': bookform,
62 def file_upload(request, repo):
63 form = forms.BookUploadForm(request.POST, request.FILES)
65 f = request.FILES['file']
66 print 'Adding file: %s' % f.name
67 repo.add_file(f.name, f.read().decode('utf-8'))
68 return HttpResponseRedirect( reverse('editor_view', kwargs={'path': f.name}) )
70 return direct_to_template(request, 'explorer/file_upload.html',
71 extra_context = {'form' : form} )
79 def file_xml(request, repo, path):
80 if request.method == 'POST':
81 form = forms.BookForm(request.POST)
83 print 'Saving whole text.', request.user.username
85 print 'In branch: ' + repo.repo[None].branch()
86 repo._add_file(path, form.cleaned_data['content'])
87 repo._commit(message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), user=request.user.username)
89 print repo.in_branch(save_action, models.user_branch(request.user) );
94 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
95 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
97 form = forms.BookForm()
98 data = repo.get_file(path, models.user_branch(request.user))
99 form.fields['content'].initial = data
100 return HttpResponse( json.dumps({'result': 'ok', 'content': data}) )
104 def file_dc(request, path, repo):
105 if request.method == 'POST':
106 form = forms.DublinCoreForm(request.POST)
108 form.save(repo, path)
113 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
114 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
116 fulltext = repo.get_file(path, models.user_branch(request.user))
117 form = forms.DublinCoreForm(text=fulltext)
118 return HttpResponse( json.dumps({'result': 'ok', 'content': fulltext}) )
120 # Display the main editor view
123 def display_editor(request, path):
124 return direct_to_template(request, 'explorer/editor.html', extra_context={
125 'hash': path, 'panel_list': ['lewy', 'prawy'],
134 def xmleditor_panel(request, path, repo):
135 form = forms.BookForm()
136 text = repo.get_file(path, models.user_branch(request.user))
138 return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
145 def gallery_panel(request, path):
146 return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
148 'form': forms.ImageFoldersForm(),
153 def htmleditor_panel(request, path, repo):
154 user_branch = models.user_branch(request.user)
155 return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
157 'html': html.transform(repo.get_file(path, user_branch), is_file=False),
163 def dceditor_panel(request, path, repo):
164 user_branch = models.user_branch(request.user)
165 text = repo.get_file(path, user_branch)
166 form = forms.DublinCoreForm(text=text)
168 return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
178 def folder_images(request, folder):
179 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
180 'images': models.get_images_from_folder(folder),
184 def _add_references(message, issues):
185 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
187 def _get_issues_for_file(path):
188 if not path.endswith('.xml'):
189 raise ValueError('Path must end with .xml')
195 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
196 return json.loads(uf.read())
197 except urllib2.HTTPError:
206 def pull_requests(request):
207 return direct_to_template(request, 'manager/pull_request.html', extra_context = {
208 'objects': models.PullRequest.objects.all()} )