Zapisywanie i odczytywanie ustawień paneli z cookie. Potrzebne jest jeszcze ustawiani...
[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             result = "ok"
35         else:
36             result = "error"
37
38         errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
39         return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
40     else:
41         form = forms.BookForm()
42         form.fields['content'].initial = repo.get_file(path).data()
43
44     return direct_to_template(request, 'explorer/edit_text.html', extra_context={
45         'form': form,
46     })
47
48 def file_dc(request, path):
49     if request.method == 'POST':
50         form = forms.DublinCoreForm(request.POST)
51         if form.is_valid():
52             form.save(repo, path)
53             result = "ok"
54         else:
55             result = "error" 
56
57         errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
58         return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
59     else:
60         fulltext = repo.get_file(path).data()
61         form = forms.DublinCoreForm(text=fulltext)       
62
63     return direct_to_template(request, 'explorer/edit_dc.html', extra_context={
64         'form': form,
65         'fpath': path,
66     })
67
68 # Display the main editor view
69 def display_editor(request, path):
70     return direct_to_template(request, 'explorer/editor.html', extra_context={
71         'hash': path,
72     })
73
74 # ===============
75 # = Panel views =
76 # ===============
77
78 def xmleditor_panel(request, path):
79     form = forms.BookForm()
80     text = repo.get_file(path).data()
81     
82     return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
83         'fpath': path,
84         'text': text,
85     })
86     
87
88 def gallery_panel(request, path):
89     return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
90         'fpath': path,
91         'form': forms.ImageFoldersForm(),
92     })
93
94
95 def htmleditor_panel(request, path):
96     return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
97         'fpath': path,
98         'html': html.transform(repo.get_file(path).data(), is_file=False),
99     })
100  
101
102 def dceditor_panel(request, path):
103     text = repo.get_file(path).data()
104     form = forms.DublinCoreForm(text=text)       
105
106     return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
107         'fpath': path,
108         'form': form,
109     })
110
111
112 # =================
113 # = Utility views =
114 # =================
115 def folder_images(request, folder):
116     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
117         'images': models.get_images_from_folder(folder),
118     })
119
120
121 def _add_references(message, issues):
122     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
123
124 def _get_issues_for_file(path):
125     if not path.endswith('.xml'):
126         raise ValueError('Path must end with .xml')
127
128     book_id = path[:-4]
129     uf = None
130
131     try:
132         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
133         return json.loads(uf.read())
134     except urllib2.HTTPError:
135         return []
136     finally:
137         if uf: uf.close()