Nothing interesting.
[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 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             
24             # add references to comment
25             issues = _get_issues_for_file(path)
26             commit_message = _add_references(form.cleaned_data['commit_message'], issues)
27             print 'Commiting with: ' + commit_message
28
29             repo.commit(message=commit_message, user=form.cleaned_data['user'])
30             return HttpResponseRedirect(request.get_full_path())
31     else:
32         form = forms.BookForm()
33         form.fields['text'].initial = repo.get_file(path).data()
34     
35     return direct_to_template(request, 'explorer/file_xml.html', extra_context={
36         'hash': path,
37         'form': form,
38         'image_folders_form': forms.ImageFoldersForm(),
39     })
40
41
42 # ===============
43 # = Panel views =
44 # ===============
45 def xmleditor_panel(request, path):
46     form = forms.BookForm()
47     text = repo.get_file(path).data()
48     
49     return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
50         'text': text,
51     })
52     
53
54 def gallery_panel(request, path):
55     return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
56         'form': forms.ImageFoldersForm(),
57     })
58
59
60 def htmleditor_panel(request, path):
61     return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
62         'html': html.transform(repo.get_file(path).data(), is_file=False),
63     })
64  
65
66 def folder_images(request, folder):
67     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
68         'images': models.get_images_from_folder(folder),
69     })
70
71 def _add_references(message, issues):
72     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
73
74 def _get_issues_for_file(path):
75     if not path.endswith('.xml'):
76         raise ValueError('Path must end with .xml')
77
78     book_id = path[:-4]
79     uf = None
80
81     try:
82         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
83         return json.loads(uf.read())
84     except urllib2.HTTPError:
85         return []
86     finally:
87         if uf: uf.close()