Drobne poprawki w CSS:
[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
7 from django.conf import settings
8 from django.http import HttpResponseRedirect, HttpResponse
9 from django.contrib.auth.decorators import login_required
10
11 from explorer import forms, models
12
13 repo = hg.Repository(settings.REPOSITORY_PATH)
14
15 def file_list(request):
16     return direct_to_template(request, 'explorer/file_list.html', extra_context={
17         'objects': repo.all_files(),
18     })
19
20 def file_xml(request, path):
21     if request.method == 'POST':
22         form = forms.BookForm(request.POST)
23         if form.is_valid():
24             # save the changes to a local branch
25 #           repo.write_lock()
26             print request.user
27 #            repo.switch_to_branch(request.user.name)           
28 #            repo.add_file(path, form.cleaned_data['text'])
29             
30             # add references to comment
31             issues = _get_issues_for_file(path)
32             commit_message = _add_references(form.cleaned_data['commit_message'], issues)
33             print 'Commiting with: ' + commit_message
34
35 #            repo.commit(message=commit_message, user=form.cleaned_data['user'])
36         return HttpResponse( json.dumps({'message': commit_message}) )
37     else:
38         form = forms.BookForm()
39         form.fields['text'].initial = repo.get_file(path).data()
40     
41     return direct_to_template(request, 'explorer/file_xml.html', extra_context={
42         'hash': path,
43         'form': form,
44         'image_folders_form': forms.ImageFoldersForm(),
45     })
46
47
48 # ===============
49 # = Panel views =
50 # ===============
51 def xmleditor_panel(request, path):
52     form = forms.BookForm()
53     text = repo.get_file(path).data()
54     
55     return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
56         'text': text,
57     })
58     
59
60 def gallery_panel(request, path):
61     return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
62         'form': forms.ImageFoldersForm(),
63     })
64
65
66 def htmleditor_panel(request, path):
67     return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
68         'html': html.transform(repo.get_file(path).data(), is_file=False),
69     })
70  
71
72 def dceditor_panel(request, path):
73     if request.method == 'POST':
74         form = forms.DublinCoreForm(request.POST)
75         if form.is_valid():
76             form.save(repo, path)
77             repo.commit(message='%s: DublinCore edited' % path)
78     else:
79         text = repo.get_file(path).data()
80         form = forms.DublinCoreForm(text=text)       
81
82     return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
83         'form': form,
84     })
85
86
87 # =================
88 # = Utility views =
89 # =================
90 def folder_images(request, folder):
91     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
92         'images': models.get_images_from_folder(folder),
93     })
94
95
96 def _add_references(message, issues):
97     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
98
99 def _get_issues_for_file(path):
100     if not path.endswith('.xml'):
101         raise ValueError('Path must end with .xml')
102
103     book_id = path[:-4]
104     uf = None
105
106     try:
107         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
108         return json.loads(uf.read())
109     except urllib2.HTTPError:
110         return []
111     finally:
112         if uf: uf.close()