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