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)
134 rdf_ns = dcparser.BookInfo.RDF
135 dc_ns = dcparser.BookInfo.DC
137 rdf_attrs = {rdf_ns('about'): form.cleaned_data.pop('about')}
140 for key, value in form.cleaned_data.items():
141 field_dict[ dc_ns(key) ] = value if isinstance(value, list) else [value]
145 new_info = dcparser.BookInfo(rdf_attrs, field_dict)
146 document.book_info = new_info
151 repo._add_file(path, document.serialize())
153 message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), \
154 user=request.user.username )
157 repo.in_branch(save_action, models.user_branch(request.user) )
158 except (ParseError, ValidationError), e:
162 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
164 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}) );
166 fulltext = repo.get_file(path, models.user_branch(request.user))
167 form = forms.DublinCoreForm(text=fulltext)
168 return HttpResponse( json.dumps({'result': 'ok', 'content': fulltext}) )
170 # Display the main editor view
173 def display_editor(request, path):
174 return direct_to_template(request, 'explorer/editor.html', extra_context={
175 'hash': path, 'panel_list': ['lewy', 'prawy'],
184 def xmleditor_panel(request, path, repo):
185 form = forms.BookForm()
186 text = repo.get_file(path, models.user_branch(request.user))
188 return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
195 def gallery_panel(request, path):
196 return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
198 'form': forms.ImageFoldersForm(),
203 def htmleditor_panel(request, path, repo):
204 user_branch = models.user_branch(request.user)
205 return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
207 'html': html.transform(repo.get_file(path, user_branch), is_file=False),
213 def dceditor_panel(request, path, repo):
214 user_branch = models.user_branch(request.user)
215 doc_text = repo.get_file(path, user_branch)
217 document = parser.WLDocument.from_string(doc_text)
218 form = forms.DublinCoreForm(info=document.book_info)
220 return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
230 def folder_images(request, folder):
231 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
232 'images': models.get_images_from_folder(folder),
236 def _add_references(message, issues):
237 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
239 def _get_issues_for_file(path):
240 if not path.endswith('.xml'):
241 raise ValueError('Path must end with .xml')
247 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
248 return json.loads(uf.read())
249 except urllib2.HTTPError:
258 def pull_requests(request):
259 return direct_to_template(request, 'manager/pull_request.html', extra_context = {
260 'objects': models.PullRequest.objects.all()} )