90115aaa4b34758905ece9eb6f2ace23756d3cd6
[redakcja.git] / apps / explorer / views.py
1 from librarian import html
2 import hg, urllib2, json
3
4 from django.views.generic.simple import direct_to_template
5 from django.conf import settings
6 from django.http import HttpResponseRedirect
7
8 from explorer import forms, models
9
10 repo = hg.Repository(settings.REPOSITORY_PATH)
11
12 def file_list(request):
13     return direct_to_template(request, 'explorer/file_list.html', extra_context={
14         'objects': repo.all_files(),
15     })
16
17
18 def file_xml(request, path):
19     if request.method == 'POST':
20         form = forms.BookForm(request.POST)
21         if form.is_valid():
22             repo.add_file(path, form.cleaned_data['text'])
23             # issues = _get_issues_for_file(path)
24             # commit_message = _add_references(form.cleaned_data['commit_message'], issued)
25             repo.commit(message=form.cleaned_data['commit_message'], user=form.cleaned_data['user'])
26             return HttpResponseRedirect(request.get_full_path())
27     else:
28         form = forms.BookForm()
29         form.fields['text'].initial = repo.get_file(path).data()
30     
31     return direct_to_template(request, 'explorer/file_xml.html', extra_context={
32         'hash': path,
33         'form': form,
34         'image_folders_form': forms.ImageFoldersForm(),
35     })
36
37
38 def file_html(request, path):
39     return direct_to_template(request, 'explorer/file_html.html', extra_context={
40         'object': html.transform(repo.get_file(path).data(), is_file=False),
41         'hash': path,
42         'image_folders_form': forms.ImageFoldersForm(),
43     })
44   
45 def folder_images(request, folder):
46     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
47         'images': models.get_images_from_folder(folder),
48     })
49
50 def _add_references(message, issues):
51     # TODO
52     pass
53
54 def _get_issues_for_file(path):
55     if not path.endswith('.xml'):
56         raise ValueError('Path must end with .xml')
57
58     book_id = path[:-3]
59     uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/%s/issues' % book_id)
60
61     try:
62         return json.loads(uf.read())
63     finally:
64         uf.close()