Poprawienie wyglądu tytułu dokumentu w document_details.html.
[redakcja.git] / apps / explorer / views.py
1 # -*- coding: utf-8 -*-
2 import urllib2
3
4 import logging
5 log = logging.getLogger('platforma.explorer.views')
6
7 from django.conf import settings
8 from django.contrib.auth.decorators import login_required, permission_required
9
10 from django.core.urlresolvers import reverse
11 from django.http import HttpResponse
12 from django.utils import simplejson as json
13 from django.views.generic.simple import direct_to_template
14 from django.shortcuts import render_to_response
15 from django.template import RequestContext
16 from django.contrib.auth.decorators import login_required
17
18 from explorer import forms
19 # from api.models import PullRequest
20 from bookthemes.models import Theme
21
22 def ajax_login_required(view):
23     """Similar ro @login_required, but instead of redirect, 
24     just return some JSON stuff with error."""
25     def view_with_auth(request, *args, **kwargs):
26         if request.user.is_authenticated():
27             return view(request, *args, **kwargs)
28         # not authenticated
29         return HttpResponse( json.dumps({'result': 'access_denied', 'errors': ['Brak dostępu.']}) );
30     return view_with_auth
31
32 @login_required
33 def display_editor(request, path):
34     user = request.GET.get('user', request.user.username)
35     gallery_form = forms.GalleryChoiceForm()
36     
37     return render_to_response('explorer/editor.html',
38         # mimetype="text/html",
39         dictionary = {
40             'fileid': path,
41             'euser': user,
42             'gallery_form': gallery_form,
43             'available_themes': ({'first_letter': t.name[0].upper(), 'name': t.name} for t in Theme.objects.order_by('name')),
44         }, context_instance=RequestContext(request))
45
46 #
47 # View all files
48 #
49 def file_list(request):   
50     from wiki.views import document_list
51
52     # short-circut the api document list
53     # doctree = library_resource.handler.read(request)
54     # print "DOCTREE:", doctree['documents']
55         
56     return document_list(request, 'explorer/file_list.html')
57     
58     # return direct_to_template(request, 'explorer/file_list.html', extra_context={
59     #     'files': files, 'bookform': bookform,
60     # })
61
62 @permission_required('api.document.can_add')
63 def file_upload(request):
64     from api.resources import library_resource
65     from api.forms import DocumentUploadForm
66     from django.http import HttpRequest, HttpResponseRedirect
67
68     response = library_resource.handler.create(request)
69
70     if isinstance(response, HttpResponse):
71         data = json.loads(response.content)
72         
73         if response.status_code == 201:
74             return HttpResponseRedirect( \
75                 reverse("editor_view", args=[ data['name'] ]) )
76         else:
77             bookform = DocumentUploadForm(request.POST, request.FILES)
78             bookform.is_valid()
79             
80             return direct_to_template(request, 'explorer/file_upload.html',
81                 extra_context={'bookform': bookform } )
82           
83
84 @login_required
85 def print_html(request, **kwargs):
86     from api.resources import document_html_resource
87
88     kwargs['stylesheet'] = 'legacy'
89     
90     output = document_html_resource.handler.read(request, **kwargs)
91
92     if isinstance(output, HttpResponse):
93         # errors = json.loads(output.content)
94         output.mimetype = "text/plain"
95         return output
96     
97     return direct_to_template(request, 'html4print.html',
98         extra_context={'output': output, 'docid': kwargs['docid']},
99         mimetype="text/html" )
100
101
102 def _add_references(message, issues):
103     return message + " - " + ", ".join(map(lambda issue: "Refs #%d" % issue['id'], issues))
104
105 def _get_issues_for_file(fileid):
106     uf = None
107     try:
108         uf = urllib2.urlopen(settings.REDMINE_URL + 'publications/issues/%s.json' % fileid)
109         return json.loads(uf.read())
110     except urllib2.HTTPError:
111         return []
112     finally:
113         if uf: uf.close()
114
115 # =================
116 # = Pull requests =
117 # =================
118 def pull_requests(request):    
119     objects = PullRequest.objects.order_by('-status', 'timestamp')
120
121     if not request.user.has_perm('explorer.book.can_share'):
122         objects = objects.filter(comitter=request.user)
123
124     return direct_to_template(request, 'manager/pull_request.html', 
125         extra_context = {'objects': objects} )
126
127
128 #
129 # Testing
130 #
131 def renderer_test(request):
132     return direct_to_template(request, 'renderer.html', mimetype="text/html",
133         extra_context = {} )