896ed09ce702c0ed0e7644ba156a68a18699eac1
[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
14 def with_repo(func):
15     def inner(request, *args, **kwargs):          
16         kwargs['repo'] = hg.Repository(settings.REPOSITORY_PATH)
17         return func(request, *args, **kwargs)
18     return inner
19
20 @with_repo
21 def file_list(request, repo):
22     return direct_to_template(request, 'explorer/file_list.html', extra_context={
23         'objects': repo.all_files(),
24     })
25 #
26 # Edit the file
27 #
28
29 @with_repo
30 def file_xml(request, repo, path):
31     if request.method == 'POST':
32         form = forms.BookForm(request.POST)
33         if form.is_valid():
34             print 'Saving whole text.', request.user.username
35             def save_action():
36                 print 'In branch: ' + repo.repo[None].branch()
37                 print repo._add_file(path, form.cleaned_data['content'])
38                 print repo.repo.status()
39                 print repo._commit(message='Local save at %s' % time.ctime(), user=request.user.username)
40
41             print repo.in_branch(save_action, models.user_branch(request.user) );
42             result = "ok"
43         else:
44             result = "error"
45
46         errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
47         return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
48     else:
49         form = forms.BookForm()
50         form.fields['content'].initial = repo.get_file(path, models.user_branch(request.user))
51     return direct_to_template(request, 'explorer/edit_text.html', extra_context={
52         'form': form,
53     })
54
55 @with_repo
56 def file_dc(request, path, repo):
57     if request.method == 'POST':
58         form = forms.DublinCoreForm(request.POST)
59         if form.is_valid():
60             form.save(repo, path)
61             result = "ok"
62         else:
63             result = "error" 
64
65         errors = dict( (field[0], field[1].as_text()) for field in form.errors.iteritems() )
66         return HttpResponse( json.dumps({'result': result, 'errors': errors}) );
67     else:
68         fulltext = repo.get_file(path, models.user_branch(request.user))
69         form = forms.DublinCoreForm(text=fulltext)       
70
71     return direct_to_template(request, 'explorer/edit_dc.html', extra_context={
72         'form': form,
73         'fpath': path,
74     })
75
76 # Display the main editor view
77 def display_editor(request, path):
78     return direct_to_template(request, 'explorer/editor.html', extra_context={
79         'hash': path, 'panel_list': ['lewy', 'prawy'],
80     })
81
82 # ===============
83 # = Panel views =
84 # ===============
85
86 @with_repo
87 def xmleditor_panel(request, path, repo):
88     form = forms.BookForm()
89     text = repo.get_file(path, models.user_branch(request.user))
90     
91     return direct_to_template(request, 'explorer/panels/xmleditor.html', extra_context={
92         'fpath': path,
93         'text': text,
94     })
95     
96
97 def gallery_panel(request, path):
98     return direct_to_template(request, 'explorer/panels/gallery.html', extra_context={
99         'fpath': path,
100         'form': forms.ImageFoldersForm(),
101     })
102
103 @with_repo
104 def htmleditor_panel(request, path, repo):
105     user_branch = models.user_branch(request.user)
106     return direct_to_template(request, 'explorer/panels/htmleditor.html', extra_context={
107         'fpath': path,
108         'html': html.transform(repo.get_file(path, user_branch), is_file=False),
109     })
110  
111
112 @with_repo
113 def dceditor_panel(request, path, repo):
114     user_branch = models.user_branch(request.user)
115     text = repo.get_file(path, user_branch)
116     form = forms.DublinCoreForm(text=text)       
117
118     return direct_to_template(request, 'explorer/panels/dceditor.html', extra_context={
119         'fpath': path,
120         'form': form,
121     })
122
123
124 # =================
125 # = Utility views =
126 # =================
127 def folder_images(request, folder):
128     return direct_to_template(request, 'explorer/folder_images.html', extra_context={
129         'images': models.get_images_from_folder(folder),
130     })
131
132
133 def _add_references(message, issues):
134     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
135
136 def _get_issues_for_file(path):
137     if not path.endswith('.xml'):
138         raise ValueError('Path must end with .xml')
139
140     book_id = path[:-4]
141     uf = None
142
143     try:
144         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % book_id)
145         return json.loads(uf.read())
146     except urllib2.HTTPError:
147         return []
148     finally:
149         if uf: uf.close()