Server-side refactor.
[redakcja.git] / apps / explorer / views.py
1 from librarian import html
2 import hg, urllib2, time
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
21 #
22 # Edit the file
23 #
24 def file_xml(request, path):
25     if request.method == 'POST':
26         form = forms.BookForm(request.POST)
27         if form.is_valid():
28             print 'Saving whole text.', request.user.username
29             def save_action():
30                 repo.add_file(path, form.cleaned_data['content'])
31                 repo.commit(message='Local save at %s' % time.ctime(), user=request.user.username)
32
33             repo.in_branch('local_'+request.user.username, save_action);
34             return HttpResponse( json.dumps({'result': 'ok', 'errors': []}) );
35     else:
36         form = forms.BookForm()
37         form.fields['content'].initial = repo.get_file(path).data()
38
39     return direct_to_template(request, 'explorer/edit_text.html', extra_context={
40         'form': form,
41     })
42
43 def file_dc(request, path):
44     return HttpResponse("N/A")
45
46 # Display the main editor view
47 def display_editor(request, path):
48     return direct_to_template(request, 'explorer/editor.html', extra_context={
49         'hash': path,
50     })
51
52 # ===============
53 # = Panel views =
54 # ===============
55
56 def xmleditor_panel(request, path):
57     form = forms.BookForm()
58     text = repo.get_file(path).data()
59     
60     return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
61         'fpath': path,
62         'text': text,
63     })
64     
65
66 def gallery_panel(request, path):
67     return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
68         'fpath': path,
69         'form': forms.ImageFoldersForm(),
70     })
71
72
73 def htmleditor_panel(request, path):
74     return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
75         'fpath': path,
76         'html': html.transform(repo.get_file(path).data(), is_file=False),
77     })
78  
79
80 def dceditor_panel(request, path):
81     if request.method == 'POST':
82         form = forms.DublinCoreForm(request.POST)
83         if form.is_valid():
84             form.save(repo, path)
85             repo.commit(message='%s: DublinCore edited' % path)
86     else:
87         text = repo.get_file(path).data()
88         form = forms.DublinCoreForm(text=text)       
89
90     return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
91         'fpath': path,
92         'form': form,
93     })
94
95
96 # =================
97 # = Utility views =
98 # =================
99 def folder_images(request, folder):
100     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
101         'images': models.get_images_from_folder(folder),
102     })
103
104
105 def _add_references(message, issues):
106     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
107
108 def _get_issues_for_file(path):
109     if not path.endswith('.xml'):
110         raise ValueError('Path must end with .xml')
111
112     book_id = path[:-4]
113     uf = None
114
115     try:
116         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
117         return json.loads(uf.read())
118     except urllib2.HTTPError:
119         return []
120     finally:
121         if uf: uf.close()