1 # -*- coding: utf-8 -*-
2 from librarian import html
3 import hg, urllib2, time
5 from django.utils import simplejson as json
7 from librarian import dcparser, parser
8 from librarian import ParseError, ValidationError
10 from django.views.generic.simple import direct_to_template
12 from django.conf import settings
13 from django.http import HttpResponseRedirect, HttpResponse
15 from django.core.urlresolvers import reverse
16 from django.core.paginator import Paginator, InvalidPage, EmptyPage
18 from django.contrib.auth.decorators import login_required, permission_required
20 from explorer import forms, models
23 # Some useful decorators
26 """Open a repository for this view"""
27 def view_with_repo(request, *args, **kwargs):
28 kwargs['repo'] = hg.Repository(settings.REPOSITORY_PATH)
29 return view(request, *args, **kwargs)
33 def ajax_login_required(view):
34 """Similar ro @login_required, but instead of redirect,
35 just return some JSON stuff with error."""
36 def view_with_auth(request, *args, **kwargs):
37 if request.user.is_authenticated():
38 return view(request, *args, **kwargs)
40 return HttpResponse( json.dumps({'result': 'access_denied'}) );
47 def file_list(request, repo):
48 paginator = Paginator( repo.file_list('default'), 100);
49 bookform = forms.BookUploadForm()
52 page = int(request.GET.get('page', '1'))
57 files = paginator.page(page)
58 except (EmptyPage, InvalidPage):
59 files = paginator.page(paginator.num_pages)
61 return direct_to_template(request, 'explorer/file_list.html', extra_context={
62 'files': files, 'page': page, 'bookform': bookform,
65 @permission_required('explorer.can_add_files')
67 def file_upload(request, repo):
68 form = forms.BookUploadForm(request.POST, request.FILES)
70 f = request.FILES['file']
73 print 'Adding file: %s' % f.name
74 repo._add_file(f.name, f.read().decode('utf-8'))
75 repo._commit(message="File %s uploaded from platform by %s" %
76 (f.name, request.user.username), user=request.user.username)
78 repo.in_branch(upload_action, 'default')
79 return HttpResponseRedirect( reverse('editor_view', kwargs={'path': f.name}) )
81 return direct_to_template(request, 'explorer/file_upload.html',
82 extra_context = {'form' : form} )
90 def file_xml(request, repo, path):
91 if request.method == 'POST':
93 form = forms.BookForm(request.POST)
95 print 'Saving whole text.', request.user.username
97 print 'In branch: ' + repo.repo[None].branch()
98 repo._add_file(path, form.cleaned_data['content'])
99 repo._commit(message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'),\
100 user=request.user.username)
102 # wczytaj dokument z ciągu znaków -> weryfikacja
103 document = parser.WLDocument.from_string(form.cleaned_data['content'])
105 # save to user's branch
106 repo.in_branch(save_action, models.user_branch(request.user) );
107 except (ParseError, ValidationError), e:
111 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
113 return HttpResponse(json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}));
115 form = forms.BookForm()
116 data = repo.get_file(path, models.user_branch(request.user))
117 form.fields['content'].initial = data
118 return HttpResponse( json.dumps({'result': 'ok', 'content': data}) )
122 def file_dc(request, path, repo):
123 if request.method == 'POST':
124 form = forms.DublinCoreForm(request.POST)
129 file_contents = repo._get_file(path)
131 # wczytaj dokument z repozytorium
132 document = parser.WLDocument.from_string(file_contents)
133 document.book_info.update(form.cleaned_data)
138 repo._add_file(path, document.serialize())
140 message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), \
141 user=request.user.username )
144 repo.in_branch(save_action, models.user_branch(request.user) )
145 except (ParseError, ValidationError), e:
149 errors = ["Pole '%s': %s\n" % (field[0], field[1].as_text()) for field in form.errors.iteritems()]
151 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}) );
153 fulltext = repo.get_file(path, models.user_branch(request.user))
154 form = forms.DublinCoreForm(text=fulltext)
155 return HttpResponse( json.dumps({'result': 'ok', 'content': fulltext}) )
157 # Display the main editor view
160 def display_editor(request, path):
161 return direct_to_template(request, 'explorer/editor.html', extra_context={
162 'hash': path, 'panel_list': ['lewy', 'prawy'],
171 def xmleditor_panel(request, path, repo):
172 form = forms.BookForm()
173 text = repo.get_file(path, models.user_branch(request.user))
175 return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
182 def gallery_panel(request, path):
183 return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
185 'form': forms.ImageFoldersForm(),
190 def htmleditor_panel(request, path, repo):
191 user_branch = models.user_branch(request.user)
192 return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
194 'html': html.transform(repo.get_file(path, user_branch), is_file=False),
200 def dceditor_panel(request, path, repo):
201 user_branch = models.user_branch(request.user)
202 doc_text = repo.get_file(path, user_branch)
204 document = parser.WLDocument.from_string(doc_text)
205 form = forms.DublinCoreForm(info=document.book_info)
207 return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
217 def folder_images(request, folder):
218 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
219 'images': models.get_images_from_folder(folder),
223 def _add_references(message, issues):
224 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
226 def _get_issues_for_file(path):
227 if not path.endswith('.xml'):
228 raise ValueError('Path must end with .xml')
234 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
235 return json.loads(uf.read())
236 except urllib2.HTTPError:
245 def pull_requests(request):
246 return direct_to_template(request, 'manager/pull_request.html', extra_context = {
247 'objects': models.PullRequest.objects.all()} )