1 # -*- coding: utf-8 -*-
4 from librarian import html, parser, dcparser, ParseError, ValidationError
6 from django.conf import settings
7 from django.contrib.auth.decorators import login_required, permission_required
9 from django.core.urlresolvers import reverse
10 from django.http import HttpResponseRedirect, HttpResponse
11 from django.utils import simplejson as json
12 from django.views.generic.simple import direct_to_template
14 from explorer import forms, models
15 from toolbar import models as toolbar_models
18 # Some useful decorators
20 def file_branch(path, user=None):
21 return ('personal_'+user.username + '_' if user is not None else '') \
25 """Open a repository for this view"""
26 def view_with_repo(request, *args, **kwargs):
27 kwargs['repo'] = hg.Repository(settings.REPOSITORY_PATH)
28 return view(request, *args, **kwargs)
32 def ajax_login_required(view):
33 """Similar ro @login_required, but instead of redirect,
34 just return some JSON stuff with error."""
35 def view_with_auth(request, *args, **kwargs):
36 if request.user.is_authenticated():
37 return view(request, *args, **kwargs)
39 return HttpResponse( json.dumps({'result': 'access_denied', 'errors': ['Brak dostępu.']}) );
46 def file_list(request, repo):
48 latest_default = repo.get_branch_tip('default')
49 files = [ f for f in repo.repo[latest_default] if not f.startswith('.')]
50 bookform = forms.BookUploadForm()
52 return direct_to_template(request, 'explorer/file_list.html', extra_context={
53 'files': files, 'bookform': bookform,
56 @permission_required('explorer.can_add_files')
58 def file_upload(request, repo):
60 if request.method == 'POST':
61 form = forms.BookUploadForm(request.POST, request.FILES)
65 f = request.FILES['file']
66 decoded = f.read().decode('utf-8')
67 path = form.cleaned_data['bookname']
70 repo._add_file(path ,decoded.encode('utf-8') )
71 repo._commit(message="File %s uploaded by user %s" % \
72 (path, request.user.username), user=request.user.username)
74 repo.in_branch(upload_action, 'default')
76 # if everything is ok, redirect to the editor
77 return HttpResponseRedirect( reverse('editor_view',
78 kwargs={'path': path}) )
80 except hg.RepositoryException, e:
81 other_errors.append(u'Błąd repozytorium: ' + unicode(e) )
82 #except UnicodeDecodeError, e:
83 # other_errors.append(u'Niepoprawne kodowanie pliku: ' + e.reason \
84 # + u'. Żądane kodowanie: ' + e.encoding)
88 form = forms.BookUploadForm()
89 return direct_to_template(request, 'explorer/file_upload.html',\
90 extra_context = {'form' : form, 'other_errors': other_errors})
98 def file_xml(request, repo, path):
99 if request.method == 'POST':
102 form = forms.BookForm(request.POST)
104 print 'Saving whole text.', request.user.username
106 # encode it back to UTF-8, so we can put it into repo
107 encoded_data = form.cleaned_data['content'].encode('utf-8')
110 repo._add_file(path, encoded_data)
111 repo._commit(message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'),\
112 user=request.user.username)
115 # wczytaj dokument z ciągu znaków -> weryfikacja
116 document = parser.WLDocument.from_string(form.cleaned_data['content'])
117 except (ParseError, ValidationError), e:
118 warnings = [u'Niepoprawny dokument XML: ' + unicode(e.message)]
120 # save to user's branch
121 repo.in_branch(save_action, file_branch(path, request.user) );
122 except UnicodeDecodeError, e:
123 errors = [u'Błąd kodowania danych przed zapisem: ' + unicode(e.message)]
124 except hg.RepositoryException, e:
125 errors = [u'Błąd repozytorium: ' + unicode(e.message)]
128 errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
130 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok',
131 'errors': errors, 'warnings': warnings}) );
133 form = forms.BookForm()
134 data = repo.get_file(path, file_branch(path, request.user))
135 form.fields['content'].initial = data
136 return HttpResponse( json.dumps({'result': 'ok', 'content': data}) )
140 def file_update_local(request, path, repo):
144 wlock = repo.write_lock()
146 tipA = repo.get_branch_tip('default')
147 tipB = repo.get_branch_tip( file_branch(path, request.user) )
149 nodeA = repo.getnode(tipA)
150 nodeB = repo.getnode(tipB)
152 # do some wild checks - see file_commit() for more info
153 if (repo.common_ancestor(tipA, tipB) == nodeA) \
154 or (nodeB in nodeA.parents()):
155 result = 'nothing-to-do'
158 repo.merge_revisions(tipB, tipA, \
159 request.user.username, 'Personal branch update.')
161 except hg.UncleanMerge, e:
163 result = 'fatal-error'
164 except hg.RepositoryException, e:
166 result = 'fatal-error'
171 raise Exception("Ouch, this shouldn't happen!")
173 return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
177 def file_commit(request, path, repo):
180 local_modified = False
181 if request.method == 'POST':
182 form = forms.MergeForm(request.POST)
185 wlock = repo.write_lock()
187 tipA = repo.get_branch_tip('default')
188 tipB = repo.get_branch_tip( file_branch(path, request.user) )
190 nodeA = repo.getnode(tipA)
191 nodeB = repo.getnode(tipB)
193 print repr(nodeA), repr(nodeB), repo.common_ancestor(tipA, tipB), repo.common_ancestor(tipB, tipA)
195 if repo.common_ancestor(tipB, tipA) == nodeA:
199 # * <- can also be here!
204 # The local branch has been recently updated,
205 # so we don't need to update yet again, but we need to
206 # merge down to default branch, even if there was
207 # no commit's since last update
208 repo.merge_revisions(tipA, tipB, \
209 request.user.username, form.cleaned_data['message'])
211 elif any( p.branch()==nodeB.branch() for p in nodeA.parents()):
219 # Default has no changes, to update from this branch
220 # since the last merge of local to default.
221 if nodeB not in nodeA.parents():
222 repo.merge_revisions(tipA, tipB, \
223 request.user.username, form.cleaned_data['message'])
226 result = 'nothing-to-do'
227 elif repo.common_ancestor(tipA, tipB) == nodeB:
231 # * <- this case overlaps with previos one
237 # There was a recent merge to the defaul branch and
238 # no changes to local branch recently.
240 # Use the fact, that user is prepared to see changes, to
241 # update his branch if there are any
242 if nodeB not in nodeA.parents():
243 repo.merge_revisions(tipB, tipA, \
244 request.user.username, 'Personal branch update during merge.')
245 local_modified = True
248 result = 'nothing-to-do'
250 # both branches have changes made to them, so
252 repo.merge_revisions(tipB, tipA, \
253 request.user.username, 'Personal branch update during merge.')
255 local_modified = True
258 tipB = repo.get_branch_tip( file_branch(path, request.user) )
260 # and merge back to the default
261 repo.merge_revisions(tipA, tipB, \
262 request.user.username, form.cleaned_data['message'])
264 except hg.UncleanMerge, e:
266 result = 'fatal-error'
267 except hg.RepositoryException, e:
269 result = 'fatal-error'
274 errors = [ form.errors['message'].as_text() ]
276 result = 'fatal-error'
278 return HttpResponse( json.dumps({'result': result, 'errors': errors, 'localmodified': local_modified}) );
280 return HttpResponse( json.dumps({'result': 'fatal-error', 'errors': ['No data posted']}) )
285 def file_dc(request, path, repo):
288 if request.method == 'POST':
289 form = forms.DublinCoreForm(request.POST)
294 file_contents = repo._get_file(path)
296 # wczytaj dokument z repozytorium
297 document = parser.WLDocument.from_string(file_contents)
298 document.book_info.update(form.cleaned_data)
301 repo._write_file(path, document.serialize().encode('utf-8'))
303 message=(form.cleaned_data['commit_message'] or 'Lokalny zapis platformy.'), \
304 user=request.user.username )
307 repo.in_branch(save_action, file_branch(path, request.user) )
308 except UnicodeEncodeError, e:
309 errors = ['Bład wewnętrzny: nie można zakodować pliku do utf-8']
310 except (ParseError, ValidationError), e:
314 errors = ["Pole '%s': %s\n" % (field[0], field[1].as_text()) for field in form.errors.iteritems()]
316 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok', 'errors': errors}) );
318 # this is unused currently, but may come in handy
322 fulltext = repo.get_file(path, file_branch(path, request.user))
323 bookinfo = dcparser.BookInfo.from_string(fulltext)
324 content = bookinfo.to_dict()
325 except (ParseError, ValidationError), e:
328 return HttpResponse( json.dumps({'result': errors and 'error' or 'ok',
329 'errors': errors, 'content': content }) )
331 # Display the main editor view
335 def display_editor(request, path, repo):
337 # this is the only entry point where we create an autobranch for the user
338 # if it doesn't exists. All other views SHOULD fail.
339 def ensure_branch_exists():
340 parent = repo.get_branch_tip('default')
341 repo._create_branch(file_branch(path, request.user), parent)
344 repo.with_wlock(ensure_branch_exists)
346 return direct_to_template(request, 'explorer/editor.html', extra_context={
348 'panel_list': ['lewy', 'prawy'],
349 'scriptlets': toolbar_models.Scriptlet.objects.all()
352 return direct_to_template(request, 'explorer/nofile.html', \
353 extra_context = { 'path': path })
361 def xmleditor_panel(request, path, repo):
362 text = repo.get_file(path, file_branch(path, request.user))
364 return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
371 def gallery_panel(request, path):
372 return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
374 'form': forms.ImageFoldersForm(),
379 def htmleditor_panel(request, path, repo):
380 user_branch = file_branch(path, request.user)
382 return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
384 'html': html.transform(repo.get_file(path, user_branch), is_file=False),
386 except (ParseError, ValidationError), e:
387 return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
388 'fpath': path, 'exception_type': type(e).__name__, 'exception': e, 'panel_name': 'Edytor HTML'})
392 def dceditor_panel(request, path, repo):
393 user_branch = file_branch(path, request.user)
396 doc_text = repo.get_file(path, user_branch)
397 document = parser.WLDocument.from_string(doc_text)
398 form = forms.DublinCoreForm(info=document.book_info)
399 return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
403 except (ParseError, ValidationError), e:
404 return direct_to_template(request, 'explorer/panels/parse_error.html', extra_context={
405 'fpath': path, 'exception_type': type(e).__name__, 'exception': e,
406 'panel_name': 'Edytor DublinCore'})
412 def folder_images(request, folder):
413 return direct_to_template(request, 'explorer/folder_images.html', extra_context={
414 'images': models.get_images_from_folder(folder),
418 def _add_references(message, issues):
419 return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
421 def _get_issues_for_file(path):
422 if not path.endswith('.xml'):
423 raise ValueError('Path must end with .xml')
429 uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
430 return json.loads(uf.read())
431 except urllib2.HTTPError:
440 def pull_requests(request):
441 return direct_to_template(request, 'manager/pull_request.html', extra_context = {
442 'objects': models.PullRequest.objects.all()} )